-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·158 lines (110 loc) · 2.46 KB
/
test.js
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
import test from 'ava'
import MongodbProvider from './'
function sleep(ttl = 1000) {
return new Promise(resolve => {
setTimeout(resolve, ttl)
})
}
test('should create a mongodb client', t => {
const provider = new MongodbProvider({
customKey: 233
})
t.not(provider.client, undefined)
provider.quit().then(t.context.done)
})
test('should return undefined', async t => {
const provider = new MongodbProvider()
const sess = await provider.get('233')
t.is(sess, undefined)
await provider.quit()
})
test('should save a session', async t => {
const provider = new MongodbProvider()
await provider.set(
'377',
{
cookie: {}
},
2000
)
const sess = await provider.get('377')
t.deepEqual(sess, {
cookie: {}
})
await provider.quit()
})
test('should delete a session', async t => {
const provider = new MongodbProvider()
await provider.set(
'610',
{
cookie: {}
},
2000
)
let sess = await provider.get('610')
t.deepEqual(sess, {
cookie: {}
})
await provider.delete('610')
sess = await provider.get('610')
t.is(sess, undefined)
await provider.quit()
})
test('should throw error when mongodb is already closed', async t => {
const provider = new MongodbProvider()
await provider.set('987', 'trek engine', 2000)
const h0 = await provider.has('987')
t.is(h0, true)
const h1 = await provider.has('989')
t.is(h1, false)
const h2 = await provider.has('987')
t.is(h2, true)
await provider.quit()
const error = await t.throws(provider.get('987'))
t.true(/opology was destroyed/.test(error.message))
})
test('should automatically delete a session', async t => {
const provider = new MongodbProvider()
await provider.set(
'1024',
{
cookie: {}
},
1000
)
await sleep(500)
const sess = await provider.get('1024')
t.deepEqual(sess, {
cookie: {}
})
await sleep(500)
const sess2 = await provider.get('1024')
t.is(sess2, undefined)
await provider.quit()
})
test('should clear all sessions', async t => {
const provider = new MongodbProvider({
collectionName: 'sess2'
})
await provider.set(
'233',
{
cookie: {}
},
2000
)
await provider.set(
'377',
{
cookie: {}
},
2000
)
await provider.clear()
const sess0 = await provider.get('233')
const sess1 = await provider.get('377')
t.is(sess0, undefined)
t.is(sess1, undefined)
await provider.quit()
})