-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathwebhook.spec.js
More file actions
372 lines (351 loc) · 13.1 KB
/
Copy pathwebhook.spec.js
File metadata and controls
372 lines (351 loc) · 13.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import assert from "node:assert";
import { beforeEach, describe, it } from "node:test";
import { AxiosError } from "axios";
import Config from "../src/config.js";
import SlackError from "../src/errors.js";
import send from "../src/send.js";
import Webhook from "../src/webhook.js";
import { mocks } from "./index.spec.js";
describe("webhook", () => {
beforeEach(() => {
mocks.reset();
});
describe("success", () => {
it("sends the parsed payload to the provided webhook trigger", async () => {
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("webhook-trigger");
mocks.core.getInput.withArgs("payload").returns("drinks: coffee");
mocks.axios.post.returns(
Promise.resolve({ status: 200, data: { ok: true } }),
);
try {
await send(mocks.core);
assert.equal(mocks.axios.post.getCalls().length, 1);
const [url, payload, options] = mocks.axios.post.getCall(0).args;
assert.equal(url, "https://hooks.slack.com");
assert.deepEqual(payload, { drinks: "coffee" });
assert.deepEqual(options, {});
assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok");
assert.equal(mocks.core.setOutput.getCall(0).lastArg, true);
assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response");
assert.equal(
mocks.core.setOutput.getCall(1).lastArg,
JSON.stringify({ ok: true }),
);
} catch (err) {
console.error(err);
assert.fail("Failed to send the webhook");
}
});
it("sends the parsed payload to the provided incoming webhook", async () => {
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("payload").returns("text: greetings");
mocks.axios.post.returns(Promise.resolve({ status: 200, data: "ok" }));
try {
await send(mocks.core);
assert.equal(mocks.axios.post.getCalls().length, 1);
const [url, payload, options] = mocks.axios.post.getCall(0).args;
assert.equal(url, "https://hooks.slack.com");
assert.deepEqual(payload, { text: "greetings" });
assert.deepEqual(options, {});
assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok");
assert.equal(mocks.core.setOutput.getCall(0).lastArg, true);
assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response");
assert.equal(
mocks.core.setOutput.getCall(1).lastArg,
JSON.stringify("ok"),
);
} catch (err) {
console.error(err);
assert.fail("Failed to send the webhook");
}
});
});
describe("failure", () => {
it("requires that a webhook is provided in inputs", async () => {
/**
* @type {Config}
*/
const config = {
core: mocks.core,
inputs: {},
};
try {
await new Webhook().post(config);
assert.fail("Failed to throw for missing input");
} catch (err) {
if (err instanceof SlackError) {
assert.ok(err.message.includes("No webhook was provided to post to"));
} else {
assert.fail(err);
}
}
});
it("returns the failures from a webhook trigger", async () => {
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("webhook-trigger");
mocks.core.getInput.withArgs("payload").returns("drinks: coffee");
const response = new AxiosError(
"Request failed with status code 400",
"ERR_BAD_REQUEST",
{},
{},
{ status: 400 },
);
mocks.axios.post.resolves(Promise.reject(response));
try {
await send(mocks.core);
} catch (err) {
if (err instanceof SlackError) {
assert.ok(
err.message.includes("Request failed with status code 400"),
);
} else {
assert.fail(err);
}
}
assert.equal(mocks.axios.post.getCalls().length, 1);
const [url, payload, options] = mocks.axios.post.getCall(0).args;
assert.equal(url, "https://hooks.slack.com");
assert.deepEqual(payload, { drinks: "coffee" });
assert.deepEqual(options, {});
assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok");
assert.equal(mocks.core.setOutput.getCall(0).lastArg, false);
assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response");
});
it("returns the failures from an incoming webhook", async () => {
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("payload").returns("textt: oops");
const response = new AxiosError(
"Request failed with status code 400",
"ERR_BAD_REQUEST",
{},
{},
{ status: 400 },
);
mocks.axios.post.resolves(Promise.reject(response));
try {
await send(mocks.core);
} catch (err) {
if (err instanceof SlackError) {
assert.ok(
err.message.includes("Request failed with status code 400"),
);
} else {
assert.fail(err);
}
}
assert.equal(mocks.axios.post.getCalls().length, 1);
const [url, payload, options] = mocks.axios.post.getCall(0).args;
assert.equal(url, "https://hooks.slack.com");
assert.deepEqual(payload, { textt: "oops" });
assert.deepEqual(options, {});
assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok");
assert.equal(mocks.core.setOutput.getCall(0).lastArg, false);
assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response");
});
it("handles errors without toJSON", async () => {
mocks.core.getInput
.withArgs("webhook")
.returns("\"https://hooks.slack.com\"");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("payload").returns("text: test");
mocks.core.getBooleanInput.withArgs("errors").returns(true);
mocks.axios.post.returns(Promise.reject(new TypeError("Invalid URL")));
try {
await send(mocks.core);
assert.fail("Failed to throw for non-axios error");
} catch (err) {
if (err instanceof SlackError) {
assert.ok(err.message.includes("Invalid URL"));
} else {
assert.fail(err);
}
}
assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok");
assert.equal(mocks.core.setOutput.getCall(0).lastArg, false);
assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response");
assert.equal(mocks.core.setOutput.getCall(1).lastArg, '"Invalid URL"');
});
});
describe("proxies", () => {
it("requires a webhook is included in the inputs", async () => {
/**
* @type {Config}
*/
const config = {
core: mocks.core,
inputs: {},
};
try {
new Webhook().proxies(config);
assert.fail("Failed to throw for missing input");
} catch (err) {
if (err instanceof SlackError) {
assert.ok(
err.message.includes("No webhook was provided to proxy to"),
);
} else {
assert.fail(err);
}
}
});
it("skips proxying an http webhook url altogether", async () => {
mocks.core.getInput.withArgs("webhook").returns("http://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("proxy").returns("https://example.com");
const config = new Config(mocks.core);
const webhook = new Webhook();
const request = webhook.proxies(config);
assert.strictEqual(request, undefined);
});
it("sets up the proxy agent for the provided https proxy", async () => {
const proxy = "https://example.com";
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("proxy").returns(proxy);
const config = new Config(mocks.core);
const webhook = new Webhook();
const { httpsAgent, proxy: proxying } = webhook.proxies(config);
assert.deepEqual(httpsAgent.proxy, new URL(proxy));
assert.notStrictEqual(proxying, false);
});
it("sets up the agent without proxy for http proxies", async () => {
const proxy = "http://example.com";
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("proxy").returns(proxy);
const config = new Config(mocks.core);
const webhook = new Webhook();
const { httpsAgent, proxy: proxying } = webhook.proxies(config);
assert.deepEqual(httpsAgent.proxy, new URL(proxy));
assert.strictEqual(proxying, false);
});
it("fails to configure proxies with an invalid proxied url", async () => {
const proxy = "https://";
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("proxy").returns(proxy);
try {
const config = new Config(mocks.core);
const webhook = new Webhook();
webhook.proxies(config);
assert.fail("An invalid proxy URL was not thrown as error!");
} catch (err) {
if (err instanceof SlackError) {
assert.ok(
err.message.includes("Failed to configure the HTTPS proxy"),
);
} else {
assert.fail(err);
}
}
});
it("fails to configure proxies with an unknown url protocol", async () => {
const proxy = "ssh://";
mocks.core.getInput
.withArgs("webhook")
.returns("https://hooks.slack.com");
mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook");
mocks.core.getInput.withArgs("proxy").returns(proxy);
try {
const config = new Config(mocks.core);
const webhook = new Webhook();
webhook.proxies(config);
assert.fail("An unknown URL protocol was not thrown as error!");
} catch (err) {
if (err instanceof SlackError) {
assert.ok(
err.message.includes("Failed to configure the HTTPS proxy"),
);
assert.ok(err.cause.message.includes("Unsupported URL protocol"));
} else {
assert.fail(err);
}
}
});
});
describe("retries", () => {
it("uses a default of five retries in requests", async () => {
const webhook = new Webhook();
const result = webhook.retries();
assert.equal(result.retries, 5);
});
it('does not attempt retries when "0" is set', async () => {
const webhook = new Webhook();
const result = webhook.retries("0");
assert.equal(result.retries, 0);
});
it('attempts a default amount of "5" retries', async () => {
const webhook = new Webhook();
const result = webhook.retries("5");
assert.equal(result.retries, 5);
if (!result.retryDelay) {
assert.fail("No retry delay found!");
}
assert.equal(
result.retryDelay(5, mocks.errors.axios.network_failed),
300000,
"5th retry after 5 seconds",
);
});
it('attempts "10" retries in around "30" minutes', async () => {
const webhook = new Webhook();
const result = webhook.retries("10");
assert.equal(result.retries, 10);
if (!result.retryDelay) {
assert.fail("No retry delay found!");
}
assert.ok(
result.retryDelay(10, mocks.errors.axios.network_failed) > 1800000,
"last attempt is around 30 minutes after starting",
);
assert.ok(
result.retryDelay(10, mocks.errors.axios.network_failed) < 3600000,
"last attempt is no more than an hour later",
);
});
it('attempts a " rapid" burst of "12" retries in seconds', async () => {
const webhook = new Webhook();
const result = webhook.retries(" rapid");
assert.equal(result.retries, 12);
if (!result.retryDelay) {
assert.fail("No retry delay found!");
}
assert.equal(
result.retryDelay(12, mocks.errors.axios.network_failed),
12000,
"12th retry after 12 seconds",
);
});
it('attempts a "RAPID" burst of "12" retries in seconds', async () => {
const webhook = new Webhook();
const result = webhook.retries("RAPID");
assert.equal(result.retries, 12);
if (!result.retryDelay) {
assert.fail("No retry delay found!");
}
assert.equal(
result.retryDelay(12, mocks.errors.axios.network_failed),
12000,
"12th retry after 12 seconds",
);
});
});
});