Skip to content

Commit

Permalink
chore: refactor hook validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Nov 8, 2024
1 parent 090c7c1 commit 08040c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
31 changes: 11 additions & 20 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,19 +734,20 @@ func (c *baseConfig) Validate(fsys fs.FS) error {
c.Auth.Sms.EnableSignup = false
fmt.Fprintln(os.Stderr, "WARN: no SMS provider is enabled. Disabling phone login")
}
if err := c.Auth.Hook.MFAVerificationAttempt.HandleHook("mfa_verification_attempt"); err != nil {
// Validate hook config
if err := c.Auth.Hook.MFAVerificationAttempt.validate("mfa_verification_attempt"); err != nil {
return err
}
if err := c.Auth.Hook.PasswordVerificationAttempt.HandleHook("password_verification_attempt"); err != nil {
if err := c.Auth.Hook.PasswordVerificationAttempt.validate("password_verification_attempt"); err != nil {
return err
}
if err := c.Auth.Hook.CustomAccessToken.HandleHook("custom_access_token"); err != nil {
if err := c.Auth.Hook.CustomAccessToken.validate("custom_access_token"); err != nil {
return err
}
if err := c.Auth.Hook.SendSMS.HandleHook("send_sms"); err != nil {
if err := c.Auth.Hook.SendSMS.validate("send_sms"); err != nil {
return err
}
if err := c.Auth.Hook.SendEmail.HandleHook("send_email"); err != nil {
if err := c.Auth.Hook.SendEmail.validate("send_email"); err != nil {
return err
}
// Validate oauth config
Expand Down Expand Up @@ -917,16 +918,17 @@ func (c *seed) loadSeedPaths(basePath string, fsys fs.FS) error {
return nil
}

func (h *hookConfig) HandleHook(hookType string) error {
func (h *hookConfig) validate(hookType string) error {
// If not enabled do nothing
if !h.Enabled {
return nil
}
if h.URI == "" {
return errors.Errorf("missing required field in config: auth.hook.%s.uri", hookType)
}
if err := validateHookURI(h.URI, hookType); err != nil {
return err
} else if parsed, err := url.Parse(h.URI); err != nil {
return errors.Errorf("failed to parse template url: %w", err)
} else if !(parsed.Scheme == "http" || parsed.Scheme == "https" || parsed.Scheme == "pg-functions") {
return errors.Errorf("Invalid HTTP hook config: auth.hook.%v should be a Postgres function URI, or a HTTP or HTTPS URL", hookType)
}
var err error
if h.Secrets, err = maybeLoadEnv(h.Secrets); err != nil {
Expand All @@ -935,17 +937,6 @@ func (h *hookConfig) HandleHook(hookType string) error {
return nil
}

func validateHookURI(uri, hookName string) error {
parsed, err := url.Parse(uri)
if err != nil {
return errors.Errorf("failed to parse template url: %w", err)
}
if !(parsed.Scheme == "http" || parsed.Scheme == "https" || parsed.Scheme == "pg-functions") {
return errors.Errorf("Invalid HTTP hook config: auth.hook.%v should be a Postgres function URI, or a HTTP or HTTPS URL", hookName)
}
return nil
}

// TODO: use field tag validator instead
var funcSlugPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]*$`)

Expand Down
7 changes: 6 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ func TestValidateHookURI(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateHookURI(tt.uri, tt.hookName)
h := hookConfig{
Enabled: true,
URI: tt.uri,
Secrets: "test-secret",
}
err := h.validate(tt.hookName)
if tt.shouldErr {
assert.Error(t, err, "Expected an error for %v", tt.name)
assert.EqualError(t, err, tt.errorMsg, "Expected error message does not match for %v", tt.name)
Expand Down

0 comments on commit 08040c7

Please sign in to comment.