-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.test.js
137 lines (114 loc) · 4.62 KB
/
utils.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
const utils = require("../netlify/utils");
const chai = require("chai");
let assert = chai.assert;
describe("parseRequestBody", () => {
it("returns empty string when empty string is passed", () => {
let result = utils.parseRequestBody("", "application/x-www-form-urlencoded");
assert.isEmpty(result);
});
it("returns empty string when null value is passed", () => {
let result = utils.parseRequestBody(null, "application/x-www-form-urlencoded");
assert.isEmpty(result);
});
it("returns empty string when a simple string is passed", () => {
let result = utils.parseRequestBody("abc123", "application/json");
assert.isEmpty(result);
});
it("returns expected JSON result when url encoded value is passed", () => {
let expectedJson = {
token: "AbCD123",
team_id: "T1234ABCD",
team_domain: "demoapp"
};
let mockBodyInput = `token=AbCD123&team_id=T1234ABCD&team_domain=demoapp`;
let result = utils.parseRequestBody(mockBodyInput, "application/x-www-form-urlencoded");
assert.strictEqual(result.token, expectedJson.token);
assert.strictEqual(result.team_domain, expectedJson.team_domain);
assert.strictEqual(result.team_id, expectedJson.team_id);
});
it("returns expected JSON result when JSON value is passed", () => {
let expectedJson = {
token: "TOKEN123",
team_id: "TEAM123",
api_app_id: "APPID123",
event: {
bot_id: "BOTID",
bot_profile: {
id: "BOTPROFLEID"
}
}
};
let mockBodyInput = `{"token":"TOKEN123","team_id":"TEAM123","api_app_id":"APPID123","event":
{"bot_id":"BOTID","bot_profile":{"id":"BOTPROFLEID"}}}`;
let result = utils.parseRequestBody(mockBodyInput, "application/json");
assert.deepEqual(result, expectedJson);
});
});
describe("isUrlVerificationRequest", () => {
it("returns true when type is url_verification", () => {
let payload = {
type: "url_verification",
};
let result = utils.isUrlVerificationRequest(payload);
assert.isTrue(result);
});
it("returns false when type is some_other_event", () => {
let payload = {
type: "some_other_event",
};
let result = utils.isUrlVerificationRequest(payload);
assert.isFalse(result);
});
it("returns false when type is not passed", () => {
let payload = {
body: "demo",
header: "slack",
};
let result = utils.isUrlVerificationRequest(payload);
assert.isFalse(result);
});
it("returns false when payload passed is null", () => {
let payload = null;
let result = utils.isUrlVerificationRequest(payload);
assert.isFalse(result);
});
it("returns false when payload passed is undefined", () => {
let payload = undefined;
let result = utils.isUrlVerificationRequest(payload);
assert.isFalse(result);
});
});
describe("generateReceiverEvent", () => {
it("returns expected payload in the object", () => {
let payload = "";
let result = utils.generateReceiverEvent(payload);
assert.strictEqual(result.body, payload);
});
it("returns ack() as a promise", () => {
let payload = "";
let result = utils.generateReceiverEvent(payload);
assert.isTrue(result.ack() instanceof Promise);
});
it("returns expected response when no response is passed to ack()", async() => {
let payload = "";
let result = utils.generateReceiverEvent(payload);
let actualResponse = null;
await Promise.resolve(result.ack()).then((promiseReturnValue)=> {
let promiseResponse = JSON.parse(JSON.stringify(promiseReturnValue));
actualResponse = {...promiseResponse};
});
assert.strictEqual(actualResponse.body, "");
assert.strictEqual(actualResponse.statusCode, 200);
});
it("returns expected response when mock response is passed to ack()", async() => {
let payload = "";
let result = utils.generateReceiverEvent(payload);
let actualResponse = null;
await Promise.resolve(result.ack("mock response")).then((promiseReturnValue)=> {
let promiseResponse = JSON.parse(JSON.stringify(promiseReturnValue));
actualResponse = {...promiseResponse};
});
assert.strictEqual(actualResponse.body, "mock response");
assert.strictEqual(actualResponse.statusCode, 200);
});
});