-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathbasic.spec.ts
More file actions
406 lines (391 loc) · 17 KB
/
basic.spec.ts
File metadata and controls
406 lines (391 loc) · 17 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { LogLevel } from '@slack/logger';
import assert from 'node:assert/strict';
import sinon from 'sinon';
import { ErrorCode } from '../../../src/errors';
import SocketModeReceiver from '../../../src/receivers/SocketModeReceiver';
import {
FakeReceiver,
createFakeConversationStore,
createFakeLogger,
importApp,
mergeOverrides,
noop,
noopMiddleware,
withConversationContext,
withMemoryStore,
withNoopAppMetadata,
withNoopWebClient,
withSuccessfulBotUserFetchingWebClient,
} from '../helpers';
import { describe, it } from 'node:test';
const fakeAppToken = 'xapp-1234';
const fakeBotId = 'B_FAKE_BOT_ID';
const fakeBotUserId = 'U_FAKE_BOT_USER_ID';
describe('App basic features', () => {
const overrides = mergeOverrides(
withNoopAppMetadata(),
withSuccessfulBotUserFetchingWebClient(fakeBotId, fakeBotUserId),
);
describe('constructor', () => {
describe('with a custom port value in HTTP Mode', () => {
it('should accept a port value at the top-level', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({ token: '', signingSecret: '', port: 9999 });
// biome-ignore lint/complexity/useLiteralKeys: reaching into private fields
assert.ok(app['receiver'] && typeof app['receiver'] === 'object');
assert.ok('port' in app['receiver']);
assert.deepStrictEqual((app['receiver'] as unknown as Record<PropertyKey, unknown>)['port'], 9999);
});
it('should accept a port value under installerOptions', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({ token: '', signingSecret: '', port: 7777, installerOptions: { port: 9999 } });
// biome-ignore lint/complexity/useLiteralKeys: reaching into private fields
assert.ok(app['receiver'] && typeof app['receiver'] === 'object');
assert.ok('port' in app['receiver']);
assert.deepStrictEqual((app['receiver'] as unknown as Record<PropertyKey, unknown>)['port'], 9999);
});
});
describe('with a custom port value in Socket Mode', () => {
const installationStore = {
storeInstallation: async () => {},
fetchInstallation: async () => {
throw new Error('Failed fetching installation');
},
deleteInstallation: async () => {},
};
it('should accept a port value at the top-level', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({
socketMode: true,
appToken: fakeAppToken,
port: 9999,
clientId: '',
clientSecret: '',
stateSecret: '',
installerOptions: {},
installationStore,
});
// biome-ignore lint/complexity/useLiteralKeys: reaching into private fields
assert.ok(app['receiver'] && typeof app['receiver'] === 'object');
assert.ok('httpServerPort' in app['receiver']);
assert.deepStrictEqual((app['receiver'] as unknown as Record<PropertyKey, unknown>)['httpServerPort'], 9999);
});
it('should accept a port value under installerOptions', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({
socketMode: true,
appToken: fakeAppToken,
port: 7777,
clientId: '',
clientSecret: '',
stateSecret: '',
installerOptions: {
port: 9999,
},
installationStore,
});
// biome-ignore lint/complexity/useLiteralKeys: reaching into private fields
assert.ok(app['receiver'] && typeof app['receiver'] === 'object');
assert.ok('httpServerPort' in app['receiver']);
assert.deepStrictEqual((app['receiver'] as unknown as Record<PropertyKey, unknown>)['httpServerPort'], 9999);
});
});
// TODO: test when the single team authorization results fail. that should still succeed but warn. it also means
// that the `ignoreSelf` middleware will fail (or maybe just warn) a bunch.
describe('with successful single team authorization results', () => {
it('should succeed with a token for single team authorization', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({ token: '', signingSecret: '' });
// TODO: verify that the fake bot ID and fake bot user ID are retrieved
assert.ok(app instanceof MockApp);
});
it('should pass the given token to app.client', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({ token: 'xoxb-foo-bar', signingSecret: '' });
assert.notStrictEqual(app.client, undefined);
assert.equal(app.client.token, 'xoxb-foo-bar');
});
});
it('should succeed with an authorize callback', async () => {
const authorizeCallback = sinon.fake();
const MockApp = importApp();
new MockApp({ authorize: authorizeCallback, signingSecret: '' });
assert(authorizeCallback.notCalled, 'Should not call the authorize callback on instantiation');
});
it('should fail without a token for single team authorization, authorize callback, nor oauth installer', async () => {
const MockApp = importApp();
try {
new MockApp({ signingSecret: '' });
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
}
});
it('should fail when both a token and authorize callback are specified', async () => {
const authorizeCallback = sinon.fake();
const MockApp = importApp();
try {
new MockApp({ token: '', authorize: authorizeCallback, signingSecret: '' });
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
assert(authorizeCallback.notCalled);
}
});
it('should fail when both a token is specified and OAuthInstaller is initialized', async () => {
const authorizeCallback = sinon.fake();
const MockApp = importApp();
try {
new MockApp({ token: '', clientId: '', clientSecret: '', stateSecret: '', signingSecret: '' });
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
assert(authorizeCallback.notCalled);
}
});
it('should fail when both a authorize callback is specified and OAuthInstaller is initialized', async () => {
const authorizeCallback = sinon.fake();
const MockApp = importApp();
try {
new MockApp({
authorize: authorizeCallback,
clientId: '',
clientSecret: '',
stateSecret: '',
signingSecret: '',
});
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
assert(authorizeCallback.notCalled);
}
});
describe('with a custom receiver', () => {
it('should succeed with no signing secret', async () => {
const MockApp = importApp();
new MockApp({
receiver: new FakeReceiver(),
authorize: noop,
});
});
});
it('should fail when no signing secret for the default receiver is specified', async () => {
const MockApp = importApp();
try {
new MockApp({ authorize: noop });
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
}
});
it('should fail when both socketMode and a custom receiver are specified', async () => {
const fakeReceiver = new FakeReceiver();
const MockApp = importApp();
try {
new MockApp({ token: '', signingSecret: '', socketMode: true, receiver: fakeReceiver });
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
}
});
it('should succeed when both socketMode and SocketModeReceiver are specified', async () => {
const MockApp = importApp(overrides);
const socketModeReceiver = new SocketModeReceiver({ appToken: fakeAppToken });
new MockApp({ token: '', signingSecret: '', socketMode: true, receiver: socketModeReceiver });
});
it('should initialize MemoryStore conversation store by default', async () => {
const fakeMemoryStore = sinon.fake();
const fakeConversationContext = sinon.fake.returns(noopMiddleware);
const overrides = mergeOverrides(
withNoopAppMetadata(),
withNoopWebClient(),
withMemoryStore(fakeMemoryStore),
withConversationContext(fakeConversationContext),
);
const MockApp = importApp(overrides);
new MockApp({ authorize: noop, signingSecret: '' });
assert(fakeMemoryStore.calledWithNew);
assert(fakeConversationContext.called);
});
describe('conversation store', () => {
const fakeConversationContext = sinon.fake.returns(noopMiddleware);
const overrides = mergeOverrides(
withNoopAppMetadata(),
withNoopWebClient(),
withConversationContext(fakeConversationContext),
);
it('should initialize without a conversation store when option is false', async () => {
const MockApp = importApp(overrides);
new MockApp({ convoStore: false, authorize: noop, signingSecret: '' });
assert(fakeConversationContext.notCalled);
});
it('should initialize the conversation store', async () => {
const dummyConvoStore = createFakeConversationStore();
const MockApp = importApp(overrides);
const app = new MockApp({ convoStore: dummyConvoStore, authorize: noop, signingSecret: '' });
assert.ok(app instanceof MockApp);
assert(fakeConversationContext.firstCall.calledWith(dummyConvoStore));
});
});
describe('with custom redirectUri supplied', () => {
it('should fail when missing installerOptions', async () => {
const MockApp = importApp();
try {
new MockApp({ token: '', signingSecret: '', redirectUri: 'http://example.com/redirect' }); // eslint-disable-line no-new
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
}
});
it('should fail when missing installerOptions.redirectUriPath', async () => {
const MockApp = importApp();
try {
new MockApp({
token: '',
signingSecret: '',
redirectUri: 'http://example.com/redirect',
installerOptions: {},
});
assert.fail();
} catch (error) {
assert.ok(error && typeof error === 'object');
assert.ok('code' in error);
assert.deepStrictEqual((error as unknown as Record<PropertyKey, unknown>)['code'], ErrorCode.AppInitializationError);
}
});
});
it('with WebClientOptions', async () => {
const fakeConstructor = sinon.fake();
const overrides = mergeOverrides(withNoopAppMetadata(), {
'@slack/web-api': {
WebClient: class {
// biome-ignore lint/suspicious/noExplicitAny: test overrides can be anything
public constructor(...args: any[]) {
fakeConstructor(...args);
}
},
},
});
const MockApp = importApp(overrides);
const clientOptions = { slackApiUrl: 'proxy.slack.com' };
new MockApp({ clientOptions, authorize: noop, signingSecret: '', logLevel: LogLevel.ERROR });
assert.ok(fakeConstructor.called);
const [token, options] = fakeConstructor.lastCall.args;
assert.strictEqual(token, undefined, 'token should be undefined');
assert.strictEqual(clientOptions.slackApiUrl, options.slackApiUrl);
assert.strictEqual(LogLevel.ERROR, options.logLevel, 'override logLevel');
});
describe('with auth.test failure', () => {
const fakeConstructor = sinon.fake();
const exception = 'This API method call should not be performed';
const overrides = mergeOverrides(withNoopAppMetadata(), {
'@slack/web-api': {
WebClient: class {
// biome-ignore lint/suspicious/noExplicitAny: test overrides can be anything
public constructor(...args: any[]) {
fakeConstructor(args);
}
public auth = {
test: () => {
throw new Error(exception);
},
};
},
},
});
it('should not perform auth.test API call if tokenVerificationEnabled is false', async () => {
const MockApp = importApp(overrides);
new MockApp({
token: 'xoxb-completely-invalid-token',
signingSecret: 'invalid-one',
tokenVerificationEnabled: false,
});
});
it('should fail in await App#init()', async () => {
const MockApp = importApp(overrides);
const app = new MockApp({
token: 'xoxb-completely-invalid-token',
signingSecret: 'invalid-one',
deferInitialization: true,
});
assert.ok(app instanceof MockApp);
try {
await app.start();
assert.fail('The start() method should fail before init() call');
} catch (err) {
assert.ok(err && typeof err === 'object');
assert.ok('message' in err);
assert.deepStrictEqual((err as unknown as Record<PropertyKey, unknown>)['message'], 'This App instance is not yet initialized. Call `await App#init()` before starting the app.');
}
try {
await app.init();
assert.fail('The init() method should fail here');
} catch (err) {
console.log(err);
assert.ok(err && typeof err === 'object');
assert.ok('message' in err);
assert.deepStrictEqual((err as unknown as Record<PropertyKey, unknown>)['message'], exception);
}
});
});
describe('with developerMode', () => {
it('should accept developerMode: true', async () => {
const overrides = mergeOverrides(
withNoopAppMetadata(),
withSuccessfulBotUserFetchingWebClient('B_FAKE_BOT_ID', 'U_FAKE_BOT_USER_ID'),
);
const fakeLogger = createFakeLogger();
const MockApp = importApp(overrides);
const app = new MockApp({ logger: fakeLogger, token: '', appToken: fakeAppToken, developerMode: true });
assert.ok(app && typeof app === 'object');
assert.ok('logLevel' in app);
assert.deepStrictEqual((app as unknown as Record<PropertyKey, unknown>)['logLevel'], LogLevel.DEBUG);
assert.ok(app && typeof app === 'object');
assert.ok('socketMode' in app);
assert.deepStrictEqual((app as unknown as Record<PropertyKey, unknown>)['socketMode'], true);
});
});
// TODO: tests for logger and logLevel option
// TODO: tests for providing botId and botUserId options
// TODO: tests for providing endpoints option
});
describe('#start', () => {
it('should pass calls through to receiver', async () => {
// Arrange
const dummyReturn = Symbol();
const fakeReceiver = new FakeReceiver();
const MockApp = importApp();
const app = new MockApp({ receiver: fakeReceiver, authorize: noop });
fakeReceiver.start = sinon.fake.returns(sinon.promise().resolve(dummyReturn));
await app.start(1337);
assert.deepEqual(fakeReceiver.start.firstCall.args, [1337]);
});
});
describe('#stop', () => {
it('should pass calls through to receiver', async () => {
const dummyReturn = Symbol();
const dummyParams = [Symbol(), Symbol()];
const fakeReceiver = new FakeReceiver();
const MockApp = importApp();
fakeReceiver.stop = sinon.fake.returns(sinon.promise().resolve(dummyReturn));
const app = new MockApp({ receiver: fakeReceiver, authorize: noop });
const actualReturn = await app.stop(...dummyParams);
assert.deepEqual(actualReturn, dummyReturn);
assert.deepEqual(fakeReceiver.stop.firstCall.args, dummyParams as unknown);
});
});
});