Skip to content

Commit

Permalink
test: resolve test regression issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Apr 9, 2021
1 parent dbf2668 commit ccf9fed
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 71 deletions.
2 changes: 1 addition & 1 deletion cmd/identities/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func readIdentities(cmd *cobra.Command, args []string) (map[string]string, error
}

func setup(t *testing.T, cmd *cobra.Command) driver.Registry {
conf, reg := internal.NewRegistryDefaultWithDSN(t, config.DefaultSQLiteMemoryDSN)
conf, reg := internal.NewFastRegistryWithMocks(t)
_, admin := testhelpers.NewKratosServerWithCSRF(t, reg)
conf.MustSet(config.ViperKeyDefaultIdentitySchemaURL, "file://./stubs/identity.schema.json")
// setup command
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/bxcodec/faker/v3 v3.3.1
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/davecgh/go-spew v1.1.1
github.com/davidrjonas/semver-cli v0.0.0-20190116233701-ee19a9a0dda6
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fatih/color v1.9.0
Expand Down
11 changes: 4 additions & 7 deletions selfservice/flow/login/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package login

import (
"context"
"github.com/ory/x/assertx"
"testing"

"github.com/ory/kratos/ui/container"
Expand Down Expand Up @@ -43,10 +44,6 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)
var r Flow
require.NoError(t, faker.FakeData(&r))
clearids(&r)

nodes := len(r.UI.Nodes)
assert.NotZero(t, nodes)

return &r
}

Expand Down Expand Up @@ -76,7 +73,7 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)
x.AssertEqualTime(t, expected.ExpiresAt, actual.ExpiresAt)
assert.EqualValues(t, expected.RequestURL, actual.RequestURL)
assert.EqualValues(t, expected.Active, actual.Active)
require.Equal(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
})

t.Run("case=should properly set the flow type", func(t *testing.T) {
Expand All @@ -102,7 +99,7 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)
require.NoError(t, err)
assert.Equal(t, flow.TypeBrowser, actual.Type)
assert.True(t, actual.Forced)
assert.Equal(t, "not.ory-sh", actual.UI.Action)
assert.Equal(t, "not-ory-sh", actual.UI.Action)
})

t.Run("case=should not cause data loss when updating a request without changes", func(t *testing.T) {
Expand All @@ -117,7 +114,7 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)

actual, err = p.GetLoginFlow(ctx, expected.ID)
require.NoError(t, err)
assert.EqualValues(t, expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI)
})
}
}
54 changes: 36 additions & 18 deletions selfservice/flow/recovery/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package recovery

import (
"context"
"github.com/ory/kratos/ui/node"
"github.com/ory/x/assertx"
"testing"

"github.com/ory/kratos/selfservice/flow"

"github.com/ory/kratos/ui/container"

"github.com/bxcodec/faker/v3"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -77,30 +75,50 @@ func TestFlowPersister(ctx context.Context, conf *config.Config, p interface {
x.AssertEqualTime(t, expected.IssuedAt, actual.IssuedAt)
x.AssertEqualTime(t, expected.ExpiresAt, actual.ExpiresAt)
assert.EqualValues(t, expected.RequestURL, actual.RequestURL)
require.Equal(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
})

t.Run("case=should create and update a recovery request", func(t *testing.T) {
expected := newFlow(t)
expected.Type = flow.TypeAPI
expected.UI = container.New("ory-sh")
expected.UI.Nodes = node.Nodes{}
expected.UI.Nodes.Append(node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

err := p.CreateRecoveryFlow(ctx, expected)
require.NoError(t, err)
expected.UI.Nodes.Append(node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

actual, err := p.GetRecoveryFlow(ctx, expected.ID)
err := p.CreateRecoveryFlow(ctx, expected)
require.NoError(t, err)
assert.Equal(t, flow.TypeAPI, actual.Type)

actual.UI = container.New("not-ory-sh")
actual.Type = flow.TypeBrowser
expected.UI.Action = "/new-action"
expected.UI.Nodes.Append(
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})))

require.NoError(t, p.UpdateRecoveryFlow(ctx, actual))
expected.RequestURL = "/new-request-url"
require.NoError(t, p.UpdateRecoveryFlow(ctx, expected))

actual, err = p.GetRecoveryFlow(ctx, actual.ID)
actual, err := p.GetRecoveryFlow(ctx, expected.ID)
require.NoError(t, err)
assert.Equal(t, flow.TypeBrowser, actual.Type)
assert.Equal(t, "not.ory-sh", actual.UI.Action)

assert.Equal(t, "/new-action", actual.UI.Action)
assert.Equal(t, "/new-request-url", actual.RequestURL)
assertx.EqualAsJSON(t, node.Nodes{
// v0.5: {Name: "zab", Type: "zab", Pattern: "zab"},
node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
// v0.5: {Name: "zab", Type: "bar", Pattern: "baz"},
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})),
}, actual.UI.Nodes)
})

t.Run("case=should not cause data loss when updating a request without changes", func(t *testing.T) {
Expand All @@ -115,7 +133,7 @@ func TestFlowPersister(ctx context.Context, conf *config.Config, p interface {

actual, err = p.GetRecoveryFlow(ctx, expected.ID)
require.NoError(t, err)
assert.EqualValues(t, expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI)
})
}
}
55 changes: 35 additions & 20 deletions selfservice/flow/registration/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registration

import (
"context"
"github.com/ory/kratos/ui/node"
"testing"

"github.com/ory/x/assertx"
Expand Down Expand Up @@ -39,10 +40,6 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)
var r Flow
require.NoError(t, faker.FakeData(&r))
clearids(&r)

nodes := len(r.UI.Nodes)
assert.NotZero(t, nodes)

return &r
}

Expand Down Expand Up @@ -73,32 +70,50 @@ func TestFlowPersister(ctx context.Context, p FlowPersister) func(t *testing.T)
x.AssertEqualTime(t, expected.ExpiresAt, actual.ExpiresAt)
assert.EqualValues(t, expected.RequestURL, actual.RequestURL)
assert.EqualValues(t, expected.Active, actual.Active)
require.Equal(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
})

t.Run("case=should not cause data loss when updating a request without changes", func(t *testing.T) {
expected := newFlow(t)
expected.Active = ""
expected.UI.Nodes = node.Nodes{}
expected.UI.Nodes.Append(node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

expected.UI.Nodes.Append(node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

err := p.CreateRegistrationFlow(ctx, expected)
require.NoError(t, err)

actual, err := p.GetRegistrationFlow(ctx, expected.ID)
require.NoError(t, err)
require.Len(t, actual.UI.Nodes, 2)
assertx.EqualAsJSON(t,
expected.UI,
actual.UI,
)
expected.UI.Action = "/new-action"
expected.UI.Nodes.Append(
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})))

require.NoError(t, p.UpdateRegistrationFlow(ctx, actual))
expected.RequestURL = "/new-request-url"
require.NoError(t, p.UpdateRegistrationFlow(ctx, expected))

actual, err = p.GetRegistrationFlow(ctx, expected.ID)
actual, err := p.GetRegistrationFlow(ctx, expected.ID)
require.NoError(t, err)
require.Len(t, actual.UI.Nodes, 2)
assertx.EqualAsJSON(t,
expected.UI,
actual.UI,
)

assert.Equal(t, "/new-action", actual.UI.Action)
assert.Equal(t, "/new-request-url", actual.RequestURL)
assertx.EqualAsJSON(t, node.Nodes{
// v0.5: {Name: "zab", Type: "zab", Pattern: "zab"},
node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
// v0.5: {Name: "zab", Type: "bar", Pattern: "baz"},
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})),
}, actual.UI.Nodes)
})
}
}
1 change: 0 additions & 1 deletion selfservice/flow/settings/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestFakeFlow(t *testing.T) {
assert.NotEmpty(t, r.RequestURL)
assert.NotEmpty(t, r.Active)
assert.NotEmpty(t, r.UI)
assert.NotEmpty(t, r.UI.Nodes)
}

func TestNewFlow(t *testing.T) {
Expand Down
15 changes: 10 additions & 5 deletions selfservice/flow/settings/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package settings
import (
"context"
"encoding/json"
"github.com/ory/x/assertx"
"testing"

"github.com/ory/kratos/ui/node"
Expand Down Expand Up @@ -103,6 +104,7 @@ func TestRequestPersister(ctx context.Context, conf *config.Config, p interface

t.Run("case=should create and update a settings request", func(t *testing.T) {
expected := newFlow(t)
expected.UI.Nodes = node.Nodes{}
expected.UI.Nodes.Append(node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))
Expand All @@ -128,15 +130,18 @@ func TestRequestPersister(ctx context.Context, conf *config.Config, p interface

assert.Equal(t, "/new-action", actual.UI.Action)
assert.Equal(t, "/new-request-url", actual.RequestURL)
assert.EqualValues(t, node.Nodes{
assertx.EqualAsJSON(t, node.Nodes{
// v0.5: {Name: "zab", Type: "zab", Pattern: "zab"},
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})),
// v0.5: {Name: "zab", Type: "bar", Pattern: "baz"},
node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
// v0.5: {Name: "zab", Type: "bar", Pattern: "baz"},
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})),
}, actual.UI.Nodes)
})
}
Expand Down
53 changes: 35 additions & 18 deletions selfservice/flow/verification/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package verification

import (
"context"
"github.com/ory/kratos/ui/node"
"github.com/ory/x/assertx"
"testing"

"github.com/ory/kratos/selfservice/flow"

"github.com/ory/kratos/ui/container"

"github.com/bxcodec/faker/v3"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -50,7 +48,6 @@ func TestFlowPersister(ctx context.Context, conf *config.Config, p interface {
var r Flow
require.NoError(t, faker.FakeData(&r))
clearids(&r)
require.Len(t, r.UI, 1)
return &r
}

Expand Down Expand Up @@ -78,30 +75,50 @@ func TestFlowPersister(ctx context.Context, conf *config.Config, p interface {
x.AssertEqualTime(t, expected.IssuedAt, actual.IssuedAt)
x.AssertEqualTime(t, expected.ExpiresAt, actual.ExpiresAt)
assert.EqualValues(t, expected.RequestURL, actual.RequestURL)
require.Equal(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
assertx.EqualAsJSON(t, expected.UI, actual.UI, "expected:\t%s\nactual:\t%s", expected.UI, actual.UI)
})

t.Run("case=should create and update a verification request", func(t *testing.T) {
expected := newFlow(t)
expected.Type = flow.TypeAPI
expected.UI = container.New("ory-sh")
expected.UI.Nodes = node.Nodes{}
expected.UI.Nodes.Append(node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

err := p.CreateVerificationFlow(ctx, expected)
require.NoError(t, err)
expected.UI.Nodes.Append(node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})))

actual, err := p.GetVerificationFlow(ctx, expected.ID)
err := p.CreateVerificationFlow(ctx, expected)
require.NoError(t, err)
assert.Equal(t, flow.TypeAPI, actual.Type)

actual.UI = container.New("not-ory-sh")
actual.Type = flow.TypeBrowser
expected.UI.Action = "/new-action"
expected.UI.Nodes.Append(
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})))

require.NoError(t, p.UpdateVerificationFlow(ctx, actual))
expected.RequestURL = "/new-request-url"
require.NoError(t, p.UpdateVerificationFlow(ctx, expected))

actual, err = p.GetVerificationFlow(ctx, actual.ID)
actual, err := p.GetVerificationFlow(ctx, expected.ID)
require.NoError(t, err)
assert.Equal(t, flow.TypeBrowser, actual.Type)
assert.Equal(t, "not.ory-sh", actual.UI.Action)

assert.Equal(t, "/new-action", actual.UI.Action)
assert.Equal(t, "/new-request-url", actual.RequestURL)
assertx.EqualAsJSON(t, node.Nodes{
// v0.5: {Name: "zab", Type: "zab", Pattern: "zab"},
node.NewInputField("zab", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
node.NewInputField("foo", nil, node.DefaultGroup, "bar", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "baz"
})),
// v0.5: {Name: "zab", Type: "bar", Pattern: "baz"},
node.NewInputField("zab", nil, node.DefaultGroup, "zab", node.WithInputAttributes(func(a *node.InputAttributes) {
a.Pattern = "zab"
})),
}, actual.UI.Nodes)
})

t.Run("case=should not cause data loss when updating a request without changes", func(t *testing.T) {
Expand Down

0 comments on commit ccf9fed

Please sign in to comment.