This repository was 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
Expand file tree
/
Copy pathhash-password.test.js
More file actions
144 lines (124 loc) · 3.79 KB
/
hash-password.test.js
File metadata and controls
144 lines (124 loc) · 3.79 KB
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
/* eslint-disable no-unused-expressions */
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const { hashPassword } = require('../../lib/hooks');
const { expect } = chai;
chai.use(sinonChai);
describe('hooks:hashPassword', () => {
let hook;
beforeEach(() => {
hook = {
type: 'before',
data: { password: 'secret' },
params: {},
app: {
get: () => {
return {
local: {
passwordField: 'password'
}
};
}
}
};
});
describe('when not called as a before hook', () => {
it('returns an error', () => {
hook.type = 'after';
return hashPassword()(hook).catch(error => {
expect(error).to.not.equal(undefined);
});
});
});
describe('when data does not exist', () => {
it('does not do anything', () => {
delete hook.data;
return hashPassword()(hook).then(returnedHook => {
expect(returnedHook).to.deep.equal(hook);
});
});
});
describe('when password does not exist', () => {
it('does not do anything', () => {
delete hook.data.password;
return hashPassword()(hook).then(returnedHook => {
expect(returnedHook).to.deep.equal(hook);
});
});
});
describe('when password exists', () => {
it('hashes with options from global auth config', () => {
return hashPassword()(hook).then(hook => {
expect(hook.data.password).to.not.equal(undefined);
expect(hook.data.password).to.not.equal('secret');
});
});
it('hashes with custom options', () => {
hook.data.pass = 'secret';
return hashPassword({ passwordField: 'pass' })(hook).then(hook => {
expect(hook.data.pass).to.not.equal(undefined);
expect(hook.data.pass).to.not.equal('secret');
});
});
it('calls custom hash function', () => {
const fn = sinon.stub().returns(Promise.resolve());
return hashPassword({ hash: fn })(hook).then(() => {
expect(fn).to.have.been.calledOnce;
expect(fn).to.have.been.calledWith('secret');
});
});
it('returns an error when custom hash is not a function', () => {
return hashPassword({ hash: true })(hook).catch(error => {
expect(error).to.not.equal(undefined);
});
});
});
describe('when password exists in bulk', () => {
beforeEach(() => {
hook.data = [
{password: 'secret'},
{password: 'secret'}
];
});
it('hashes with options from global auth config', () => {
return hashPassword()(hook).then(hook => {
hook.data.map(item => {
expect(item.password).to.not.equal(undefined);
expect(item.password).to.not.equal('secret');
});
});
});
it('does not remove things if there is no password', () => {
hook.data = [
{ id: 0, password: 'secret' },
{ id: 1 }
];
return hashPassword()(hook).then(hook => {
const { data } = hook;
expect(data.length).to.equal(2);
expect(data[0].password).to.not.equal('secret');
expect(data[1]).to.exist;
});
});
it('hashes with custom options', () => {
hook.data = [
{pass: 'secret'},
{pass: 'secret'}
];
return hashPassword({ passwordField: 'pass' })(hook).then(hook => {
hook.data.map(item => {
expect(item.pass).to.not.equal(undefined);
expect(item.pass).to.not.equal('secret');
});
});
});
it('calls custom hash function', () => {
const fn = sinon.stub().returns(Promise.resolve());
return hashPassword({ hash: fn })(hook).then(() => {
expect(fn).to.have.been.calledTwice;
expect(fn).to.have.been.calledWith('secret');
});
});
});
});