forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow user to update phone number (supabase#421)
* refactor: sendPhoneConfirmation should accept an otpType and smsProvider * fix: allow update phone for users endpoint * fix: add phone change verification method
- Loading branch information
1 parent
9c92569
commit 9403e25
Showing
10 changed files
with
263 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/netlify/gotrue/conf" | ||
"github.com/netlify/gotrue/models" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type PhoneTestSuite struct { | ||
suite.Suite | ||
API *API | ||
Config *conf.Configuration | ||
|
||
instanceID uuid.UUID | ||
} | ||
|
||
type TestSmsProvider struct { | ||
mock.Mock | ||
} | ||
|
||
func (t TestSmsProvider) SendSms(phone string, message string) error { | ||
return nil | ||
} | ||
|
||
func TestPhone(t *testing.T) { | ||
api, config, instanceID, err := setupAPIForTestForInstance() | ||
require.NoError(t, err) | ||
|
||
ts := &PhoneTestSuite{ | ||
API: api, | ||
Config: config, | ||
instanceID: instanceID, | ||
} | ||
defer api.db.Close() | ||
|
||
suite.Run(t, ts) | ||
} | ||
|
||
func (ts *PhoneTestSuite) SetupTest() { | ||
models.TruncateAll(ts.API.db) | ||
|
||
// Create user | ||
u, err := models.NewUser(ts.instanceID, "", "password", ts.Config.JWT.Aud, nil) | ||
u.Phone = "123456789" | ||
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 := ts.API.validateE164Format("0123456789") | ||
assert.Equal(ts.T(), false, isValid) | ||
} | ||
|
||
func (ts *PhoneTestSuite) TestFormatPhoneNumber() { | ||
actual := ts.API.formatPhoneNumber("+1 23456789 ") | ||
assert.Equal(ts.T(), "123456789", actual) | ||
} | ||
|
||
func (ts *PhoneTestSuite) TestSendPhoneConfirmation() { | ||
u, err := models.FindUserByPhoneAndAudience(ts.API.db, ts.instanceID, "123456789", ts.Config.JWT.Aud) | ||
require.NoError(ts.T(), err) | ||
ctx, err := WithInstanceConfig(context.Background(), ts.Config, ts.instanceID) | ||
require.NoError(ts.T(), err) | ||
cases := []struct { | ||
desc string | ||
otpType string | ||
expected error | ||
}{ | ||
{ | ||
"send confirmation otp", | ||
phoneConfirmationOtp, | ||
nil, | ||
}, | ||
{ | ||
"send phone_change otp", | ||
phoneChangeOtp, | ||
nil, | ||
}, | ||
{ | ||
"send invalid otp type ", | ||
"invalid otp type", | ||
internalServerError("invalid otp type"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
err = ts.API.sendPhoneConfirmation(ctx, ts.API.db, u, "123456789", c.otpType, TestSmsProvider{}) | ||
require.Equal(ts.T(), c.expected, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.