|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Test framework dependencies |
| 4 | +const Lab = require('@hapi/lab') |
| 5 | +const Code = require('@hapi/code') |
| 6 | +const Sinon = require('sinon') |
| 7 | + |
| 8 | +const { experiment, test, beforeEach, afterEach } = exports.lab = Lab.script() |
| 9 | +const { expect } = Code |
| 10 | +const sandbox = Sinon.createSandbox() |
| 11 | + |
| 12 | +// Things we need to stub |
| 13 | +const SystemProxyService = require( |
| 14 | + '../../../../src/internal/lib/connectors/services/water/SystemProxyService' |
| 15 | +) |
| 16 | + |
| 17 | +// Thing under test |
| 18 | +const controller = require('../../../../src/internal/modules/system-proxy/controller') |
| 19 | + |
| 20 | +experiment('System proxy controller', () => { |
| 21 | + let request |
| 22 | + let h |
| 23 | + |
| 24 | + let codeFake |
| 25 | + let typeFake |
| 26 | + let headerFake |
| 27 | + let responseFake |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + codeFake = sandbox.fake() |
| 31 | + typeFake = sandbox.fake() |
| 32 | + headerFake = sandbox.fake.returns({ type: typeFake }) |
| 33 | + responseFake = sandbox.fake.returns({ header: headerFake, code: codeFake }) |
| 34 | + |
| 35 | + h = { |
| 36 | + response: responseFake |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + sandbox.restore() |
| 42 | + }) |
| 43 | + |
| 44 | + experiment('.getSystemProxy', () => { |
| 45 | + beforeEach(() => { |
| 46 | + request = { |
| 47 | + params: { |
| 48 | + tail: 'status' |
| 49 | + }, |
| 50 | + payload: null |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + experiment('when the request is valid', () => { |
| 55 | + beforeEach(() => { |
| 56 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').resolves('OK') |
| 57 | + }) |
| 58 | + |
| 59 | + test('returns whatever the system returns', async () => { |
| 60 | + await controller.getSystemProxy(request, h) |
| 61 | + |
| 62 | + const result = responseFake.lastCall.args[0] |
| 63 | + |
| 64 | + expect(result).to.equal('OK') |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + experiment('when the request is invalid', () => { |
| 69 | + let error |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + error = { |
| 73 | + error: 'OH NO', |
| 74 | + statusCode: 404, |
| 75 | + message: '404 - "OH NO"', |
| 76 | + name: 'StatusCodeError' |
| 77 | + } |
| 78 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').rejects(error) |
| 79 | + }) |
| 80 | + |
| 81 | + test('returns the system error details', async () => { |
| 82 | + await controller.getSystemProxy(request, h) |
| 83 | + |
| 84 | + const result = responseFake.lastCall.args[0] |
| 85 | + |
| 86 | + expect(result).to.equal(error.error) |
| 87 | + }) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + experiment('.getSystemJsProxy', () => { |
| 92 | + beforeEach(() => { |
| 93 | + request = { |
| 94 | + payload: null |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + experiment('when the request is valid', () => { |
| 99 | + beforeEach(() => { |
| 100 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').resolves('OK') |
| 101 | + }) |
| 102 | + |
| 103 | + test('returns whatever the system returns', async () => { |
| 104 | + await controller.getSystemJsProxy(request, h) |
| 105 | + |
| 106 | + const result = responseFake.lastCall.args[0] |
| 107 | + |
| 108 | + expect(result).to.equal('OK') |
| 109 | + }) |
| 110 | + |
| 111 | + test('sets the expected header', async () => { |
| 112 | + await controller.getSystemJsProxy(request, h) |
| 113 | + |
| 114 | + const result = headerFake.lastCall.args |
| 115 | + |
| 116 | + expect(result).to.include(['cache-control', 'no-cache']) |
| 117 | + }) |
| 118 | + |
| 119 | + test('sets the expected type', async () => { |
| 120 | + await controller.getSystemJsProxy(request, h) |
| 121 | + |
| 122 | + const result = typeFake.lastCall.args[0] |
| 123 | + |
| 124 | + expect(result).to.equal('application/javascript') |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + experiment('when the request is invalid', () => { |
| 129 | + beforeEach(() => { |
| 130 | + const error = { |
| 131 | + statusCode: 404, |
| 132 | + message: 'OH NO' |
| 133 | + } |
| 134 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').rejects(error) |
| 135 | + }) |
| 136 | + |
| 137 | + test('returns the system error status code', async () => { |
| 138 | + await controller.getSystemJsProxy(request, h) |
| 139 | + |
| 140 | + const result = codeFake.lastCall.firstArg |
| 141 | + |
| 142 | + expect(result).to.equal(404) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + experiment('.getSystemCssProxy', () => { |
| 148 | + beforeEach(() => { |
| 149 | + request = { |
| 150 | + payload: null |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + experiment('when the request is valid', () => { |
| 155 | + beforeEach(() => { |
| 156 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').resolves('OK') |
| 157 | + }) |
| 158 | + |
| 159 | + test('returns whatever the system returns', async () => { |
| 160 | + await controller.getSystemCssProxy(request, h) |
| 161 | + |
| 162 | + const result = responseFake.lastCall.args[0] |
| 163 | + |
| 164 | + expect(result).to.equal('OK') |
| 165 | + }) |
| 166 | + |
| 167 | + test('sets the expected header', async () => { |
| 168 | + await controller.getSystemCssProxy(request, h) |
| 169 | + |
| 170 | + const result = headerFake.lastCall.args |
| 171 | + |
| 172 | + expect(result).to.include(['cache-control', 'no-cache']) |
| 173 | + }) |
| 174 | + |
| 175 | + test('sets the expected type', async () => { |
| 176 | + await controller.getSystemCssProxy(request, h) |
| 177 | + |
| 178 | + const result = typeFake.lastCall.args[0] |
| 179 | + |
| 180 | + expect(result).to.equal('text/css') |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + experiment('when the request is invalid', () => { |
| 185 | + beforeEach(() => { |
| 186 | + const error = { |
| 187 | + statusCode: 404, |
| 188 | + message: 'OH NO' |
| 189 | + } |
| 190 | + sandbox.stub(SystemProxyService.prototype, 'getToPath').rejects(error) |
| 191 | + }) |
| 192 | + |
| 193 | + test('returns the system error status code', async () => { |
| 194 | + await controller.getSystemCssProxy(request, h) |
| 195 | + |
| 196 | + const result = codeFake.lastCall.firstArg |
| 197 | + |
| 198 | + expect(result).to.equal(404) |
| 199 | + }) |
| 200 | + }) |
| 201 | + }) |
| 202 | +}) |
0 commit comments