Finished adding unit tests for user update

This commit is contained in:
Chris Troutner
2021-03-28 14:42:11 -07:00
parent e48372e125
commit 8c21029c7c
6 changed files with 44 additions and 33 deletions
+4 -5
View File
@@ -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",
+6 -5
View File
@@ -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
+1
View File
@@ -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.
+5
View File
@@ -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',
+4 -3
View File
@@ -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)}`)
+24 -20
View File
@@ -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 = {