-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
session_issuer_test.go
75 lines (60 loc) · 2.54 KB
/
session_issuer_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
package hook_test
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/ory/viper"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/selfservice/hook"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)
func TestSessionIssuer(t *testing.T) {
_, reg := internal.NewFastRegistryWithMocks(t)
viper.Set(configuration.ViperKeyPublicBaseURL, "http://localhost/")
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/stub.schema.json")
var r http.Request
h := hook.NewSessionIssuer(reg)
t.Run("method=sign-up", func(t *testing.T) {
t.Run("flow=browser", func(t *testing.T) {
w := httptest.NewRecorder()
sid := x.NewUUID()
i := identity.NewIdentity(configuration.DefaultIdentityTraitsSchemaID)
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), i))
require.NoError(t, h.ExecutePostRegistrationPostPersistHook(w, &r,
®istration.Flow{Type: flow.TypeBrowser}, &session.Session{ID: sid, Identity: i}))
got, err := reg.SessionPersister().GetSession(context.Background(), sid)
require.NoError(t, err)
assert.Equal(t, sid, got.ID)
assert.True(t, got.AuthenticatedAt.After(time.Now().Add(-time.Minute)))
assert.Contains(t, w.Header().Get("Set-Cookie"), session.DefaultSessionCookieName)
})
t.Run("flow=api", func(t *testing.T) {
w := httptest.NewRecorder()
i := identity.NewIdentity(configuration.DefaultIdentityTraitsSchemaID)
s := &session.Session{ID: x.NewUUID(), Identity: i}
f := ®istration.Flow{Type: flow.TypeAPI}
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), i))
require.True(t, errors.Is(h.ExecutePostRegistrationPostPersistHook(w, &r, f, s), registration.ErrHookAbortFlow))
got, err := reg.SessionPersister().GetSession(context.Background(), s.ID)
require.NoError(t, err)
assert.Equal(t, s.ID, got.ID)
assert.True(t, got.AuthenticatedAt.After(time.Now().Add(-time.Minute)))
assert.Empty(t, w.Header().Get("Set-Cookie"))
body := w.Body.Bytes()
assert.Equal(t, i.ID, gjson.GetBytes(body, "identity.id").String())
assert.Equal(t, s.ID, gjson.GetBytes(body, "session.id").String())
assert.Equal(t, got.Token, gjson.GetBytes(body, "session_token").String())
})
})
}