Skip to content

Commit

Permalink
fix: improve hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent 1caefac commit 55ba485
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions selfservice/flow/login/hook_test.go
Expand Up @@ -32,17 +32,17 @@ func TestLoginExecutor(t *testing.T) {
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/login.schema.json")
viper.Set(configuration.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh/")

newServer := func(t *testing.T) *httptest.Server {
newServer := func(t *testing.T, ft flow.Type) *httptest.Server {
router := httprouter.New()

router.GET("/login/pre", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if testhelpers.SelfServiceHookLoginErrorHandler(t, w, r, reg.LoginHookExecutor().PreLoginHook(w, r, login.NewFlow(time.Minute, "", r, flow.TypeBrowser))) {
if testhelpers.SelfServiceHookLoginErrorHandler(t, w, r, reg.LoginHookExecutor().PreLoginHook(w, r, login.NewFlow(time.Minute, "", r, ft))) {
_, _ = w.Write([]byte("ok"))
}
})

router.GET("/login/post", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
a := login.NewFlow(time.Minute, "", r, flow.TypeBrowser)
a := login.NewFlow(time.Minute, "", r, ft)
a.RequestURL = x.RequestURL(r).String()
testhelpers.SelfServiceHookLoginErrorHandler(t, w, r,
reg.LoginHookExecutor().PostLoginHook(w, r, identity.CredentialsType(strategy), a, testhelpers.SelfServiceHookCreateFakeIdentity(t, reg)))
Expand All @@ -61,7 +61,7 @@ func TestLoginExecutor(t *testing.T) {
t.Run("case=pass without hooks", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)

res, _ := makeRequestPost(t, newServer(t), false, url.Values{})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
Expand All @@ -70,7 +70,7 @@ func TestLoginExecutor(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)
viperSetPost(strategy, []configuration.SelfServiceHook{{Name: "err", Config: []byte(`{}`)}})

res, _ := makeRequestPost(t, newServer(t), false, url.Values{})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})
Expand All @@ -79,23 +79,23 @@ func TestLoginExecutor(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)
viperSetPost(strategy, []configuration.SelfServiceHook{{Name: "err", Config: []byte(`{"ExecuteLoginPostHook": "abort"}`)}})

res, body := makeRequestPost(t, newServer(t), false, url.Values{})
res, body := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.Equal(t, "", body)
})

t.Run("case=prevent return_to value because domain not whitelisted", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)

res, _ := makeRequestPost(t, newServer(t), false, url.Values{"return_to": {"https://www.ory.sh/kratos/"}})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{"return_to": {"https://www.ory.sh/kratos/"}})
assert.EqualValues(t, http.StatusInternalServerError, res.StatusCode)
})

t.Run("case=use return_to value", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)
viper.Set(configuration.ViperKeyURLsWhitelistedReturnToDomains, []string{"https://www.ory.sh/"})

res, _ := makeRequestPost(t, newServer(t), false, url.Values{"return_to": {"https://www.ory.sh/kratos/"}})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{"return_to": {"https://www.ory.sh/kratos/"}})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
Expand All @@ -104,7 +104,7 @@ func TestLoginExecutor(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)
viper.Set(configuration.ViperKeySelfServiceLoginAfter+"."+configuration.DefaultBrowserReturnURL, "https://www.ory.sh/kratos")

res, _ := makeRequestPost(t, newServer(t), false, url.Values{})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
Expand All @@ -114,7 +114,7 @@ func TestLoginExecutor(t *testing.T) {
testhelpers.SelfServiceHookLoginSetDefaultRedirectTo("https://www.ory.sh/not-kratos")
testhelpers.SelfServiceHookLoginSetDefaultRedirectToStrategy(strategy, "https://www.ory.sh/kratos")

res, _ := makeRequestPost(t, newServer(t), false, url.Values{})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/kratos/", res.Request.URL.String())
})
Expand All @@ -123,25 +123,39 @@ func TestLoginExecutor(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)
viperSetPost(strategy, []configuration.SelfServiceHook{{Name: "err", Config: []byte(`{}`)}})

res, _ := makeRequestPost(t, newServer(t), false, url.Values{})
res, _ := makeRequestPost(t, newServer(t, flow.TypeBrowser), false, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, "https://www.ory.sh/", res.Request.URL.String())
})

t.Run("case=send a json response for API clients", func(t *testing.T) {
t.Cleanup(testhelpers.SelfServiceHookConfigReset)

res, body := makeRequestPost(t, newServer(t), true, url.Values{})
res, body := makeRequestPost(t, newServer(t, flow.TypeAPI), true, url.Values{})
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, gjson.Get(body, "identity.id"))
assert.NotEmpty(t, gjson.Get(body, "session.identity.id"))
})
})

t.Run("method=PreLoginHook", testhelpers.TestSelfServicePreHook(
configuration.ViperKeySelfServiceLoginBeforeHooks,
testhelpers.SelfServiceMakeLoginPreHookRequest,
newServer,
))
t.Run("type=api", func(t *testing.T) {
t.Run("method=PreLoginHook", testhelpers.TestSelfServicePreHook(
configuration.ViperKeySelfServiceLoginBeforeHooks,
testhelpers.SelfServiceMakeLoginPreHookRequest,
func(t *testing.T) *httptest.Server {
return newServer(t, flow.TypeAPI)
},
))
})

t.Run("type=browser", func(t *testing.T) {
t.Run("method=PreLoginHook", testhelpers.TestSelfServicePreHook(
configuration.ViperKeySelfServiceLoginBeforeHooks,
testhelpers.SelfServiceMakeLoginPreHookRequest,
func(t *testing.T) *httptest.Server {
return newServer(t, flow.TypeBrowser)
},
))
})
})
}
}

0 comments on commit 55ba485

Please sign in to comment.