-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
persistence.go
199 lines (166 loc) · 7.06 KB
/
persistence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package login
import (
"context"
"testing"
"github.com/bxcodec/faker/v3"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/form"
"github.com/ory/kratos/x"
)
type (
FlowPersister interface {
UpdateLoginFlow(context.Context, *Flow) error
CreateLoginFlow(context.Context, *Flow) error
GetLoginFlow(context.Context, uuid.UUID) (*Flow, error)
UpdateLoginFlowMethod(context.Context, uuid.UUID, identity.CredentialsType, *FlowMethod) error
ForceLoginFlow(ctx context.Context, id uuid.UUID) error
}
FlowPersistenceProvider interface {
LoginFlowPersister() FlowPersister
}
)
func TestFlowPersister(p FlowPersister) func(t *testing.T) {
var clearids = func(r *Flow) {
r.ID = uuid.UUID{}
for k := range r.Methods {
r.Methods[k].ID = uuid.UUID{}
}
}
return func(t *testing.T) {
t.Run("case=should error when the login flow does not exist", func(t *testing.T) {
_, err := p.GetLoginFlow(context.Background(), x.NewUUID())
require.Error(t, err)
})
var newFlow = func(t *testing.T) *Flow {
var r Flow
require.NoError(t, faker.FakeData(&r))
clearids(&r)
methods := len(r.Methods)
assert.NotZero(t, methods)
return &r
}
t.Run("case=should create with set ids", func(t *testing.T) {
var r Flow
require.NoError(t, faker.FakeData(&r))
require.NoError(t, p.CreateLoginFlow(context.Background(), &r))
})
t.Run("case=should create a new login flow and properly set IDs", func(t *testing.T) {
r := newFlow(t)
methods := len(r.Methods)
err := p.CreateLoginFlow(context.Background(), r)
require.NoError(t, err, "%#v", err)
assert.Nil(t, r.MethodsRaw)
assert.NotEqual(t, uuid.Nil, r.ID)
for _, m := range r.Methods {
assert.NotEqual(t, uuid.Nil, m.ID)
}
assert.Len(t, r.Methods, methods)
})
t.Run("case=should create and fetch a login flow", func(t *testing.T) {
expected := newFlow(t)
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
actual, err := p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
assert.Empty(t, actual.MethodsRaw)
assert.EqualValues(t, expected.ID, actual.ID)
x.AssertEqualTime(t, expected.IssuedAt, actual.IssuedAt)
x.AssertEqualTime(t, expected.ExpiresAt, actual.ExpiresAt)
assert.EqualValues(t, expected.RequestURL, actual.RequestURL)
assert.EqualValues(t, expected.Active, actual.Active)
require.Equal(t, len(expected.Methods), len(actual.Methods), "expected:\t%s\nactual:\t%s", expected.Methods, actual.Methods)
})
t.Run("case=should properly set the flow type", func(t *testing.T) {
expected := newFlow(t)
expected.Forced = true
expected.Type = flow.TypeAPI
expected.Methods = map[identity.CredentialsType]*FlowMethod{
identity.CredentialsTypeOIDC: {
Method: identity.CredentialsTypeOIDC,
Config: &FlowMethodConfig{FlowMethodConfigurator: form.NewHTMLForm(string(identity.CredentialsTypeOIDC))},
},
identity.CredentialsTypePassword: {
Method: identity.CredentialsTypePassword,
Config: &FlowMethodConfig{FlowMethodConfigurator: form.NewHTMLForm(string(identity.CredentialsTypePassword))},
},
}
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
actual, err := p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
assert.Equal(t, flow.TypeAPI, actual.Type)
actual.Methods = map[identity.CredentialsType]*FlowMethod{identity.CredentialsTypeOIDC: {
Method: identity.CredentialsTypeOIDC,
Config: &FlowMethodConfig{FlowMethodConfigurator: form.NewHTMLForm("ory-sh")},
}}
actual.Type = flow.TypeBrowser
actual.Forced = true
require.NoError(t, p.UpdateLoginFlow(context.Background(), actual))
actual, err = p.GetLoginFlow(context.Background(), actual.ID)
require.NoError(t, err)
assert.Equal(t, flow.TypeBrowser, actual.Type)
assert.True(t, actual.Forced)
require.Len(t, actual.Methods, 1)
assert.Equal(t, "ory-sh",
actual.Methods[identity.CredentialsTypeOIDC].Config.
FlowMethodConfigurator.(*form.HTMLForm).Action)
})
t.Run("case=should properly update a flow", func(t *testing.T) {
expected := newFlow(t)
expected.Type = flow.TypeAPI
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
actual, err := p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
assert.Equal(t, flow.TypeAPI, actual.Type)
})
t.Run("case=should update a login flow", func(t *testing.T) {
expected := newFlow(t)
delete(expected.Methods, identity.CredentialsTypeOIDC)
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
actual, err := p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
assert.Len(t, actual.Methods, 1)
require.NoError(t, p.UpdateLoginFlowMethod(context.Background(), expected.ID, identity.CredentialsTypeOIDC, &FlowMethod{
Method: identity.CredentialsTypeOIDC,
Config: &FlowMethodConfig{FlowMethodConfigurator: form.NewHTMLForm(string(identity.CredentialsTypeOIDC))},
}))
require.NoError(t, p.UpdateLoginFlowMethod(context.Background(), expected.ID, identity.CredentialsTypePassword, &FlowMethod{
Method: identity.CredentialsTypePassword,
Config: &FlowMethodConfig{FlowMethodConfigurator: form.NewHTMLForm(string(identity.CredentialsTypePassword))},
}))
actual, err = p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
require.Len(t, actual.Methods, 2)
assert.EqualValues(t, identity.CredentialsTypePassword, actual.Active)
assert.Equal(t, string(identity.CredentialsTypePassword), actual.Methods[identity.CredentialsTypePassword].Config.FlowMethodConfigurator.(*form.HTMLForm).Action)
assert.Equal(t, string(identity.CredentialsTypeOIDC), actual.Methods[identity.CredentialsTypeOIDC].Config.FlowMethodConfigurator.(*form.HTMLForm).Action)
})
t.Run("case=should not cause data loss when updating a request without changes", func(t *testing.T) {
expected := newFlow(t)
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
actual, err := p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
assert.Len(t, actual.Methods, 2)
require.NoError(t, p.UpdateLoginFlow(context.Background(), actual))
actual, err = p.GetLoginFlow(context.Background(), expected.ID)
require.NoError(t, err)
require.Len(t, actual.Methods, 2)
assert.EqualValues(t, identity.CredentialsTypePassword, actual.Active)
assert.Equal(t,
expected.Methods[identity.CredentialsTypePassword].Config.FlowMethodConfigurator.(*form.HTMLForm).Action,
actual.Methods[identity.CredentialsTypePassword].Config.FlowMethodConfigurator.(*form.HTMLForm).Action,
)
assert.Equal(t,
expected.Methods[identity.CredentialsTypeOIDC].Config.FlowMethodConfigurator.(*form.HTMLForm).Action,
actual.Methods[identity.CredentialsTypeOIDC].Config.FlowMethodConfigurator.(*form.HTMLForm).Action,
)
})
}
}