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.
feat: send over user in SendSMS Hook instead of UserID (supabase#1551)
## What kind of change does this PR introduce? We align convention with `SendEmail` and send over a user to avoid having the user make an additional `getUser` call. Also allows access to `app_metadata` and `user_metadata` which would be useful for internationalization where you may want the locale of the user to determine which template to send. We also introduce a `PhoneData` struct through which we can introduce any potential phone related fields. This struct currently lives under the `hooks` package as there is no `phone` package currently and introducing one might require a significant refactor. Importing it as as is under `api` package would cause a circular dependency between `hooks` and `api` packages. --------- Co-authored-by: Stojan Dimitrovski <[email protected]>
- Loading branch information
Showing
3 changed files
with
35 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ import ( | |
"testing" | ||
|
||
"errors" | ||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/supabase/auth/internal/conf" | ||
"github.com/supabase/auth/internal/hooks" | ||
"github.com/supabase/auth/internal/models" | ||
"github.com/supabase/auth/internal/storage" | ||
"net/http/httptest" | ||
|
||
|
@@ -22,8 +22,9 @@ var handleApiRequest func(*http.Request) (*http.Response, error) | |
|
||
type HooksTestSuite struct { | ||
suite.Suite | ||
API *API | ||
Config *conf.GlobalConfiguration | ||
API *API | ||
Config *conf.GlobalConfiguration | ||
TestUser *models.User | ||
} | ||
|
||
type MockHttpClient struct { | ||
|
@@ -47,13 +48,22 @@ func TestHooks(t *testing.T) { | |
suite.Run(t, ts) | ||
} | ||
|
||
func (ts *HooksTestSuite) SetupTest() { | ||
models.TruncateAll(ts.API.db) | ||
u, err := models.NewUser("123456789", "[email protected]", "securetestpassword", 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") | ||
ts.TestUser = u | ||
} | ||
|
||
func (ts *HooksTestSuite) TestRunHTTPHook() { | ||
defer gock.OffAll() | ||
|
||
input := hooks.SendSMSInput{ | ||
UserID: uuid.Must(uuid.NewV4()), | ||
Phone: "1234567890", | ||
OTP: "123456", | ||
User: ts.TestUser, | ||
SMS: hooks.SMS{ | ||
OTP: "123456", | ||
}, | ||
} | ||
successOutput := hooks.SendSMSOutput{Success: true} | ||
testURL := "http://localhost:54321/functions/v1/custom-sms-sender" | ||
|
@@ -117,9 +127,10 @@ func (ts *HooksTestSuite) TestShouldRetryWithRetryAfterHeader() { | |
defer gock.OffAll() | ||
|
||
input := hooks.SendSMSInput{ | ||
UserID: uuid.Must(uuid.NewV4()), | ||
Phone: "1234567890", | ||
OTP: "123456", | ||
User: ts.TestUser, | ||
SMS: hooks.SMS{ | ||
OTP: "123456", | ||
}, | ||
} | ||
successOutput := hooks.SendSMSOutput{Success: true} | ||
testURL := "http://localhost:54321/functions/v1/custom-sms-sender" | ||
|
@@ -159,9 +170,10 @@ func (ts *HooksTestSuite) TestShouldReturnErrorForNonJSONContentType() { | |
defer gock.OffAll() | ||
|
||
input := hooks.SendSMSInput{ | ||
UserID: uuid.Must(uuid.NewV4()), | ||
Phone: "1234567890", | ||
OTP: "123456", | ||
User: ts.TestUser, | ||
SMS: hooks.SMS{ | ||
OTP: "123456", | ||
}, | ||
} | ||
testURL := "http://localhost:54321/functions/v1/custom-sms-sender" | ||
ts.Config.Hook.SendSMS.URI = testURL | ||
|
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