Skip to content

Commit

Permalink
feat: finalize tests for registration flow refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent 4772f71 commit 8e52c3a
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .schema/api.swagger.json
Expand Up @@ -1289,7 +1289,7 @@
{
"type": "string",
"description": "The Registration Flow ID\n\nThe value for this parameter comes from `flow` URL Query parameter sent to your\napplication (e.g. `/registration?flow=abcde`).",
"name": "flow",
"name": "id",
"in": "query",
"required": true
}
Expand Down
2 changes: 1 addition & 1 deletion driver/configuration/provider.go
Expand Up @@ -78,7 +78,7 @@ type Provider interface {
SelfServiceFlowLoginReturnTo(strategy string) *url.URL
SelfServiceFlowLoginRequestLifespan() time.Duration

SelfServiceFlowRegisterUI() *url.URL
SelfServiceFlowRegistrationUI() *url.URL
SelfServiceFlowRegistrationBeforeHooks() []SelfServiceHook
SelfServiceFlowRegistrationAfterHooks(strategy string) []SelfServiceHook
SelfServiceFlowRegistrationReturnTo(strategy string) *url.URL
Expand Down
2 changes: 1 addition & 1 deletion driver/configuration/provider_viper.go
Expand Up @@ -376,7 +376,7 @@ func (p *ViperProvider) SelfServiceFlowErrorURL() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceErrorUI)
}

func (p *ViperProvider) SelfServiceFlowRegisterUI() *url.URL {
func (p *ViperProvider) SelfServiceFlowRegistrationUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceRegistrationUI)
}

Expand Down
4 changes: 2 additions & 2 deletions driver/configuration/provider_viper_test.go
Expand Up @@ -54,7 +54,7 @@ func TestViperProvider(t *testing.T) {
t.Run("group=urls", func(t *testing.T) {
assert.Equal(t, "http://test.kratos.ory.sh/login", p.SelfServiceFlowLoginUI().String())
assert.Equal(t, "http://test.kratos.ory.sh/settings", p.SelfServiceFlowSettingsUI().String())
assert.Equal(t, "http://test.kratos.ory.sh/register", p.SelfServiceFlowRegisterUI().String())
assert.Equal(t, "http://test.kratos.ory.sh/register", p.SelfServiceFlowRegistrationUI().String())
assert.Equal(t, "http://test.kratos.ory.sh/error", p.SelfServiceFlowErrorURL().String())

assert.Equal(t, "http://admin.kratos.ory.sh", p.SelfAdminURL().String())
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestViperProvider_Defaults(t *testing.T) {
p := configuration.NewViperProvider(logrusx.New("", ""), false)
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/login", p.SelfServiceFlowLoginUI().String())
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/settings", p.SelfServiceFlowSettingsUI().String())
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/registration", p.SelfServiceFlowRegisterUI().String())
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/registration", p.SelfServiceFlowRegistrationUI().String())
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/recovery", p.SelfServiceFlowRecoveryUI().String())
assert.Equal(t, "https://www.ory.sh/kratos/docs/fallback/verification", p.SelfServiceFlowVerificationUI().String())
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion selfservice/flow/login/handler_test.go
Expand Up @@ -176,7 +176,7 @@ func TestGetFlow(t *testing.T) {
IssuedAt: time.Now().Add(-time.Minute * 2),
RequestURL: public.URL + login.RouteInitBrowserFlow,
CSRFToken: x.FakeCSRFToken,
Type: flow.TypeBrowser,
Type: flow.TypeBrowser,
}
}

Expand Down
62 changes: 33 additions & 29 deletions selfservice/flow/registration/error.go
@@ -1,7 +1,6 @@
package registration

import (
"context"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -69,67 +68,72 @@ func (s *ErrorHandler) WriteFlowError(
w http.ResponseWriter,
r *http.Request,
ct identity.CredentialsType,
rr *Flow,
f *Flow,
err error,
) {
s.d.Audit().
WithError(err).
WithRequest(r).
WithField("registration_flow", rr).
WithField("registration_flow", f).
Info("Encountered self-service flow error.")

if f == nil {
s.forward(w, r, nil, err)
return
}

if e := new(FlowExpiredError); errors.As(err, &e) {
// create new flow because the old one is not valid
a, err := s.d.RegistrationHandler().NewRegistrationFlow(w, r, rr.Type)
a, err := s.d.RegistrationHandler().NewRegistrationFlow(w, r, f.Type)
if err != nil {
// failed to create a new session and redirect to it, handle that error as a new one
s.WriteFlowError(w, r, ct, rr, err)
s.WriteFlowError(w, r, ct, f, err)
return
}

a.Messages.Add(text.NewErrorValidationRegistrationFlowExpired(e.ago))
if err := s.d.RegistrationFlowPersister().UpdateRegistrationFlow(context.TODO(), a); err != nil {
redirTo, err := s.d.SelfServiceErrorManager().Create(r.Context(), w, r, err)
if err != nil {
s.WriteFlowError(w, r, ct, rr, err)
return
}
http.Redirect(w, r, redirTo, http.StatusFound)
if err := s.d.RegistrationFlowPersister().UpdateRegistrationFlow(r.Context(), a); err != nil {
s.forward(w, r, a, err)
return
}

http.Redirect(w, r, urlx.CopyWithQuery(s.c.SelfServiceFlowRegisterUI(), url.Values{"request": {a.ID.String()}}).String(), http.StatusFound)
if f.Type == flow.TypeAPI {
http.Redirect(w, r, urlx.CopyWithQuery(urlx.AppendPaths(s.c.SelfPublicURL(),
RouteGetFlow), url.Values{"id": {a.ID.String()}}).String(), http.StatusFound)
} else {
http.Redirect(w, r, a.AppendTo(s.c.SelfServiceFlowRegistrationUI()).String(), http.StatusFound)
}
return
}

if rr == nil {
s.d.SelfServiceErrorManager().Forward(r.Context(), w, r, err)
return
} else if x.IsJSONRequest(r) {
s.d.Writer().WriteError(w, r, err)
method, ok := f.Methods[ct]
if !ok {
s.forward(w, r, f, errors.WithStack(herodot.ErrInternalServerError.
WithErrorf(`Expected registration method "%s" to exist in flow. This is a bug in the code and should be reported on GitHub.`, ct)))
return
}

method, ok := rr.Methods[ct]
if !ok {
s.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError.WithDebugf("Methods: %+v", rr.Methods).WithErrorf(`Expected registration method "%s" to exist in request. This is a bug in the code and should be reported on GitHub.`, ct)))
if err := method.Config.ParseError(err); err != nil {
s.forward(w, r, f, err)
return
}

if err := method.Config.ParseError(err); err != nil {
s.d.SelfServiceErrorManager().Forward(r.Context(), w, r, err)
if err := s.d.RegistrationFlowPersister().UpdateRegistrationFlowMethod(r.Context(), f.ID, ct, method); err != nil {
s.forward(w, r, f, err)
return
}

if err := s.d.RegistrationFlowPersister().UpdateRegistrationFlowMethod(r.Context(), rr.ID, ct, method); err != nil {
s.d.SelfServiceErrorManager().Forward(r.Context(), w, r, err)
if f.Type == flow.TypeBrowser {
http.Redirect(w, r, f.AppendTo(s.c.SelfServiceFlowRegistrationUI()).String(), http.StatusFound)
return
}

http.Redirect(w, r,
urlx.CopyWithQuery(s.c.SelfServiceFlowRegisterUI(), url.Values{"request": {rr.ID.String()}}).String(),
http.StatusFound,
)
innerRegistrationFlow, innerErr := s.d.RegistrationFlowPersister().GetRegistrationFlow(r.Context(), f.ID)
if innerErr != nil {
s.forward(w, r, innerRegistrationFlow, innerErr)
}

s.d.Writer().WriteCode(w, r, x.RecoverStatusCode(err, http.StatusBadRequest), innerRegistrationFlow)
}

func (s *ErrorHandler) forward(w http.ResponseWriter, r *http.Request, rr *Flow, err error) {
Expand Down

0 comments on commit 8e52c3a

Please sign in to comment.