Skip to content

Commit 3dbd63f

Browse files
authored
Adding more tests (#18)
* Adding tests for cleanAuthLevels Bonus: updating the glob for ava so it catches the things in the top level __tests__ folder * Tests for embedPermissions * Upgrading to latest version of ava * Updating tests to support latest version of ava - Async specific methods for throwing exceptions - Making sure authIsDisabled returns a boolean in all cases
1 parent 61f7581 commit 3dbd63f

9 files changed

Lines changed: 500 additions & 362 deletions

__tests__/cleanAuthLevels.test.js

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,78 @@
11
const test = require('ava');
22
const mongoose = require('mongoose');
3-
// const cleanAuthLevels = require('../src/cleanAuthLevels');
3+
const cleanAuthLevels = require('../src/cleanAuthLevels');
44

55
test.before((t) => {
6-
t.context.schema = new mongoose.Schema({ friend: String });
6+
const schema = new mongoose.Schema({ friend: String });
7+
schema.permissions = { default: {}, manager: {}, report: {} };
8+
t.context.schema = schema;
79
});
810

9-
test.todo('Schema passed in is not valid');
10-
test.todo('Falsey authLevel value');
11-
test.todo('Empty array authLevel value');
12-
test.todo('Remove duplicate entries');
13-
test.todo('Remove false entries');
14-
test.todo('Remove entries that are not in the permissions object');
15-
test.todo('authLevel with no issues');
11+
test('Schema passed in is not valid', (t) => {
12+
t.deepEqual(
13+
cleanAuthLevels(false, ['foo']),
14+
[],
15+
'should return empty list when schema is falsy',
16+
);
17+
18+
t.deepEqual(
19+
cleanAuthLevels(false, []),
20+
[],
21+
'should return empty list when schema is falsy and there are no authLevels',
22+
);
23+
24+
t.deepEqual(
25+
cleanAuthLevels({}, ['foo']),
26+
[],
27+
'should return empty list when schema is empty object',
28+
);
29+
30+
t.deepEqual(
31+
cleanAuthLevels({}, []),
32+
[],
33+
'should return empty list when schema is empty object & there are no authLevels',
34+
);
35+
});
36+
37+
test('Falsey authLevel value', (t) => {
38+
t.deepEqual(cleanAuthLevels(t.context.schema, false), []);
39+
t.deepEqual(cleanAuthLevels(t.context.schema, 0), []);
40+
});
41+
42+
test('Empty array authLevel value', (t) => {
43+
t.deepEqual(cleanAuthLevels(t.context.schema, []), []);
44+
});
45+
46+
test('Remove duplicate entries', (t) => {
47+
t.deepEqual(cleanAuthLevels(t.context.schema, ['manager', 'manager']), ['manager']);
48+
});
49+
50+
test('Remove false entries', (t) => {
51+
t.deepEqual(cleanAuthLevels(t.context.schema, [0, 'manager', false]), ['manager']);
52+
});
53+
54+
test('Remove entries that are not in the permissions object', (t) => {
55+
t.deepEqual(cleanAuthLevels(t.context.schema, ['fake', 'manager', 'notthere']), ['manager']);
56+
t.deepEqual(cleanAuthLevels(t.context.schema, ['fake', 'notthere']), []);
57+
});
58+
59+
test('authLevel with no issues', (t) => {
60+
t.deepEqual(
61+
cleanAuthLevels(t.context.schema, ['manager']),
62+
['manager'],
63+
'should handle single item array',
64+
);
65+
66+
t.deepEqual(
67+
cleanAuthLevels(t.context.schema, ['manager', 'report']),
68+
['manager', 'report'],
69+
'should handle mutliple item array',
70+
);
71+
72+
t.deepEqual(
73+
cleanAuthLevels(t.context.schema, 'manager'),
74+
['manager'],
75+
'should handle single level, not in array',
76+
);
77+
});
1678

__tests__/embedPermissions.test.js

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,124 @@
11
const test = require('ava');
2+
const mongoose = require('mongoose');
3+
const embedPermissions = require('../src/embedPermissions');
24

3-
test.todo('Migrate tests for embedPermissions');
5+
test.before((t) => {
6+
const schema = new mongoose.Schema({ friend: String });
7+
schema.permissions = {
8+
default: {},
9+
manager: {
10+
read: ['friend'],
11+
write: ['friend'],
12+
create: true,
13+
remove: true,
14+
},
15+
};
16+
t.context.schema = schema;
17+
});
18+
19+
test('No schema', (t) => {
20+
const blankDoc = {};
21+
embedPermissions(false, { permissions: true }, ['manager'], blankDoc);
22+
t.deepEqual(blankDoc, {}, 'Nothing should have been added to doc');
23+
});
24+
25+
test('No document', (t) => {
26+
t.notThrows(() => { embedPermissions(t.context.schema, { permissions: true }, ['manager'], false); });
27+
});
28+
29+
test('Options say to do nothing', (t) => {
30+
const blankDoc = {};
31+
embedPermissions(t.context.schema, false, ['manager'], blankDoc);
32+
t.deepEqual(blankDoc, {}, 'Should not add permissions for false options');
33+
34+
embedPermissions(t.context.schema, {}, ['manager'], blankDoc);
35+
t.deepEqual(blankDoc, {}, 'Should not add permissions for {} options');
36+
37+
embedPermissions(t.context.schema, { permissions: false }, ['manager'], blankDoc);
38+
t.deepEqual(blankDoc, {}, 'Should not add permissions for { permissions: false} options');
39+
});
40+
41+
test('Permissions embeded under default key', (t) => {
42+
const managerDoc = {};
43+
embedPermissions(t.context.schema, { permissions: true }, ['manager'], managerDoc);
44+
t.deepEqual(
45+
managerDoc.permissions,
46+
{
47+
read: ['friend'],
48+
write: ['friend'],
49+
remove: true,
50+
},
51+
'Incorrect permissions embedded',
52+
);
53+
54+
const defaultDoc = {};
55+
embedPermissions(t.context.schema, { permissions: true }, [], defaultDoc);
56+
t.deepEqual(
57+
defaultDoc.permissions,
58+
{
59+
read: [],
60+
write: [],
61+
remove: false,
62+
},
63+
'Incorrect permissions embedded',
64+
);
65+
});
66+
test('Permissions embded under custom key', (t) => {
67+
const managerDoc = {};
68+
embedPermissions(t.context.schema, { permissions: 'customKey' }, ['manager'], managerDoc);
69+
t.deepEqual(
70+
managerDoc.customKey,
71+
{
72+
read: ['friend'],
73+
write: ['friend'],
74+
remove: true,
75+
},
76+
'Incorrect permissions embedded',
77+
);
78+
79+
const defaultDoc = {};
80+
embedPermissions(t.context.schema, { permissions: 'customKey' }, [], defaultDoc);
81+
t.deepEqual(
82+
defaultDoc.customKey,
83+
{
84+
read: [],
85+
write: [],
86+
remove: false,
87+
},
88+
'Incorrect permissions embedded',
89+
);
90+
});
91+
92+
test('If there\'s already a permissions field', (t) => {
93+
const doc = { permissions: 'app level info' };
94+
t.throws(
95+
() => {
96+
embedPermissions(t.context.schema, { permissions: true }, ['manager'], doc);
97+
},
98+
Error,
99+
'An error should be thrown when a permissions key is already present in the target object',
100+
);
101+
});
102+
103+
test('Verify that the permissions data cannot be changed', (t) => {
104+
const doc = {};
105+
embedPermissions(t.context.schema, { permissions: true }, ['manager'], doc);
106+
107+
t.throws(
108+
() => { doc.permissions.read = []; },
109+
Error,
110+
'The permissions object shouldn\'t be writable [read]',
111+
);
112+
113+
t.throws(
114+
() => { doc.permissions.write = []; },
115+
Error,
116+
'The permissions object shouldn\'t be writable [write]',
117+
);
118+
119+
t.throws(
120+
() => { doc.permissions.remove = false; },
121+
Error,
122+
'The permissions object shouldn\'t be writable [remove]',
123+
);
124+
});

__tests__/methods/Model.create.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('Model.create should be callable without the plugin installed', async (t) =
2222
const schema = new mongoose.Schema({ friend: String });
2323
const MyModel = mongoose.model('ModelCreateWithoutPluggin', schema);
2424

25-
await t.notThrows(MyModel.create({ friend: 'bar' }));
25+
await t.notThrowsAsync(MyModel.create({ friend: 'bar' }));
2626
});
2727

2828
test.after.always(async () => {

__tests__/methods/Model.remove.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('Model.remove should be callable without the plugin installed', async (t) =
2222
const schema = new mongoose.Schema({ friend: String });
2323
const MyModel = mongoose.model('ModelRemoveWithoutPlugin', schema);
2424

25-
await t.notThrows(MyModel.remove({}).exec());
25+
await t.notThrowsAsync(MyModel.remove({}).exec());
2626
});
2727

2828
test.after.always(async () => {

0 commit comments

Comments
 (0)