Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit da96078

Browse files
authored
Merge pull request #172 from Human-Connection/fix_171
Fix #171
2 parents 2f0508b + 65f907d commit da96078

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

server/hooks/exclude-blacklisted.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ module.exports = function excludeBlacklisted() {
1313
if (usersettings.total <= 0){
1414
return hook;
1515
}
16-
const blacklist = usersettings.data[0].blacklist;
17-
hook.params.query.userId = {$nin: blacklist};
16+
const { blacklist } = usersettings.data[0];
17+
if (blacklist) {
18+
let { query } = hook.params;
19+
query.userId = query.userId || {};
20+
query.userId.$nin = blacklist;
21+
}
1822
return hook;
1923
}
2024

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const assert = require('assert');
2+
const feathers = require('@feathersjs/feathers');
3+
const excludeBlacklisted = require('../../server/hooks/exclude-blacklisted');
4+
5+
let app;
6+
let usersettings = {};
7+
beforeEach(() => {
8+
// Create a new plain Feathers application
9+
app = feathers();
10+
11+
// Register a dummy custom service that just return the
12+
// message data back
13+
app.use('/usersettings', {
14+
async find() {
15+
return {
16+
data: [usersettings]
17+
};
18+
}
19+
});
20+
});
21+
22+
describe('\'exclude-blacklisted\' hook', () => {
23+
context('given a blacklist', () => {
24+
let mock;
25+
beforeEach(() => {
26+
usersettings = { blacklist: ['4711'] };
27+
mock = {
28+
type: 'before',
29+
method: 'find',
30+
params: {
31+
user: { _id: 'whatever' }
32+
},
33+
app
34+
};
35+
});
36+
37+
context('query param `userId` is an object', () => {
38+
it('adds one key', () => {
39+
mock.params.query = {
40+
userId: { $ne: 'user id' }
41+
};
42+
43+
const hook = excludeBlacklisted();
44+
return hook(mock).then(result => {
45+
assert.deepEqual(result.params.query.userId, {
46+
$ne: 'user id',
47+
$nin: ['4711']
48+
});
49+
});
50+
});
51+
});
52+
53+
context('query param `userId` is set to an exact id', () => {
54+
it('has no effect', () => {
55+
mock.params.query = {
56+
userId: 'exact user id'
57+
};
58+
59+
const hook = excludeBlacklisted();
60+
return hook(mock).then(result => {
61+
assert.deepEqual(result.params.query.userId, 'exact user id' );
62+
});
63+
});
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)