Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit f163296

Browse files
authored
Safely dispatch without password (#40)
1 parent 97f76ce commit f163296

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

lib/hooks/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const hashPassword = require('./hash-password');
2+
const protect = require('./protect');
23

34
module.exports = {
4-
hashPassword
5+
hashPassword,
6+
protect
57
};

lib/hooks/protect.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const omit = require('lodash.omit');
2+
3+
module.exports = function (...fields) {
4+
return function protect (hook) {
5+
const result = hook.dispatch || hook.result;
6+
const o = current => omit(current, fields);
7+
8+
if (!result) {
9+
return hook;
10+
}
11+
12+
if (Array.isArray(result)) {
13+
hook.dispatch = result.map(o);
14+
} else if (result.data) {
15+
hook.dispatch = Object.assign({}, result, {
16+
data: result.data.map(o)
17+
});
18+
} else {
19+
hook.dispatch = o(result);
20+
}
21+
22+
return hook;
23+
};
24+
};

test/hooks/protect.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-disable no-unused-expressions */
2+
const chai = require('chai');
3+
4+
const { protect } = require('../../lib/hooks');
5+
const { expect } = chai;
6+
7+
function testOmit (title, property) {
8+
describe(title, () => {
9+
const fn = protect('password');
10+
11+
it('omits from object', () => {
12+
const data = {
13+
email: 'test@user.com',
14+
password: 'supersecret'
15+
};
16+
const context = {
17+
[property]: data
18+
};
19+
const result = fn(context);
20+
21+
expect(result).to.deep.equal({
22+
[property]: data,
23+
dispatch: { email: 'test@user.com' }
24+
});
25+
});
26+
27+
it('omits from array', () => {
28+
const data = [{
29+
email: 'test1@user.com',
30+
password: 'supersecret'
31+
}, {
32+
email: 'test2@user.com',
33+
password: 'othersecret'
34+
}];
35+
const context = {
36+
[property]: data
37+
};
38+
const result = fn(context);
39+
40+
expect(result).to.deep.equal({
41+
[property]: data,
42+
dispatch: [
43+
{ email: 'test1@user.com' },
44+
{ email: 'test2@user.com' }
45+
]
46+
});
47+
});
48+
49+
it('omits from pagination object', () => {
50+
const data = {
51+
total: 2,
52+
data: [{
53+
email: 'test1@user.com',
54+
password: 'supersecret'
55+
}, {
56+
email: 'test2@user.com',
57+
password: 'othersecret'
58+
}]
59+
};
60+
const context = {
61+
[property]: data
62+
};
63+
const result = fn(context);
64+
65+
expect(result).to.deep.equal({
66+
[property]: data,
67+
dispatch: {
68+
total: 2,
69+
data: [
70+
{ email: 'test1@user.com' },
71+
{ email: 'test2@user.com' }
72+
]
73+
}
74+
});
75+
});
76+
});
77+
}
78+
79+
describe('hooks:protect', () => {
80+
it('does nothing when called with no result', () => {
81+
const fn = protect();
82+
const original = {};
83+
84+
expect(fn(original)).to.deep.equal(original);
85+
});
86+
87+
testOmit('with hook.result', 'result');
88+
testOmit('with hook.dispatch already set', 'dispatch');
89+
});

0 commit comments

Comments
 (0)