Skip to content

Commit

Permalink
refactor: improve NewFlowExpiredError
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent 2495629 commit 1caefac
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion selfservice/flow/registration/error.go
Expand Up @@ -47,7 +47,8 @@ type (
}
)

func NewFlowExpiredError(ago time.Duration) *FlowExpiredError {
func NewFlowExpiredError(at time.Time) *FlowExpiredError {
ago := time.Since(at)
return &FlowExpiredError{
ago: ago,
DefaultError: herodot.ErrBadRequest.
Expand Down
6 changes: 4 additions & 2 deletions selfservice/flow/registration/error_test.go
Expand Up @@ -86,6 +86,8 @@ func TestHandleError(t *testing.T) {
return sse.Payload.Errors, nil
}

anHourAgo := time.Now().Add(-time.Hour)

t.Run("case=error with nil flow defaults to error ui redirect", func(t *testing.T) {
t.Cleanup(reset)

Expand Down Expand Up @@ -118,7 +120,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

registrationFlow = newFlow(t, time.Minute, flow.TypeAPI)
flowError = registration.NewFlowExpiredError(time.Hour)
flowError = registration.NewFlowExpiredError(anHourAgo)
ct = identity.CredentialsTypePassword

res, err := ts.Client().Do(testhelpers.NewHTTPGetJSONRequest(t, ts.URL+"/error"))
Expand Down Expand Up @@ -203,7 +205,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

registrationFlow = &registration.Flow{Type: flow.TypeBrowser}
flowError = registration.NewFlowExpiredError(time.Hour)
flowError = registration.NewFlowExpiredError(anHourAgo)
ct = identity.CredentialsTypePassword

lf, _ := expectRegistrationUI(t)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/registration/flow.go
Expand Up @@ -125,7 +125,7 @@ func (f *Flow) GetID() uuid.UUID {

func (f *Flow) Valid() error {
if f.ExpiresAt.Before(time.Now()) {
return errors.WithStack(NewFlowExpiredError(time.Since(f.ExpiresAt)))
return errors.WithStack(NewFlowExpiredError(f.ExpiresAt))
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion selfservice/flow/registration/handler.go
Expand Up @@ -5,9 +5,10 @@ import (
"time"

"github.com/julienschmidt/httprouter"
"github.com/ory/x/urlx"
"github.com/pkg/errors"

"github.com/ory/x/urlx"

"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow"
Expand Down

0 comments on commit 1caefac

Please sign in to comment.