Skip to content

Commit 866e794

Browse files
committed
test: Add webhook filter / http server integration test
1 parent 178be63 commit 866e794

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

src/handle-github-webhook.spec.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
3+
/* tslint:disable:no-implicit-dependencies no-magic-numbers no-let no-duplicate-imports */
44

55
import { expect } from 'chai';
66
import Sinon from 'sinon';
77
import nock from 'nock';
8+
import Http from 'http';
9+
import { promisify } from 'util';
10+
import { AddressInfo } from 'net';
11+
import Discord from 'discord.js';
12+
import fetch from 'node-fetch';
813
import handleGithubWebhook from './handle-github-webhook';
14+
import * as handleGithubWebhookModule from './handle-github-webhook';
915
import * as Config from './config';
16+
import createHttpServer from './http-server';
1017

1118
const MOCK_DISCORD_WEBHOOK_URL = 'https://discord.com/api/webhooks/12345/s3creTk3Y';
1219

13-
describe('handleGithubWebhook', () => {
20+
describe('handleGithubWebhook - unit tests', () => {
1421
beforeEach(() => {
1522
Sinon.stub(Config, 'getConfig').returns(MOCK_DISCORD_WEBHOOK_URL);
1623
});
@@ -43,3 +50,65 @@ describe('handleGithubWebhook', () => {
4350
expect(result).to.eql({ statusCode: 599 });
4451
});
4552
});
53+
54+
describe('handleGithubWebhook - integration tests', () => {
55+
let httpServer: Http.Server;
56+
let baseUrl: string;
57+
58+
beforeEach(async () => {
59+
nock.cleanAll();
60+
nock.enableNetConnect();
61+
62+
const discordClientMock = {
63+
uptime: 10,
64+
} as Discord.Client;
65+
66+
httpServer = createHttpServer(discordClientMock, [], [], []);
67+
await promisify(httpServer.listen.bind(httpServer))(0);
68+
69+
baseUrl = `http://localhost:${(httpServer.address() as AddressInfo).port}`;
70+
});
71+
72+
afterEach(() => {
73+
httpServer.close();
74+
Sinon.restore();
75+
});
76+
77+
it('should respond with 400 on invalid request', async () => {
78+
const jsonParseSpy = Sinon.spy(JSON, 'parse');
79+
80+
const { status } = await fetch(`${baseUrl}/githubWebhook/test`, {
81+
method: 'POST',
82+
body: 'invalid json',
83+
});
84+
85+
expect(jsonParseSpy).to.throw();
86+
expect(status).to.eql(400);
87+
});
88+
89+
it('should call handleGithubWebhook with url and body', async () => {
90+
const handleGithubWebhookStub = Sinon.stub(handleGithubWebhookModule, 'default');
91+
handleGithubWebhookStub.resolves({ statusCode: 200 });
92+
93+
await fetch(`${baseUrl}/githubWebhook/test`, {
94+
method: 'POST',
95+
body: '{"a":1}',
96+
});
97+
98+
expect(handleGithubWebhookStub).to.have.been.calledOnceWithExactly('/githubWebhook/test', {
99+
a: 1,
100+
});
101+
});
102+
103+
it('should respond with status code from handleGithubWebhook', async () => {
104+
const handleGithubWebhookStub = Sinon.stub(handleGithubWebhookModule, 'default');
105+
handleGithubWebhookStub.resolves({ statusCode: 599 });
106+
107+
const { status } = await fetch(`${baseUrl}/githubWebhook/test`, {
108+
method: 'POST',
109+
body: '{"a":1}',
110+
});
111+
112+
expect(status).eql(599);
113+
});
114+
});

src/http-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function createHttpServer(
1010
errors: Error[],
1111
warnings: string[],
1212
debugs: string[]
13-
) {
13+
): Http.Server {
1414
return Http.createServer(async (req, res) => {
1515
if (req.url?.startsWith('/githubWebhook')) {
1616
const chunks = [];

0 commit comments

Comments
 (0)