Skip to content

Commit

Permalink
fix: rename from CustomSMSProvider to SendSMS (#1513)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?

We are renaming `CustomEmailProvider` to `SendEmail`. For consistency, I
guess we should rename `CustomSMSProvider` to `SendSMS`. More context in
#1496

This should be fine since [the PR to add env configs for this
hook](supabase/infrastructure#17604) has not
landed in prod
  • Loading branch information
J0 authored Apr 3, 2024
1 parent 765db08 commit c0bc37b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ func (a *API) runHTTPHook(ctx context.Context, r *http.Request, hookConfig conf.

func (a *API) invokeHTTPHook(ctx context.Context, r *http.Request, input, output any) error {
switch input.(type) {
case *hooks.CustomSMSProviderInput:
hookOutput, ok := output.(*hooks.CustomSMSProviderOutput)
case *hooks.SendSMSInput:
hookOutput, ok := output.(*hooks.SendSMSOutput)
if !ok {
panic("output should be *hooks.CustomSMSProviderOutput")
panic("output should be *hooks.SendSMSOutput")
}
var response []byte
var err error

if response, err = a.runHTTPHook(ctx, r, a.config.Hook.CustomSMSProvider, input, output); err != nil {
if response, err = a.runHTTPHook(ctx, r, a.config.Hook.SendSMS, input, output); err != nil {
return internalServerError("Error invoking custom SMS provider hook.").WithInternalError(err)
}

Expand Down
26 changes: 13 additions & 13 deletions internal/api/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func TestHooks(t *testing.T) {
func (ts *HooksTestSuite) TestRunHTTPHook() {
defer gock.OffAll()

input := hooks.CustomSMSProviderInput{
input := hooks.SendSMSInput{
UserID: uuid.Must(uuid.NewV4()),
Phone: "1234567890",
OTP: "123456",
}
successOutput := hooks.CustomSMSProviderOutput{Success: true}
successOutput := hooks.SendSMSOutput{Success: true}
testURL := "http://localhost:54321/functions/v1/custom-sms-sender"
ts.Config.Hook.CustomSMSProvider.URI = testURL
ts.Config.Hook.SendSMS.URI = testURL

testCases := []struct {
description string
Expand All @@ -78,24 +78,24 @@ func (ts *HooksTestSuite) TestRunHTTPHook() {
for _, tc := range testCases {
ts.Run(tc.description, func() {
if tc.status == http.StatusOK {
gock.New(ts.Config.Hook.CustomSMSProvider.URI).
gock.New(ts.Config.Hook.SendSMS.URI).
Post("/").
MatchType("json").
Reply(tc.status).
JSON(tc.mockResponse).SetHeader("content-length", "21")
} else {
gock.New(ts.Config.Hook.CustomSMSProvider.URI).
gock.New(ts.Config.Hook.SendSMS.URI).
Post("/").
MatchType("json").
Reply(tc.status).
JSON(tc.mockResponse)

}

var output hooks.CustomSMSProviderOutput
req, _ := http.NewRequest("POST", ts.Config.Hook.CustomSMSProvider.URI, nil)
var output hooks.SendSMSOutput
req, _ := http.NewRequest("POST", ts.Config.Hook.SendSMS.URI, nil)
ctx := req.Context()
body, err := ts.API.runHTTPHook(ctx, req, ts.Config.Hook.CustomSMSProvider, &input, &output)
body, err := ts.API.runHTTPHook(ctx, req, ts.Config.Hook.SendSMS, &input, &output)

if !tc.expectError {
require.NoError(ts.T(), err)
Expand All @@ -114,14 +114,14 @@ func (ts *HooksTestSuite) TestRunHTTPHook() {
func (ts *HooksTestSuite) TestShouldRetryWithRetryAfterHeader() {
defer gock.OffAll()

input := hooks.CustomSMSProviderInput{
input := hooks.SendSMSInput{
UserID: uuid.Must(uuid.NewV4()),
Phone: "1234567890",
OTP: "123456",
}
successOutput := hooks.CustomSMSProviderOutput{Success: true}
successOutput := hooks.SendSMSOutput{Success: true}
testURL := "http://localhost:54321/functions/v1/custom-sms-sender"
ts.Config.Hook.CustomSMSProvider.URI = testURL
ts.Config.Hook.SendSMS.URI = testURL

gock.New(testURL).
Post("/").
Expand All @@ -136,14 +136,14 @@ func (ts *HooksTestSuite) TestShouldRetryWithRetryAfterHeader() {
Reply(http.StatusOK).
JSON(successOutput).SetHeader("content-length", "21")

var output hooks.CustomSMSProviderOutput
var output hooks.SendSMSOutput

// Simulate the original HTTP request which triggered the hook
req, err := http.NewRequest("POST", "http://localhost:9998/otp", nil)
require.NoError(ts.T(), err)
ctx := req.Context()

body, err := ts.API.runHTTPHook(ctx, req, ts.Config.Hook.CustomSMSProvider, &input, &output)
body, err := ts.API.runHTTPHook(ctx, req, ts.Config.Hook.SendSMS, &input, &output)
require.NoError(ts.T(), err)

err = json.Unmarshal(body, &output)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func (a *API) sendPhoneConfirmation(ctx context.Context, r *http.Request, tx *st
if err != nil {
return "", err
}
if config.Hook.CustomSMSProvider.Enabled {
input := hooks.CustomSMSProviderInput{
if config.Hook.SendSMS.Enabled {
input := hooks.SendSMSInput{
UserID: user.ID,
Phone: user.Phone.String(),
OTP: otp,
}
output := hooks.CustomSMSProviderOutput{}
output := hooks.SendSMSOutput{}
err := a.invokeHTTPHook(ctx, r, &input, &output)
if err != nil {
return "", err
Expand Down
8 changes: 4 additions & 4 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ type HookConfiguration struct {
MFAVerificationAttempt ExtensibilityPointConfiguration `json:"mfa_verification_attempt" split_words:"true"`
PasswordVerificationAttempt ExtensibilityPointConfiguration `json:"password_verification_attempt" split_words:"true"`
CustomAccessToken ExtensibilityPointConfiguration `json:"custom_access_token" split_words:"true"`
CustomSMSProvider ExtensibilityPointConfiguration `json:"custom_sms_provider" split_words:"true"`
SendSMS ExtensibilityPointConfiguration `json:"send_sms" split_words:"true"`
}

type HTTPHookSecrets []string
Expand Down Expand Up @@ -480,7 +480,7 @@ func (h *HookConfiguration) Validate() error {
h.MFAVerificationAttempt,
h.PasswordVerificationAttempt,
h.CustomAccessToken,
h.CustomSMSProvider,
h.SendSMS,
}
for _, point := range points {
if err := point.ValidateExtensibilityPoint(); err != nil {
Expand Down Expand Up @@ -582,8 +582,8 @@ func LoadGlobal(filename string) (*GlobalConfiguration, error) {
}
}

if config.Hook.CustomSMSProvider.Enabled {
if err := config.Hook.CustomSMSProvider.PopulateExtensibilityPoint(); err != nil {
if config.Hook.SendSMS.Enabled {
if err := config.Hook.SendSMS.PopulateExtensibilityPoint(); err != nil {
return nil, err
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/hooks/auth_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ type CustomAccessTokenOutput struct {
HookError AuthHookError `json:"error,omitempty"`
}

type CustomSMSProviderInput struct {
type SendSMSInput struct {
UserID uuid.UUID `json:"user_id"`
Phone string `json:"phone"`
OTP string `json:"otp"`
}

type CustomSMSProviderOutput struct {
type SendSMSOutput struct {
Success bool `json:"success"`
HookError AuthHookError `json:"error,omitempty"`
}
Expand Down Expand Up @@ -174,15 +174,15 @@ func (ca *CustomAccessTokenOutput) Error() string {
return ca.HookError.Message
}

func (cs *CustomSMSProviderOutput) IsError() bool {
func (cs *SendSMSOutput) IsError() bool {
return cs.HookError.Message != ""
}

func (cs *CustomSMSProviderOutput) Error() string {
func (cs *SendSMSOutput) Error() string {
return cs.HookError.Message
}

func (cs *CustomSMSProviderOutput) IsHTTPHook() bool {
func (cs *SendSMSOutput) IsHTTPHook() bool {
return true
}

Expand Down

0 comments on commit c0bc37b

Please sign in to comment.