/* Unit tests for the REST API handler for the /verify endpoints. */ // Public npm libraries import { assert } from 'chai' import sinon from 'sinon' // Local support libraries import adapters from '../../../mocks/adapters/index.js' import UseCasesMock from '../../../mocks/use-cases/index.js' // const app = require('../../../mocks/app-mock') import VerifyRESTController from '../../../../../src/controllers/rest-api/verify/controller.js' import { context as mockContext } from '../../../mocks/ctx-mock.js' let uut let sandbox let ctx describe('#Verify-REST-Controller', () => { // const testUser = {} beforeEach(() => { const useCases = new UseCasesMock() uut = new VerifyRESTController({ adapters, useCases }) sandbox = sinon.createSandbox() // Mock the context object. ctx = mockContext() }) afterEach(() => sandbox.restore()) describe('#constructor', () => { it('should throw an error if adapters are not passed in', () => { try { uut = new VerifyRESTController() assert.fail('Unexpected code path') } catch (err) { assert.include( err.message, 'Instance of Adapters library required when instantiating /verify REST Controller.' ) } }) it('should throw an error if useCases are not passed in', () => { try { uut = new VerifyRESTController({ adapters }) assert.fail('Unexpected code path') } catch (err) { assert.include( err.message, 'Instance of Use Cases library required when instantifying /verify REST Controller.' ) } }) }) describe('#verify', () => { it('should return the verify result', async () => { // Mock dependencies ctx.body = {} const result = { valid: true } sandbox.stub(uut.useCases.verify, 'verify').resolves(result) await uut.verify(ctx) assert.isObject(ctx.body) assert.property(ctx.body, 'valid') }) it('should catch and throw useCases error', async () => { try { // Force an error sandbox.stub(uut.useCases.verify, 'verify').throws(new Error('test error')) await uut.verify(ctx) } catch (err) { // console.log('err: ', err) assert.include(err.message, 'test error') } }) }) describe('#handleError', () => { it('should still throw error if there is no message', () => { try { const err = { status: 404 } uut.handleError(ctx, err) } catch (err) { assert.include(err.message, 'Not Found') } }) it('should throw error with message', () => { try { const err = { status: 422, message: 'test error' } uut.handleError(ctx, err) } catch (err) { assert.include(err.message, 'test error') } }) }) })