-
Notifications
You must be signed in to change notification settings - Fork 2
/
oidc_test.go
118 lines (103 loc) · 2.83 KB
/
oidc_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
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
package main
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/lstoll/oidc/core"
)
// versionedSession is a core.Session used for testing our SessionManager
// implementation. Adapted from github.com/lstoll/oidc/core.
type versionedSession struct {
Version string `json:"version"`
Session json.RawMessage `json:"session"`
sess *sessionV2
}
func (v *versionedSession) ID() string {
return v.sess.ID
}
func (v *versionedSession) Expiry() time.Time {
return v.sess.Expiry
}
type sessionV2 struct {
ID string `json:"id,omitempty"`
Expiry time.Time `json:"expiry,omitempty"`
}
func TestOIDCSessionManager(t *testing.T) {
t.Parallel()
sm := openTestDB(t).SessionManager()
var vsess versionedSession
ok, err := sm.GetSession(context.Background(), "ID", &vsess)
if err != nil {
t.Fatalf("GetSession: %v", err)
}
if ok {
t.Fatal("GetSession: want session not found, got ok")
}
exp := time.Now()
makeSession := func(version, id string, expiry time.Time) core.Session {
sess := &sessionV2{ID: id, Expiry: expiry}
msg, err := json.Marshal(sess)
if err != nil {
t.Fatal(err)
}
return &versionedSession{
Version: version,
Session: msg,
sess: sess,
}
}
err = sm.PutSession(context.Background(), makeSession("1", "session-1", exp))
if err != nil {
t.Fatalf("PutSession: %v", err)
}
ok, err = sm.GetSession(context.Background(), "session-1", &vsess)
if err != nil {
t.Fatalf("GetSession: %v", err)
}
if !ok {
t.Fatal("GetSession: want ok to be true, got false")
}
if vsess.Version != "1" {
t.Fatalf("GetSession: want Version 1, got: %s", vsess.Version)
}
var sess sessionV2
if err := json.Unmarshal(vsess.Session, &sess); err != nil {
t.Fatalf("GetSession: %v", err)
}
var v core.Session = &versionedSession{sess: &sess}
if want, got := "session-1", v.ID(); got != want {
t.Fatalf("GetSession: want ID() %s, got: %s", want, got)
}
if !v.Expiry().Equal(exp) {
t.Fatalf("GetSession: want Expiry() %s, got: %s", exp, v.Expiry())
}
err = sm.PutSession(context.Background(), makeSession("1", "session-1", exp.Add(time.Minute)))
if err != nil {
t.Fatalf("PutSession: %v", err)
}
ok, err = sm.GetSession(context.Background(), "session-1", &vsess)
if err != nil {
t.Fatalf("GetSession: %v", err)
}
if !ok {
t.Fatal("GetSession: want ok to be true, got false")
}
if err := json.Unmarshal(vsess.Session, &sess); err != nil {
t.Fatalf("GetSession: %v", err)
}
if !v.Expiry().Equal(exp.Add(time.Minute)) {
t.Fatalf("Expiry() not updated after PutSession")
}
err = sm.DeleteSession(context.Background(), "session-1")
if err != nil {
t.Fatalf("DeleteSession: %v", err)
}
ok, err = sm.GetSession(context.Background(), "session-1", &vsess)
if err != nil {
t.Fatalf("GetSession: %v", err)
}
if ok {
t.Fatal("GetSession returned ok after DeleteSession")
}
}