-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhook.test.js
More file actions
87 lines (74 loc) · 2.95 KB
/
Copy pathwebhook.test.js
File metadata and controls
87 lines (74 loc) · 2.95 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
const test = require("node:test");
const assert = require("node:assert/strict");
const { loadMaxBotContext } = require("./helpers/gas");
function response(status, text) {
return {
getResponseCode: () => status,
getContentText: () => text,
};
}
function runtime(options) {
const context = loadMaxBotContext(() => response(200, "{}"));
return { context, bot: context.create("token", options) };
}
test("handleWebhook сверяет query secret с секретом экземпляра до JSON parse", () => {
const { bot } = runtime();
assert.throws(
() => bot.handleWebhook({ parameter: { secret: "anything" }, postData: { contents: "not json" } }),
(error) => !(error instanceof SyntaxError) && /secret/i.test(error.message)
);
const configured = runtime({ webhookSecret: "expected-secret" }).bot;
assert.throws(
() => configured.handleWebhook(
{ parameter: { secret: "wrong-secret" }, postData: { contents: "not json" } }
),
(error) => !(error instanceof SyntaxError) && /secret/i.test(error.message)
);
});
test("handleWebhook требует postData.contents после успешной проверки секрета", () => {
const { bot } = runtime({ webhookSecret: "expected-secret" });
for (const event of [
{ parameter: { secret: "expected-secret" } },
{ parameter: { secret: "expected-secret" }, postData: {} },
{ parameter: { secret: "expected-secret" }, postData: { contents: "" } },
]) {
assert.throws(() => bot.handleWebhook(event), /postData|contents|body/i);
}
});
test("handleWebhook парсит update, синхронно dispatch-ит и возвращает HtmlOutput OK", () => {
const { bot } = runtime({ webhookSecret: "expected-secret" });
let seen = null;
bot.on("message_created", (ctx) => { seen = ctx.update; });
const update = { update_type: "message_created", message: { body: { text: "hello", mid: "m1" } } };
const output = bot.handleWebhook({
parameter: { secret: "expected-secret" },
headers: { "X-Max-Bot-Api-Secret": "ignored-header" },
postData: { contents: JSON.stringify(update) },
});
assert.deepEqual(JSON.parse(JSON.stringify(seen)), update);
assert.equal(output.getContent(), "OK");
});
test("handleWebhook не ловит ошибки JSON, validation и handlers", () => {
const { bot } = runtime({ webhookSecret: "expected-secret" });
assert.throws(
() => bot.handleWebhook(
{ parameter: { secret: "expected-secret" }, postData: { contents: "not json" } }
),
SyntaxError
);
assert.throws(
() => bot.handleWebhook(
{ parameter: { secret: "expected-secret" }, postData: { contents: "{}" } }
),
/update/i
);
const expected = new Error("handler failed");
bot.on("*", () => { throw expected; });
assert.throws(
() => bot.handleWebhook({
parameter: { secret: "expected-secret" },
postData: { contents: '{"update_type":"future_update"}' },
}),
(error) => error === expected
);
});