-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
flow_test.go
88 lines (77 loc) · 2.19 KB
/
flow_test.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
package login_test
import (
"crypto/tls"
"net/http"
"testing"
"time"
"github.com/bxcodec/faker/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/x/urlx"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/x"
)
func TestFakeFlow(t *testing.T) {
var r login.Flow
require.NoError(t, faker.FakeData(&r))
assert.NotEmpty(t, r.ID)
assert.NotEmpty(t, r.IssuedAt)
assert.NotEmpty(t, r.ExpiresAt)
assert.NotEmpty(t, r.RequestURL)
assert.NotEmpty(t, r.Active)
assert.NotEmpty(t, r.Methods)
for _, m := range r.Methods {
assert.NotEmpty(t, m.Method)
assert.NotEmpty(t, m.Config)
}
}
func TestNewFlow(t *testing.T) {
t.Run("case=0", func(t *testing.T) {
r := login.NewFlow(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh", TLS: &tls.ConnectionState{},
}, flow.TypeBrowser)
assert.EqualValues(t, r.IssuedAt, r.ExpiresAt)
assert.Equal(t, flow.TypeBrowser, r.Type)
assert.False(t, r.Forced)
assert.Equal(t, "https://ory.sh/", r.RequestURL)
})
t.Run("case=1", func(t *testing.T) {
r := login.NewFlow(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/?refresh=true"),
Host: "ory.sh"}, flow.TypeAPI)
assert.Equal(t, r.IssuedAt, r.ExpiresAt)
assert.Equal(t, flow.TypeAPI, r.Type)
assert.True(t, r.Forced)
assert.Equal(t, "http://ory.sh/?refresh=true", r.RequestURL)
})
t.Run("case=2", func(t *testing.T) {
r := login.NewFlow(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("https://ory.sh/"),
Host: "ory.sh"}, flow.TypeBrowser)
assert.Equal(t, "http://ory.sh/", r.RequestURL)
})
}
func TestFlow(t *testing.T) {
r := &login.Flow{ID: x.NewUUID()}
assert.Equal(t, r.ID, r.GetID())
t.Run("case=expired", func(t *testing.T) {
for _, tc := range []struct {
r *login.Flow
valid bool
}{
{
r: &login.Flow{ExpiresAt: time.Now().Add(time.Hour), IssuedAt: time.Now().Add(-time.Minute)},
valid: true,
},
{r: &login.Flow{ExpiresAt: time.Now().Add(-time.Hour), IssuedAt: time.Now().Add(-time.Minute)}},
} {
if tc.valid {
require.NoError(t, tc.r.Valid())
} else {
require.Error(t, tc.r.Valid())
}
}
})
}