Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial implementation for '/health' #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/routes/health/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ResponseObject, ResponseToolkit, ServerRoute } from '@hapi/hapi';
import { Config } from '../../config';

export const routes = (config: Config): ServerRoute[] => {
return [
{
method: 'GET',
options: {
handler: (_: Request, h: ResponseToolkit): ResponseObject => {
const isTwitchConfigOk =
!!config.TWITCH_BOT_CHANNEL && !!config.TWITCH_BOT_NAME && !!config.TWITCH_BOT_TOKEN;

const isStreamLabsConfigOk = !!config.STREAMLABS_TOKEN;

return h
.response({ config: { twitch: isTwitchConfigOk, streamLabs: isStreamLabsConfigOk } })
.code(200);
},
},
path: '/health',
},
];
};
3 changes: 2 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ServerRoute } from '@hapi/hapi';
import { routes as gitlab } from './gitlab';
import { routes as github } from './github';
import { routes as health } from './health';
import { Config } from '../config';

export const routes = (config: Config): ServerRoute[] => {
return [...github(config), ...gitlab(config)];
return [...github(config), ...gitlab(config), ...health(config)];
};
75 changes: 75 additions & 0 deletions test/routes/health/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ServerInjectOptions } from '@hapi/hapi';
import { getConfig } from '../../../src/config';
import { initServer } from '../../../src/server';

describe('/health', () => {
describe('GET /health', () => {
it('returns 200 OK if everything is well configured', async () => {
const subject = await initServer(getConfig());
const request: ServerInjectOptions = { method: 'GET', url: '/health' };

const { statusCode } = await subject.inject(request);

expect(statusCode).toEqual(200);
});

describe('Twitch Config', () => {
it('returns that the Twitch Bot is configured', async () => {
const subject = await initServer({
...getConfig(),
TWITCH_BOT_CHANNEL: 'streamdevs',
TWITCH_BOT_NAME: 'streamdevs',
TWITCH_BOT_TOKEN: 'token',
});
const request: ServerInjectOptions = { method: 'GET', url: '/health' };

const { result } = await subject.inject(request);

expect(result).toEqual(
expect.objectContaining({ config: expect.objectContaining({ twitch: true }) }),
);
});

it("returns that the Twitch Bot isn't configured", async () => {
const subject = await initServer({ ...getConfig(), TWITCH_BOT_CHANNEL: undefined });
const request: ServerInjectOptions = { method: 'GET', url: '/health' };

const { result } = await subject.inject(request);

expect(result).toEqual(
expect.objectContaining({ config: expect.objectContaining({ twitch: false }) }),
);
});
});

describe('StreamLabs Config', () => {
it('returns that StreamLabs is configured', async () => {
const subject = await initServer({
...getConfig(),
STREAMLABS_TOKEN: 'token',
});
const request: ServerInjectOptions = { method: 'GET', url: '/health' };

const { result } = await subject.inject(request);

expect(result).toEqual(
expect.objectContaining({ config: expect.objectContaining({ streamLabs: true }) }),
);
});

it('returns that StreamLabs is configured', async () => {
const subject = await initServer({
...getConfig(),
STREAMLABS_TOKEN: undefined,
});
const request: ServerInjectOptions = { method: 'GET', url: '/health' };

const { result } = await subject.inject(request);

expect(result).toEqual(
expect.objectContaining({ config: expect.objectContaining({ streamLabs: false }) }),
);
});
});
});
});