This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
force_auth.js
239 lines (200 loc) · 6.65 KB
/
force_auth.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
define([
'chai',
'jquery',
'sinon',
'views/force_auth',
'lib/session',
'lib/fxa-client',
'lib/promise',
'models/reliers/relier',
'models/auth_brokers/base',
'../../mocks/window',
'../../mocks/router',
'../../lib/helpers'
],
function (chai, $, sinon, View, Session, FxaClient, p, Relier, Broker,
WindowMock, RouterMock, TestHelpers) {
var assert = chai.assert;
describe('/views/force_auth', function () {
var email;
var view;
var router;
var windowMock;
var fxaClient;
var relier;
var profileClientMock;
describe('missing email address', function () {
beforeEach(function () {
windowMock = new WindowMock();
windowMock.location.search = '';
relier = new Relier();
broker = new Broker();
fxaClient = new FxaClient();
profileClientMock = TestHelpers.stubbedProfileClient();
Session.clear();
view = new View({
window: windowMock,
fxaClient: fxaClient,
profileClient: profileClientMock,
relier: relier,
broker: broker
});
return view.render()
.then(function () {
$('#container').html(view.el);
});
});
afterEach(function () {
view.remove();
view.destroy();
windowMock = view = null;
});
it('prints an error message', function () {
windowMock.location.search = '';
assert.notEqual(view.$('.error').text(), '');
});
it('shows no avatar if Session.avatar is undefined', function (done) {
relier.set('email', 'a@a.com');
assert.isNull(view.context().avatar);
return view.render()
.then(function () {
assert.notOk(view.$('.avatar-view img').length);
done();
})
.fail(done);
});
it('shows no avatar when there is no Session.email', function (done) {
relier.set('email', 'a@a.com');
Session.set('avatar', 'avatar.jpg');
assert.isNull(view.context().avatar);
return view.render()
.then(function () {
assert.notOk(view.$('.avatar-view img').length);
done();
})
.fail(done);
});
it('shows avatar when Session.email and relier.email match', function (done) {
relier.set('email', 'a@a.com');
Session.set('email', 'a@a.com');
Session.set('avatar', 'avatar.jpg');
assert.equal(view.context().avatar, 'avatar.jpg');
return view.render()
.then(function () {
assert.ok(view.$('.avatar-view img').length);
done();
})
.fail(done);
});
it('shows no avatar when Session.email and relier.email do not match', function (done) {
relier.set('email', 'a@a.com');
Session.set('email', 'b@b.com');
Session.set('avatar', 'avatar.jpg');
assert.isNull(view.context().avatar);
return view.render()
.then(function () {
assert.notOk(view.$('.avatar-view img').length);
done();
})
.fail(done);
});
});
describe('with email', function () {
beforeEach(function () {
email = TestHelpers.createEmail();
Session.set('prefillPassword', 'password');
windowMock = new WindowMock();
windowMock.location.search = '?email=' + encodeURIComponent(email);
relier = new Relier();
relier.set('email', email);
broker = new Broker();
fxaClient = new FxaClient();
profileClientMock = TestHelpers.stubbedProfileClient();
router = new RouterMock();
view = new View({
window: windowMock,
router: router,
fxaClient: fxaClient,
profileClient: profileClientMock,
relier: relier,
broker: broker
});
return view.render()
.then(function () {
$('#container').html(view.el);
});
});
afterEach(function () {
view.remove();
view.destroy();
windowMock = router = view = null;
$('#container').empty();
});
it('is able to submit the form', function (done) {
sinon.stub(view.fxaClient, 'signIn', function () {
done();
});
$('#submit-btn').click();
});
describe('submit', function () {
it('submits the sign in', function () {
var password = 'password';
sinon.stub(view.fxaClient, 'signIn', function () {
return p({
verified: true
});
});
sinon.stub(view.fxaClient, 'recoveryEmailStatus', function () {
return p.reject(assert.fail);
});
view.$('input[type=password]').val(password);
return view.submit()
.then(function () {
assert.isTrue(view.fxaClient.signIn.calledWith(
email, password, relier));
});
});
});
it('does not print an error message', function () {
assert.equal(view.$('.error').text(), '');
});
it('does not allow the email to be edited', function () {
assert.equal($('input[type=email]').length, 0);
});
it('prefills password', function () {
assert.equal($('input[type=password]').val(), 'password');
});
it('user cannot create an account', function () {
assert.equal($('a[href="/signup"]').length, 0);
});
it('isValid is successful when the password is filled out', function () {
$('.password').val('password');
assert.isTrue(view.isValid());
});
it('forgot password request redirects directly to confirm_reset_password', function () {
sinon.stub(view.fxaClient, 'passwordReset', function () {
return p();
});
relier.set('email', email);
return view.resetPasswordNow()
.then(function () {
assert.equal(router.page, 'confirm_reset_password');
assert.isTrue(view.fxaClient.passwordReset.calledWith(
email, relier));
});
});
it('only one forget password request at a time', function () {
var event = $.Event('click');
view.resetPasswordNow(event);
return view.resetPasswordNow(event)
.then(assert.fail, function (err) {
assert.equal(err.message, 'submit already in progress');
});
});
});
});
});