Files
ipfs-bch-wallet-service/test/unit/misc/server-unit.js
T

88 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-06-19 20:00:25 -07:00
/*
Unit tests for the bin/server.js file
*/
// Public npm libraries
2022-09-08 08:58:39 -07:00
import { assert } from 'chai'
import sinon from 'sinon'
2022-06-19 20:00:25 -07:00
// Local libraries
2022-09-08 08:58:39 -07:00
import Server from '../../../bin/server.js'
2022-06-19 20:00:25 -07:00
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()
2022-06-27 10:09:08 -07:00
sandbox.stub(uut.controllers, 'initAdapters').resolves()
sandbox.stub(uut.controllers, 'initUseCases').resolves()
2022-06-19 20:00:25 -07:00
sandbox.stub(uut.controllers, 'attachRESTControllers').resolves()
sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true)
sandbox.stub(uut.controllers, 'attachControllers').resolves()
uut.config.env = 'dev'
2025-06-27 20:11:27 -04:00
uut.config.port = 5040
2022-06-19 20:00:25 -07:00
const result = await uut.startServer()
// console.log('result: ', result)
assert.property(result, 'env')
// Turn off the server.
uut.server.close()
// Restor config env
uut.config.env = 'test'
})
it('should exit on failure', async () => {
// Force an error
sandbox.stub(uut.mongoose, 'connect').rejects(new Error('test error'))
// Prevent default behavior of exiting the program.
sandbox.stub(uut, 'sleep').resolves()
sandbox.stub(uut.process, 'exit').returns()
await uut.startServer()
// Not throwing an error is a success
})
2023-02-08 05:37:12 -08:00
it('should warn if user admin can not be created', async () => {
// Mock dependencies and force desired code path
sandbox.stub(uut.mongoose, 'connect').resolves()
sandbox.stub(uut.controllers, 'initAdapters').resolves()
sandbox.stub(uut.controllers, 'initUseCases').resolves()
sandbox.stub(uut.controllers, 'attachRESTControllers').resolves()
sandbox.stub(uut.adminLib, 'createSystemUser').rejects('test error')
sandbox.stub(uut.controllers, 'attachControllers').resolves()
uut.config.env = 'dev'
const result = await uut.startServer()
// console.log('result: ', result)
assert.property(result, 'env')
// Turn off the server.
uut.server.close()
// Restor config env
uut.config.env = 'test'
})
})
describe('#sleep', () => {
it('should execute', async () => {
await uut.sleep(1)
2022-06-19 20:00:25 -07:00
})
})
})