2021-08-12 15:04:37 +02:00
|
|
|
import supertest from 'supertest';
|
|
|
|
import { createServices } from '../services';
|
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
2016-11-13 15:31:10 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
import createStores from '../../test/fixtures/store';
|
|
|
|
import getLogger from '../../test/fixtures/no-logger';
|
|
|
|
import getApp from '../app';
|
|
|
|
import { IUnleashStores } from '../types';
|
2016-11-13 15:31:10 +01:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
async function getSetup() {
|
2021-08-12 15:04:37 +02:00
|
|
|
const stores = createStores();
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig();
|
2021-05-28 11:10:24 +02:00
|
|
|
const services = createServices(stores, config);
|
2022-01-06 10:31:00 +01:00
|
|
|
const app = await getApp(config, stores, services);
|
2016-11-13 15:31:10 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
request: supertest(app),
|
2021-08-12 15:04:37 +02:00
|
|
|
stores,
|
2021-05-28 11:10:24 +02:00
|
|
|
destroy: () => {
|
|
|
|
services.versionService.destroy();
|
2021-12-09 21:25:06 +01:00
|
|
|
services.clientInstanceService.destroy();
|
2021-05-28 11:10:24 +02:00
|
|
|
services.apiTokenService.destroy();
|
|
|
|
},
|
2016-11-13 15:31:10 +01:00
|
|
|
};
|
|
|
|
}
|
2021-05-28 11:10:24 +02:00
|
|
|
let request;
|
|
|
|
let destroy;
|
2021-08-12 15:04:37 +02:00
|
|
|
let stores;
|
2022-01-06 10:31:00 +01:00
|
|
|
beforeEach(async () => {
|
|
|
|
const setup = await getSetup();
|
2021-05-28 11:10:24 +02:00
|
|
|
request = setup.request;
|
|
|
|
destroy = setup.destroy;
|
2021-08-12 15:04:37 +02:00
|
|
|
stores = setup.stores;
|
2021-05-28 11:10:24 +02:00
|
|
|
});
|
2016-11-13 15:31:10 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterEach(() => {
|
|
|
|
destroy();
|
2021-01-06 13:25:25 +01:00
|
|
|
getLogger.setMuteError(false);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('should give 500 when db is failing', async () => {
|
2021-10-29 11:22:31 +02:00
|
|
|
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
|
2021-08-12 15:04:37 +02:00
|
|
|
const config = createTestConfig();
|
|
|
|
const failingStores: Partial<IUnleashStores> = {
|
|
|
|
// @ts-ignore
|
|
|
|
featureTypeStore: {
|
|
|
|
getAll: () => Promise.reject(new Error('db error')),
|
|
|
|
},
|
|
|
|
clientMetricsStore: {
|
|
|
|
// @ts-ignore
|
|
|
|
on: () => {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// @ts-ignore
|
|
|
|
const services = createServices(failingStores, config);
|
|
|
|
// @ts-ignore
|
2022-01-06 10:31:00 +01:00
|
|
|
const app = await getApp(createTestConfig(), failingStores, services);
|
2021-08-12 15:04:37 +02:00
|
|
|
request = supertest(app);
|
2021-01-06 13:25:25 +01:00
|
|
|
getLogger.setMuteError(true);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect.assertions(2);
|
2021-08-12 15:04:37 +02:00
|
|
|
stores.featureToggleStore.getAll = () =>
|
|
|
|
Promise.reject(new Error('db error'));
|
2017-11-02 09:23:38 +01:00
|
|
|
return request
|
|
|
|
.get('/health')
|
|
|
|
.expect(500)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.status).toBe(500);
|
|
|
|
expect(res.body.health).toBe('BAD');
|
2017-11-02 09:23:38 +01:00
|
|
|
});
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should give 200 when db is not failing', () => {
|
|
|
|
expect.assertions(0);
|
2017-06-28 10:20:22 +02:00
|
|
|
return request.get('/health').expect(200);
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should give health=GOOD when db is not failing', () => {
|
|
|
|
expect.assertions(2);
|
2017-11-02 09:23:38 +01:00
|
|
|
return request
|
|
|
|
.get('/health')
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.status).toBe(200);
|
|
|
|
expect(res.body.health).toBe('GOOD');
|
2017-11-02 09:23:38 +01:00
|
|
|
});
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|