Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Jul 2, 2024
1 parent 265bd01 commit 6c8f8a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 0 additions & 9 deletions selfservice/hook/password_migration_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ func (p *PasswordMigration) Execute(ctx context.Context, data *PasswordMigration

resp, err := httpClient.Do(req)
if err != nil {
if isTimeoutError(err) {
return herodot.DefaultError{
CodeField: http.StatusGatewayTimeout,
StatusField: http.StatusText(http.StatusGatewayTimeout),
GRPCCodeField: grpccodes.DeadlineExceeded,
ReasonField: "A third-party upstream service could not be reached. Please try again later.",
ErrorField: "calling the password migration hook failed",
}.WithWrap(errors.WithStack(err))
}
return herodot.DefaultError{
CodeField: http.StatusBadGateway,
StatusField: http.StatusText(http.StatusBadGateway),
Expand Down
1 change: 0 additions & 1 deletion selfservice/strategy/password/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func (s *Strategy) Login(w http.ResponseWriter, r *http.Request, f *login.Flow,
if o.ShouldUsePasswordMigrationHook() {
pwHook := s.d.Config().PasswordMigrationHook(r.Context())
if !pwHook.Enabled {
// TODO: How can we make sure this does not trigger on-call?
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Password migration hook is not enabled but password migration is requested."))
}

Expand Down
20 changes: 20 additions & 0 deletions selfservice/strategy/password/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ func TestCompleteLogin(t *testing.T) {
_, _ = io.WriteString(w, `{"status":"password_match"}`)

// Set up diverse fake passwords to trigger special response status codes:
case "timeout":
// We'll just block until the request gets cancelled
<-r.Context().Done()
case "500":
w.WriteHeader(http.StatusInternalServerError)
case "201":
Expand All @@ -905,6 +908,7 @@ func TestCompleteLogin(t *testing.T) {
for _, tc := range []struct {
name string
addPassword func(identifier, password string)
setupFn func() func()
credentialsConfig string
expectSuccess bool
}{{
Expand Down Expand Up @@ -937,9 +941,25 @@ func TestCompleteLogin(t *testing.T) {
credentialsConfig: `{"use_password_migration_hook": true, "hashed_password":"hash"}`,
addPassword: func(identifier, password string) { passwordDB[identifier] = password },
expectSuccess: false,
}, {
name: "should not call migration hook if disabled",
credentialsConfig: `{"use_password_migration_hook": true}`,
addPassword: func(identifier, password string) { passwordDB[identifier] = password },
setupFn: func() func() {
require.NoError(t, reg.Config().Set(ctx, config.ViperKeyPasswordMigrationHook+".enabled", false))
return func() {
require.NoError(t, reg.Config().Set(ctx, config.ViperKeyPasswordMigrationHook+".enabled", true))
}
},
expectSuccess: false,
}} {

t.Run("case="+tc.name, func(t *testing.T) {
if tc.setupFn != nil {
cleanup := tc.setupFn()
t.Cleanup(cleanup)
}

identifier := x.NewUUID().String()
password := x.NewUUID().String()
if tc.addPassword != nil {
Expand Down

0 comments on commit 6c8f8a1

Please sign in to comment.