2016-11-13 15:31:10 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2017-06-28 10:20:22 +02:00
|
|
|
const store = require('./../../test/fixtures/store');
|
2016-11-13 15:31:10 +01:00
|
|
|
const supertest = require('supertest');
|
2017-06-28 10:20:22 +02:00
|
|
|
const getApp = require('../app');
|
2016-11-13 15:31:10 +01:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
function getSetup() {
|
2016-11-13 15:31:10 +01:00
|
|
|
const stores = store.createStores();
|
|
|
|
const db = stores.db;
|
2016-11-13 21:07:14 +01:00
|
|
|
const app = getApp({
|
2016-11-13 15:31:10 +01:00
|
|
|
baseUriPath: '',
|
|
|
|
stores,
|
2016-11-30 23:41:57 +01:00
|
|
|
eventBus,
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
db,
|
|
|
|
request: supertest(app),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should give 500 when db is failing', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(2);
|
2016-11-13 15:31:10 +01:00
|
|
|
const { request, db } = getSetup();
|
|
|
|
db.select = () => ({
|
|
|
|
from: () => Promise.reject(new Error('db error')),
|
|
|
|
});
|
2017-11-02 09:23:38 +01:00
|
|
|
return request
|
|
|
|
.get('/health')
|
|
|
|
.expect(500)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.status === 500);
|
|
|
|
t.true(res.body.health === 'BAD');
|
|
|
|
});
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
test('should give 200 when db is not failing', t => {
|
|
|
|
t.plan(0);
|
2016-11-13 15:31:10 +01:00
|
|
|
const { request } = getSetup();
|
2017-06-28 10:20:22 +02:00
|
|
|
return request.get('/health').expect(200);
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should give health=GOOD when db is not failing', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(2);
|
2016-11-13 15:31:10 +01:00
|
|
|
const { request } = getSetup();
|
2017-11-02 09:23:38 +01:00
|
|
|
return request
|
|
|
|
.get('/health')
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.status === 200);
|
|
|
|
t.true(res.body.health === 'GOOD');
|
|
|
|
});
|
2016-11-13 15:31:10 +01:00
|
|
|
});
|