2021-08-12 15:04:37 +02:00
|
|
|
import supertest from 'supertest';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
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
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
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);
|
|
|
|
const app = getApp(config, stores, services, eventBus);
|
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();
|
|
|
|
services.clientMetricsService.destroy();
|
|
|
|
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;
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
const setup = getSetup();
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should give 500 when db is failing', () => {
|
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
|
|
|
|
const app = getApp(createTestConfig(), failingStores, services, eventBus);
|
|
|
|
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
|
|
|
});
|