-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
request_test.go
85 lines (74 loc) · 2.13 KB
/
request_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
package registration_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/registration"
"github.com/ory/kratos/x"
)
func TestFakeRequestData(t *testing.T) {
var r registration.Request
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 TestNewRequest(t *testing.T) {
t.Run("case=0", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh", TLS: &tls.ConnectionState{},
})
assert.Equal(t, r.IssuedAt, r.ExpiresAt)
// assert.Equal(t, flow.TypeBrowser, r.Type)
assert.Equal(t, "https://ory.sh/", r.RequestURL)
})
t.Run("case=1", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh"})
assert.Equal(t, r.IssuedAt, r.ExpiresAt)
// assert.Equal(t, flow.TypeBrowser, r.Type)
assert.Equal(t, "http://ory.sh/", r.RequestURL)
})
t.Run("case=2", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("https://ory.sh/"),
Host: "ory.sh"})
assert.Equal(t, "http://ory.sh/", r.RequestURL)
})
}
func TestRequest(t *testing.T) {
r := ®istration.Request{ID: x.NewUUID()}
assert.Equal(t, r.ID, r.GetID())
t.Run("case=expired", func(t *testing.T) {
for _, tc := range []struct {
r *registration.Request
valid bool
}{
{
r: ®istration.Request{ExpiresAt: time.Now().Add(time.Hour), IssuedAt: time.Now().Add(-time.Minute)},
valid: true,
},
{r: ®istration.Request{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())
}
}
})
}