-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-notifier.test.ts
105 lines (90 loc) · 3.1 KB
/
generic-notifier.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import testInvalidPayload from '@test/payloads/generic-notifier/testInvalidPayload.json';
import testPayload from '@test/payloads/generic-notifier/testPayload.json';
import { createNotifierRequest } from '@test/utils/createGenericMessageRequest';
import { buildServer } from '@/buildServer';
import { DATADOG_API_INSTANCE } from '@/config';
import { GenericEvent, SlackMessage } from '@/types';
import { bolt } from '@api/slack';
import { messageSlack } from './generic-notifier';
describe('generic messages webhook', function () {
let fastify;
beforeEach(async function () {
fastify = await buildServer(false);
});
afterEach(function () {
fastify.close();
jest.clearAllMocks();
});
it('correctly inserts generic notifier when stage starts', async function () {
jest.spyOn(bolt.client.chat, 'postMessage').mockImplementation(jest.fn());
jest
.spyOn(DATADOG_API_INSTANCE, 'createEvent')
.mockImplementation(jest.fn());
const response = await createNotifierRequest(
fastify,
testPayload as GenericEvent
);
expect(response.statusCode).toBe(200);
});
it('returns 400 for an invalid source', async function () {
const response = await fastify.inject({
method: 'POST',
url: '/event-notifier/v1',
payload: testInvalidPayload,
});
expect(response.statusCode).toBe(400);
});
it('returns 400 for invalid signature', async function () {
const response = await fastify.inject({
method: 'POST',
url: '/event-notifier/v1',
headers: {
'x-infra-hub-signature': 'invalid',
},
payload: testPayload,
});
expect(response.statusCode).toBe(400);
});
it('returns 400 for no signature', async function () {
const response = await fastify.inject({
method: 'POST',
url: '/event-notifier/v1',
payload: testPayload,
});
expect(response.statusCode).toBe(400);
});
describe('messageSlack tests', function () {
afterEach(function () {
jest.clearAllMocks();
});
it('writes to slack', async function () {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
await messageSlack(testPayload.data[0] as SlackMessage);
expect(postMessageSpy).toHaveBeenCalledTimes(1);
const message = postMessageSpy.mock.calls[0][0];
expect(message).toEqual({
channel: '#aaaaaa',
text: 'Random text here',
unfurl_links: false,
});
});
});
it('checks that slack msg is sent', async function () {
const postMessageSpy = jest.spyOn(bolt.client.chat, 'postMessage');
const response = await createNotifierRequest(
fastify,
testPayload as GenericEvent
);
expect(postMessageSpy).toHaveBeenCalledTimes(2);
expect(response.statusCode).toBe(200);
});
it('checks that dd msg is sent', async function () {
const ddMessageSpy = jest.spyOn(DATADOG_API_INSTANCE, 'createEvent');
const response = await createNotifierRequest(
fastify,
testPayload as GenericEvent
);
expect(ddMessageSpy).toHaveBeenCalledTimes(1);
expect(response.statusCode).toBe(200);
});
});