From c46f7357c310010e3db1801392a287aa7d44e528 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 25 Jul 2021 16:34:56 -0700 Subject: [PATCH 1/4] fix(wlogger): Refactored into Class for better unit test control --- src/adapters/wlogger.js | 115 ++++++++++++++------- test/unit/adapters/wlogger.adapter.unit.js | 30 ++++-- 2 files changed, 98 insertions(+), 47 deletions(-) diff --git a/src/adapters/wlogger.js b/src/adapters/wlogger.js index 3c78ffe..be502e7 100644 --- a/src/adapters/wlogger.js +++ b/src/adapters/wlogger.js @@ -11,52 +11,93 @@ require('winston-daily-rotate-file') const config = require('../../config') -// Configure daily-rotation transport. -const transport = new winston.transports.DailyRotateFile({ - filename: `${__dirname.toString()}/../../logs/koa-${config.env}-%DATE%.log`, - datePattern: 'YYYY-MM-DD', - zippedArchive: false, - maxSize: '1m', // 1 megabyte - maxFiles: '5d', // 5 days - format: winston.format.combine( - winston.format.timestamp(), - winston.format.json() - ) -}) +class Wlogger { + constructor (localConfig = {}) { + this.config = config -transport.on('rotate', notifyRotation) + // Configure daily-rotation transport. + this.transport = new winston.transports.DailyRotateFile({ + filename: `${__dirname.toString()}/../../logs/koa-${ + this.config.env + }-%DATE%.log`, + datePattern: 'YYYY-MM-DD', + zippedArchive: false, + maxSize: '1m', // 1 megabyte + maxFiles: '5d', // 5 days + format: winston.format.combine( + winston.format.timestamp(), + winston.format.json() + ) + }) -function notifyRotation (oldFilename, newFilename) { - wlogger.info('Rotating log files') + this.transport.on('rotate', this.notifyRotation) + + // This controls what goes into the log FILES + this.wlogger = winston.createLogger({ + level: 'verbose', + format: winston.format.json(), + transports: [ + // + // - Write to all logs with level `info` and below to `combined.log` + // - Write all logs error (and below) to `error.log`. + // + // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), + // new winston.transports.File({ filename: 'logs/combined.log' }) + this.transport + ] + }) + } + + notifyRotation (oldFilename, newFilename) { + this.wlogger.info('Rotating log files') + } + + outputToConsole () { + this.wlogger.add( + new winston.transports.Console({ + format: winston.format.simple(), + level: 'info' + }) + ) + } } +// transport.on('rotate', notifyRotation) + +// function notifyRotation (oldFilename, newFilename) { +// wlogger.info('Rotating log files') +// } + // This controls what goes into the log FILES -const wlogger = winston.createLogger({ - level: 'verbose', - format: winston.format.json(), - transports: [ - // - // - Write to all logs with level `info` and below to `combined.log` - // - Write all logs error (and below) to `error.log`. - // - // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), - // new winston.transports.File({ filename: 'logs/combined.log' }) - transport - ] -}) +// const wlogger = winston.createLogger({ +// level: 'verbose', +// format: winston.format.json(), +// transports: [ +// // +// // - Write to all logs with level `info` and below to `combined.log` +// // - Write all logs error (and below) to `error.log`. +// // +// // new winston.transports.File({ filename: 'logs/error.log', level: 'error' }), +// // new winston.transports.File({ filename: 'logs/combined.log' }) +// transport +// ] +// }) -function outputToConsole () { - wlogger.add( - new winston.transports.Console({ - format: winston.format.simple(), - level: 'info' - }) - ) -} +// function outputToConsole () { +// wlogger.add( +// new winston.transports.Console({ +// format: winston.format.simple(), +// level: 'info' +// }) +// ) +// } // This controls the logs to CONSOLE // if (config.env !== 'test') { // outputToConsole() // } -module.exports = { wlogger, notifyRotation, outputToConsole } +const logger = new Wlogger() +const wlogger = logger.wlogger + +module.exports = { wlogger, Wlogger } diff --git a/test/unit/adapters/wlogger.adapter.unit.js b/test/unit/adapters/wlogger.adapter.unit.js index 1333adb..fc157f6 100644 --- a/test/unit/adapters/wlogger.adapter.unit.js +++ b/test/unit/adapters/wlogger.adapter.unit.js @@ -1,30 +1,40 @@ -// const assert = require('chai').assert -const { - notifyRotation, - outputToConsole -} = require('../../../src/adapters/wlogger') +const assert = require('chai').assert +const { Wlogger } = require('../../../src/adapters/wlogger') const sinon = require('sinon') -// let uut +let uut let sandbox -describe('#wlogger.js', () => { +describe('#wlogger', () => { beforeEach(() => { sandbox = sinon.createSandbox() }) - afterEach(() => sandbox.restore()) + afterEach(() => { + sandbox.restore() + + uut = new Wlogger() + }) + + describe('#constructor', () => { + it('should create a new wlogger instance', () => { + uut = new Wlogger() + // console.log('uut: ', uut) + + assert.property(uut, 'transport') + }) + }) describe('#notifyRotation', () => { it('should notify of a log rotation', () => { - notifyRotation() + uut.notifyRotation() }) }) describe('#envronment', () => { it('should write to console in non-test environment', () => { - outputToConsole() + uut.outputToConsole() }) }) }) From 44867f01736f1aed21f03fdb96437a801ad711fd Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 25 Jul 2021 16:49:01 -0700 Subject: [PATCH 2/4] fix(controllers): Made Controllers library a class --- bin/server.js | 3 +- src/controllers/index.js | 98 ++++++++++++++++------- test/unit/controllers/controllers.unit.js | 9 ++- 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/bin/server.js b/bin/server.js index b826e5b..4fa37fe 100644 --- a/bin/server.js +++ b/bin/server.js @@ -53,7 +53,8 @@ async function startServer () { app.use(passport.session()) // Attach REST API and JSON RPC controllers to the app. - const controllers = require('../src/controllers') + const Controllers = require('../src/controllers') + const controllers = new Controllers() controllers.attachControllers(app) // Enable CORS for testing diff --git a/src/controllers/index.js b/src/controllers/index.js index 1692740..4b33244 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -14,39 +14,81 @@ const JSONRPC = require('./json-rpc') // Load the Clean Architecture Use Case libraries. const UseCases = require('../use-cases') -const useCases = new UseCases({ adapters }) +// const useCases = new UseCases({ adapters }) // Load the REST API Controllers. const RESTControllers = require('./rest-api') +class Controllers { + constructor (localConfig = {}) { + this.adapters = adapters + this.useCases = new UseCases({ adapters }) + } + + async attachControllers (app) { + // Attach the REST controllers to the Koa app. + this.attachRESTControllers(app) + + // Start IPFS. + await this.adapters.ipfs.start() + + this.attachRPCControllers() + } + + // Top-level function for this library. + // Start the various Controllers and attach them to the app. + attachRESTControllers (app) { + const rESTControllers = new RESTControllers({ + adapters: this.adapters, + useCases: this.useCases + }) + + // Attach the REST API Controllers associated with the boilerplate code to the Koa app. + rESTControllers.attachRESTControllers(app) + } + + // Add the JSON RPC router to the ipfs-coord adapter. + attachRPCControllers () { + const jsonRpcController = new JSONRPC({ + adapters: this.adapters, + useCases: this.useCases + }) + + // Attach the input of the JSON RPC router to the output of ipfs-coord. + this.adapters.ipfs.ipfsCoordAdapter.attachRPCRouter( + jsonRpcController.router + ) + } +} + // Top-level function for this library. // Start the various Controllers and attach them to the app. -async function attachControllers (app) { - // Attach the REST controllers to the Koa app. - attachRESTControllers(app) +// async function attachControllers (app) { +// // Attach the REST controllers to the Koa app. +// attachRESTControllers(app) +// +// // Start IPFS. +// await adapters.ipfs.start() +// +// attachRPCControllers() +// } +// +// function attachRESTControllers (app) { +// const rESTControllers = new RESTControllers({ +// adapters, +// useCases +// }) +// +// // Attach the REST API Controllers associated with the boilerplate code to the Koa app. +// rESTControllers.attachRESTControllers(app) +// } - // Start IPFS. - await adapters.ipfs.start() +// // Add the JSON RPC router to the ipfs-coord adapter. +// function attachRPCControllers () { +// const jsonRpcController = new JSONRPC({ adapters, useCases }) +// +// // Attach the input of the JSON RPC router to the output of ipfs-coord. +// adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(jsonRpcController.router) +// } - attachRPCControllers() -} - -function attachRESTControllers (app) { - const rESTControllers = new RESTControllers({ - adapters, - useCases - }) - - // Attach the REST API Controllers associated with the boilerplate code to the Koa app. - rESTControllers.attachRESTControllers(app) -} - -// Add the JSON RPC router to the ipfs-coord adapter. -function attachRPCControllers () { - const jsonRpcController = new JSONRPC({ adapters, useCases }) - - // Attach the input of the JSON RPC router to the output of ipfs-coord. - adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(jsonRpcController.router) -} - -module.exports = { attachControllers } +module.exports = Controllers diff --git a/test/unit/controllers/controllers.unit.js b/test/unit/controllers/controllers.unit.js index 7600abc..674e587 100644 --- a/test/unit/controllers/controllers.unit.js +++ b/test/unit/controllers/controllers.unit.js @@ -7,14 +7,17 @@ const sinon = require('sinon') const adapters = require('../../../src/adapters') -const { attachControllers } = require('../../../src/controllers') +// const { attachControllers } = require('../../../src/controllers') +const Controllers = require('../../../src/controllers') describe('#Controllers', () => { - // let uut + let uut let sandbox beforeEach(() => { sandbox = sinon.createSandbox() + + uut = new Controllers() }) afterEach(() => sandbox.restore()) @@ -31,7 +34,7 @@ describe('#Controllers', () => { use: () => {} } - await attachControllers(app) + await uut.attachControllers(app) }) }) }) From 9dc40a417a9450e365b2c11bcde18da9dca25b06 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 25 Jul 2021 17:30:15 -0700 Subject: [PATCH 3/4] Removing comments --- src/controllers/index.js | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/controllers/index.js b/src/controllers/index.js index 4b33244..69034fe 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -61,34 +61,4 @@ class Controllers { } } -// Top-level function for this library. -// Start the various Controllers and attach them to the app. -// async function attachControllers (app) { -// // Attach the REST controllers to the Koa app. -// attachRESTControllers(app) -// -// // Start IPFS. -// await adapters.ipfs.start() -// -// attachRPCControllers() -// } -// -// function attachRESTControllers (app) { -// const rESTControllers = new RESTControllers({ -// adapters, -// useCases -// }) -// -// // Attach the REST API Controllers associated with the boilerplate code to the Koa app. -// rESTControllers.attachRESTControllers(app) -// } - -// // Add the JSON RPC router to the ipfs-coord adapter. -// function attachRPCControllers () { -// const jsonRpcController = new JSONRPC({ adapters, useCases }) -// -// // Attach the input of the JSON RPC router to the output of ipfs-coord. -// adapters.ipfs.ipfsCoordAdapter.attachRPCRouter(jsonRpcController.router) -// } - module.exports = Controllers From c5a1eb6f81300de0fa3c76158338bfc9ad9ad457 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Sat, 31 Jul 2021 15:00:38 -0400 Subject: [PATCH 4/4] fix(users): Using the users entity in the use cases --- src/use-cases/user.js | 15 ++++----------- test/unit/use-cases/users.use-case.unit.js | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/use-cases/user.js b/src/use-cases/user.js index c72ffae..c879717 100644 --- a/src/use-cases/user.js +++ b/src/use-cases/user.js @@ -3,7 +3,7 @@ functions are called by the /user REST API endpoints. */ -// const UserModel = require('../adapters/localdb/models/users') +const UserEntity = require('../entities/user') const { wlogger } = require('../adapters/wlogger') class UserLib { @@ -17,6 +17,7 @@ class UserLib { } // Encapsulate dependencies + this.UserEntity = new UserEntity() this.UserModel = this.adapters.localdb.Users } @@ -24,17 +25,9 @@ class UserLib { async createUser (userObj) { try { // Input Validation - if (!userObj.email || typeof userObj.email !== 'string') { - throw new Error("Property 'email' must be a string!") - } - if (!userObj.password || typeof userObj.password !== 'string') { - throw new Error("Property 'password' must be a string!") - } - if (!userObj.name || typeof userObj.name !== 'string') { - throw new Error("Property 'name' must be a string!") - } - const user = new this.UserModel(userObj) + const userEntity = this.UserEntity.validate(userObj) + const user = new this.UserModel(userEntity) // Enforce default value of 'user' user.type = 'user' diff --git a/test/unit/use-cases/users.use-case.unit.js b/test/unit/use-cases/users.use-case.unit.js index 0ef372d..153da62 100644 --- a/test/unit/use-cases/users.use-case.unit.js +++ b/test/unit/use-cases/users.use-case.unit.js @@ -57,7 +57,7 @@ describe('#users-use-case', () => { } catch (err) { // console.log(err) // assert.equal(err.status, 422) - assert.include(err.message, 'Cannot read property') + assert.include(err.message, "Property 'email' must be a string!") } })