|
1 | 1 | /* eslint no-implicit-dependencies: "off" */ |
2 | 2 | /* 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 */ |
4 | 4 |
|
5 | 5 | import { expect } from 'chai'; |
6 | 6 | import Sinon from 'sinon'; |
7 | 7 | 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'; |
8 | 13 | import handleGithubWebhook from './handle-github-webhook'; |
| 14 | +import * as handleGithubWebhookModule from './handle-github-webhook'; |
9 | 15 | import * as Config from './config'; |
| 16 | +import createHttpServer from './http-server'; |
10 | 17 |
|
11 | 18 | const MOCK_DISCORD_WEBHOOK_URL = 'https://discord.com/api/webhooks/12345/s3creTk3Y'; |
12 | 19 |
|
13 | | -describe('handleGithubWebhook', () => { |
| 20 | +describe('handleGithubWebhook - unit tests', () => { |
14 | 21 | beforeEach(() => { |
15 | 22 | Sinon.stub(Config, 'getConfig').returns(MOCK_DISCORD_WEBHOOK_URL); |
16 | 23 | }); |
@@ -43,3 +50,65 @@ describe('handleGithubWebhook', () => { |
43 | 50 | expect(result).to.eql({ statusCode: 599 }); |
44 | 51 | }); |
45 | 52 | }); |
| 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 | +}); |
0 commit comments