describe('corsMiddleware', () => { it('should send response directly', async () => { const req = new MockExpressRequest({ method: 'OPTIONS', }); corsMiddleware(req, res, () => { throw new Error('next should not be calld'); }); assert.called(res.header); assert.calledOnce(res.send); }); });
describe('deleteLogo', () => { it('should return a function.', () => { assert(typeof deleteLogo() === 'function') }) it('should delete the logo file.', async () => { fetchMock.delete(/.*\/settings\/logos\/.*/, 204) await deleteLogo('1', callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(callbacks.onSuccess.calledOnce) assert(!callbacks.onFailure.called) assert(dispatchSpy.firstCall.args[0].type === REMOVE_LOGO) assert(settings, dispatchSpy.firstCall.args[0].id === '1') }) })
// @todo https://github.com/gajus/slonik/issues/39 // eslint-disable-next-line ava/no-skip-test test.skip('57P01 error causes the connection to be rejected (EXPLICIT connection)', async (t) => { const pool = createPool(); pool.querySpy.rejects(createErrorWithCode('57P01')); const spy = sinon.spy(); const error = await t.throwsAsync(pool.connect(async (connection) => { try { await connection.query(sql`SELECT 1`); } catch { // } spy(); })); t.true(error instanceof BackendTerminatedError); t.assert(spy.called === false); });
test('works with MemberExpression', () => { const mockBody = {}; const mockProperty = {}; const mockPushState = sinon.spy(); const mockPopState = sinon.spy(); const mockScan = sinon.spy(); const mockInstance = { scan: mockScan, pushState: mockPushState, popState: mockPopState, }; FunctionTracer.prototype.scan.call(mockInstance, { type: 'MemberExpression', object: mockBody, property: mockProperty }); assert.ok(mockScan.called); assert.equal(mockScan.args[0][0], mockBody); assert.equal(mockScan.args[1][0], mockProperty); assert.equal(mockPushState.args[0][0], 'memberExpression'); assert.equal(mockPopState.args[0][0], 'memberExpression'); });
describe('GET /service/philips-hue/light', () => { it('should get lights', async () => { const philipsHueController = PhilipsHueControllers(philipsHueLightService); await philipsHueController['get /api/v1/service/philips-hue/light'].controller({}, res); assert.called(philipsHueLightService.getLights); }); });
describe('gateway.login', () => { const variable = { getValue: fake.resolves(null), setValue: fake.resolves(null), }; const gateway = new Gateway(variable, event, system, sequelize, config); it('should login to gladys gateway', async () => { const loginResults = await gateway.login('tony.stark@gladysassistant.com', 'warmachine123'); expect(loginResults).to.have.property('two_factor_token'); assert.calledWith(gateway.gladysGatewayClient.login, 'tony.stark@gladysassistant.com', 'warmachine123'); }); it('should login two factor to gladys gateway', async () => { await gateway.loginTwoFactor('token', '123456'); assert.calledWith(gateway.gladysGatewayClient.loginInstance, 'token', '123456'); assert.called(variable.getValue); assert.called(variable.setValue); assert.calledOnce(gateway.gladysGatewayClient.createInstance); }); });
describe('postMaintenance', () => { it('should return a function.', () => { assert(typeof postMaintenance({}) === 'function') }) it('should post a new maintenance.', async () => { fetchMock.post(/.*\/maintenances/, { body: [maintenance], headers: {'Content-Type': 'application/json'} }) await postMaintenance({}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(callbacks.onSuccess.calledOnce) assert(!callbacks.onFailure.called) assert(dispatchSpy.firstCall.args[0].type === ADD_MAINTENANCE) assert.deepEqual([maintenance], dispatchSpy.firstCall.args[0].response) }) it('should handle error properly.', async () => { fetchMock.post(/.*\/maintenances/, { status: 400, body: {} }) await postMaintenance({}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(!callbacks.onSuccess.called) assert(callbacks.onFailure.calledOnce) assert(!dispatchSpy.called) }) })
describe('GET /service/philips-hue/scene', () => { it('should get scenes', async () => { const philipsHueController = PhilipsHueControllers(philipsHueLightService); await philipsHueController['get /api/v1/service/philips-hue/scene'].controller({}, res); assert.called(philipsHueLightService.getScenes); }); });
describe('updateMaintenance', () => { it('should return a function.', () => { assert(typeof updateMaintenance({}) === 'function') }) it('should update the existing maintenance.', async () => { fetchMock.patch(/.*\/maintenances\/.*/, { body: [maintenance], headers: {'Content-Type': 'application/json'} }) await updateMaintenance({maintenanceID: 'id'}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(callbacks.onSuccess.calledOnce) assert(!callbacks.onFailure.called) assert(dispatchSpy.firstCall.args[0].type === EDIT_MAINTENANCE) assert.deepEqual([maintenance], dispatchSpy.firstCall.args[0].response) }) it('should handle error properly.', async () => { fetchMock.patch(/.*\/maintenances\/.*/, { status: 400, body: {} }) await updateMaintenance({maintenanceID: 'id'}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(!callbacks.onSuccess.called) assert(callbacks.onFailure.calledOnce) assert(!dispatchSpy.called) }) })
describe('corsMiddleware', () => { it('should send response directly', async () => { const req = new MockExpressRequest({ method: 'OPTIONS', }); corsMiddleware(req, res, () => { throw new Error('next should not be calld'); }); assert.called(res.header); assert.calledOnce(res.send); }); });
describe('GET /service/philips-hue/scene', () => { it('should get scenes', async () => { const philipsHueController = PhilipsHueControllers(philipsHueLightService); await philipsHueController['get /api/v1/service/philips-hue/scene'].controller({}, res); assert.called(philipsHueLightService.getScenes); }); });
describe('GET /service/philips-hue/light', () => { it('should get lights', async () => { const philipsHueController = PhilipsHueControllers(philipsHueLightService); await philipsHueController['get /api/v1/service/philips-hue/light'].controller({}, res); assert.called(philipsHueLightService.getLights); }); });
describe('deleteLogo', () => { it('should return a function.', () => { assert(typeof deleteLogo() === 'function') }) it('should delete the logo file.', async () => { fetchMock.delete(/.*\/settings\/logos\/.*/, 204) await deleteLogo('1', callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(callbacks.onSuccess.calledOnce) assert(!callbacks.onFailure.called) assert(dispatchSpy.firstCall.args[0].type === REMOVE_LOGO) assert(settings, dispatchSpy.firstCall.args[0].id === '1') }) })
describe('gateway.login', () => { const variable = { getValue: fake.resolves(null), setValue: fake.resolves(null), }; const gateway = new Gateway(variable, event, system, sequelize, config); it('should login to gladys gateway', async () => { const loginResults = await gateway.login('tony.stark@gladysassistant.com', 'warmachine123'); expect(loginResults).to.have.property('two_factor_token'); assert.calledWith(gateway.gladysGatewayClient.login, 'tony.stark@gladysassistant.com', 'warmachine123'); }); it('should login two factor to gladys gateway', async () => { await gateway.loginTwoFactor('token', '123456'); assert.calledWith(gateway.gladysGatewayClient.loginInstance, 'token', '123456'); assert.called(variable.getValue); assert.called(variable.setValue); assert.calledOnce(gateway.gladysGatewayClient.createInstance); }); });
describe('postMaintenance', () => { it('should return a function.', () => { assert(typeof postMaintenance({}) === 'function') }) it('should post a new maintenance.', async () => { fetchMock.post(/.*\/maintenances/, { body: [maintenance], headers: {'Content-Type': 'application/json'} }) await postMaintenance({}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(callbacks.onSuccess.calledOnce) assert(!callbacks.onFailure.called) assert(dispatchSpy.firstCall.args[0].type === ADD_MAINTENANCE) assert.deepEqual([maintenance], dispatchSpy.firstCall.args[0].response) }) it('should handle error properly.', async () => { fetchMock.post(/.*\/maintenances/, { status: 400, body: {} }) await postMaintenance({}, callbacks)(dispatchSpy) assert(callbacks.onLoad.calledOnce) assert(!callbacks.onSuccess.called) assert(callbacks.onFailure.calledOnce) assert(!dispatchSpy.called) }) })