Skip to content

Commit 8fe4396

Browse files
authored
Does not add permissions when a doc is empty (#23)
1 parent 5b4b62a commit 8fe4396

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- Removing the ability to call Model.remove() and Model.create() since those aren't compatible with how this library works.
44
- Muuuuch better tests
55
- Embedded permissions object cannot be overwritten
6-
- When a document has embedded permissions, those permissions will be checks when a save or remove is being done. That way someone cannot write to an object in a way that changes their permssions and then try to save it.
6+
- When a document has embedded permissions, those permissions will be checks when a save or remove is being done. That way someone cannot write to an object in a way that changes their permssions and then try to save it.
7+
- Does not add permissions when a doc is empty

__tests__/embedPermissions.test.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,36 @@ test.before((t) => {
1717
});
1818

1919
test('No schema', (t) => {
20-
const blankDoc = {};
20+
const blankDoc = { _id: 'someId' };
2121
embedPermissions(false, { permissions: true }, ['manager'], blankDoc);
22-
t.deepEqual(blankDoc, {}, 'Nothing should have been added to doc');
22+
t.deepEqual(blankDoc, { _id: 'someId' }, 'Nothing should have been added to doc');
2323
});
2424

2525
test('No document', (t) => {
2626
t.notThrows(() => { embedPermissions(t.context.schema, { permissions: true }, ['manager'], false); });
2727
});
2828

29+
test('Do not embed permissions if doc is empty', (t) => {
30+
const doc = {};
31+
embedPermissions(t.context.schema, { permissions: true }, ['manager'], doc);
32+
33+
t.deepEqual(doc, {}, 'Should not add permissions when doc is empty object');
34+
});
35+
2936
test('Options say to do nothing', (t) => {
30-
const blankDoc = {};
37+
const blankDoc = { _id: 'someId' };
3138
embedPermissions(t.context.schema, false, ['manager'], blankDoc);
32-
t.deepEqual(blankDoc, {}, 'Should not add permissions for false options');
39+
t.deepEqual(blankDoc, { _id: 'someId' }, 'Should not add permissions for false options');
3340

3441
embedPermissions(t.context.schema, {}, ['manager'], blankDoc);
35-
t.deepEqual(blankDoc, {}, 'Should not add permissions for {} options');
42+
t.deepEqual(blankDoc, { _id: 'someId' }, 'Should not add permissions for {} options');
3643

3744
embedPermissions(t.context.schema, { permissions: false }, ['manager'], blankDoc);
38-
t.deepEqual(blankDoc, {}, 'Should not add permissions for { permissions: false} options');
45+
t.deepEqual(blankDoc, { _id: 'someId' }, 'Should not add permissions for { permissions: false} options');
3946
});
4047

4148
test('Permissions embeded under default key', (t) => {
42-
const managerDoc = {};
49+
const managerDoc = { _id: 'someId' };
4350
embedPermissions(t.context.schema, { permissions: true }, ['manager'], managerDoc);
4451
t.deepEqual(
4552
managerDoc.permissions,
@@ -51,7 +58,7 @@ test('Permissions embeded under default key', (t) => {
5158
'Incorrect permissions embedded',
5259
);
5360

54-
const defaultDoc = {};
61+
const defaultDoc = { _id: 'someId' };
5562
embedPermissions(t.context.schema, { permissions: true }, [], defaultDoc);
5663
t.deepEqual(
5764
defaultDoc.permissions,
@@ -64,7 +71,7 @@ test('Permissions embeded under default key', (t) => {
6471
);
6572
});
6673
test('Permissions embded under custom key', (t) => {
67-
const managerDoc = {};
74+
const managerDoc = { _id: 'someId' };
6875
embedPermissions(t.context.schema, { permissions: 'customKey' }, ['manager'], managerDoc);
6976
t.deepEqual(
7077
managerDoc.customKey,
@@ -76,7 +83,7 @@ test('Permissions embded under custom key', (t) => {
7683
'Incorrect permissions embedded',
7784
);
7885

79-
const defaultDoc = {};
86+
const defaultDoc = { _id: 'someId' };
8087
embedPermissions(t.context.schema, { permissions: 'customKey' }, [], defaultDoc);
8188
t.deepEqual(
8289
defaultDoc.customKey,
@@ -101,7 +108,7 @@ test('If there\'s already a permissions field', (t) => {
101108
});
102109

103110
test('Verify that the permissions data cannot be changed', (t) => {
104-
const doc = {};
111+
const doc = { _id: 'foobar' };
105112
embedPermissions(t.context.schema, { permissions: true }, ['manager'], doc);
106113

107114
t.throws(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
"nodeunit": "^0.11.2"
5151
},
5252
"optionalDependencies": {
53-
"mongodb-runner": "3.6.1"
53+
"mongodb-runner": "4.3.2"
5454
}
5555
}

src/embedPermissions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
const _ = require('lodash');
12
const getAuthorizedFields = require('./getAuthorizedFields');
23
const hasPermission = require('./hasPermission');
34

45
function embedPermissions(schema, options, authLevels, doc) {
5-
if (!schema || !options || !options.permissions || !doc) { return; }
6+
if (!schema || !options || !options.permissions || _.isEmpty(doc)) { return; }
67

78
const permsKey = options.permissions === true ? 'permissions' : options.permissions;
89

0 commit comments

Comments
 (0)