-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsessions_test.go
112 lines (106 loc) · 2.88 KB
/
sessions_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
package main
import (
"net/http"
"testing"
"time"
"github.com/shurcooL/users"
)
func TestLookUpSessionViaCookie(t *testing.T) {
defer func() {
global = state{sessions: make(map[string]session)}
}()
var (
sessionA = session{
UserSpec: users.UserSpec{ID: 1, Domain: "example.com"},
Expiry: time.Now().Add(6*24*time.Hour + time.Minute),
AccessToken: "aaa",
}
sessionB = session{
UserSpec: users.UserSpec{ID: 2, Domain: "example.com"},
Expiry: time.Now().Add(6*24*time.Hour - time.Minute),
AccessToken: "bbb",
}
)
global = state{sessions: map[string]session{
sessionA.AccessToken: sessionA,
sessionB.AccessToken: sessionB,
}}
tests := []struct {
in *http.Request
wantSession *session
wantExtended bool
wantError error
}{
{
in: &http.Request{},
wantSession: nil,
wantExtended: false,
},
{
in: &http.Request{
Header: http.Header{
"Cookie": []string{"accessToken=YWFh"}, // Base64-encoded "aaa".
},
},
wantSession: &sessionA,
wantExtended: false,
},
{
in: &http.Request{
Header: http.Header{
"Cookie": []string{"accessToken=YmJi"}, // Base64-encoded "bbb".
},
},
wantSession: &session{
UserSpec: users.UserSpec{ID: 2, Domain: "example.com"},
Expiry: time.Now().Add(7 * 24 * time.Hour), // Extended expiry.
AccessToken: "bbb",
},
wantExtended: true,
},
{
in: &http.Request{
Header: http.Header{
"Cookie": []string{"accessToken=eA"}, // Base64-encoded "x".
},
},
wantError: errBadAccessToken,
},
{
in: &http.Request{
Header: http.Header{
"Cookie": []string{"accessToken=x"}, // Invalid base64 encoding.
},
},
wantError: errBadAccessToken,
},
}
for _, tc := range tests {
u, extended, err := lookUpSessionViaCookie(tc.in)
if got, want := err, tc.wantError; !equalError(got, want) {
t.Fatalf("got error: %v, want: %v", got, want)
}
if tc.wantError != nil {
continue
}
if got, want := u, tc.wantSession; !equalSession(got, want) {
t.Errorf("got session: %v, want: %v", got, want)
}
if got, want := extended, tc.wantExtended; got != want {
t.Errorf("got extended: %v, want: %v", got, want)
}
}
}
// equalSession reports whether sessions a and b are considered equal.
// They're equal if both are nil, or both are not nil and have equal fields.
func equalSession(a, b *session) bool {
return a == nil && b == nil || a != nil && b != nil &&
a.UserSpec == b.UserSpec &&
-time.Second < a.Expiry.Sub(b.Expiry) && a.Expiry.Sub(b.Expiry) < time.Second && // Expiry times within a second.
a.AccessToken == b.AccessToken
}
// equalError reports whether errors a and b are considered equal.
// They're equal if both are nil, or both are not nil and a.Error() == b.Error().
func equalError(a, b error) bool {
return a == nil && b == nil || a != nil && b != nil && a.Error() == b.Error()
}