feat(refactor): Completed refactoring auth and user libs

This commit is contained in:
Chris Troutner
2021-03-28 15:01:37 -07:00
parent 8c21029c7c
commit 345c55c150
7 changed files with 163 additions and 101 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
"scripts": {
"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:all": "export KOA_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/",
"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/",
-1
View File
@@ -84,7 +84,6 @@ class Admin {
// Handle existing system user.
if (err.response.status === 422) {
try {
console.log('ping03')
// Delete the existing user
await _this.deleteExistingSystemUser()
+10 -1
View File
@@ -107,7 +107,7 @@ class UserLib {
// Save a copy of the original user type.
const userType = existingUser.type
console.log('userType: ', userType)
// console.log('userType: ', userType)
// If user 'type' property is sent by the client
if (newData.type) {
@@ -136,6 +136,15 @@ class UserLib {
throw err
}
}
async deleteUser (user) {
try {
await user.remove()
} catch (err) {
wlogger.error('Error in lib/users.js/deleteUser()')
throw err
}
}
}
module.exports = UserLib
+14 -7
View File
@@ -209,8 +209,8 @@ class UserController {
ctx.body = {
user
}
} catch (error) {
ctx.throw(422, error.message)
} catch (err) {
ctx.throw(422, err.message)
}
}
@@ -234,11 +234,18 @@ class UserController {
* @apiUse TokenError
*/
async deleteUser (ctx) {
const user = ctx.body.user
await user.remove()
ctx.status = 200
ctx.body = {
success: true
try {
const user = ctx.body.user
// await user.remove()
await _this.userLib.deleteUser(user)
ctx.status = 200
ctx.body = {
success: true
}
} catch (err) {
ctx.throw(422, err.message)
}
}
+91 -91
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: ${admi nJWT}`)
context.adminJWT = adminJWT
// const admin = await testUtils.loginAdminUser()
@@ -693,94 +693,94 @@ describe('Users', () => {
})
})
// describe('DELETE /users/:id', () => {
// it('should not delete user if token is invalid', async () => {
// try {
// const options = {
// method: 'DELETE',
// url: `${LOCALHOST}/users/1`,
// headers: {
// Accept: 'application/json',
// Authorization: 'Bearer 1'
// }
// }
// await axios(options)
//
// assert.equal(true, false, 'Unexpected behavior')
// } catch (err) {
// assert.equal(err.response.status, 401)
// }
// })
//
// it('should throw 401 if deleting invalid user', async () => {
// const { token } = context
//
// try {
// const options = {
// method: 'DELETE',
// url: `${LOCALHOST}/users/1`,
// headers: {
// Accept: 'application/json',
// Authorization: `Bearer ${token}`
// }
// }
// await axios(options)
//
// assert.equal(true, false, 'Unexpected behavior')
// } catch (err) {
// assert.equal(err.response.status, 401)
// }
// })
//
// it('should not be able to delete other users unless admin', async () => {
// try {
// const options = {
// method: 'DELETE',
// url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
// headers: {
// Authorization: `Bearer ${context.token}`
// }
// }
// await axios(options)
// } catch (err) {
// assert.equal(err.response.status, 401)
// }
// })
//
// it('should delete own user', async () => {
// const _id = context.user._id
// const token = context.token
//
// const options = {
// method: 'DELETE',
// url: `${LOCALHOST}/users/${_id}`,
// headers: {
// Accept: 'application/json',
// Authorization: `Bearer ${token}`
// }
// }
// const result = await axios(options)
// // console.log(`result: ${util.inspect(result.data.success)}`)
//
// assert.equal(result.data.success, true)
// })
//
// it('should be able to delete other users when admin', async () => {
// const id = context.id2
// const adminJWT = context.adminJWT
//
// const options = {
// method: 'DELETE',
// url: `${LOCALHOST}/users/${id}`,
// headers: {
// Accept: 'application/json',
// Authorization: `Bearer ${adminJWT}`
// }
// }
// const result = await axios(options)
// // console.log(`result: ${util.inspect(result.data)}`)
//
// assert.equal(result.data.success, true)
// })
// })
describe('DELETE /users/:id', () => {
it('should not delete user if token is invalid', async () => {
try {
const options = {
method: 'DELETE',
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: 'Bearer 1'
}
}
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.response.status, 401)
}
})
it('should throw 401 if deleting invalid user', async () => {
const { token } = context
try {
const options = {
method: 'DELETE',
url: `${LOCALHOST}/users/1`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await axios(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.response.status, 401)
}
})
it('should not be able to delete other users unless admin', async () => {
try {
const options = {
method: 'DELETE',
url: `${LOCALHOST}/users/${context.user2._id.toString()}`,
headers: {
Authorization: `Bearer ${context.token}`
}
}
await axios(options)
} catch (err) {
assert.equal(err.response.status, 401)
}
})
it('should delete own user', async () => {
const _id = context.user._id
const token = context.token
const options = {
method: 'DELETE',
url: `${LOCALHOST}/users/${_id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
const result = await axios(options)
// console.log(`result: ${util.inspect(result.data.success)}`)
assert.equal(result.data.success, true)
})
it('should be able to delete other users when admin', async () => {
const id = context.id2
const adminJWT = context.adminJWT
const options = {
method: 'DELETE',
url: `${LOCALHOST}/users/${id}`,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${adminJWT}`
}
}
const result = await axios(options)
// console.log(`result: ${util.inspect(result.data)}`)
assert.equal(result.data.success, true)
})
})
})
+19
View File
@@ -335,4 +335,23 @@ describe('#users', () => {
// TODO: verify that an admin can change the type of a user
})
describe('#deleteUser', () => {
it('should throw error if no user provided', async () => {
try {
await uut.deleteUser()
assert.fail('Unexpected code path.')
} catch (err) {
// console.log(err)
assert.include(err.message, 'Cannot read property')
}
})
it('should delete the user from the database', async () => {
await uut.deleteUser(testUser)
assert.isOk('Not throwing an error is a pass!')
})
})
})
+28
View File
@@ -223,4 +223,32 @@ describe('Users', () => {
assert.property(ctx.response.body, 'user')
})
})
describe('DELETE /users/:id', () => {
it('should return 422 if no input data given', async () => {
try {
await uut.deleteUser(ctx)
assert.fail('Unexpected result')
} catch (err) {
// console.log(err)
assert.equal(err.status, 422)
assert.include(err.message, 'Cannot read property')
}
})
it('should return 200 status on success', async () => {
// Replace the testUser variable with an actual model from the DB.
const existingUser = await User.findById(testUser._id)
ctx.body = {
user: existingUser
}
await uut.deleteUser(ctx)
// Assert the expected HTTP response
assert.equal(ctx.status, 200)
})
})
})