From 6ece560ae2033a343d7e5ad94ff8831d60e963f7 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 5 Nov 2018 09:09:24 -0800 Subject: [PATCH 1/4] Tests for /GET users --- test/a02-users.spec.js | 142 ++++++++++++++++++++++++++++------------- 1 file changed, 97 insertions(+), 45 deletions(-) diff --git a/test/a02-users.spec.js b/test/a02-users.spec.js index 17697d4..dc740d2 100644 --- a/test/a02-users.spec.js +++ b/test/a02-users.spec.js @@ -5,6 +5,9 @@ const utils = require('./utils') const rp = require('request-promise') const assert = require('chai').assert +const util = require('util') +util.inspect.defaultOptions = {depth: 1} + const LOCALHOST = 'http://localhost:5000' should() @@ -65,73 +68,122 @@ describe('Users', () => { context.user = result.body.user context.token = result.body.token - } catch (err) { console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2)) throw err } }) }) -/* + describe('GET /users', () => { - it('should not fetch users if the authorization header is missing', (done) => { - request - .get('/users') - .set('Accept', 'application/json') - .expect(401, done) + it('should not fetch users if the authorization header is missing', async () => { + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json' + } + } + + await rp(options) + + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should not fetch users if the authorization header is missing the scheme', (done) => { - request - .get('/users') - .set({ - Accept: 'application/json', - Authorization: '1' - }) - .expect(401, done) + it('should not fetch users if the authorization header is missing the scheme', async () => { + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': '1' + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should not fetch users if the authorization header has invalid scheme', (done) => { + it('should not fetch users if the authorization header has invalid scheme', async () => { const { token } = context - request - .get('/users') - .set({ - Accept: 'application/json', - Authorization: `Unknown ${token}` - }) - .expect(401, done) + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Unknown ${token}` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should not fetch users if token is invalid', (done) => { - request - .get('/users') - .set({ - Accept: 'application/json', - Authorization: 'Bearer 1' - }) - .expect(401, done) + it('should not fetch users if token is invalid', async () => { + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer 1` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should fetch all users', (done) => { + it('should fetch all users', async () => { const { token } = context - request - .get('/users') - .set({ - Accept: 'application/json', - Authorization: `Bearer ${token}` - }) - .expect(200, (err, res) => { - if (err) { return done(err) } - res.body.should.have.property('users') + const options = { + method: 'GET', + uri: `${LOCALHOST}/users`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer ${token}` + } + } - res.body.users.should.have.length(1) + const result = await rp(options) + const users = result.body.users + // console.log(`users: ${util.inspect(users)}`) - done() - }) + assert.hasAnyKeys(users[0], [ + 'type', + '_id', + 'username' + ]) + assert.equal(users.length, 1) }) }) - +/* describe('GET /users/:id', () => { it('should not fetch user if token is invalid', (done) => { request From 5b1fb2f7a3ad84948740df0eae881927d4022195 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 5 Nov 2018 09:22:12 -0800 Subject: [PATCH 2/4] Added test for GET /user/:id --- test/a02-users.spec.js | 94 +++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/test/a02-users.spec.js b/test/a02-users.spec.js index dc740d2..d854a06 100644 --- a/test/a02-users.spec.js +++ b/test/a02-users.spec.js @@ -183,55 +183,81 @@ describe('Users', () => { assert.equal(users.length, 1) }) }) -/* + describe('GET /users/:id', () => { - it('should not fetch user if token is invalid', (done) => { - request - .get('/users/1') - .set({ - Accept: 'application/json', - Authorization: 'Bearer 1' - }) - .expect(401, done) + it('should not fetch user if token is invalid', async () => { + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer 1` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should throw 404 if user doesn\'t exist', (done) => { + it('should throw 404 if user doesn\'t exist', async () => { const { token } = context - request - .get('/users/1') - .set({ - Accept: 'application/json', - Authorization: `Bearer ${token}` - }) - .expect(404, done) + + try { + const options = { + method: 'GET', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer ${token}` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 404) + } }) - it('should fetch user', (done) => { + it('should fetch user', async () => { const { user: { _id }, token } = context - request - .get(`/users/${_id}`) - .set({ - Accept: 'application/json', - Authorization: `Bearer ${token}` - }) - .expect(200, (err, res) => { - if (err) { return done(err) } + const options = { + method: 'GET', + uri: `${LOCALHOST}/users/${_id}`, + resolveWithFullResponse: true, + json: true, + headers: { + 'Accept': 'application/json', + 'Authorization': `Bearer ${token}` + } + } - // console.log(`res: ${JSON.stringify(res, null, 2)}`) + const result = await rp(options) + const user = result.body.user + // console.log(`user: ${util.inspect(user)}`) - res.body.should.have.property('user') - - expect(res.body.user.password).to.not.exist - - done() - }) + assert.hasAnyKeys(user, [ + 'type', + '_id', + 'username' + ]) + assert.equal(user._id, _id) + assert.notProperty(user, 'password', 'Password property should not be returned') }) }) - +/* describe('PUT /users/:id', () => { it('should not update user if token is invalid', (done) => { request From 4c674d6443613163546f45194356daba1b74cf10 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 5 Nov 2018 09:28:28 -0800 Subject: [PATCH 3/4] Added tests for PUT /user/:id --- test/a02-users.spec.js | 149 ++++++++++++++++++++++++----------------- 1 file changed, 89 insertions(+), 60 deletions(-) diff --git a/test/a02-users.spec.js b/test/a02-users.spec.js index d854a06..0cd9645 100644 --- a/test/a02-users.spec.js +++ b/test/a02-users.spec.js @@ -6,7 +6,7 @@ const rp = require('request-promise') const assert = require('chai').assert const util = require('util') -util.inspect.defaultOptions = {depth: 1} +util.inspect.defaultOptions = { depth: 1 } const LOCALHOST = 'http://localhost:5000' @@ -69,7 +69,9 @@ describe('Users', () => { context.user = result.body.user context.token = result.body.token } catch (err) { - console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2)) + console.log( + 'Error authenticating test user: ' + JSON.stringify(err, null, 2) + ) throw err } }) @@ -84,7 +86,7 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json' + Accept: 'application/json' } } @@ -104,8 +106,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': '1' + Accept: 'application/json', + Authorization: '1' } } @@ -125,8 +127,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Unknown ${token}` + Accept: 'application/json', + Authorization: `Unknown ${token}` } } @@ -145,8 +147,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer 1` + Accept: 'application/json', + Authorization: `Bearer 1` } } @@ -166,8 +168,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` + Accept: 'application/json', + Authorization: `Bearer ${token}` } } @@ -175,11 +177,7 @@ describe('Users', () => { const users = result.body.users // console.log(`users: ${util.inspect(users)}`) - assert.hasAnyKeys(users[0], [ - 'type', - '_id', - 'username' - ]) + assert.hasAnyKeys(users[0], ['type', '_id', 'username']) assert.equal(users.length, 1) }) }) @@ -193,8 +191,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer 1` + Accept: 'application/json', + Authorization: `Bearer 1` } } @@ -205,7 +203,7 @@ describe('Users', () => { } }) - it('should throw 404 if user doesn\'t exist', async () => { + it("should throw 404 if user doesn't exist", async () => { const { token } = context try { @@ -215,8 +213,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` + Accept: 'application/json', + Authorization: `Bearer ${token}` } } @@ -239,8 +237,8 @@ describe('Users', () => { resolveWithFullResponse: true, json: true, headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` + Accept: 'application/json', + Authorization: `Bearer ${token}` } } @@ -248,63 +246,94 @@ describe('Users', () => { const user = result.body.user // console.log(`user: ${util.inspect(user)}`) - assert.hasAnyKeys(user, [ - 'type', - '_id', - 'username' - ]) + assert.hasAnyKeys(user, ['type', '_id', 'username']) assert.equal(user._id, _id) - assert.notProperty(user, 'password', 'Password property should not be returned') + assert.notProperty( + user, + 'password', + 'Password property should not be returned' + ) }) }) -/* + describe('PUT /users/:id', () => { - it('should not update user if token is invalid', (done) => { - request - .put('/users/1') - .set({ - Accept: 'application/json', - Authorization: 'Bearer 1' - }) - .expect(401, done) + it('should not update user if token is invalid', async () => { + try { + const options = { + method: 'PUT', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + Accept: 'application/json', + Authorization: `Bearer 1` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should throw 404 if user doesn\'t exist', (done) => { + it("should throw 404 if user doesn't exist", async () => { const { token } = context - request - .put('/users/1') - .set({ - Accept: 'application/json', - Authorization: `Bearer ${token}` - }) - .expect(404, done) + + try { + const options = { + method: 'PUT', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 404) + } }) - it('should update user', (done) => { + it('should update user', async () => { const { user: { _id }, token } = context - request - .put(`/users/${_id}`) - .set({ + const options = { + method: 'PUT', + uri: `${LOCALHOST}/users/${_id}`, + resolveWithFullResponse: true, + json: true, + headers: { Accept: 'application/json', Authorization: `Bearer ${token}` - }) - .send({ user: { username: 'updatedcoolname' } }) - .expect(200, (err, res) => { - if (err) { return done(err) } + }, + body: { + user: { username: 'updatedcoolname' } + } + } - res.body.user.should.have.property('username') - res.body.user.username.should.equal('updatedcoolname') - expect(res.body.user.password).to.not.exist + const result = await rp(options) + const user = result.body.user + // console.log(`user: ${util.inspect(user)}`) - done() - }) + assert.hasAnyKeys(user, ['type', '_id', 'username']) + assert.equal(user._id, _id) + assert.notProperty( + user, + 'password', + 'Password property should not be returned' + ) + assert.equal(user.username, 'updatedcoolname') }) }) - + /* describe('DELETE /users/:id', () => { it('should not delete user if token is invalid', (done) => { request From ff79decc02cb307adafa6dc46230d124076d41ae Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 5 Nov 2018 11:40:10 -0800 Subject: [PATCH 4/4] Added tests for delete user --- src/models/users.js | 4 +- src/modules/users/controller.js | 2 +- test/a02-users.spec.js | 77 ++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/models/users.js b/src/models/users.js index 2cb55c8..fb20037 100644 --- a/src/models/users.js +++ b/src/models/users.js @@ -51,8 +51,8 @@ User.methods.generateToken = function generateToken () { const user = this const token = jwt.sign({ id: user.id }, config.token) - //console.log(`config.token: ${config.token}`) - //console.log(`generated token: ${token}`) + // console.log(`config.token: ${config.token}`) + // console.log(`generated token: ${token}`) return token } diff --git a/src/modules/users/controller.js b/src/modules/users/controller.js index 9fc3ba7..3ce1d02 100644 --- a/src/modules/users/controller.js +++ b/src/modules/users/controller.js @@ -213,7 +213,7 @@ async function updateUser (ctx) { async function deleteUser (ctx) { const user = ctx.body.user - await user.deleteOne() + await user.remove() ctx.status = 200 ctx.body = { diff --git a/test/a02-users.spec.js b/test/a02-users.spec.js index 0cd9645..c8857ba 100644 --- a/test/a02-users.spec.js +++ b/test/a02-users.spec.js @@ -333,44 +333,71 @@ describe('Users', () => { assert.equal(user.username, 'updatedcoolname') }) }) - /* + describe('DELETE /users/:id', () => { - it('should not delete user if token is invalid', (done) => { - request - .delete('/users/1') - .set({ - Accept: 'application/json', - Authorization: 'Bearer 1' - }) - .expect(401, done) + it('should not delete user if token is invalid', async () => { + try { + const options = { + method: 'DELETE', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + Accept: 'application/json', + Authorization: `Bearer 1` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 401) + } }) - it('should throw 404 if user doesn\'t exist', (done) => { + it('should throw 404 if user doesn\'t exist', async () => { const { token } = context - request - .delete('/users/1') - .set({ - Accept: 'application/json', - Authorization: `Bearer ${token}` - }) - .expect(404, done) + + try { + const options = { + method: 'DELETE', + uri: `${LOCALHOST}/users/1`, + resolveWithFullResponse: true, + json: true, + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}` + } + } + + await rp(options) + assert.equal(true, false, 'Unexpected behavior') + } catch (err) { + assert.equal(err.statusCode, 404) + } }) - it('should delete user', (done) => { + it('should delete user', async () => { const { user: { _id }, token } = context - request - .delete(`/users/${_id}`) - .set({ + const options = { + method: 'DELETE', + uri: `${LOCALHOST}/users/${_id}`, + resolveWithFullResponse: true, + json: true, + headers: { Accept: 'application/json', Authorization: `Bearer ${token}` - }) - .expect(200, done) - }) + } + } + const result = await rp(options) + // console.log(`result: ${util.inspect(result.body)}`) + + assert.equal(result.body.success, true) + }) }) - */ })