forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expire_test.go
76 lines (59 loc) · 1.64 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
package authboss
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// These tests use the global variable nowTime so cannot be parallelized
func TestDudeIsExpired(t *testing.T) {
ab := New()
session := mockClientStore{SessionKey: "username"}
ab.refreshExpiry(session)
nowTime = func() time.Time {
return time.Now().UTC().Add(ab.ExpireAfter * 2)
}
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return session
}
r, _ := http.NewRequest("GET", "tra/la/la", nil)
w := httptest.NewRecorder()
called := false
m := ab.ExpireMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
m.ServeHTTP(w, r)
if !called {
t.Error("Expected middleware to call handler")
}
if key, ok := session.Get(SessionKey); ok {
t.Error("Unexpcted session key:", key)
}
if key, ok := session.Get(SessionLastAction); ok {
t.Error("Unexpcted last action key:", key)
}
}
func TestDudeIsNotExpired(t *testing.T) {
ab := New()
session := mockClientStore{SessionKey: "username"}
ab.refreshExpiry(session)
nowTime = func() time.Time {
return time.Now().UTC().Add(ab.ExpireAfter / 2)
}
ab.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return session
}
r, _ := http.NewRequest("GET", "tra/la/la", nil)
w := httptest.NewRecorder()
called := false
m := ab.ExpireMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
m.ServeHTTP(w, r)
if !called {
t.Error("Expected middleware to call handler")
}
if key, ok := session.Get(SessionKey); !ok {
t.Error("Expected session key:", key)
}
}