Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Aug 10, 2023
1 parent 7d6f143 commit 3390fc2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions selfservice/strategy/oidc/strategy_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func createClient(t *testing.T, remote string, redir string) (id, secret string)
}
defer res.Body.Close()

body := ioutilx.MustReadAll(res.Body)
if http.StatusCreated != res.StatusCode {
return errors.Errorf("got status code: %d", res.StatusCode)
return errors.Errorf("got status code: %d\n%s", res.StatusCode, body)
}

body := ioutilx.MustReadAll(res.Body)
id = gjson.GetBytes(body, "client_id").String()
secret = gjson.GetBytes(body, "client_secret").String()
return nil
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/strategy_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *Strategy) processLogin(w http.ResponseWriter, r *http.Request, loginFlo
}

if loginFlow.OAuth2LoginChallenge.String() != "" {
opts = append(opts, registration.WithFlowOAuth2LoginChallenge(loginFlow.ReturnTo))
opts = append(opts, registration.WithFlowOAuth2LoginChallenge(loginFlow.OAuth2LoginChallenge.String()))
}

registrationFlow, err := s.d.RegistrationHandler().NewRegistrationFlow(w, r, loginFlow.Type, opts...)
Expand Down
29 changes: 23 additions & 6 deletions selfservice/strategy/oidc/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"
"time"

"github.com/ory/kratos/hydra"
"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/kratos/session"
"github.com/ory/x/snapshotx"
Expand Down Expand Up @@ -241,12 +242,12 @@ func TestStrategy(t *testing.T) {
assert.Equal(t, claims.metadataPublic.picture, gjson.GetBytes(body, "identity.metadata_public.picture").String(), "%s", body)
}

var newLoginFlow = func(t *testing.T, redirectTo string, exp time.Duration, flowType flow.Type) (req *login.Flow) {
var newLoginFlow = func(t *testing.T, requestURL string, exp time.Duration, flowType flow.Type) (req *login.Flow) {
// Use NewLoginFlow to instantiate the request but change the things we need to control a copy of it.
req, _, err := reg.LoginHandler().NewLoginFlow(httptest.NewRecorder(),
&http.Request{URL: urlx.ParseOrPanic(redirectTo)}, flowType)
&http.Request{URL: urlx.ParseOrPanic(requestURL)}, flowType)
require.NoError(t, err)
req.RequestURL = redirectTo
req.RequestURL = requestURL
req.ExpiresAt = time.Now().Add(exp)
require.NoError(t, reg.LoginFlowPersister().UpdateLoginFlow(context.Background(), req))

Expand Down Expand Up @@ -552,17 +553,33 @@ func TestStrategy(t *testing.T) {
})

t.Run("case=login without registered account with return_to", func(t *testing.T) {
subject = "[email protected]"
scope = []string{"openid"}
returnTo := "/foo"

t.Run("case=should pass login", func(t *testing.T) {
subject = "[email protected]"
scope = []string{"openid"}
returnTo := "/foo"
r := newBrowserLoginFlow(t, fmt.Sprintf("%s?return_to=%s", returnTS.URL, returnTo), time.Minute)
action := assertFormValues(t, r.ID, "valid")
res, body := makeRequest(t, "valid", action, url.Values{})
assert.True(t, strings.HasSuffix(res.Request.URL.String(), returnTo))
assertIdentity(t, res, body)
})

t.Run("case=should pass login and carry over login_challenge to registration", func(t *testing.T) {
subject = "[email protected]"
scope = []string{"openid"}
conf.MustSet(ctx, config.ViperKeyOAuth2ProviderURL, "http://fake-hydra")

reg.WithHydra(hydra.NewFake())
r := newBrowserLoginFlow(t, fmt.Sprintf("%s?login_challenge=%s", returnTS.URL, hydra.FakeValidLoginChallenge), time.Minute)
action := assertFormValues(t, r.ID, "valid")
fv := url.Values{}
fv.Set("provider", "valid")
res, err := testhelpers.NewClientWithCookieJar(t, nil, false).PostForm(action, fv)
require.NoError(t, err)
// Expect to be returned to the hydra instance, that instantiated the request
assert.Equal(t, hydra.FakePostLoginURL, res.Request.URL.String())
})
})

t.Run("case=register and register again but login", func(t *testing.T) {
Expand Down

0 comments on commit 3390fc2

Please sign in to comment.