forked from clay/amphora-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.test.js
331 lines (254 loc) · 10.1 KB
/
utils.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'use strict';
const _startCase = require('lodash/startCase'),
handlebars = require('handlebars'),
bcrypt = require('bcryptjs'),
filename = __filename.split('/').pop().split('.').shift(),
lib = require(`./${filename}`),
storage = require('./test/fixtures/mocks/storage');
describe(_startCase(filename), function () {
let fakeDb;
beforeEach(function () {
fakeDb = storage();
lib.setDb(fakeDb);
});
describe('compileTemplate', function () {
const fn = lib[this.description];
it('calls the compile method', function () {
handlebars.compile = jest.fn();
fn('login.handlebars');
expect(handlebars.compile).toHaveBeenCalled();
});
});
describe('serializeUser', function () {
const fn = lib[this.description];
it('calls `next` if no error', function () {
const done = jest.fn(),
mockUser = { username: 'fake', provider: 'google' };
fn(mockUser, done);
expect(done).toBeCalled();
});
});
describe('deserializeUser', function () {
const fn = lib[this.description];
it('calls `next` if no error', function () {
const next = jest.fn(),
mockUser = { username: 'fake', provider: 'google' };
fakeDb.get.mockResolvedValue(mockUser);
return fn('foo', next)
.then(() => {
expect(fakeDb.get).toBeCalledWith('/_users/foo');
expect(next).toBeCalledWith(null, mockUser);
});
});
it('calls `next` with error', function () {
const next = jest.fn(),
mockError = new Error('Error getting user data');
fakeDb.get.mockRejectedValue();
return fn('bar', next)
.catch(() => {
expect(fakeDb.get).toBeCalledWith('/_users/bar');
expect(next).toBeCalledWith(mockError);
});
});
});
describe('getPathOrBase', function () {
const fn = lib[this.description];
it('adds initial slash if site path is emptystring', function () {
expect(fn()).toEqual('/');
});
it('returns site path if it exists', function () {
expect(fn({ path: '/foo'})).toEqual('/foo');
});
});
describe('getCallbackUrl', function () {
const fn = lib[this.description];
it('adds initial slash (after the site path) if site has a path', function () {
expect(fn({ path: '/foo', prefix: 'domain.com/foo', port: '80'}, 'twitter')).toEqual('http://domain.com/foo/_auth/twitter/callback');
});
it('does not add slash if site has no path', function () {
expect(fn({ prefix: 'domain.com/', port: '80'}, 'twitter')).toEqual('http://domain.com/_auth/twitter/callback');
});
});
describe('encode', function () {
const fn = lib[this.description];
it('hashes a username and password as base64', function () {
const expected = Buffer.from('foo@bar', 'utf8').toString('base64');
expect(fn('foo', 'bar')).toEqual(expected);
});
});
describe('getAuthUrl', function () {
const fn = lib[this.description];
it('does not add slash to auth path if url has trailing slash', function () {
const site = {
prefix: 'foo.com',
protocol: 'http',
port: '80'
};
expect(fn(site)).toEqual('http://foo.com/_auth');
});
it('does add slash to auth path if url has no trailing slash', function () {
const site = {
prefix: 'foo.com/',
protocol: 'http',
port: '80'
};
expect(fn(site)).toEqual('http://foo.com/_auth');
});
});
describe('removePrefix', function () {
const fn = lib[this.description];
it('removes the prefix token and anything before it', function () {
expect(fn('foo/bar', '/')).toEqual('bar');
});
it('returns the same string if the prefix token is not found', function () {
expect(fn('foo-bar', '/')).toEqual('foo-bar');
});
});
describe('generateStrategyName', function () {
const fn = lib[this.description];
it('generates a string with provider and slug', function () {
const site = { slug: 'nymag' };
expect(fn('twitter', site)).toEqual('twitter-nymag');
});
});
describe('removeQueryString', function () {
const fn = lib[this.description];
it('removes the queryString from path url', function () {
expect(fn('http://nymag.com?edit=true')).toEqual('http://nymag.com');
});
it('returns the same path url if there are no queryStrings', function () {
expect(fn('http://nymag.com')).toEqual('http://nymag.com');
});
});
describe('removeExtension', function () {
const fn = lib[this.description];
it('removes the extension from the url path', function () {
expect(fn('http://nymag.com/_pages/homepage.html')).toEqual('http://nymag.com/_pages/homepage');
expect(fn('http://nymag.com/_pages/homepage.html?edit=true')).toEqual('http://nymag.com/_pages/homepage');
});
it('returns the same path url if there are no extensions', function () {
expect(fn('http://nymag.com/_pages/homepage')).toEqual('http://nymag.com/_pages/homepage');
});
it('removes the extension if it does not have a leading slash', function () {
expect(fn('homepage.html')).toEqual('homepage');
});
});
describe('getUri', function () {
const fn = lib[this.description];
it('returns a formatted uri', function () {
const req = {
hostname: 'foo.com',
baseUrl: '/bar',
path: '/pizza'
};
expect(fn(req)).toEqual('foo.com/bar/pizza');
});
});
describe('getProviders', function () {
const fn = lib[this.description];
it('removes apikey and local providers', function () {
const providers = ['apikey', 'local', 'google'],
site = {
prefix: 'foo.com',
protocol: 'http',
port: 80
},
expected = [{
name: 'google',
url: 'http://foo.com/_auth/google',
title: 'Log in with Google',
icon: () => {}
}],
results = fn(providers, site);
expect(results.length).toEqual(1);
expect(results[0].url).toEqual(expected[0].url);
});
});
describe('verify', function () {
const fn = lib[this.description];
let properties, profile, req, next;
beforeEach(function () {
properties = { username: 'username', provider: 'google' };
profile = { username: 'foo' };
req = {};
next = jest.fn();
});
it('throws if there is no username field', function () {
properties = { username: 'username' };
profile = { username: '' };
const cb = () => fn(properties)({}, '', '', profile, next);
expect(cb).toThrowError('Provider hasn\'t given a username at username');
});
it('should exit if the user data is not found', function () {
fakeDb.get = jest.fn().mockRejectedValue();
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, false, { message: 'User not found!' });
});
});
it('should exit if there was an error updating the user', function () {
const mockError = new Error('Something went wrong'),
mockUser = { username: 'foo', provider: 'google', auth: 'admin' };
fakeDb.get = jest.fn().mockResolvedValue(mockUser);
fakeDb.put = jest.fn().mockRejectedValue(mockError);
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(mockError);
});
});
it('should return user data after updating', function () {
const mockUser = { username: 'foo', provider: 'google', auth: 'admin' };
fakeDb.get = jest.fn().mockResolvedValue(mockUser);
fakeDb.put = jest.fn().mockResolvedValue();
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, mockUser);
});
});
it('should exit if the password does not match', function () {
const mockUser = { username: 'foo', provider: 'google', auth: 'admin', password: 'secret123foo' };
properties = { username: 'username', provider: 'google', password: 'password' };
profile = { username: 'foo', password: 'secret' };
fakeDb.get = jest.fn().mockResolvedValue(mockUser);
fakeDb.put = jest.fn().mockResolvedValue();
bcrypt.compareSync = jest.fn().mockReturnValue(false);
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, false, { message: 'Invalid Password' });
});
});
it('should exit if password does not match and user is already authenticated', function () {
const mockUser = { username: 'foo', provider: 'google', auth: 'admin', password: 'secret123foo' };
properties = { username: 'username', provider: 'google', password: 'password' };
profile = { username: 'foo', password: 'secret' };
req = { user: mockUser };
fakeDb.get = jest.fn().mockResolvedValue(mockUser);
bcrypt.compareSync = jest.fn().mockReturnValue(false);
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, false, { message: 'Invalid Password' });
});
});
it('should return data if user is already authenticated', function () {
const mockUser = { username: 'foo', provider: 'google', auth: 'admin', password: 'secret123foo' };
properties = { username: 'username', provider: 'google', password: 'password' };
profile = { username: 'foo', password: 'secret' };
req = { user: mockUser };
fakeDb.get = jest.fn().mockResolvedValue(mockUser);
bcrypt.compareSync = jest.fn().mockReturnValue(true);
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, mockUser);
});
});
it('should exit if user is already authenticated but data cannot be found', function () {
const mockUser = { username: 'foo', provider: 'google', auth: 'admin' };
req = { user: mockUser };
fakeDb.get = jest.fn().mockRejectedValue();
return fn(properties)(req, '', '', profile, next)
.then(() => {
expect(next).toBeCalledWith(null, false, { message: 'User not found!' });
});
});
});
});