-
Notifications
You must be signed in to change notification settings - Fork 3
/
session_test.go
133 lines (106 loc) · 2.77 KB
/
session_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
package carrot
import (
"crypto/rand"
"encoding/base64"
"testing"
"time"
//"fmt"
)
func TestRefreshExpiryTime(t *testing.T) {
refreshExpiryTime()
}
func TestDefaultSessionManagerNewSessionGet(t *testing.T) {
store := NewDefaultSessionManager()
token, _, err := store.NewSession()
if err != nil {
t.Errorf("Failed to create session")
}
_, err = store.Get(token)
if err != nil {
t.Errorf("Failed to get session %v", err)
}
}
func TestContextPersistence(t *testing.T) {
store := NewDefaultSessionManager()
token, _, err := store.NewSession()
if err != nil {
t.Errorf("Failed to create session")
}
ctx, _ := store.Get(token)
if ctx == nil {
t.Error("Session was not received")
}
}
func TestSessionDelete(t *testing.T) {
store := NewDefaultSessionManager()
token, _, err := store.NewSession()
if err != nil {
t.Errorf("Failed to create session")
}
beforeLength := store.Length()
store.Delete(token)
afterLength := store.Length()
if afterLength != beforeLength-1 {
t.Errorf("Failed to delete session \n Before: %v \n After: %v", beforeLength, afterLength-1)
}
}
func TestSessionExists(t *testing.T) {
store := NewDefaultSessionManager()
token, _, err := store.NewSession()
if err != nil {
t.Errorf("Failed to create session")
}
exists := store.Exists(token)
if exists != true {
t.Error("context does not exist when it should")
}
b := make([]byte, 64)
_, err = rand.Read(b)
if err != nil {
t.Errorf("Could not generate random string")
}
stringToken := base64.URLEncoding.EncodeToString(b)
badToken := SessionToken(stringToken)
exists = store.Exists(badToken)
if exists == true {
t.Error("context exists when it should not")
}
}
func TestSessionExpired(t *testing.T) {
store := NewDefaultSessionManager()
token, _, err := store.NewSession()
if err != nil {
t.Errorf("Failed to create session")
}
ctx, _ := store.Get(token)
expireTime := time.Now().Add(time.Second)
ctx.expireTime = expireTime
time.Sleep(time.Second)
if !ctx.sessionDurationExpired() {
t.Errorf("Session did not expire after period of disconnection")
}
}
//TODO: make sure to test completely empty session store
func TestPrimaryDeviceAssignmentAndRetrieval(t *testing.T) {
store := NewDefaultSessionManager()
_, session, err := store.NewSession()
if err != nil {
t.Error(err)
}
if session.isPrimaryDevice() {
t.Errorf("The session should not be marked as a primary device")
}
//TODO: make the following method call test error (empty session store with no primary device)
_, err = store.GetPrimaryDeviceToken()
if err != nil {
t.Error(err)
}
session.primaryDevice = true
if !session.isPrimaryDevice() {
t.Errorf("The session should have been marked as a primary device")
}
_, err = store.GetPrimaryDeviceToken()
if err != nil {
t.Error(err)
}
}