-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_integration_test.go
304 lines (269 loc) · 8.83 KB
/
store_integration_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package sqlitestore_test
import (
"context"
"database/sql"
"net"
"testing"
"time"
sqlitestore "github.com/hyzual/sessionup-sqlitestore"
_ "github.com/mattn/go-sqlite3"
"github.com/swithek/sessionup"
)
func TestSessionByIDIntegration(t *testing.T) {
db, err := sql.Open("sqlite3", "file:database.db?mode=memory")
if err != nil {
db.Close()
t.Fatalf("could not open in-memory database: %v", err)
}
defer db.Close()
store, err := sqlitestore.New(db, "sessions", 0)
if err != nil {
t.Fatalf("could not create a new sessions table: %v", err)
}
validSession := sessionup.Session{
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour * 1),
ID: "valid",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
expiredSession := sessionup.Session{
CreatedAt: time.Now().Add(time.Hour * -2),
ExpiresAt: time.Now().Add(time.Hour * -1),
ID: "expired",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
sessions := []sessionup.Session{validSession, expiredSession}
for _, s := range sessions {
err = store.Create(context.Background(), s)
if err != nil {
t.Fatalf("could not create a session: %v", err)
}
}
retrievedSession, ok, err := store.FetchByID(context.Background(), "valid")
if err != nil {
t.Fatalf("unexpected error while fetching the session by its ID: %v", err)
}
if !ok {
t.Fatalf("expected to find session by its ID, but it was not found")
}
assertSessionEquals(t, retrievedSession, validSession)
err = store.DeleteByID(context.Background(), "valid")
if err != nil {
t.Fatalf("unexpected error while deleting the session by its ID: %v", err)
}
_, ok, err = store.FetchByID(context.Background(), "valid")
if err != nil {
t.Fatalf("unexpected error while fetching again the session by its ID: %v", err)
}
if ok {
t.Fatalf("expected to no longer find the session by its ID after deleting it, but it was found")
}
_, ok, err = store.FetchByID(context.Background(), "expired")
if err != nil {
t.Fatalf("unexpected error while fetching the expired session by its ID: %v", err)
}
if ok {
t.Fatalf("expected not to find an expired session by its ID, but it was found")
}
}
func TestSessionsByUserKeyIntegration(t *testing.T) {
db, err := sql.Open("sqlite3", "file:database.db?mode=memory")
if err != nil {
db.Close()
t.Fatalf("could not open in-memory database: %v", err)
}
defer db.Close()
store, err := sqlitestore.New(db, "sessions", 0)
if err != nil {
t.Fatalf("could not create a new sessions table: %v", err)
}
validSession := sessionup.Session{
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour * 1),
ID: "valid",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
expiredSession := sessionup.Session{
CreatedAt: time.Now().Add(time.Hour * -2),
ExpiresAt: time.Now().Add(time.Hour * -1),
ID: "expired",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
sessionToBeDeleted := sessionup.Session{
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour * 1),
ID: "to_be_deleted",
UserKey: "key",
IP: net.ParseIP("127.0.01"),
}
sessions := []sessionup.Session{validSession, expiredSession, sessionToBeDeleted}
for _, s := range sessions {
err = store.Create(context.Background(), s)
if err != nil {
t.Fatalf("could not create a session: %v", err)
}
}
actualSessions, err := store.FetchByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while fetching the sessions by user key: %v", err)
}
if actualSessions == nil {
t.Fatalf("expected to find sessions by their key, but they were not found")
}
assertSessionsContains(t, validSession, actualSessions)
assertSessionsContains(t, expiredSession, actualSessions)
assertSessionsContains(t, sessionToBeDeleted, actualSessions)
err = store.DeleteByUserKey(context.Background(), "key", "valid", "expired")
if err != nil {
t.Fatalf("unexpected error while deleting sessions with exceptions: %v", err)
}
sessionsAfterDeletion, err := store.FetchByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while fetching again the sessions by user key: %v", err)
}
assertSessionsContains(t, validSession, sessionsAfterDeletion)
assertSessionsContains(t, expiredSession, sessionsAfterDeletion)
assertSessionsDoesNotContain(t, sessionToBeDeleted, sessionsAfterDeletion)
err = store.DeleteByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while deleting all sessions by key: %v", err)
}
sessionsAfterSecondDeletion, err := store.FetchByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while fetching for the third time sessions by user key: %v", err)
}
if len(sessionsAfterSecondDeletion) > 0 {
t.Fatal("expected sessions to be empty after deletion")
}
}
func TestExpiredSessionsCleanupIntegration(t *testing.T) {
db, err := sql.Open("sqlite3", "file:database.db?mode=memory")
if err != nil {
db.Close()
t.Fatalf("could not open in-memory database: %v", err)
}
defer db.Close()
store, err := sqlitestore.New(db, "sessions", time.Millisecond*20)
if err != nil {
t.Fatalf("could not create a new sessions table: %v", err)
}
firstExpiredSession := sessionup.Session{
CreatedAt: time.Now().Add(time.Hour * -2),
ExpiresAt: time.Now().Add(time.Hour * -1),
ID: "first",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
secondExpiredSession := sessionup.Session{
CreatedAt: time.Now().Add(time.Hour * -3),
ExpiresAt: time.Now().Add(time.Hour * -2),
ID: "second",
UserKey: "key",
IP: net.ParseIP("127.0.0.1"),
}
sessions := []sessionup.Session{firstExpiredSession, secondExpiredSession}
createSessions := func() {
for _, s := range sessions {
err = store.Create(context.Background(), s)
if err != nil {
t.Fatalf("could not create a session: %v", err)
}
}
}
waitForCleanup := func() {
// wait for cleanup after 20 ms
time.Sleep(time.Millisecond * 21)
}
createSessions()
waitForCleanup()
assertErrorChannelIsEmpty(t, store.CleanupErr())
retrievedSessions, err := store.FetchByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while fetching the sessions by user key: %v", err)
}
if len(retrievedSessions) > 0 {
t.Fatal("expected sessions to be empty after cleanup")
}
createSessions()
store.StopCleanup()
waitForCleanup()
retrievedSessions, err = store.FetchByUserKey(context.Background(), "key")
if err != nil {
t.Fatalf("unexpected error while fetching the sessions by user key: %v", err)
}
if len(retrievedSessions) == 0 {
t.Fatal("expected to find expired sessions after stopping cleanup, but none were found")
}
}
func assertSessionEquals(t *testing.T, actual sessionup.Session, expected sessionup.Session) {
t.Helper()
if actual.ID != expected.ID {
t.Errorf("got ID %s, want %s", actual.ID, expected.ID)
}
if !expected.CreatedAt.Equal(actual.CreatedAt) {
t.Errorf("got CreatedAt %s, want %s", actual.CreatedAt.String(), expected.CreatedAt.String())
}
if !expected.ExpiresAt.Equal(actual.ExpiresAt) {
t.Errorf("got ExpiresAt %s, want %s", actual.CreatedAt.String(), expected.CreatedAt.String())
}
if !expected.IP.Equal(actual.IP) {
t.Errorf("got IP %s, want %s", actual.IP.String(), expected.IP.String())
}
if actual.UserKey != expected.UserKey {
t.Errorf("got UserKey %s, want %s", actual.UserKey, expected.UserKey)
}
if actual.Agent.OS != expected.Agent.OS {
t.Errorf("got Agent.OS %s, want %s", actual.Agent.OS, expected.Agent.OS)
}
if actual.Agent.Browser != expected.Agent.Browser {
t.Errorf("got Agent.Browser %s, want %s", actual.Agent.Browser, expected.Agent.Browser)
}
}
func sessionEquals(actual, expected sessionup.Session) bool {
if !expected.CreatedAt.Equal(actual.CreatedAt) {
return false
}
if !expected.ExpiresAt.Equal(actual.ExpiresAt) {
return false
}
if !expected.IP.Equal(actual.IP) {
return false
}
return actual.ID == expected.ID &&
actual.UserKey == expected.UserKey &&
actual.Agent.OS == expected.Agent.OS &&
actual.Agent.Browser == expected.Agent.Browser
}
func assertSessionsContains(t *testing.T, needle sessionup.Session, haystack []sessionup.Session) {
t.Helper()
for _, current := range haystack {
if sessionEquals(current, needle) {
return
}
}
t.Errorf("could not find session %v among sessions %v", needle, haystack)
}
func assertSessionsDoesNotContain(t *testing.T, needle sessionup.Session, haystack []sessionup.Session) {
t.Helper()
for _, current := range haystack {
if sessionEquals(current, needle) {
t.Errorf("expected not to find session %v among retrieved sessions, but it was found", needle)
return
}
}
}
func assertErrorChannelIsEmpty(t *testing.T, errChan <-chan error) {
t.Helper()
for {
select {
case err := <-errChan:
t.Fatalf("unexpected error during cleanup of expired sessions: %v", err)
default:
return
}
}
}