forked from slackapi/bolt-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.spec.ts
307 lines (287 loc) · 9.32 KB
/
helpers.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
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
import 'mocha';
import { assert } from 'chai';
import { isBodyWithTypeEnterpriseInstall, getTypeAndConversation, IncomingEventType, isEventTypeToSkipAuthorize } from './helpers';
import { AnyMiddlewareArgs, ReceiverEvent, SlackEventMiddlewareArgs } from './types';
describe('Helpers', () => {
describe('getTypeAndConversation()', () => {
describe('event types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyEventBody = {
event: {
type: 'app_home_opened',
channel: conversationId,
},
};
it('should find Event type for generic event', () => {
// Act
const typeAndConversation = getTypeAndConversation(dummyEventBody);
// Assert
assert(typeAndConversation.type === IncomingEventType.Event);
assert(typeAndConversation.conversationId === conversationId);
});
});
describe('command types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyCommandBody = {
command: 'COMMAND_NAME',
channel_id: conversationId,
response_url: 'https://hooks.slack.com/commands/RESPONSE_URL',
};
it('should find Command type for generic command', () => {
// Act
const typeAndConversation = getTypeAndConversation(dummyCommandBody);
// Assert
assert(typeAndConversation.type === IncomingEventType.Command);
assert(typeAndConversation.conversationId === conversationId);
});
});
describe('options types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyActionBodies = createFakeOptions(conversationId);
dummyActionBodies.forEach((option) => {
it(`should find Option type for ${option.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(option);
// Assert
assert(typeAndConversation.type === IncomingEventType.Options);
assert(typeAndConversation.conversationId === conversationId);
});
});
});
describe('action types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyActionBodies = createFakeActions(conversationId);
dummyActionBodies.forEach((action) => {
it(`should find Action type for ${action.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(action);
// Assert
assert(typeAndConversation.type === IncomingEventType.Action);
assert(typeAndConversation.conversationId === conversationId);
});
});
});
describe('shortcut types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyShortcutBodies = createFakeShortcuts(conversationId);
dummyShortcutBodies.forEach((shortcut) => {
it(`should find Shortcut type for ${shortcut.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(shortcut);
// Assert
assert(typeAndConversation.type === IncomingEventType.Shortcut);
if (typeAndConversation.conversationId != null) {
assert(typeAndConversation.conversationId === conversationId);
}
});
});
});
describe('view types', () => {
// Arrange
const dummyViewBodies = createFakeViews();
dummyViewBodies.forEach((viewBody) => {
it(`should find Action type for ${viewBody.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(viewBody);
// Assert
assert(typeAndConversation.type === IncomingEventType.ViewAction);
});
});
});
describe('invalid events', () => {
// Arrange
const fakeEventBody = {
fake: 'THIS_IS_FAKE',
channel: { id: 'FAKE_CONVERSATION_ID' },
};
it('should not find type for invalid event', () => {
// Act
const typeAndConversation = getTypeAndConversation(fakeEventBody);
// Assert
assert.isEmpty(typeAndConversation);
});
});
});
describe(`${isBodyWithTypeEnterpriseInstall.name}()`, () => {
describe('with body of event type', () => {
// Arrange
const dummyEventBody: SlackEventMiddlewareArgs['body'] = {
token: '',
team_id: '',
api_app_id: '',
event_id: '',
event_time: 0,
type: 'event_callback',
event: {
type: 'app_home_opened',
user: '',
channel: '',
event_ts: '',
},
authorizations: [{
enterprise_id: '',
is_bot: true,
team_id: '',
user_id: '',
is_enterprise_install: true,
}],
};
it('should resolve the is_enterprise_install field', () => {
// Act
const isEnterpriseInstall = isBodyWithTypeEnterpriseInstall(dummyEventBody);
// Assert
assert(isEnterpriseInstall === true);
});
it('should resolve the is_enterprise_install with provided event type', () => {
// Act
const isEnterpriseInstall = isBodyWithTypeEnterpriseInstall(dummyEventBody, IncomingEventType.Event);
// Assert
assert(isEnterpriseInstall === true);
});
});
describe('with is_enterprise_install as a string value', () => {
// Arrange
const dummyEventBody = {
is_enterprise_install: 'true',
} as AnyMiddlewareArgs['body'];
it('should resolve is_enterprise_install as truthy', () => {
// Act
const isEnterpriseInstall = isBodyWithTypeEnterpriseInstall(dummyEventBody);
// Assert
assert(isEnterpriseInstall === true);
});
});
describe('with is_enterprise_install as boolean value', () => {
// Arrange
const dummyEventBody = {
is_enterprise_install: true,
} as AnyMiddlewareArgs['body'];
it('should resolve is_enterprise_install as truthy', () => {
// Act
const isEnterpriseInstall = isBodyWithTypeEnterpriseInstall(dummyEventBody);
// Assert
assert(isEnterpriseInstall === true);
});
});
describe('with is_enterprise_install undefined', () => {
// Arrange
const dummyEventBody = {} as AnyMiddlewareArgs['body'];
it('should resolve is_enterprise_install as falsy', () => {
// Act
const isEnterpriseInstall = isBodyWithTypeEnterpriseInstall(dummyEventBody);
// Assert
assert(isEnterpriseInstall === false);
});
});
});
describe(`${isEventTypeToSkipAuthorize.name}()`, () => {
describe('receiver events that can be skipped', () => {
it('should return truthy when event can be skipped', () => {
// Arrange
const dummyEventBody = { ack: async () => { }, body: { event: { type: 'app_uninstalled' } } } as ReceiverEvent;
// Act
const isEnterpriseInstall = isEventTypeToSkipAuthorize(dummyEventBody);
// Assert
assert(isEnterpriseInstall === true);
});
it('should return falsy when event can not be skipped', () => {
// Arrange
const dummyEventBody = { ack: async () => { }, body: { event: { type: '' } } } as ReceiverEvent;
// Act
const isEnterpriseInstall = isEventTypeToSkipAuthorize(dummyEventBody);
// Assert
assert(isEnterpriseInstall === false);
});
it('should return falsy when event is invalid', () => {
// Arrange
const dummyEventBody = { ack: async () => { }, body: {} } as ReceiverEvent;
// Act
const isEnterpriseInstall = isEventTypeToSkipAuthorize(dummyEventBody);
// Assert
assert(isEnterpriseInstall === false);
});
});
});
});
function createFakeActions(conversationId: string): any[] {
return [
// Body for a dialog submission
{
type: 'dialog_submission',
channel: { id: conversationId },
},
// Body for an action within an interactive message
{
type: 'interactive_message',
channel: { id: conversationId },
actions: [
{
type: 'button',
},
],
},
// Body for an action within a block
{
type: 'block_actions',
channel: { id: conversationId },
actions: [
{
type: 'static_select',
},
],
},
];
}
function createFakeShortcuts(conversationId: string): any[] {
return [
// Body for a message shortcut
{
type: 'message_action',
channel: { id: conversationId },
},
// Body for a global shortcut
{
type: 'shortcut',
},
];
}
function createFakeOptions(conversationId: string): any[] {
return [
// Body for an options request in an interactive message
{
type: 'interactive_message',
channel: { id: conversationId },
name: 'OPTIONS_NAME',
},
// Body for an options request in a dialog
{
type: 'dialog_suggestion',
channel: { id: conversationId },
name: 'OPTIONS_NAME',
},
// Body for an action within a block
{
type: 'block_suggestion',
channel: { id: conversationId },
},
];
}
function createFakeViews(): any[] {
return [
// Body for a view_submission event
{
type: 'view_submission',
view: { id: 'V123' },
},
// Body for a view_closed event
{
type: 'view_closed',
view: { id: 'V456' },
},
];
}