Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account feature #80

Merged
merged 29 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
55b5b4e
Avatar skeleton and refractor of frontend
Mar 14, 2021
93cb5ef
Added ID to frontend after login or create account
Mar 14, 2021
86153c4
Frontend now sends image to backend and saves in database
Mar 15, 2021
adddcec
Merge branch 'master' of github.com:itslupus/gamersnet into avatar
Mar 18, 2021
6e784dc
Added get avatar endpoint and profile skeleton
Mar 20, 2021
5ea56d7
Added profile related components
Mar 20, 2021
e9ccb0f
Added password change to user profile
Mar 20, 2021
5835293
Added profile avatar change to profile page
Mar 20, 2021
84ccdce
Skeleton for user info change
Mar 21, 2021
0f5dc6b
Added change user details
Mar 21, 2021
c989e27
Skeleton for display profile data
Mar 21, 2021
ee4628f
Resolved code smells
Mar 23, 2021
2b86dec
Merge branch 'master' of github.com:itslupus/gamersnet into account_f…
Mar 23, 2021
1800375
Added tests
Mar 23, 2021
4c223f5
Finished create account tests
Mar 23, 2021
142da8a
Added tests
Mar 24, 2021
30ccad5
Added initial password tests
Mar 24, 2021
7046176
Finished password tests
Mar 24, 2021
7a24f96
Added unfinished frontend test
Mar 24, 2021
bdf790c
Merge branch 'master' of github.com:itslupus/gamersnet into account_f…
Mar 24, 2021
e3d27a1
Updated package lock
Mar 24, 2021
ff6ef2f
Fixed packages
Mar 24, 2021
d56880f
Merge branch 'master' of github.com:itslupus/gamersnet into account_f…
Mar 24, 2021
15888b7
Merged display posts into profile
Mar 24, 2021
84af450
Added edit post
Mar 24, 2021
b092e30
Recording timing (so that we can come back)
Mar 24, 2021
ece41e7
Removed extra files (related to prev commit)
Mar 24, 2021
325b430
Added achievements
Mar 25, 2021
4ec5254
Added rank change
Mar 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions gamersnet_backend/__tests__/users/testAuthenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'use strict';

let app = require('../../app');
let request = require('supertest');

let MongoDB = require('../../persistence/mongodb');
let db;

beforeEach(async () => {
if (!db) db = await MongoDB.open();
});

afterEach(async () => {
if (db) {
await MongoDB.close();
db = null;
}
})

describe('Test Authenticate Account (function testing)', () => {
test('Blank password and username', (done) => {
return request(app).post('/users/authenticate').send({
username: '',
password: ''
}).expect(400).end(done);
});

test('No body', (done) => {
return request(app).post('/users/authenticate').expect(400).end(done);
});

test('Incorrect method', (done) => {
return request(app).get('/users/authenticate').expect(404).end(done);
});

test('Valid username, invalid password', (done) => {
return request(app).post('/users/authenticate').send({
username: 'usrnme',
password: ''
}).expect(400).end(done);
});

test('Invalid username, valid password', (done) => {
return request(app).post('/users/authenticate').send({
username: '',
password: 'pwd'
}).expect(400).end(done);
});
});

describe('Test Authenticate Account (single user + token document)', () => {
beforeEach(async () => {
// username bob, password 9999
db.collection('users').insertOne({username: 'bob', password: '$2b$10$intP40iXYmNia/XUWjq4Pu/aamccICNOwhOXVsPXmFacpH1/acofG'});
});

test('Authenticate with correct details', (done) => {
return request(app).post('/users/authenticate').send({
username: 'bob',
password: '9999'
}).expect(200).end(done);
});

test('Authenticate with correct details (token)', (done) => {
return request(app).post('/users/authenticate').send({
username: 'bob',
password: '9999'
}).expect(200).end((error, response) => {
if (error) return done(error);

expect(response.header['set-cookie']).toBeDefined();

done();
});
});

test('Authenticate with correct details (user ID)', (done) => {
return request(app).post('/users/authenticate').send({
username: 'bob',
password: '9999'
}).expect(200).end((error, response) => {
if (error) return done(error);

expect(response.text).toBeDefined();

let json = JSON.parse(response.text);

expect(json).toBeDefined();
expect(json['user_id']).toBeDefined();

done();
});
});

test('Authenticate with incorrect password', (done) => {
return request(app).post('/users/authenticate').send({
username: 'bob',
password: 'abc'
}).expect(401).end(done);
});

test('Authenticate with non exist username', (done) => {
return request(app).post('/users/authenticate').send({
username: 'not bob',
password: 'abc'
}).expect(401).end(done);
});
});
107 changes: 107 additions & 0 deletions gamersnet_backend/__tests__/users/testCreateAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

let app = require('../../app');
let request = require('supertest');

let MongoDB = require('../../persistence/mongodb');
let db;

beforeEach(async () => {
if (!db) db = await MongoDB.open();
});

afterEach(async () => {
if (db) {
await MongoDB.close();
db = null;
}
})

describe('Test Create User Account (empty database)', () => {
test('Blank password and username', (done) => {
return request(app).post('/users/createAccount').send({
username: '',
password: ''
}).expect(400).end(done);
});

test('No body', (done) => {
return request(app).post('/users/createAccount').expect(400).end(done);
});

test('Incorrect method', (done) => {
return request(app).get('/users/createAccount').expect(404).end(done);
});

test('Valid username, invalid password', (done) => {
return request(app).post('/users/createAccount').send({
username: 'usrnme',
password: ''
}).expect(400).end(done);
});

test('Invalid username, valid password', (done) => {
return request(app).post('/users/createAccount').send({
username: '',
password: 'pwd'
}).expect(400).end(done);
});

test('Valid account creation', (done) => {
return request(app).post('/users/createAccount').send({
username: 'usrnme',
password: 'pwd'
}).expect(200).end(done);
});

test('Valid token returned (non empty)', (done) => {
return request(app).post('/users/createAccount').send({
username: 'usrnme',
password: 'pwd'
}).expect(200).end((error, response) => {
if (error) return done(error);

expect(response.header['set-cookie']).toBeDefined();

done();
});
});

test('Valid user ID returned in text response (non empty)', (done) => {
return request(app).post('/users/createAccount').send({
username: 'usrnme',
password: 'pwd'
}).expect(200).end((error, response) => {
if (error) return done(error);

expect(response.text).toBeDefined();

let json = JSON.parse(response.text);

expect(json).toBeDefined();
expect(json['user_id']).toBeDefined();

done();
});
});
});

describe('Test Create User Account (populated database)', () => {
beforeEach(async () => {
db.collection('users').insertOne({username: 'taken', password: '123'});
});

test('Insert new account with already used username', (done) => {
return request(app).post('/users/createAccount').send({
username: 'taken',
password: 'pwd'
}).expect(400).end(done);
});

test('Insert new account with new username', (done) => {
return request(app).post('/users/createAccount').send({
username: 'nottaken',
password: 'pwd'
}).expect(200).end(done);
});
})
84 changes: 84 additions & 0 deletions gamersnet_backend/__tests__/users/testPassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

let ObjectID = require('mongodb').ObjectId;
let request = require('supertest');

let app = require('../../app');
let MongoDB = require('../../persistence/mongodb');
let db;

beforeEach(async () => {
if (!db) db = await MongoDB.open();
});

afterEach(async () => {
if (db) {
await MongoDB.close();
db = null;
}
})

describe('Test password change (empty database)', () => {
test('Invalid method', (done) => {
return request(app).get('/users/updatePassword').expect(404).end(done);
});

test('Missing token cookie', (done) => {
return request(app).patch('/users/updatePassword').send({
oldPassword: '123',
newPassword: '123'
}).expect(400).end(done);
});

test('Missing token cookie and body', (done) => {
return request(app).patch('/users/updatePassword').expect(400).end(done);
});
});

describe('Test password change (single user)', () => {
// password 9999
let hashedPassword = '$2b$10$intP40iXYmNia/XUWjq4Pu/aamccICNOwhOXVsPXmFacpH1/acofG';

beforeEach(async () => {
let result = await db.collection('users').insertOne({username: 'bob', password: hashedPassword});
let id = await result.insertedId;

await db.collection('tokens').insertOne({
userID: ObjectID(await id),
token: 'abc',
expires: 99999999999999
});
});

test('Missing body, but valid token', (done) => {
return request(app).patch('/users/updatePassword')
.set('Cookie', 'token=abc')
.expect(400).end(done);
});

test('Invalid old password', (done) => {
return request(app).patch('/users/updatePassword')
.set('Cookie', 'token=abc')
.send({
oldPassword: '8888',
newPassword: '9999'
})
.expect(401).end(done);
});

test('Test valid password change', (done) => {
return request(app).patch('/users/updatePassword')
.set('Cookie', 'token=abc')
.send({
oldPassword: '9999',
newPassword: '9999'
})
.expect(204).end(async () => {
let result = await db.collection('users').find({username: 'bob'});

expect(await result.password).not.toBe(hashedPassword);

done();
});
});
});
31 changes: 0 additions & 31 deletions gamersnet_backend/__tests__/users/testUsers.js

This file was deleted.

15 changes: 0 additions & 15 deletions gamersnet_backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ app.get('/', (request, response) => {
});
});

// this is the page1 route, they would access "example.com/page1" to get here
app.get('/page1', (request, response) => {
response.json({
'str1': 'this is page1',
1: 'lol'
});
});

app.get('/page2', (request, response) => {
response.json({
'str1': 'this is page2',
1: 'lol'
});
});

app.use('/posts', posts);

module.exports = app;
2 changes: 1 addition & 1 deletion gamersnet_backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ server.listen(port, async () => {
console.log(`Backend ready on port ${port}.`);

await MongoDB.open();
});
});
Loading