Files
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

172 lines
5.3 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package registration_test
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/go-faker/faker/v4"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/pkg"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/x"
"github.com/ory/x/clock"
"github.com/ory/x/jsonx"
"github.com/ory/x/urlx"
)
func TestFakeFlow(t *testing.T) {
var r registration.Flow
require.NoError(t, faker.FakeData(&r))
assert.Equal(t, uuid.Nil, r.ID)
assert.NotEmpty(t, r.IssuedAt)
assert.NotEmpty(t, r.ExpiresAt)
assert.NotEmpty(t, r.RequestURL)
assert.NotEmpty(t, r.Active)
}
func TestNewFlow(t *testing.T) {
ctx := context.Background()
conf, reg := pkg.NewFastRegistryWithMocks(t)
t.Run("case=0", func(t *testing.T) {
r, err := registration.NewFlow(reg, &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh", TLS: &tls.ConnectionState{},
}, flow.TypeBrowser)
require.NoError(t, err)
assert.True(t, r.ExpiresAt.After(r.IssuedAt))
assert.Equal(t, flow.TypeBrowser, r.Type)
assert.Equal(t, "https://ory.sh/", r.RequestURL)
})
t.Run("type=return_to", func(t *testing.T) {
_, err := registration.NewFlow(reg, &http.Request{URL: &url.URL{Path: "/", RawQuery: "return_to=https://not-allowed/foobar"}, Host: "ory.sh"}, flow.TypeBrowser)
require.Error(t, err)
_, err = registration.NewFlow(reg, &http.Request{URL: &url.URL{Path: "/", RawQuery: "return_to=" + urlx.AppendPaths(conf.SelfPublicURL(ctx), "/self-service/login/browser").String()}, Host: "ory.sh"}, flow.TypeBrowser)
require.NoError(t, err)
})
t.Run("case=1", func(t *testing.T) {
r, err := registration.NewFlow(reg, &http.Request{
URL: urlx.ParseOrPanic("/?refresh=true"),
Host: "ory.sh",
}, flow.TypeAPI)
require.NoError(t, err)
assert.True(t, r.ExpiresAt.After(r.IssuedAt))
assert.Equal(t, flow.TypeAPI, r.Type)
assert.Equal(t, "http://ory.sh/?refresh=true", r.RequestURL)
})
t.Run("case=2", func(t *testing.T) {
r, err := registration.NewFlow(reg, &http.Request{
URL: urlx.ParseOrPanic("https://ory.sh/"),
Host: "ory.sh",
}, flow.TypeBrowser)
require.NoError(t, err)
assert.Equal(t, "https://ory.sh/", r.RequestURL)
})
t.Run("should parse login_challenge when Hydra is configured", func(t *testing.T) {
_, err := registration.NewFlow(reg, &http.Request{URL: urlx.ParseOrPanic("https://ory.sh/?login_challenge=badee1"), Host: "ory.sh"}, flow.TypeBrowser)
require.Error(t, err)
conf.MustSet(ctx, config.ViperKeyOAuth2ProviderURL, "https://hydra")
r, err := registration.NewFlow(reg, &http.Request{URL: urlx.ParseOrPanic("https://ory.sh/?login_challenge=8aadcb8fc1334186a84c4da9813356d9"), Host: "ory.sh"}, flow.TypeBrowser)
require.NoError(t, err)
assert.Equal(t, "8aadcb8fc1334186a84c4da9813356d9", string(r.OAuth2LoginChallenge))
})
}
func TestFlow(t *testing.T) {
r := &registration.Flow{ID: x.NewUUID()}
assert.Equal(t, r.ID, r.GetID())
t.Run("case=expired", func(t *testing.T) {
for _, tc := range []struct {
r *registration.Flow
valid bool
}{
{
r: &registration.Flow{ExpiresAt: time.Now().Add(time.Hour), IssuedAt: time.Now().Add(-time.Minute)},
valid: true,
},
{r: &registration.Flow{ExpiresAt: time.Now().Add(-time.Hour), IssuedAt: time.Now().Add(-time.Minute)}},
} {
if tc.valid {
require.NoError(t, tc.r.Valid(clock.New()))
} else {
require.Error(t, tc.r.Valid(clock.New()))
}
}
})
}
func TestGetType(t *testing.T) {
for _, ft := range []flow.Type{
flow.TypeAPI,
flow.TypeBrowser,
} {
t.Run(fmt.Sprintf("case=%s", ft), func(t *testing.T) {
r := &registration.Flow{Type: ft}
assert.Equal(t, ft, r.GetType())
})
}
}
func TestGetRequestURL(t *testing.T) {
expectedURL := "http://foo/bar/baz"
f := &registration.Flow{RequestURL: expectedURL}
assert.Equal(t, expectedURL, f.GetRequestURL())
}
func TestFlowEncodeJSON(t *testing.T) {
assert.EqualValues(t, "", gjson.Get(jsonx.TestMarshalJSONString(t, &registration.Flow{RequestURL: "https://foo.bar?foo=bar"}), "return_to").String())
assert.EqualValues(t, "/bar", gjson.Get(jsonx.TestMarshalJSONString(t, &registration.Flow{RequestURL: "https://foo.bar?return_to=/bar"}), "return_to").String())
assert.EqualValues(t, "/bar", gjson.Get(jsonx.TestMarshalJSONString(t, registration.Flow{RequestURL: "https://foo.bar?return_to=/bar"}), "return_to").String())
}
func TestFlowDontOverrideReturnTo(t *testing.T) {
f := &registration.Flow{ReturnTo: "/foo"}
f.SetReturnTo()
assert.Equal(t, "/foo", f.ReturnTo)
f = &registration.Flow{RequestURL: "https://foo.bar?return_to=/bar"}
f.SetReturnTo()
assert.Equal(t, "/bar", f.ReturnTo)
}
func TestFlowAccountLinkingMarker(t *testing.T) {
t.Parallel()
f := &registration.Flow{}
assert.False(t, f.IsAccountLinking())
registration.WithFlowAccountLinking()(f)
assert.True(t, f.IsAccountLinking())
// The marker must survive an internal-context round trip, because the
// flow is persisted between requests.
var copied registration.Flow
copied.SetInternalContext(f.GetInternalContext())
assert.True(t, copied.IsAccountLinking())
}