-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathexpire_test.go
179 lines (147 loc) · 4.25 KB
/
expire_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
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
package expire
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/volatiletech/authboss/v3"
"github.com/volatiletech/authboss/v3/mocks"
)
func TestExpireSetup(t *testing.T) {
ab := authboss.New()
clientRW := mocks.NewClientRW()
ab.Storage.SessionState = clientRW
Setup(ab)
w := httptest.NewRecorder()
wr := ab.NewResponse(w)
handled, err := ab.Events.FireAfter(authboss.EventAuth, wr, nil)
if handled {
t.Error("it should not handle the event")
}
if err != nil {
t.Error(err)
}
wr.WriteHeader(http.StatusOK)
if _, ok := clientRW.ClientValues[authboss.SessionLastAction]; !ok {
t.Error("last action should have been set")
}
}
func TestExpireIsExpired(t *testing.T) {
ab := authboss.New()
clientRW := mocks.NewClientRW()
clientRW.ClientValues[authboss.SessionKey] = "username"
clientRW.ClientValues[authboss.SessionLastAction] = time.Now().UTC().Format(time.RFC3339)
ab.Storage.SessionState = clientRW
r := httptest.NewRequest("GET", "/", nil)
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyPID, "primaryid"))
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, struct{}{}))
w := ab.NewResponse(httptest.NewRecorder())
r, err := ab.LoadClientState(w, r)
if err != nil {
t.Error(err)
}
// No t.Parallel() - Also must be after refreshExpiry() call
nowTime = func() time.Time {
return time.Now().UTC().Add(time.Hour * 2)
}
defer func() {
nowTime = time.Now
}()
called := false
hadUser := false
m := Middleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
if r.Context().Value(authboss.CTXKeyPID) != nil {
hadUser = true
}
if r.Context().Value(authboss.CTXKeyUser) != nil {
hadUser = true
}
}))
m.ServeHTTP(w, r)
if !called {
t.Error("expected middleware to call handler")
}
if hadUser {
t.Error("expected user not to be present")
}
w.WriteHeader(200)
if _, ok := clientRW.ClientValues[authboss.SessionKey]; ok {
t.Error("this key should have been deleted\n", clientRW)
}
if _, ok := clientRW.ClientValues[authboss.SessionLastAction]; ok {
t.Error("this key should have been deleted\n", clientRW)
}
}
func TestExpireNotExpired(t *testing.T) {
ab := authboss.New()
clientRW := mocks.NewClientRW()
clientRW.ClientValues[authboss.SessionKey] = "username"
clientRW.ClientValues[authboss.SessionLastAction] = time.Now().UTC().Format(time.RFC3339)
ab.Storage.SessionState = clientRW
var err error
r := httptest.NewRequest("GET", "/", nil)
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyPID, "primaryid"))
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, struct{}{}))
w := ab.NewResponse(httptest.NewRecorder())
r, err = ab.LoadClientState(w, r)
if err != nil {
t.Error(err)
}
// No t.Parallel() - Also must be after refreshExpiry() call
newTime := time.Now().UTC().Add(ab.Modules.ExpireAfter / 2)
nowTime = func() time.Time {
return newTime
}
defer func() {
nowTime = time.Now
}()
called := false
hadUser := true
m := Middleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
if r.Context().Value(authboss.CTXKeyPID) == nil {
hadUser = false
}
if r.Context().Value(authboss.CTXKeyUser) == nil {
hadUser = false
}
}))
m.ServeHTTP(w, r)
if !called {
t.Error("expected middleware to call handler")
}
if !hadUser {
t.Error("expected user to be present")
}
want := newTime.Format(time.RFC3339)
w.WriteHeader(200)
if last, ok := clientRW.ClientValues[authboss.SessionLastAction]; !ok {
t.Error("this key should be present", clientRW)
} else if want != last {
t.Error("want:", want, "got:", last)
}
}
func TestExpireTimeToExpiry(t *testing.T) {
t.Parallel()
r := httptest.NewRequest("GET", "/", nil)
want := 5 * time.Second
dur := TimeToExpiry(r, want)
if dur != want {
t.Error("duration was wrong:", dur)
}
}
func TestExpireRefreshExpiry(t *testing.T) {
t.Parallel()
ab := authboss.New()
clientRW := mocks.NewClientRW()
ab.Storage.SessionState = clientRW
r := httptest.NewRequest("GET", "/", nil)
w := ab.NewResponse(httptest.NewRecorder())
RefreshExpiry(w, r)
w.WriteHeader(200)
if _, ok := clientRW.ClientValues[authboss.SessionLastAction]; !ok {
t.Error("this key should have been set")
}
}