fix(users): Ported users and unit tests REST API to controllers dir

This commit is contained in:
Chris Troutner
2021-07-06 13:16:14 -07:00
parent 61bc01b783
commit 08cf043e37
2 changed files with 42 additions and 16 deletions
+32 -4
View File
@@ -7,15 +7,21 @@ const Router = require('koa-router')
// Local libraries.
const UserRESTControllerLib = require('./controller')
const Validators = require('../../../middleware/validators')
let _this
class UserRESTRouter {
constructor (localConfig = {}) {
// Encapsulate dependencies.
this.userRESTController = new UserRESTControllerLib()
this.validators = new Validators()
// Instantiate the router and set the base route.
const baseUrl = '/users'
this.router = new Router({ prefix: baseUrl })
_this = this
}
attachControllers (app) {
@@ -27,15 +33,37 @@ class UserRESTRouter {
// Define the routes and attach the controller.
this.router.post('/', this.userRESTController.createUser)
this.router.get('/', this.userRESTController.getUsers)
this.router.get('/:id', this.userRESTController.getUsers)
this.router.put('/', this.userRESTController.updateUser)
this.router.delete('/', this.userRESTController.deleteUser)
this.router.get('/', this.getAll)
this.router.get('/:id', this.getById)
this.router.put('/:id', this.updateUser)
this.router.delete('/:id', this.deleteUser)
// Attach the Controller routes to the Koa app.
app.use(this.router.routes())
app.use(this.router.allowedMethods())
}
async getAll (ctx, next) {
await _this.validators.ensureUser(ctx, next)
await _this.userRESTController.getUsers(ctx, next)
}
async getById (ctx, next) {
await _this.validators.ensureUser(ctx, next)
await _this.userRESTController.getUser(ctx, next)
}
async updateUser (ctx, next) {
await _this.validators.ensureTargetUserOrAdmin(ctx, next)
await _this.userRESTController.getUser(ctx, next)
await _this.userRESTController.updateUser(ctx, next)
}
async deleteUser (ctx, next) {
await _this.validators.ensureTargetUserOrAdmin(ctx, next)
await _this.userRESTController.getUser(ctx, next)
await _this.userRESTController.deleteUser(ctx, next)
}
}
module.exports = UserRESTRouter
+10 -12
View File
@@ -26,27 +26,27 @@ class Validators {
const token = _this.getToken(ctx)
if (!token) {
// console.log(`Err: Token not provided.`)
// console.log(`Err: Token not provided.`)
ctx.throw(401)
}
let decoded = null
try {
// console.log(`token: ${JSON.stringify(token, null, 2)}`)
// console.log(`config: ${JSON.stringify(config, null, 2)}`)
// console.log(`token: ${JSON.stringify(token, null, 2)}`)
// console.log(`config: ${JSON.stringify(config, null, 2)}`)
decoded = _this.jwt.verify(token, config.token)
} catch (err) {
// console.log(`Err: Token could not be decoded: ${err}`)
// console.log(`Err: Token could not be decoded: ${err}`)
ctx.throw(401)
}
ctx.state.user = await _this.User.findById(decoded.id, '-password')
if (!ctx.state.user) {
// console.log(`Err: Could not find user.`)
// console.log(`Err: Could not find user.`)
ctx.throw(401)
}
return next()
// return next()
} catch (error) {
ctx.throw(401)
}
@@ -84,7 +84,7 @@ class Validators {
ctx.throw(401, 'not admin')
}
return next()
// return next()
} catch (error) {
ctx.throw(401, error.message)
}
@@ -130,20 +130,18 @@ class Validators {
if (ctx.state.user._id.toString() !== targetId.toString()) {
wlogger.verbose(
`Calling user and target user do not match! Calling user: ${
ctx.state.user._id
}, Target user: ${targetId}`
`Calling user and target user do not match! Calling user: ${ctx.state.user._id}, Target user: ${targetId}`
)
// If they don't match, then the calling user better be an admin.
if (ctx.state.user.type !== 'admin') {
ctx.throw(401, 'not admin')
} else {
wlogger.verbose('It\'s ok. The user is an admin.')
wlogger.verbose("It's ok. The user is an admin.")
}
}
return next()
// return next()
} catch (error) {
ctx.throw(401, error.message)
}