diff --git a/bin/server.js b/bin/server.js index 2d84d13..0df3b65 100644 --- a/bin/server.js +++ b/bin/server.js @@ -57,7 +57,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/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/src/controllers/index.js b/src/controllers/index.js index 1692740..69034fe 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -14,39 +14,51 @@ 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') -// 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) +class Controllers { + constructor (localConfig = {}) { + this.adapters = adapters + this.useCases = new UseCases({ adapters }) + } - // Start IPFS. - await adapters.ipfs.start() + async attachControllers (app) { + // Attach the REST controllers to the Koa app. + this.attachRESTControllers(app) - attachRPCControllers() + // 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 + ) + } } -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/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/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() }) }) }) 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) }) }) }) 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!") } })