This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
/
verifier.test.js
260 lines (214 loc) · 7.14 KB
/
verifier.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
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
import feathers from 'feathers';
import authentication from 'feathers-authentication';
import hasher from '../src/utils/hash';
import { Verifier, defaults } from '../src';
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
describe('Verifier', () => {
let service;
let app;
let options;
let verifier;
let user;
beforeEach(() => {
app = feathers();
return hasher('admin').then(password => {
user = {
email: 'admin@feathersjs.com',
password
};
service = {
find () {}
};
sinon.stub(service, "find", function (params) {
return new Promise((resolve, reject) => {
const { email } = params && params.query
if (email === 'nonexistinguser@gmail.com') {
return resolve([])
}
return resolve([user])
})
});
app.use('users', service)
.configure(authentication({ secret: 'supersecret' }));
options = Object.assign({}, defaults, app.get('authentication'));
verifier = new Verifier(app, options);
});
});
it('is CommonJS compatible', () => {
expect(typeof require('../lib/verifier')).to.equal('function');
});
it('exposes the Verifier class', () => {
expect(typeof Verifier).to.equal('function');
});
describe('constructor', () => {
it('retains an app reference', () => {
expect(verifier.app).to.deep.equal(app);
});
it('sets options', () => {
expect(verifier.options).to.deep.equal(options);
});
it('sets service using service path', () => {
expect(verifier.service).to.deep.equal(app.service('users'));
});
it('sets a passed in service instance', () => {
options.service = service;
expect(new Verifier(app, options).service).to.deep.equal(service);
});
describe('when service is undefined', () => {
it('throws an error', () => {
expect(() => {
new Verifier(app, {});
}).to.throw();
});
});
});
describe('_comparePassword', () => {
describe('when entity is missing password field', () => {
it('returns an error', () => {
return verifier._comparePassword({}).catch(error => {
expect(error).to.not.equal(undefined);
});
});
});
describe('password comparison fails', () => {
it('rejects with false', () => {
return verifier._comparePassword(user, 'invalid').catch(error => {
expect(error).to.equal(false);
});
});
});
describe('password comparison succeeds', () => {
it('returns the entity', () => {
return verifier._comparePassword(user, 'admin').then(result => {
expect(result).to.deep.equal(user);
});
});
it('allows dot notation for password field', () => {
user.password = {
value: user.password
};
verifier.options.passwordField = 'password.value';
return verifier._comparePassword(user, 'admin').then(result => {
expect(result).to.deep.equal(user);
});
});
it('prefers entityPasswordField over passwordField', () => {
user.password = {
value: user.password
};
verifier.options.passwordField = 'password';
verifier.options.entityPasswordField = 'password.value';
return verifier._comparePassword(user, 'admin').then(result => {
expect(result).to.deep.equal(user);
});
})
});
});
describe('_normalizeResult', () => {
describe('when has results', () => {
it('returns entity when paginated', () => {
return verifier._normalizeResult({ data: [user] }).then(result => {
expect(result).to.deep.equal(user);
});
});
it('returns entity when not paginated', () => {
return verifier._normalizeResult([user]).then(result => {
expect(result).to.deep.equal(user);
});
});
});
describe('when no results', () => {
it('rejects with false when paginated', () => {
return verifier._normalizeResult({ data: [] }).catch(error => {
expect(error).to.equal(false);
});
});
it('rejects with false when not paginated', () => {
return verifier._normalizeResult([]).catch(error => {
expect(error).to.equal(false);
});
});
});
});
describe('verify', () => {
it('calls find on the provided service', done => {
verifier.verify({}, user.email, 'admin', () => {
const query = { email: user.email, $limit: 1 };
expect(service.find).to.have.been.calledOnce;
expect(service.find).to.have.been.calledWith({ query });
done();
});
});
it('allows overriding of usernameField', done => {
verifier.options.usernameField = 'username';
user.username = 'username';
verifier.verify({}, 'username', 'admin', (error, entity) => {
expect(error).to.equal(null);
expect(entity).to.deep.equal(user);
done();
})
});
it('prefers entityUsernameField over usernameField', done => {
verifier.options.usernameField = 'username';
verifier.options.entityUsernameField = 'users.username';
user.username = 'invalid';
user.users = {
username: 'valid'
};
verifier.verify({}, 'valid', 'admin', (error, entity) => {
expect(error).to.equal(null);
expect(entity).to.deep.equal(user);
done();
})
});
it('calls _normalizeResult', done => {
sinon.spy(verifier, '_normalizeResult');
verifier.verify({}, user.email, 'admin', () => {
expect(verifier._normalizeResult).to.have.been.calledOnce;
verifier._normalizeResult.restore();
done();
});
});
it('produces an error message when the user did not exist', done => {
verifier.verify({}, 'nonexistinguser@gmail.com', 'admin', (err, user, info) => {
expect(info.message).to.equal('Invalid login');
done();
});
});
it('calls _comparePassword', done => {
sinon.spy(verifier, '_comparePassword');
verifier.verify({}, user.email, 'admin', () => {
expect(verifier._comparePassword).to.have.been.calledOnce;
verifier._comparePassword.restore();
done();
});
});
it('returns the entity', done => {
verifier.verify({}, user.email, 'admin', (error, entity) => {
expect(error).to.equal(null);
expect(entity).to.deep.equal(user);
done();
});
});
it('handles false rejections in promise chain', (done) => {
verifier._normalizeResult = () => Promise.reject(false);
verifier.verify({}, user.email, 'admin', (error, entity) => {
expect(error).to.equal(null);
expect(entity).to.equal(false);
done();
});
});
it('returns errors', (done) => {
const authError = new Error('An error');
verifier._normalizeResult = () => Promise.reject(authError);
verifier.verify({}, user.email, 'admin', (error, entity) => {
expect(error).to.equal(authError);
expect(entity).to.equal(undefined);
done();
});
});
});
});