-
Notifications
You must be signed in to change notification settings - Fork 154
/
slack-send-test.js
328 lines (297 loc) · 15.8 KB
/
slack-send-test.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const { assert } = require('chai');
const sinon = require('sinon');
const core = require('@actions/core');
const github = require('@actions/github');
const rewiremock = require('rewiremock/node');
const ChatStub = {
postMessage: sinon.fake.resolves({ ok: true, ts: '1503435957.111111', thread_ts: '1503435956.000247' }),
update: sinon.fake.resolves({ ok: true, thread_ts: '1503435956.000247' }),
};
/* eslint-disable-next-line global-require */
rewiremock(() => require('@slack/web-api')).with({
WebClient: class {
constructor(token) {
this.token = token;
this.chat = ChatStub;
}
},
});
const AxiosMock = {
post: sinon.stub().resolves(),
};
/* eslint-disable-next-line global-require */
rewiremock(() => require('axios')).with(AxiosMock);
rewiremock.enable();
const slackSend = require('../src/slack-send');
rewiremock.disable();
const ORIG_TOKEN_VAR = process.env.SLACK_BOT_TOKEN;
const ORIG_WEBHOOK_VAR = process.env.SLACK_WEBHOOK_URL;
describe('slack-send', () => {
const fakeCore = sinon.stub(core);
const fakeGithub = sinon.stub(github);
beforeEach(() => {
sinon.reset();
});
after(() => {
process.env.SLACK_BOT_TOKEN = ORIG_TOKEN_VAR;
process.env.SLACK_WEBHOOK_URL = ORIG_WEBHOOK_VAR;
});
it('should set an error if no webhook URL or token is provided', async () => {
delete process.env.SLACK_BOT_TOKEN;
delete process.env.SLACK_WEBHOOK_URL;
await slackSend(fakeCore);
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'Need to provide at least one botToken or webhook', 'Error set specifying what env vars need to be set.');
});
describe('using a bot token', () => {
beforeEach(() => {
process.env.SLACK_BOT_TOKEN = 'xoxb-xxxxx';
delete process.env.SLACK_WEBHOOK_URL;
});
describe('happy path', () => {
it('should send a message using the postMessage API', async () => {
fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?');
fakeCore.getInput.withArgs('channel-id').returns('C123456');
await slackSend(fakeCore);
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
const chatArgs = ChatStub.postMessage.lastCall.firstArg;
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage');
});
it('should send a message using the update API', async () => {
fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?');
fakeCore.getInput.withArgs('channel-id').returns('C123456');
fakeCore.getInput.withArgs('update-ts').returns('123456');
await slackSend(fakeCore);
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
const chatArgs = ChatStub.update.lastCall.firstArg;
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage');
});
it('should send payload-file-path values with replaced context variables', async () => {
// Prepare
fakeCore.getInput.withArgs('channel-id').returns('C123456');
fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/valid-payload.json');
fakeCore.getBooleanInput.withArgs('payload-file-path-parsed').returns(true);
fakeGithub.context.actor = 'user123';
// Run
await slackSend(fakeCore);
// Assert
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
const chatArgs = ChatStub.postMessage.lastCall.firstArg;
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
assert.equal(chatArgs.text, '', 'Correct message provided to postMessage');
assert.equal(chatArgs.bonny, 'clyde', 'Correct message provided to postMessage');
assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage');
assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage');
});
it('should send payload-file-path values without replacing context variables', async () => {
// Prepare
fakeCore.getInput.withArgs('channel-id').returns('C123456');
fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/valid-payload.json');
fakeCore.getBooleanInput.withArgs('payload-file-path-parsed').returns(false);
fakeGithub.context.actor = 'user123';
// Run
await slackSend(fakeCore);
// Assert
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
const chatArgs = ChatStub.postMessage.lastCall.firstArg;
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
assert.equal(chatArgs.text, '', 'Correct message provided to postMessage');
assert.equal(chatArgs.bonny, 'clyde', 'Correct message provided to postMessage');
assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage');
/* eslint-disable-next-line no-template-curly-in-string */
assert.equal(chatArgs.actor, '${{github.actor}}', 'Correct message provided to postMessage');
});
it('should send the same message to multiple channels', async () => {
fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?');
fakeCore.getInput.withArgs('channel-id').returns('C123456,C987654');
await slackSend(fakeCore);
const firstChatArgs = ChatStub.postMessage.firstCall.firstArg;
const secondChatArgs = ChatStub.postMessage.lastCall.firstArg;
assert.oneOf('C123456', [firstChatArgs.channel, secondChatArgs.channel], 'First comma-separated channel provided to postMessage');
assert.oneOf('C987654', [firstChatArgs.channel, secondChatArgs.channel], 'Second comma-separated channel provided to postMessage');
assert.equal(firstChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with first comma-separated channel');
assert.equal(secondChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with second comma-separated channel');
});
it("should send a reply-message using the postMessage API if thread_ts payload field is used'", async () => {
fakeCore.getInput
.withArgs('payload')
.returns('{"thread_ts":"123456","text":"who let the dogs out?"}');
fakeCore.getInput.withArgs('channel-id').returns('C123456');
await slackSend(fakeCore);
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
const chatArgs = ChatStub.postMessage.lastCall.firstArg;
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
assert.equal(chatArgs.thread_ts, '123456', 'Correct thread_ts provided to postMessage');
assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage');
});
});
describe('sad path', () => {
it('should set an error if payload cannot be JSON parsed', async () => {
fakeCore.getInput.withArgs('payload').returns('{not-valid-json');
await slackSend(fakeCore);
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'Need to provide valid JSON', 'Error set specifying JSON was invalid.');
});
it('should fail if an invalid payload-file-path is provided', async () => {
// Prepare
fakeCore.getInput.withArgs('channel-id').returns('C123456');
fakeCore.getInput.withArgs('payload-file-path').returns('non-existing-path.json');
// Run
await slackSend(fakeCore);
// Assert
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'The payload-file-path may be incorrect. Failed to load the file: non-existing-path.json', 'Error set specifying JSON was invalid.');
});
it('should fail if a valid payload-file-path with an invalid JSON is provided', async () => {
// Prepare
fakeCore.getInput.withArgs('channel-id').returns('C123456');
fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/invalid-payload.json');
// Run
await slackSend(fakeCore);
// Assert
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'Need to provide valid JSON payload', 'Error set specifying JSON was invalid.');
});
it('should fail if Channel ID is missing', async () => {
// Run
await slackSend(fakeCore);
// Assert
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'Channel ID is required to run this action. An empty one has been provided', 'Error set specifying JSON was invalid.');
});
it('should fail if payload is missing or empty', async () => {
// Prepare
fakeCore.getInput.withArgs('channel-id').returns('C123456');
// Run
await slackSend(fakeCore);
// Assert
assert.include(fakeCore.setFailed.lastCall.firstArg.message, 'Missing message content, please input a valid payload or message to send. No Message has been send.', 'Error set specifying JSON was invalid.');
});
});
});
describe('using a webhook URL', () => {
beforeEach(() => {
process.env.SLACK_WEBHOOK_URL = 'https://someurl';
delete process.env.SLACK_BOT_TOKEN;
});
describe('happy path', () => {
const payload = {
batman: 'robin',
thor: 'loki',
};
beforeEach(() => {
fakeCore.getInput.withArgs('payload').returns(JSON.stringify(payload));
});
it('should post the payload to the webhook URL', async () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload));
});
describe('proxy config', () => {
beforeEach(() => {
delete process.env.https_proxy;
delete process.env.HTTPS_PROXY;
});
it('should use https proxy agent when proxy uses HTTP', async () => {
process.env.HTTPS_PROXY = 'http://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload, sinon.match.has('httpsAgent').and(sinon.match.has('proxy'))));
});
it('should use default axios config when no proxy set', async () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy uses HTTPS', async () => {
process.env.HTTPS_PROXY = 'https://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy URL is invalid', async () => {
process.env.HTTPS_PROXY = 'invalid string';
const consoleSpy = sinon.spy(console, 'log');
await slackSend(fakeCore);
assert(consoleSpy.calledWith('failed to configure https proxy agent for http proxy. Using default axios configuration'));
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
});
describe('flatten', () => {
const mockPayload = {
apples: 'tree',
bananas: { truthiness: true },
};
beforeEach(() => {
fakeCore.getInput.withArgs('payload').returns(JSON.stringify(mockPayload));
});
afterEach(() => {
delete process.env.SLACK_WEBHOOK_TYPE;
});
it('defaults to using a period to flatten nested paylods', async () => {
process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER';
await slackSend(fakeCore);
const expected = {
apples: 'tree',
'bananas.truthiness': 'true',
};
const count = AxiosMock.post.callCount;
assert.equal(count, 1);
const post = AxiosMock.post.getCall(0);
const [url, actual] = post.args;
assert.equal(url, 'https://someurl');
assert.deepEqual(actual, expected);
});
it('replaces delimiter with provided payload settings', async () => {
fakeCore.getInput.withArgs('payload-delimiter').returns('_');
process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER';
await slackSend(fakeCore);
const expected = {
apples: 'tree',
bananas_truthiness: 'true',
};
const count = AxiosMock.post.callCount;
assert.equal(count, 1);
const post = AxiosMock.post.getCall(0);
const [url, actual] = post.args;
assert.equal(url, 'https://someurl');
assert.deepEqual(actual, expected);
});
it('does not flatten nested values of incoming webhook', async () => {
process.env.SLACK_WEBHOOK_TYPE = 'INCOMING_WEBHOOK';
await slackSend(fakeCore);
const expected = {
apples: 'tree',
bananas: { truthiness: true },
};
const count = AxiosMock.post.callCount;
assert.equal(count, 1);
const post = AxiosMock.post.getCall(0);
const [url, actual] = post.args;
assert.equal(url, 'https://someurl');
assert.deepEqual(actual, expected);
});
});
});
describe('sad path', () => {
it('should set an error if the POST to the webhook fails without a response', async () => {
AxiosMock.post.rejects(new Error('boom'));
await slackSend(fakeCore);
assert.include(fakeCore.setFailed.lastCall.firstArg, 'boom', 'Error set to whatever axios reports as error.');
});
});
});
});