/
session.spec.js
262 lines (216 loc) Β· 8.35 KB
/
session.spec.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
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
define([
'js/components/session',
'js/bugutils/minimal_pubsub',
'js/services/api',
'js/components/api_request',
'js/components/user',
'js/components/csrf_manager',
], function(Session, MinSub, Api, ApiRequest, User, CSRFManager) {
describe('Session Object', function() {
it('provides a hardened interface with all methods widgets might need (login, logout, register, user model get/set, etc)', function() {
var s = new Session();
var hardened = s.getHardenedInstance();
expect(hardened.__facade__).equals(true);
expect(hardened.handleCallbackError).to.be.undefined;
expect(hardened.start).to.be.undefined;
expect(_.keys(hardened)).to.eql([
'login',
'logout',
'register',
'resetPassword1',
'resetPassword2',
'setChangeToken',
'__facade__',
'mixIn',
]);
});
it('has an explicit method for every action (login, logout, register, etc) a user might need to do before he/she is authenticated', function() {
var s = new Session({ test: true });
var minsub = new (MinSub.extend({
request: function(apiRequest) { },
}))({ verbose: false });
var api = new Api();
var csrfManager = new CSRFManager();
csrfManager.getCSRF = function() {
this.deferred = $.Deferred();
return this.deferred.promise();
};
csrfManager.resolvePromiseWithNewKey = function() {
this.deferred.resolve('foo');
};
var requestStub = sinon.stub(Api.prototype, 'request');
minsub.beehive.removeService('Api');
minsub.beehive.addService('Api', api);
minsub.beehive.addObject('CSRFManager', csrfManager);
s.activate(minsub.beehive);
s.login({
email: 'goo',
password: 'foo',
});
csrfManager.resolvePromiseWithNewKey();
expect(requestStub.args[0][0]).to.be.instanceof(ApiRequest);
expect(requestStub.args[0][0].toJSON().target).to.eql('accounts/user/login');
expect(requestStub.args[0][0].toJSON().options.type).to.eql('POST');
expect(requestStub.args[0][0].toJSON().options.data).to.eql(
'{"email":"goo","password":"foo"}'
);
s.logout();
csrfManager.resolvePromiseWithNewKey();
expect(requestStub.args[1][0]).to.be.instanceof(ApiRequest);
expect(requestStub.args[1][0].toJSON().target).to.eql(
'accounts/user/logout',
);
expect(requestStub.args[1][0].toJSON().options.type).to.eql('POST');
expect(requestStub.args[1][0].toJSON().options.done).to.eql(
s.logoutSuccess
);
const registerPayload = {
email: 'goo@goo.com',
password1: 'foo',
password2: 'foo',
'g-recaptcha-response': 'boo',
given_name: 'foo',
family_name: 'bar',
};
s.register(registerPayload);
const stringify = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
csrfManager.resolvePromiseWithNewKey();
expect(requestStub.args[2][0]).to.be.instanceof(ApiRequest);
expect(requestStub.args[2][0].toJSON().target).to.eql(
'accounts/user',
);
expect(requestStub.args[2][0].toJSON().options.type).to.eql('POST');
expect(
stringify(JSON.parse(requestStub.args[2][0].toJSON().options.data)),
).to.eql(
stringify(registerPayload),
);
expect(requestStub.args[2][0].toJSON().options.done).to.eql(
s.registerSuccess
);
expect(requestStub.args[2][0].toJSON().options.fail).to.eql(
s.registerFail
);
const resetPasswordPayload = { email: 'goo@goo.com', 'g-recaptcha-response': 'boo' }
s.resetPassword1(resetPasswordPayload);
csrfManager.resolvePromiseWithNewKey();
expect(requestStub.args[3][0]).to.be.instanceof(ApiRequest);
expect(requestStub.args[3][0].toJSON().target).to.eql(
'accounts/user/reset-password/goo@goo.com',
);
expect(requestStub.args[3][0].toJSON().options.type).to.eql('POST');
expect(
requestStub.args[3][0].toJSON().options.data).to.eql(
'{"g-recaptcha-response":"boo"}',
);
expect(requestStub.args[3][0].toJSON().options.done).to.eql(
s.resetPassword1Success
);
expect(requestStub.args[3][0].toJSON().options.fail).to.eql(
s.resetPassword1Fail
);
//this happens in the router after the user has clicked on a link in their email
s.setChangeToken('fakeToken');
s.resetPassword2({ password1: '1Aaaaa', password2: '1Aaaaa' });
csrfManager.resolvePromiseWithNewKey();
expect(requestStub.args[4][0]).to.be.instanceof(ApiRequest);
//test version just uses string "location.origin", real version will use the actual origin
expect(requestStub.args[4][0].toJSON().target).to.eql(
'accounts/user/reset-password/fakeToken',
);
expect(requestStub.args[4][0].toJSON().options.type).to.eql('PUT');
expect(requestStub.args[4][0].toJSON().options.data).to.eql(
'{"password1":"1Aaaaa","password2":"1Aaaaa"}'
);
expect(requestStub.args[4][0].toJSON().options.done).to.eql(
s.resetPassword2Success
);
expect(requestStub.args[4][0].toJSON().options.fail).to.eql(
s.resetPassword2Fail
);
requestStub.restore();
});
it('handles fail of method by 1) sending pubsub method and 2) sending alert', function() {
var minsub = new (MinSub.extend({
request: function(apiRequest) { },
}))({ verbose: false });
var s = new Session();
var beehive = minsub.beehive.getHardenedInstance();
s.activate(minsub.beehive);
var pubsub = s.getPubSub();
sinon.stub(pubsub, 'publish');
var fakeXHR = { responseJson: { error: 'no account' } };
s.loginFail(fakeXHR);
expect(pubsub.publish.args[0][0]).to.eql('[Alert]-Message');
expect(pubsub.publish.args[0][1].msg).to.eql(
'Login was unsuccessful (error unknown)'
);
expect(pubsub.publish.args[1]).to.eql([
'[PubSub]-User-Announcement',
'login_fail',
'Login was unsuccessful (error unknown)',
]);
s.registerFail(fakeXHR);
expect(pubsub.publish.args[2][0]).to.eql('[Alert]-Message');
expect(pubsub.publish.args[2][1].msg).to.eql(
'Registration was unsuccessful (error unknown)'
);
expect(pubsub.publish.args[3]).to.eql([
'[PubSub]-User-Announcement',
'register_fail',
'Registration was unsuccessful (error unknown)',
]);
s.resetPassword1Fail(fakeXHR);
expect(pubsub.publish.args[4][0]).to.eql('[Alert]-Message');
expect(pubsub.publish.args[5]).to.eql([
'[PubSub]-User-Announcement',
'reset_password_1_fail',
'password reset step 1 was unsuccessful (error unknown)',
]);
s.resetPassword2Fail(fakeXHR);
expect(pubsub.publish.args[6][0]).to.eql('[Alert]-Message');
expect(pubsub.publish.args[7]).to.eql([
'[PubSub]-User-Announcement',
'reset_password_2_fail',
'password reset step 2 was unsuccessful (error unknown)',
]);
});
it('handles success of methods by 1) sending pubsub method and 2) optionally doing additional work', function() {
var minsub = new (MinSub.extend({
request: function(apiRequest) { },
}))({ verbose: false });
var s = new Session();
var u = new User();
u.completeLogOut = sinon.stub();
minsub.beehive.addObject('User', u);
s.activate(minsub.beehive);
sinon.stub(s.getPubSub(), 'publish');
sinon.stub(s, 'getApiAccess', function() {
var d = $.Deferred();
d.resolve();
return d.promise();
});
s.loginSuccess();
expect(s.getApiAccess.callCount).to.eql(1);
//called once getApiAccess is resolved
s.logoutSuccess();
// scrub the user object
expect(u.completeLogOut.callCount).to.eql(1);
s.registerSuccess();
expect(s.getPubSub().publish.args[0]).to.eql([
'[PubSub]-User-Announcement',
'register_success',
]);
s.resetPassword1Success();
expect(s.getPubSub().publish.args[1]).to.eql([
'[PubSub]-User-Announcement',
'reset_password_1_success',
]);
s.resetPassword2Success();
expect(s.getPubSub().publish.args[2]).to.eql([
'[PubSub]-User-Announcement',
'reset_password_2_success',
]);
});
});
});