-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathphone_test.go
More file actions
444 lines (400 loc) · 12.8 KB
/
phone_test.go
File metadata and controls
444 lines (400 loc) · 12.8 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/supabase/auth/internal/api/apierrors"
"github.com/supabase/auth/internal/api/sms_provider"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/models"
)
type PhoneTestSuite struct {
suite.Suite
API *API
Config *conf.GlobalConfiguration
}
type TestSmsProvider struct {
mock.Mock
SentMessages int
}
func (t *TestSmsProvider) SendMessage(phone, message, channel, otp string) (string, error) {
t.SentMessages += 1
return "", nil
}
func (t *TestSmsProvider) VerifyOTP(phone, otp string) error {
return nil
}
func TestPhone(t *testing.T) {
api, config, err := setupAPIForTest()
require.NoError(t, err)
ts := &PhoneTestSuite{
API: api,
Config: config,
}
defer api.db.Close()
suite.Run(t, ts)
}
func (ts *PhoneTestSuite) SetupTest() {
models.TruncateAll(ts.API.db)
// Create user
u, err := models.NewUser("123456789", "", "password", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error creating test user model")
require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new test user")
}
func (ts *PhoneTestSuite) TestValidateE164Format() {
isValid := validateE164Format("0123456789")
assert.Equal(ts.T(), false, isValid)
}
func (ts *PhoneTestSuite) TestFormatPhoneNumber() {
actual := formatPhoneNumber("+1 23456789 ")
assert.Equal(ts.T(), "123456789", actual)
}
func doTestSendPhoneConfirmation(ts *PhoneTestSuite, useTestOTP bool) {
u, err := models.FindUserByPhoneAndAudience(ts.API.db, "123456789", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
req, err := http.NewRequest("POST", "http://localhost:9998/otp", nil)
require.NoError(ts.T(), err)
cases := []struct {
desc string
otpType string
expected error
}{
{
desc: "send confirmation otp",
otpType: phoneConfirmationOtp,
expected: nil,
},
{
desc: "send phone_change otp",
otpType: phoneChangeVerification,
expected: nil,
},
{
desc: "send recovery otp",
otpType: phoneReauthenticationOtp,
expected: nil,
},
{
desc: "send invalid otp type ",
otpType: "invalid otp type",
expected: apierrors.NewInternalServerError("invalid otp type"),
},
}
if useTestOTP {
ts.API.config.Sms.TestOTP = conf.TestOTPMap{
"123456789": "123456",
}
} else {
ts.API.config.Sms.TestOTP = nil
}
for _, c := range cases {
ts.Run(c.desc, func() {
provider := &TestSmsProvider{}
sms_provider.MockProvider = provider
_, err = ts.API.sendPhoneConfirmation(req, ts.API.db, u, "123456789", c.otpType, sms_provider.SMSProvider)
require.Equal(ts.T(), c.expected, err)
u, err = models.FindUserByPhoneAndAudience(ts.API.db, "123456789", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
if c.expected == nil {
if useTestOTP {
require.Equal(ts.T(), provider.SentMessages, 0)
} else {
require.Equal(ts.T(), provider.SentMessages, 1)
}
}
switch c.otpType {
case phoneConfirmationOtp:
require.NotEmpty(ts.T(), u.ConfirmationToken)
require.NotEmpty(ts.T(), u.ConfirmationSentAt)
case phoneChangeVerification:
require.NotEmpty(ts.T(), u.PhoneChangeToken)
require.NotEmpty(ts.T(), u.PhoneChangeSentAt)
case phoneReauthenticationOtp:
require.NotEmpty(ts.T(), u.ReauthenticationToken)
require.NotEmpty(ts.T(), u.ReauthenticationSentAt)
default:
}
})
}
// Reset at end of test
ts.API.config.Sms.TestOTP = nil
}
func (ts *PhoneTestSuite) TestSendPhoneConfirmation() {
doTestSendPhoneConfirmation(ts, false)
}
func (ts *PhoneTestSuite) TestSendPhoneConfirmationWithTestOTP() {
doTestSendPhoneConfirmation(ts, true)
}
func (ts *PhoneTestSuite) TestMissingSmsProviderConfig() {
u, err := models.FindUserByPhoneAndAudience(ts.API.db, "123456789", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
now := time.Now()
u.PhoneConfirmedAt = &now
require.NoError(ts.T(), ts.API.db.Update(u), "Error updating new test user")
s, err := models.NewSession(u.ID, nil)
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(s))
req := httptest.NewRequest(http.MethodPost, "/token?grant_type=password", nil)
token, _, err := ts.API.generateAccessToken(req, ts.API.db, u, &s.ID, models.PasswordGrant)
require.NoError(ts.T(), err)
cases := []struct {
desc string
endpoint string
method string
header string
body map[string]string
expected map[string]interface{}
}{
{
desc: "Signup",
endpoint: "/signup",
method: http.MethodPost,
header: "",
body: map[string]string{
"phone": "1234567890",
"password": "testpassword",
},
expected: map[string]interface{}{
"code": http.StatusInternalServerError,
"message": "Unable to get SMS provider",
},
},
{
desc: "Sms OTP",
endpoint: "/otp",
method: http.MethodPost,
header: "",
body: map[string]string{
"phone": "123456789",
},
expected: map[string]interface{}{
"code": http.StatusInternalServerError,
"message": "Unable to get SMS provider",
},
},
{
desc: "Phone change",
endpoint: "/user",
method: http.MethodPut,
header: token,
body: map[string]string{
"phone": "111111111",
},
expected: map[string]interface{}{
"code": http.StatusInternalServerError,
"message": "Unable to get SMS provider",
},
},
{
desc: "Reauthenticate",
endpoint: "/reauthenticate",
method: http.MethodGet,
header: "",
body: nil,
expected: map[string]interface{}{
"code": http.StatusInternalServerError,
"message": "Unable to get SMS provider",
},
},
}
smsProviders := []string{"twilio", "messagebird", "textlocal", "vonage"}
ts.Config.External.Phone.Enabled = true
ts.Config.Sms.Twilio.AccountSid = ""
ts.Config.Sms.Messagebird.AccessKey = ""
ts.Config.Sms.Textlocal.ApiKey = ""
ts.Config.Sms.Vonage.ApiKey = ""
for _, c := range cases {
for _, provider := range smsProviders {
ts.Config.Sms.Provider = provider
desc := fmt.Sprintf("[%v] %v", provider, c.desc)
ts.Run(desc, func() {
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.body))
req := httptest.NewRequest(c.method, "http://localhost"+c.endpoint, &buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
require.Equal(ts.T(), c.expected["code"], w.Code)
body := w.Body.String()
require.True(ts.T(),
strings.Contains(body, "Unable to get SMS provider") ||
strings.Contains(body, "Error finding SMS provider") ||
strings.Contains(body, "Failed to get SMS provider"),
"unexpected body message %q", body,
)
})
}
}
}
func (ts *PhoneTestSuite) TestSendSMSHook() {
u, err := models.FindUserByPhoneAndAudience(ts.API.db, "123456789", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
now := time.Now()
u.PhoneConfirmedAt = &now
require.NoError(ts.T(), ts.API.db.Update(u), "Error updating new test user")
s, err := models.NewSession(u.ID, nil)
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(s))
req := httptest.NewRequest(http.MethodPost, "/token?grant_type=password", nil)
token, _, err := ts.API.generateAccessToken(req, ts.API.db, u, &s.ID, models.PasswordGrant)
require.NoError(ts.T(), err)
// We setup a job table to enqueue SMS requests to send. Similar in spirit to the pg_boss postgres extension
createJobsTableSQL := `CREATE TABLE job_queue (
id serial PRIMARY KEY,
job_type text,
payload jsonb,
status text DEFAULT 'pending', -- Possible values: 'pending', 'processing', 'completed', 'failed'
created_at timestamp without time zone DEFAULT NOW()
);`
require.NoError(ts.T(), ts.API.db.RawQuery(createJobsTableSQL).Exec())
type sendSMSHookTestCase struct {
desc string
uri string
endpoint string
method string
header string
body map[string]string
hookFunctionSQL string
expectedCode int
expectToken bool
hookFunctionIdentifier string
}
cases := []sendSMSHookTestCase{
{
desc: "Phone signup using Hook",
endpoint: "/signup",
method: http.MethodPost,
uri: "pg-functions://postgres/auth/send_sms_signup",
hookFunctionSQL: `
create or replace function send_sms_signup(input jsonb)
returns json as $$
begin
insert into job_queue(job_type, payload)
values ('sms_signup', input);
return input;
end; $$ language plpgsql;`,
header: "",
body: map[string]string{
"phone": "1234567890",
"password": "testpassword",
},
expectedCode: http.StatusOK,
hookFunctionIdentifier: "send_sms_signup(input jsonb)",
},
{
desc: "SMS OTP sign in using hook",
endpoint: "/otp",
method: http.MethodPost,
uri: "pg-functions://postgres/auth/send_sms_otp",
hookFunctionSQL: `
create or replace function send_sms_otp(input jsonb)
returns json as $$
begin
insert into job_queue(job_type, payload)
values ('sms_signup', input);
return input;
end; $$ language plpgsql;`,
header: "",
body: map[string]string{
"phone": "123456789",
},
expectToken: false,
expectedCode: http.StatusOK,
hookFunctionIdentifier: "send_sms_otp(input jsonb)",
},
{
desc: "Phone Change",
endpoint: "/user",
method: http.MethodPut,
uri: "pg-functions://postgres/auth/send_sms_phone_change",
hookFunctionSQL: `
create or replace function send_sms_phone_change(input jsonb)
returns json as $$
begin
insert into job_queue(job_type, payload)
values ('phone_change', input);
return input;
end; $$ language plpgsql;`,
header: token,
body: map[string]string{
"phone": "111111111",
},
expectToken: true,
expectedCode: http.StatusOK,
hookFunctionIdentifier: "send_sms_phone_change(input jsonb)",
},
{
desc: "Reauthenticate",
endpoint: "/reauthenticate",
method: http.MethodGet,
uri: "pg-functions://postgres/auth/reauthenticate",
hookFunctionSQL: `
create or replace function reauthenticate(input jsonb)
returns json as $$
begin
return input;
end; $$ language plpgsql;`,
header: "",
body: nil,
expectToken: true,
expectedCode: http.StatusOK,
hookFunctionIdentifier: "reauthenticate(input jsonb)",
},
{
desc: "SMS OTP Hook (Error)",
endpoint: "/otp",
method: http.MethodPost,
uri: "pg-functions://postgres/auth/send_sms_otp_failure",
hookFunctionSQL: `
create or replace function send_sms_otp(input jsonb)
returns json as $$
begin
RAISE EXCEPTION 'Intentional Error for Testing';
end; $$ language plpgsql;`,
header: "",
body: map[string]string{
"phone": "123456789",
},
expectToken: false,
expectedCode: http.StatusInternalServerError,
hookFunctionIdentifier: "send_sms_otp_failure(input jsonb)",
},
}
for _, c := range cases {
ts.T().Run(c.desc, func(t *testing.T) {
ts.Config.External.Phone.Enabled = true
ts.Config.Hook.SendSMS.Enabled = true
ts.Config.Hook.SendSMS.URI = c.uri
// Disable FrequencyLimit to allow back to back sending
ts.Config.Sms.MaxFrequency = 0 * time.Second
require.NoError(ts.T(), ts.Config.Hook.SendSMS.PopulateExtensibilityPoint())
require.NoError(t, ts.API.db.RawQuery(c.hookFunctionSQL).Exec())
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.body))
req := httptest.NewRequest(c.method, "http://localhost"+c.endpoint, &buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
require.Equal(t, c.expectedCode, w.Code, "Unexpected HTTP status code")
// Delete the function and reset env
cleanupHookSQL := fmt.Sprintf("drop function if exists %s", ts.Config.Hook.SendSMS.HookName)
require.NoError(t, ts.API.db.RawQuery(cleanupHookSQL).Exec())
ts.Config.Hook.SendSMS.Enabled = false
ts.Config.Sms.MaxFrequency = 1 * time.Second
})
}
// Cleanup
deleteJobsTableSQL := `drop table if exists job_queue`
require.NoError(ts.T(), ts.API.db.RawQuery(deleteJobsTableSQL).Exec())
}