/
setWebhook.spec.ts
111 lines (84 loc) · 2.8 KB
/
setWebhook.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
import Confirm from 'prompt-confirm';
import { TelegramClient } from 'messaging-api-telegram';
import getConfig from '../../../shared/getConfig';
import getWebhookFromNgrok from '../../../shared/getWebhookFromNgrok';
import { setWebhook } from '../webhook';
import * as log from '../../../shared/log';
jest.mock('messaging-api-telegram');
jest.mock('prompt-confirm');
jest.mock('../../../shared/getWebhookFromNgrok');
jest.mock('../../../shared/log');
jest.mock('../../../shared/getConfig');
const MOCK_FILE_WITH_PLATFORM = {
channels: {
telegram: {
accessToken: '__accessToken__',
},
line: {},
},
};
const setup = (
{ webhook = undefined, ngrokPort = undefined, token = undefined } = {
webhook: undefined,
ngrokPort: undefined,
token: undefined,
}
) => ({
argv: {
'--webhook': webhook,
'--ngrok-port': ngrokPort,
},
});
beforeEach(() => {
process.exit = jest.fn();
getConfig.mockReturnValue(MOCK_FILE_WITH_PLATFORM.channels.telegram);
getWebhookFromNgrok.mockResolvedValue('https://fakeDomain.ngrok.io');
Confirm.mockImplementation(() => ({
run: jest.fn().mockResolvedValue(true),
}));
TelegramClient.connect.mockReturnValue({
setWebhook: jest.fn().mockResolvedValue(true),
});
});
it('be defined', () => {
expect(setWebhook).toBeDefined();
});
describe('resolve', () => {
it('successfully set webhook', async () => {
const ctx = setup({ webhook: 'http://example.com/webhook' });
await setWebhook(ctx);
expect(log.print).toHaveBeenCalledTimes(1);
expect(log.print.mock.calls[0][0]).toMatch(/Successfully/);
});
it('get ngrok webhook to setup', async () => {
const ctx = setup();
await setWebhook(ctx);
expect(getWebhookFromNgrok).toBeCalledWith(undefined);
expect(log.print).toHaveBeenCalledTimes(1);
expect(log.print.mock.calls[0][0]).toMatch(/Successfully/);
});
it('set ngrok webhook port', async () => {
const ctx = setup({ ngrokPort: '5555' });
ctx.argv['ngrok-port'] = ctx.argv.ngrokPort;
await setWebhook(ctx);
expect(getWebhookFromNgrok).toBeCalledWith('5555');
});
});
describe('reject', () => {
it('reject when accessToken not found in config file', async () => {
const ctx = setup({ webhook: 'http://example.com/webhook' });
getConfig.mockReturnValue({});
await setWebhook(ctx);
expect(log.error).toBeCalledWith('accessToken is not found in config file');
expect(process.exit).toBeCalled();
});
it('reject when telegram return not success', async () => {
const ctx = setup({ webhook: 'http://example.com/webhook' });
TelegramClient.connect().setWebhook.mockImplementation(() => {
throw new Error();
});
await setWebhook(ctx);
expect(log.error).toBeCalledWith('Failed to set Telegram webhook');
expect(process.exit).toBeCalled();
});
});