From 08cf043e37692f740b22066ee08715aab3b36ca6 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 6 Jul 2021 13:16:14 -0700 Subject: [PATCH] fix(users): Ported users and unit tests REST API to controllers dir --- src/controllers/rest-api/users/router.js | 36 +++++++++++++++++++++--- src/middleware/validators.js | 22 +++++++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/controllers/rest-api/users/router.js b/src/controllers/rest-api/users/router.js index 785bd75..94f479c 100644 --- a/src/controllers/rest-api/users/router.js +++ b/src/controllers/rest-api/users/router.js @@ -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 diff --git a/src/middleware/validators.js b/src/middleware/validators.js index 10043e4..64d4566 100644 --- a/src/middleware/validators.js +++ b/src/middleware/validators.js @@ -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) }