-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: allow user to update phone number #421
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the function signature here to take in an
otpType
and asmsProvider
. ThesmsProvider
was added to the function params to make it easier to test this function with a mock sms provider that implements a stubSendSms
method. See TestSendPhoneConfirmation.