-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-test.js
More file actions
292 lines (237 loc) · 9.26 KB
/
session-test.js
File metadata and controls
292 lines (237 loc) · 9.26 KB
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
var keystone = require('../..');
var sinon = require('sinon');
describe('Keystone.session', function() {
describe('keystone.session.signinWithUser()', function() {
// mock args for signinWithUser(user, req, res, onSuccess)
var res = { cookie: sinon.stub() };
var onSuccess = sinon.stub();
var user;
var req;
function resetMocks() {
user = {
id: 'USERID',
password: 'PASSWORD'
};
req = {
user: null,
session: {
userId: null,
regenerate: function(callback) {
callback();
}
}
};
}
before(function() {
keystone.get('cookie secret', 'SECRET');
keystone.set('user model', 'User');
});
beforeEach(function() {
resetMocks();
sinon.spy(req.session, 'regenerate');
});
afterEach(function() {
req.session.regenerate.reset();
res.cookie.reset();
onSuccess.reset();
});
describe('with valid args, "cookie signin" on', function() {
it('should regenerate session, set user, session.userId, and res.cookie', function() {
keystone.set('cookie signin', true);
keystone.session.signinWithUser(user, req, res, onSuccess);
sinon.assert.calledOnce(req.session.regenerate);
req.user.must.equal(user);
req.session.userId.must.equal(user.id);
sinon.assert.calledOnce(res.cookie);
sinon.assert.calledWith(res.cookie, 'keystone.uid');
sinon.assert.calledOnce(onSuccess);
sinon.assert.calledWithExactly(onSuccess, user);
});
});
describe('with valid args, "cookie signin" off', function() {
it('should regenerate session, set user, session.userId', function() {
keystone.set('cookie signin', false);
keystone.session.signinWithUser(user, req, res, onSuccess);
sinon.assert.calledOnce(req.session.regenerate);
req.user.must.equal(user);
req.session.userId.must.equal(user.id);
sinon.assert.callCount(res.cookie, 0);
sinon.assert.calledOnce(onSuccess);
sinon.assert.calledWithExactly(onSuccess, user);
});
});
describe('with invalid args', function() {
it('should error when called less then 4 args', function() {
function callWithNoArgs() {
keystone.session.signinWithUser();
}
callWithNoArgs.must.throw('keystone.session.signinWithUser requires user, req and res objects, and an onSuccess callback.');
function callWithOneArg() {
keystone.session.signinWithUser(user);
}
callWithOneArg.must.throw('keystone.session.signinWithUser requires user, req and res objects, and an onSuccess callback.');
function callWithTwoArgs() {
keystone.session.signinWithUser(user, req);
}
callWithOneArg.must.throw('keystone.session.signinWithUser requires user, req and res objects, and an onSuccess callback.');
function callWithThreeArgs() {
keystone.session.signinWithUser(user, req, res);
}
callWithOneArg.must.throw('keystone.session.signinWithUser requires user, req and res objects, and an onSuccess callback.');
});
it('should error when user arg is not an object', function() {
function callWithInvalidUser() {
keystone.session.signinWithUser('user', req, res, onSuccess);
}
callWithInvalidUser.must.throw('keystone.session.signinWithUser requires user to be an object.');
});
it('should error when req arg is not an object', function() {
function callWithInvalidReq() {
keystone.session.signinWithUser(user, 'req', res, onSuccess);
}
callWithInvalidReq.must.throw('keystone.session.signinWithUser requires req to be an object.');
});
it('should error when res arg is not an object', function() {
function callWithInvalidRes() {
keystone.session.signinWithUser(user, req, 'res', onSuccess);
}
callWithInvalidRes.must.throw('keystone.session.signinWithUser requires res to be an object.');
});
it('should error when onSuccess arg is not a function', function() {
function callWithInvalidCallback() {
keystone.session.signinWithUser(user, req, res, 'onSuccess');
}
callWithInvalidCallback.must.throw('keystone.session.signinWithUser requires onSuccess to be a function.');
});
});
});
// TODO: need more test for keystone.session.signin()
describe('keystone.session.signin()', function() {
describe('case-insensitive email lookup', function() {
before(function() {
var self = this;
this.onSuccess = sinon.stub();
this.onFailure = sinon.stub();
// simulate User model
this.User = {
model: {
findOne: sinon.spy(function(query) {
self.query = query;
return this;
}),
exec: sinon.spy(function(callback) {
var email = 'test@test.com';
self.query.match = self.query.email.test(email);
if (self.query.match) {
return callback(null, self.user);
}
callback(new Error('not found'))
})
}
};
// simulate user instance
this.user = {
_: {
password: {
compare: sinon.spy(function(password, callback) {
callback(null, true);
})
}
}
};
keystone.set('user model', 'User');
sinon.stub(keystone, 'list').withArgs('User').returns(this.User);
sinon.stub(keystone.session, 'signinWithUser').callsArg(3);
});
after(function() {
keystone.list.restore();
keystone.session.signinWithUser.restore();
});
afterEach(function() {
delete this.query;
this.User.model.findOne.reset();
this.User.model.exec.reset();
this.onSuccess.reset();
this.onFailure.reset();
keystone.list.reset();
keystone.session.signinWithUser.reset();
});
it('shoud match email with mixed case', function (done) {
var lookup = { email: 'Test@Test.Com', password: 'password'};
keystone.session.signin(lookup, null, null, function(){
// make sure .findOne() is called with a regular expression
sinon.assert.calledOnce(this.User.model.findOne);
this.User.model.findOne.getCall(0).args[0].email.must.be.instanceof(RegExp);
// make sure .exec() is called after
sinon.assert.calledOnce(this.User.model.exec);
this.User.model.exec.calledAfter(this.User.model.findOne).must.be.true;
// make sure .signinWithUser() is called on successful match
sinon.assert.calledOnce(keystone.session.signinWithUser);
done();
}.bind(this), this.onFailure);
});
it('shoud match email with all uppercase', function (done) {
var lookup = { email: 'TEST@TEST.COM', password: 'password'};
keystone.session.signin(lookup, null, null, function(){
// make sure .findOne() is called with a regular expression
sinon.assert.calledOnce(this.User.model.findOne);
this.User.model.findOne.getCall(0).args[0].email.must.be.instanceof(RegExp);
// make sure .exec() is called after
sinon.assert.calledOnce(this.User.model.exec);
this.User.model.exec.calledAfter(this.User.model.findOne).must.be.true;
// make sure .signinWithUser() is called on successful match
sinon.assert.calledOnce(keystone.session.signinWithUser);
done();
}.bind(this), this.onFailure);
});
it('shoud match email with all lowercase', function (done) {
var lookup = { email: 'test@test.com', password: 'password'};
keystone.session.signin(lookup, null, null, function(){
// make sure .findOne() is called with a regular expression
sinon.assert.calledOnce(this.User.model.findOne);
this.User.model.findOne.getCall(0).args[0].email.must.be.instanceof(RegExp);
// make sure .exec() is called after
sinon.assert.calledOnce(this.User.model.exec);
this.User.model.exec.calledAfter(this.User.model.findOne).must.be.true;
// make sure .signinWithUser() is called on successful match
sinon.assert.calledOnce(keystone.session.signinWithUser);
done();
}.bind(this), this.onFailure);
});
it('should not match email when invalid', function (done) {
var lookup = { email: 'xxx', password: 'password'};
keystone.session.signin(lookup, null, null, this.onSuccess, function(err){
// make sure .findOne() was not called
sinon.assert.notCalled(this.User.model.findOne);
// make sure .exec() was not called
sinon.assert.notCalled(this.User.model.exec);
this.User.model.exec.calledAfter(this.User.model.findOne).must.be.true;
err.must.be.an.instanceof(Error)
// make sure .signinWithUser() is NOT called on failed match
sinon.assert.notCalled(keystone.session.signinWithUser);
done()
}.bind(this));
});
it('should not match email when just a regex', function (done) {
var lookup = { email: '\.', password: 'password'};
keystone.session.signin(lookup, null, null, this.onSuccess, function(err){
// make sure .findOne() was not called
sinon.assert.notCalled(this.User.model.findOne);
// make sure .exec() was not called
sinon.assert.notCalled(this.User.model.exec);
this.User.model.exec.calledAfter(this.User.model.findOne).must.be.true;
err.must.be.an.instanceof(Error)
// make sure .signinWithUser() is NOT called on failed match
sinon.assert.notCalled(keystone.session.signinWithUser);
done()
}.bind(this));
});
});
});
// TODO: test keystone.session.signout()
// describe('keystone.session.signout()');
// TODO: test keystone.session.persist()
// describe('keystone.session.persist()');
// TODO: test keystone.session.keystoneAuth()
// describe('keystone.session.keystoneAuth()');
});