This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
verifier.test.js
200 lines (167 loc) · 5.47 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
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().returns(Promise.resolve([user]))
};
app.use('users', service)
.configure(authentication({ secret: 'supersecret' }));
options = Object.assign({}, defaults, app.get('auth'));
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);
});
});
});
});
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('calls _normalizeResult', done => {
sinon.spy(verifier, '_normalizeResult');
verifier.verify({}, user.email, 'admin', () => {
expect(verifier._normalizeResult).to.have.been.calledOnce;
verifier._normalizeResult.restore();
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', () => {
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', () => {
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();
});
});
});
});