-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.test.ts
More file actions
199 lines (167 loc) · 5.29 KB
/
Copy pathhandler.test.ts
File metadata and controls
199 lines (167 loc) · 5.29 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
import handler from '../handler';
jest.mock('twilio', () => {
const mockCreate = jest.fn();
return jest.fn(() => ({
messages: { create: mockCreate },
}));
});
import twilio from 'twilio';
const mockTwilioCreate = (twilio as jest.Mock)().messages.create;
const mockLog = {
info: jest.fn(),
error: jest.fn(),
};
const mockClient = {
request: jest.fn(),
};
const mockMeta = {
request: jest.fn(),
};
const createContext = (overrides = {}) => ({
client: mockClient as any,
meta: mockMeta as any,
job: {
jobId: 'test-job-1',
workerId: 'test-worker',
databaseId: 'test-database-id',
actorId: null,
},
log: mockLog,
env: {
SMS_PROVIDER: 'stub',
SEND_SMS_DRY_RUN: 'false',
...overrides,
},
});
describe('send-sms handler', () => {
beforeEach(() => {
jest.clearAllMocks();
mockMeta.request.mockResolvedValue({
databases: {
nodes: [
{
sites: {
nodes: [
{
title: 'Test App',
siteModules: {
nodes: [
{
data: {
company: { nick: 'TestApp' },
},
},
],
},
},
],
},
},
],
},
});
});
it('should return error when sms_type is missing', async () => {
const context = createContext();
const params = { phone_number: '+1234567890', otp_code: '123456' } as any;
await expect(handler(params, context)).rejects.toThrow('Missing required field: sms_type');
});
it('should return error when phone_number is missing', async () => {
const context = createContext();
const params = { sms_type: 'sign_in_sms_otp', otp_code: '123456' } as any;
await expect(handler(params, context)).rejects.toThrow('Missing required field: phone_number');
});
it('should return error when otp_code is missing', async () => {
const context = createContext();
const params = { sms_type: 'sign_in_sms_otp', phone_number: '+1234567890' } as any;
await expect(handler(params, context)).rejects.toThrow('Missing required field: otp_code');
});
it('should send SMS successfully with stub provider', async () => {
const context = createContext();
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: '+1234567890',
otp_code: '123456',
};
const result = await handler(params, context);
expect(result).toEqual({ complete: true });
expect(mockLog.info).toHaveBeenCalledWith(
'[send-sms] Processing request',
expect.any(Object)
);
});
it('should handle dry run mode', async () => {
const context = createContext({ SEND_SMS_DRY_RUN: 'true' });
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: '+1234567890',
otp_code: '123456',
};
const result = await handler(params, context);
expect(result).toEqual({ complete: true, dryRun: true });
expect(mockLog.info).toHaveBeenCalledWith(
'[send-sms] DRY RUN - SMS not sent',
expect.any(Object)
);
});
it('should return error when databaseId is missing', async () => {
const context = {
...createContext(),
job: { jobId: 'test', workerId: 'test', databaseId: null, actorId: null },
};
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: '+1234567890',
otp_code: '123456',
};
const result = await handler(params, context as any);
expect(result).toEqual({ error: 'Missing X-Database-Id header or DEFAULT_DATABASE_ID' });
});
it('should send SMS via Twilio when configured', async () => {
mockTwilioCreate.mockResolvedValue({ sid: 'SM123456789' });
const context = createContext({
SMS_PROVIDER: 'twilio',
TWILIO_ACCOUNT_SID: 'ACtest123',
TWILIO_AUTH_TOKEN: 'token123',
TWILIO_FROM_NUMBER: '+15551234567',
});
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: '+1234567890',
otp_code: '123456',
};
const result = await handler(params, context);
expect(result).toEqual({ complete: true });
expect(mockLog.info).toHaveBeenCalledWith(
'[send-sms] SMS sent via Twilio',
expect.objectContaining({ messageId: 'SM123456789' })
);
});
it('should return error when Twilio credentials are missing', async () => {
const context = createContext({
SMS_PROVIDER: 'twilio',
// Missing credentials
});
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: '+1234567890',
otp_code: '123456',
};
await expect(handler(params, context)).rejects.toThrow('Twilio credentials not configured');
});
it('should handle Twilio API errors gracefully', async () => {
mockTwilioCreate.mockRejectedValue(new Error('Invalid phone number'));
const context = createContext({
SMS_PROVIDER: 'twilio',
TWILIO_ACCOUNT_SID: 'ACtest123',
TWILIO_AUTH_TOKEN: 'token123',
TWILIO_FROM_NUMBER: '+15551234567',
});
const params = {
sms_type: 'sign_in_sms_otp' as const,
phone_number: 'invalid',
otp_code: '123456',
};
await expect(handler(params, context)).rejects.toThrow('Invalid phone number');
});
});