From d8addd5b27a1042931cc7bde7a86ea9c0564ed68 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 19 Jun 2022 20:00:25 -0700 Subject: [PATCH] Trying to build unit tests for bin/server --- bin/server.js | 43 +++++++++++++++++++---------------- test/unit/misc/server-unit.js | 39 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 test/unit/misc/server-unit.js diff --git a/bin/server.js b/bin/server.js index b2797b8..66daf57 100644 --- a/bin/server.js +++ b/bin/server.js @@ -24,31 +24,36 @@ const config = require('../config') // this first. const AdminLib = require('../src/adapters/admin') const errorMiddleware = require('../src/controllers/rest-api/middleware/error') const { wlogger } = require('../src/adapters/wlogger') +const Controllers = require('../src/controllers') class Server { constructor () { + // Encapsulate dependencies this.adminLib = new AdminLib() + this.controllers = new Controllers() + this.mongoose = mongoose + this.config = config } async startServer () { try { // Create a Koa instance. const app = new Koa() - app.keys = [config.session] + app.keys = [this.config.session] // Connect to the Mongo Database. - mongoose.Promise = global.Promise - mongoose.set('useCreateIndex', true) // Stop deprecation warning. + this.mongoose.Promise = global.Promise + this.mongoose.set('useCreateIndex', true) // Stop deprecation warning. console.log( - `Connecting to MongoDB with this connection string: ${config.database}` + `Connecting to MongoDB with this connection string: ${this.config.database}` ) - await mongoose.connect(config.database, { + await this.mongoose.connect(this.config.database, { useUnifiedTopology: true, useNewUrlParser: true }) - console.log(`Starting environment: ${config.env}`) - console.log(`Debug level: ${config.debugLevel}`) + console.log(`Starting environment: ${this.config.env}`) + console.log(`Debug level: ${this.config.debugLevel}`) // MIDDLEWARE START @@ -74,19 +79,17 @@ class Server { app.use(cors({ origin: '*' })) // Attach REST API and JSON RPC controllers to the app. - const Controllers = require('../src/controllers') - const controllers = new Controllers() - await controllers.attachRESTControllers(app) + await this.controllers.attachRESTControllers(app) - app.controllers = controllers + app.controllers = this.controllers // MIDDLEWARE END - console.log(`Running server in environment: ${config.env}`) - wlogger.info(`Running server in environment: ${config.env}`) + console.log(`Running server in environment: ${this.config.env}`) + wlogger.info(`Running server in environment: ${this.config.env}`) - await app.listen(config.port) - console.log(`Server started on ${config.port}`) + await app.listen(this.config.port) + console.log(`Server started on ${this.config.port}`) // Create the system admin user. const success = await this.adminLib.createSystemUser() @@ -94,15 +97,15 @@ class Server { // Attach the other IPFS controllers. // Skip if this is a test environment. - if (config.env !== 'test') { - await controllers.attachControllers(app) + if (this.config.env !== 'test') { + await this.controllers.attachControllers(app) } // Display configuration settings console.log('\nConfiguration:') - console.log(`Circuit Relay: ${config.isCircuitRelay}`) - console.log(`IPFS TCP port: ${config.ipfsTcpPort}`) - console.log(`IPFS WS port: ${config.ipfsWsPort}\n`) + console.log(`Circuit Relay: ${this.config.isCircuitRelay}`) + console.log(`IPFS TCP port: ${this.config.ipfsTcpPort}`) + console.log(`IPFS WS port: ${this.config.ipfsWsPort}\n`) return app } catch (err) { diff --git a/test/unit/misc/server-unit.js b/test/unit/misc/server-unit.js new file mode 100644 index 0000000..1710fc7 --- /dev/null +++ b/test/unit/misc/server-unit.js @@ -0,0 +1,39 @@ +/* + Unit tests for the bin/server.js file +*/ + +// Public npm libraries +const assert = require('chai').assert +const sinon = require('sinon') + +// Local libraries +const Server = require('../../../bin/server') + +describe('#server', () => { + let uut, sandbox + + beforeEach(() => { + sandbox = sinon.createSandbox() + + uut = new Server() + }) + + afterEach(() => sandbox.restore()) + + describe('#startServer', () => { + it('should start the server', async () => { + // Mock dependencies + sandbox.stub(uut.mongoose, 'connect').resolves() + sandbox.stub(uut.controllers, 'attachRESTControllers').resolves() + sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true) + sandbox.stub(uut.controllers, 'attachControllers').resolves() + + const result = await uut.startServer() + // console.log('result: ', result) + + assert.property(result, 'env') + + result.close() + }) + }) +})