-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
104 lines (94 loc) · 2.83 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
var test = require('tap').test
var Jlesm = require('./')
var level = require('level-mem')
function noop() {}
function createFakeCore(t) {
return { //sid gets bound
isAuthenticated: function(sId, sessionId) {
t.equal(sId, sessionId, "session ID is correct")
},
beginAuthentication: function(sId, sessionId, addr) {
t.equal(sId, sessionId, "session ID matches")
t.equal(typeof addr, "string", "the contact address is a string :)")
},
unauthenticate: function(sId, sessionId) {
t.equal(sId, sessionId, "session ID is correct")
}
}
}
test('test for new session', function(t) {
t.plan(6)
var database = level('hello this is a test')
var sessionId = "the session"
var fakeCore = createFakeCore(t, sessionId)
var jlesm = new Jlesm(fakeCore, database)
jlesm.createSession(function(err, api, sid) {
t.notOk(err, (err && err.message) || 'no error')
t.ok(api, 'api came back')
if (api) {
api.isAuthenticated(sid)
api.beginAuthentication(sid, "I am a string")
api.unauthenticate(sid)
}
t.end()
})
})
test('test for continued sesssion giving error on bad session', function(t) {
t.plan(2)
var database = level('hello this is a test')
var fakeCore = createFakeCore(t)
var jlesm = new Jlesm(fakeCore, database)
jlesm.continueSession("this is not correct", function(err, api, sid) {
t.equal((err && err.message), 'Invalid Session Id', 'correct error')
t.notOk(api)
t.end()
})
})
test('test for continued sesssion', function(t) {
t.plan(6)
var database = level('hello this is a test')
var sessionId = "the session"
database.put(sessionId, true)
var fakeCore = createFakeCore(t, sessionId)
var jlesm = new Jlesm(fakeCore, database)
jlesm.continueSession(sessionId, function(err, api, sid) {
t.notOk(err, (err && err.message) || 'no error')
t.ok(api, 'api came back')
if (api) {
api.isAuthenticated(sid)
api.beginAuthentication(sid, "I am a string")
api.unauthenticate(sid)
}
t.end()
})
})
test('session expiration', function(t) {
t.plan(4)
var database = level('hello this is a test')
var sessionId = "the session"
var opts = {
timeoutMs: 100,
checkIntervalMs: 10
}
var fakeCore = {
isAuthenticated: noop,
beginAuthentication: noop,
unauthenticate: noop
}
var jlesm = new Jlesm(fakeCore, database, opts)
jlesm.createSession(function (err, api, sid1) {
t.notOk(err, (err && err.message) || 'successful session creation')
setTimeout(function () {
jlesm.continueSession(sid1, function (err, api, sid2) {
t.notOk(err, (err && err.message) || 'successful session continuation')
setTimeout(function () {
jlesm.continueSession(sid1, function (err, api, sid3) {
t.ok(err, 'unsuccessful session continuation...')
t.equal((err && err.message), 'Invalid Session Id', '... due to the non-existent session id (it expired)')
t.end()
})
}, 120)
})
}, 80)
})
})