forked from twilio/twilio-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook_cluster_test.spec.ts
118 lines (107 loc) · 3.2 KB
/
webhook_cluster_test.spec.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
106
107
108
109
110
111
112
113
114
115
116
117
118
import { FlowInstance } from "./lib/rest/studio/v2/flow";
jest.setTimeout(15000);
const twilio = require("./lib/index.js");
const localtunnel = require("localtunnel");
const http = require("http");
const authToken = process.env.TWILIO_AUTH_TOKEN;
const apiKey = process.env.TWILIO_API_KEY;
const apiSecret = process.env.TWILIO_API_SECRET;
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const testClient = twilio(apiKey, apiSecret, { accountSid });
describe("Validating Incoming Twilio Request", () => {
let tunnel;
let flowSid;
let validationServer;
let portNumber = 7777;
let validationCount = 0; //workaround to ensure validation server receives requests (due to localtunnel connections issue)
beforeAll(async () => {
validationServer = await http.createServer((req, res) => {
validationCount++;
let url =
req.headers["x-forwarded-proto"] +
"://" +
req.headers["host"] +
req.url;
let signatureHeader = req.headers["x-twilio-signature"];
let body = "";
req.on("data", (chunk: string) => {
body += chunk;
});
req.on("end", () => {
let params = new URLSearchParams(body);
let paramObject = Object.fromEntries(params.entries());
let requestIsValid = twilio.validateRequest(
authToken,
signatureHeader,
url,
paramObject
);
expect(requestIsValid).toBeTruthy();
});
});
validationServer.listen(portNumber);
tunnel = await localtunnel({ port: portNumber });
});
afterAll(() => {
tunnel.close();
validationServer.close();
});
afterEach(() => {
testClient.studio.v2.flows(flowSid).remove();
});
function CreateStudioFlow(
url: string,
method: string
): Promise<FlowInstance> {
return testClient.studio.v2.flows.create({
friendlyName: "Node Cluster Test Flow",
status: "published",
definition: {
description: "Studio Flow",
states: [
{
name: "Trigger",
type: "trigger",
transitions: [
{
next: "httpRequest",
event: "incomingRequest",
},
],
properties: {},
},
{
name: "httpRequest",
type: "make-http-request",
transitions: [],
properties: {
method: method,
content_type: "application/x-www-form-urlencoded;charset=utf-8",
url: url,
},
},
],
initial_state: "Trigger",
flags: {
allow_concurrent_calls: true,
},
},
});
}
async function validateRequest(method: string) {
let flow = await CreateStudioFlow(tunnel.url, method);
flowSid = flow.sid;
await testClient.studio.v2
.flows(flowSid)
.executions.create({ to: "to", from: "from" });
await new Promise((resolve) => setTimeout(resolve, 5000));
}
test("Should validate GET request", async () => {
await validateRequest("GET");
expect(validationCount).toBe(1);
});
test("Should validate POST request", async () => {
await validateRequest("POST");
expect(validationCount).toBe(2);
});
});