diff --git a/package.json b/package.json index 31b2952..3b3b6e0 100644 --- a/package.json +++ b/package.json @@ -7,14 +7,13 @@ "start": "node index.js", "test": "npm run test:all", "test:all": "npm run set-env && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/", - "test:unit:lib": "npm run set-env && mocha --exit --timeout 15000 test/unit/biz-logic/", - "test:unit:rest": "npm run set-env && mocha --exit --timeout 15000 test/unit/rest-api/", - "test:e2e:auto": "npm run set-env && mocha --exit --timeout 15000 test/e2e/automated/", - "set-env": "export KOA_ENV=test", + "test:unit:lib": "export KOA_ENV=test && mocha --exit --timeout 15000 test/unit/biz-logic/", + "test:unit:rest": "export KOA_ENV=test && mocha --exit --timeout 15000 test/unit/rest-api/", + "test:e2e:auto": "export KOA_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/", "lint": "standard --env mocha --fix", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", "coverage": "nyc report --reporter=text-lcov | coveralls", - "coverage:report": "npm run set-env && nyc --reporter=html mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/" + "coverage:report": "export KOA_ENV=test && nyc --reporter=html mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/" }, "keywords": [ "koa-api-boilerplate", diff --git a/src/lib/users.js b/src/lib/users.js index aad7fa0..6693f46 100644 --- a/src/lib/users.js +++ b/src/lib/users.js @@ -94,15 +94,16 @@ class UserLib { async updateUser (existingUser, newData) { try { // Input Validation - if (!newData.email || typeof newData.email !== 'string') { + // Optional inputs, but they must be strings if included. + if (newData.email && typeof newData.email !== 'string') { throw new Error("Property 'email' must be a string!") } - if (!newData.password || typeof newData.password !== 'string') { - throw new Error("Property 'password' must be a string!") - } - if (!newData.name || typeof newData.name !== 'string') { + if (newData.name && typeof newData.name !== 'string') { throw new Error("Property 'name' must be a string!") } + if (newData.password && typeof newData.password !== 'string') { + throw new Error("Property 'password' must be a string!") + } // Save a copy of the original user type. const userType = existingUser.type diff --git a/src/middleware/validators.js b/src/middleware/validators.js index 2fb4e15..9e9dc86 100644 --- a/src/middleware/validators.js +++ b/src/middleware/validators.js @@ -120,6 +120,7 @@ class Validators { // console.log(`Err: Could not find user.`) ctx.throw(401) } + // console.log('ctx.state.user: ', ctx.state.user) // console.log(`ctx.state.user: ${JSON.stringify(ctx.state.user, null, 2)}`) // Ensure the calling user and the target user are the same. diff --git a/test/e2e/automated/a01-auth.rest-e2e.js b/test/e2e/automated/a01-auth.rest-e2e.js index a6a877f..52099ee 100644 --- a/test/e2e/automated/a01-auth.rest-e2e.js +++ b/test/e2e/automated/a01-auth.rest-e2e.js @@ -12,6 +12,8 @@ const axios = require('axios').default const config = require('../../../config') const app = require('../../../bin/server') const testUtils = require('../../utils/test-utils') +const AdminLib = require('../../../src/lib/admin') +const adminLib = new AdminLib() // const request = supertest.agent(app.listen()) const context = {} @@ -26,6 +28,9 @@ describe('Auth', () => { // Delete all previous users in the database. await testUtils.deleteAllUsers() + // Create a new admin user. + await adminLib.createSystemUser() + const userObj = { email: 'test@test.com', password: 'pass', diff --git a/test/e2e/automated/a02-users.rest-e2e.js b/test/e2e/automated/a02-users.rest-e2e.js index 96519f5..9b0aa9c 100644 --- a/test/e2e/automated/a02-users.rest-e2e.js +++ b/test/e2e/automated/a02-users.rest-e2e.js @@ -36,7 +36,7 @@ describe('Users', () => { // Get the JWT used to log in as the admin 'system' user. const adminJWT = await testUtils.getAdminJWT() - // console.log(`adminJWT: ${adminJWT}`) + console.log(`adminJWT: ${adminJWT}`) context.adminJWT = adminJWT // const admin = await testUtils.loginAdminUser() @@ -605,11 +605,12 @@ describe('Users', () => { data: { user: { name: 'This should work', - email: 'test@test.com' - // password: 'password' + email: 'test4@test.com', + password: 'password' } } } + const result = await axios(options) // console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) diff --git a/test/unit/biz-logic/a02-users.lib-unit.js b/test/unit/biz-logic/a02-users.lib-unit.js index 0f70da4..e5f9e0c 100644 --- a/test/unit/biz-logic/a02-users.lib-unit.js +++ b/test/unit/biz-logic/a02-users.lib-unit.js @@ -23,6 +23,7 @@ describe('#users', () => { before(async () => { // Connect to the Mongo Database. + console.log(`Connecting to database: ${config.database}`) mongoose.Promise = global.Promise mongoose.set('useCreateIndex', true) // Stop deprecation warning. await mongoose.connect(config.database, { @@ -234,9 +235,11 @@ describe('#users', () => { } }) - it('should throw an error if no email given', async () => { + it('should throw an error if email is not a string', async () => { try { - await uut.updateUser(testUser, {}) + await uut.updateUser(testUser, { + email: 1234 + }) assert.fail('Unexpected code path') } catch (err) { @@ -245,26 +248,10 @@ describe('#users', () => { } }) - it('should throw an error if no password given', async () => { + it('should throw an error if name is not a string', async () => { try { const newData = { - email: 'test@test.com' - } - - await uut.updateUser(testUser, newData) - - assert.fail('Unexpected code path') - } catch (err) { - // console.log(err) - assert.include(err.message, "Property 'password' must be a string!") - } - }) - - it('should throw an error if no name given', async () => { - try { - const newData = { - email: 'test@test.com', - password: 'password' + name: 1234 } await uut.updateUser(testUser, newData) @@ -276,6 +263,23 @@ describe('#users', () => { } }) + it('should throw an error if non-string password given', async () => { + try { + const newData = { + email: 'test@test.com', + name: 'test', + password: 1234 + } + + await uut.updateUser(testUser, newData) + + assert.fail('Unexpected code path') + } catch (err) { + // console.log(err) + assert.include(err.message, "Property 'password' must be a string!") + } + }) + it('should throw an error for malformed type given', async () => { try { const newData = {