Skip to content

Commit

Permalink
Merge pull request #1 from Benehiko/status-code-tests
Browse files Browse the repository at this point in the history
Status code tests
  • Loading branch information
maurice-freitag committed Feb 14, 2022
2 parents 543d704 + b12a7d5 commit dc74151
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions selfservice/flow/login/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@ func TestFlowLifecycle(t *testing.T) {
assertion(body, true, false)
assert.Contains(t, res.Request.URL.String(), loginTS.URL)
})

t.Run("case=redirects with 303", func(t *testing.T) {
c := http.DefaultClient
// don't get the reference, instead copy the values, so we don't alter the client directly.
*c = *ts.Client()
// prevent the redirect
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
req, err := http.NewRequest("GET", ts.URL+login.RouteInitBrowserFlow, nil)
require.NoError(t, err)

res, err := c.Do(req)
require.NoError(t, err)
// here we check that the redirect status is 303
require.Equal(t, http.StatusSeeOther, res.StatusCode)
defer res.Body.Close()
})

})
t.Run("case=relative redirect when self-service login ui is a relative URL", func(t *testing.T) {
reg.Config(context.Background()).MustSet(config.ViperKeySelfServiceLoginUI, "/login-ts")
Expand Down
18 changes: 18 additions & 0 deletions selfservice/flow/logout/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,22 @@ func TestLogout(t *testing.T) {
assert.EqualValues(t, http.StatusUnauthorized, res.StatusCode)
assert.EqualValues(t, "No active session was found in this request.", gjson.GetBytes(body, "error.reason").String(), "%s", body)
})

t.Run("case=init logout through browser does 303 redirect", func(t *testing.T) {
// init the logout
hc, logoutUrl := getLogoutUrl(t)
// prevent the redirect, so we can get check the status code
hc.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
// submit the login
req, err := http.NewRequest("GET", logoutUrl, nil)
require.NoError(t, err)

res, err := hc.Do(req)
require.NoError(t, err)
// here we check that the redirect status is 303
require.Equal(t, http.StatusSeeOther, res.StatusCode)
defer res.Body.Close()
})
}

0 comments on commit dc74151

Please sign in to comment.