diff --git a/bin/server.js b/bin/server.js index a89d1b7..bf0c733 100644 --- a/bin/server.js +++ b/bin/server.js @@ -57,8 +57,8 @@ async function startServer () { restApi.attachControllers(app) // Custom Middleware Modules - const modules = require('../src/modules') - modules(app) + // const modules = require('../src/modules') + // modules(app) // Enable CORS for testing // THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION diff --git a/src/controllers/rest-api/index.js b/src/controllers/rest-api/index.js index b0c8628..ba933bc 100644 --- a/src/controllers/rest-api/index.js +++ b/src/controllers/rest-api/index.js @@ -13,6 +13,7 @@ const AuthRESTController = require('./auth') const UserRESTController = require('./users') const ContactRESTController = require('./contact') +const LogsRESTController = require('./logs') // Load the Clean Architecture Adapters library // const adapters = require('../adapters') @@ -52,6 +53,10 @@ function attachRESTControllers (app) { // Attach the REST API Controllers associated with the /contact route const contactRESTController = new ContactRESTController() contactRESTController.attach(app) + + // Attach the REST API Controllers associated with the /logs route + const logsRESTController = new LogsRESTController() + logsRESTController.attach(app) } // Add the JSON RPC router to the ipfs-coord adapter. diff --git a/src/modules/logapi/controller.js b/src/controllers/rest-api/logs/controller.js similarity index 97% rename from src/modules/logapi/controller.js rename to src/controllers/rest-api/logs/controller.js index 97346dd..2e5d710 100644 --- a/src/modules/logapi/controller.js +++ b/src/controllers/rest-api/logs/controller.js @@ -1,5 +1,4 @@ - -const LogsApiLib = require('../../lib/logapi') +const LogsApiLib = require('../../../lib/logapi') const logsApiLib = new LogsApiLib() let _this diff --git a/src/controllers/rest-api/logs/index.js b/src/controllers/rest-api/logs/index.js new file mode 100644 index 0000000..c8544c2 --- /dev/null +++ b/src/controllers/rest-api/logs/index.js @@ -0,0 +1,17 @@ +/* + REST API library for /logs route. +*/ + +const LogsRESTRouter = require('./router') + +class LogsRESTController { + constructor (localConfig = {}) { + this.logsRESTRouter = new LogsRESTRouter() + } + + attach (app) { + this.logsRESTRouter.attachControllers(app) + } +} + +module.exports = LogsRESTController diff --git a/src/controllers/rest-api/logs/router.js b/src/controllers/rest-api/logs/router.js new file mode 100644 index 0000000..d4b1ccf --- /dev/null +++ b/src/controllers/rest-api/logs/router.js @@ -0,0 +1,43 @@ +/* + REST Router for the /logs route. +*/ + +// Public npm libraries. +const Router = require('koa-router') + +// Local libraries. +const LogsRESTControllerLib = require('./controller') +const Validators = require('../middleware/validators') + +// let _this + +class LogsRESTRouter { + constructor (localConfig = {}) { + // Encapsulate dependencies. + this.logsRESTController = new LogsRESTControllerLib() + this.validators = new Validators() + + // Instantiate the router and set the base route. + const baseUrl = '/logs' + this.router = new Router({ prefix: baseUrl }) + + // _this = this + } + + attachControllers (app) { + if (!app) { + throw new Error( + 'Must pass app object when attaching REST API controllers.' + ) + } + + // Define the routes and attach the controller. + this.router.post('/', this.logsRESTController.getLogs) + + // Attach the Controller routes to the Koa app. + app.use(this.router.routes()) + app.use(this.router.allowedMethods()) + } +} + +module.exports = LogsRESTRouter diff --git a/src/modules/index.js b/src/modules/index.js deleted file mode 100644 index 5628dc2..0000000 --- a/src/modules/index.js +++ /dev/null @@ -1,55 +0,0 @@ -const glob = require('glob') -const Router = require('koa-router') - -module.exports = function initModules (app) { - glob( - `${__dirname.toString()}/*`, - { ignore: '**/index.js' }, - (err, matches) => { - if (err) { - throw err - } - - // Loop through each sub-directory in the modules directory. - matches.forEach((mod) => { - // console.log(`router = ${mod}/router`) - const router = require(`${mod}/router`) - - const routes = router.routes - const baseUrl = router.baseUrl - const instance = new Router({ prefix: baseUrl }) - - // console.log(`routes: ${JSON.stringify(routes, null, 2)}`) - - // Loop through each route defined in the router.js file. - routes.forEach((config) => { - // console.log(`modules/index.js config: ${JSON.stringify(config, null, 2)}`) - // const { - // method = '', - // route = '', - // handlers = [] - // } = config - const method = config.method || '' - const route = config.route || '' - const handlers = config.handlers || [] - - const lastHandler = handlers.pop() - - instance[method.toLowerCase()]( - route, - ...handlers, - async function (ctx) { - // console.log(`typeof lastHandler: ${typeof (lastHandler)}`) - // return await lastHandler(ctx) - return lastHandler(ctx) - } - ) - - // console.log(`instance: ${JSON.stringify(instance, null, 2)}`) - - app.use(instance.routes()).use(instance.allowedMethods()) - }) - }) - } - ) -} diff --git a/src/modules/logapi/router.js b/src/modules/logapi/router.js deleted file mode 100644 index 50fcebf..0000000 --- a/src/modules/logapi/router.js +++ /dev/null @@ -1,35 +0,0 @@ -// const validator = require('../../middleware/validators') -const LogApi = require('./controller') -const logApi = new LogApi() - -module.exports.baseUrl = '/logapi' - -module.exports.routes = [ - { - method: 'POST', - route: '/', - handlers: [logApi.getLogs] - } - /* - { - method: 'GET', - route: '/', - handlers: [validator.ensureUser, user.getUsers] - }, - { - method: 'GET', - route: '/:id', - handlers: [validator.ensureUser, user.getUser] - }, - { - method: 'PUT', - route: '/:id', - handlers: [validator.ensureTargetUserOrAdmin, user.getUser, user.updateUser] - }, - { - method: 'DELETE', - route: '/:id', - handlers: [validator.ensureTargetUserOrAdmin, user.getUser, user.deleteUser] - } - */ -] diff --git a/test/e2e/automated/temp/a06-logapi.rest-e2e.js b/test/e2e/automated/a06-logapi.rest-e2e.js similarity index 90% rename from test/e2e/automated/temp/a06-logapi.rest-e2e.js rename to test/e2e/automated/a06-logapi.rest-e2e.js index 82ee275..5ea6dc4 100644 --- a/test/e2e/automated/temp/a06-logapi.rest-e2e.js +++ b/test/e2e/automated/a06-logapi.rest-e2e.js @@ -9,7 +9,7 @@ util.inspect.defaultOptions = { depth: 1 } const LOCALHOST = `http://localhost:${config.port}` -const LogsController = require('../../../src/modules/logapi/controller') +const LogsController = require('../../../src/controllers/rest-api/logs/controller') const mockContext = require('../../unit/mocks/ctx-mock').context let sandbox @@ -23,12 +23,12 @@ describe('LogsApi', () => { afterEach(() => sandbox.restore()) - describe('POST /logapi', () => { + describe('POST /logs', () => { it('should return false if password is not provided', async () => { try { const options = { method: 'post', - url: `${LOCALHOST}/logapi`, + url: `${LOCALHOST}/logs`, data: {} } @@ -38,11 +38,12 @@ describe('LogsApi', () => { assert(false, 'Unexpected result') } }) + it('should return log', async () => { try { const options = { method: 'post', - url: `${LOCALHOST}/logapi`, + url: `${LOCALHOST}/logs`, data: { password: 'test' } @@ -59,6 +60,7 @@ describe('LogsApi', () => { assert(false, 'Unexpected result') } }) + it('should return false if files are not found!', async () => { try { sandbox.stub(uut.logsApiLib, 'getLogs').resolves({ @@ -80,10 +82,13 @@ describe('LogsApi', () => { assert.fail('Unexpected result') } }) + it('should catch and handle errors', async () => { try { // Force an error - sandbox.stub(uut.logsApiLib.fs, 'existsSync').throws(new Error('test error')) + sandbox + .stub(uut.logsApiLib.fs, 'existsSync') + .throws(new Error('test error')) // Mock the context object. const ctx = mockContext() @@ -101,6 +106,7 @@ describe('LogsApi', () => { assert.include(err.message, 'test error') } }) + it('should throw unhandled error', async () => { try { // Force an error diff --git a/test/unit/rest-api/a06-logapi.rest-unit.js b/test/unit/rest-api/a06-logapi.rest-unit.js index 88a955c..700de21 100644 --- a/test/unit/rest-api/a06-logapi.rest-unit.js +++ b/test/unit/rest-api/a06-logapi.rest-unit.js @@ -6,7 +6,7 @@ const assert = require('chai').assert const sinon = require('sinon') -const LogsApiController = require('../../../src/modules/logapi/controller') +const LogsApiController = require('../../../src/controllers/rest-api/logs/controller') let uut let sandbox let ctx @@ -14,8 +14,7 @@ let ctx const mockContext = require('../../unit/mocks/ctx-mock').context describe('Logapi', () => { - before(async () => { - }) + before(async () => {}) beforeEach(() => { uut = new LogsApiController() @@ -42,7 +41,9 @@ describe('Logapi', () => { it('should return 500 status on biz logic Unhandled error', async () => { try { // eslint-disable - sandbox.stub(uut.logsApiLib, 'getLogs').returns(Promise.reject(new Error())) + sandbox + .stub(uut.logsApiLib, 'getLogs') + .returns(Promise.reject(new Error())) ctx.request.body = { password: 'test'