This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
/
admin.js
202 lines (130 loc) · 5.44 KB
/
admin.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const Admin = require('../../../server/models/admin');
const AdminGroup = require('../../../server/models/admin-group');
const Code = require('@hapi/code');
const Config = require('../../../config');
const Fixtures = require('../fixtures');
const Lab = require('@hapi/lab');
const User = require('../../../server/models/user');
const lab = exports.lab = Lab.script();
const config = Config.get('/hapiMongoModels/mongodb');
lab.experiment('Admin Model', () => {
lab.before(async () => {
await Admin.connect(config.connection, config.options);
await Fixtures.Db.removeAllData();
});
lab.after(async () => {
await Fixtures.Db.removeAllData();
Admin.disconnect();
});
lab.test('it parses names into name fields', () => {
const justFirst = Admin.nameAdapter('Steve');
Code.expect(justFirst).to.be.an.object();
Code.expect(justFirst.first).to.equal('Steve');
Code.expect(justFirst.middle).to.equal('');
Code.expect(justFirst.last).to.equal('');
const firstAndLast = Admin.nameAdapter('Ren Höek');
Code.expect(firstAndLast).to.be.an.object();
Code.expect(firstAndLast.first).to.equal('Ren');
Code.expect(firstAndLast.middle).to.equal('');
Code.expect(firstAndLast.last).to.equal('Höek');
const withMiddle = Admin.nameAdapter('Stimpson J Cat');
Code.expect(withMiddle).to.be.an.object();
Code.expect(withMiddle.first).to.equal('Stimpson');
Code.expect(withMiddle.middle).to.equal('J');
Code.expect(withMiddle.last).to.equal('Cat');
});
lab.test('it parses returns a full name', async () => {
const admin = await Admin.create('Stan');
let name = admin.fullName();
Code.expect(name).to.equal('Stan');
admin.name = Admin.nameAdapter('Ren Höek');
name = admin.fullName();
Code.expect(name).to.equal('Ren Höek');
});
lab.test('it returns a new instance when create succeeds', async () => {
const admin = await Admin.create('Ren Höek');
Code.expect(admin).to.be.an.instanceOf(Admin);
});
lab.test('it returns an instance when finding by username', async () => {
const document = new Admin({
name: Admin.nameAdapter('Stimpson J Cat'),
user: {
id: '95EP150D35',
name: 'stimpy'
}
});
await Admin.insertOne(document);
const account = await Admin.findByUsername('stimpy');
Code.expect(account).to.be.an.instanceOf(Admin);
});
lab.test('it returns false when checking for membership when groups are missing', async () => {
const admin = await Admin.create('Ren Höek');
Code.expect(admin.isMemberOf('sales')).to.equal(false);
});
lab.test('it returns false when permissions are missing', async () => {
const admin = await Admin.create('Ren Höek');
const hasPermission = await admin.hasPermissionTo('SPACE_MADNESS');
Code.expect(hasPermission).to.equal(false);
});
lab.test('it returns boolean values when the permission exists on the admin', async () => {
const admin = new Admin({
name: Admin.nameAdapter('Ren Höek'),
permissions: {
SPACE_MADNESS: true,
UNTAMED_WORLD: false
}
});
const hasPermission = await admin.hasPermissionTo('SPACE_MADNESS');
Code.expect(hasPermission).to.equal(true);
});
lab.test('it returns boolean values when permission exits on the admin group', async () => {
// create groups
const salesGroup = new AdminGroup({
_id: 'sales',
name: 'Sales',
permissions: {
UNTAMED_WORLD: false,
WORLD_UNTAMED: true
}
});
const supportGroup = new AdminGroup({
_id: 'support',
name: 'Support',
permissions: {
SPACE_MADNESS: true,
MADNESS_SPACE: false
}
});
await AdminGroup.insertMany([salesGroup, supportGroup]);
// admin without group membership
const documentA = new Admin({
name: Admin.nameAdapter('Ren Höek')
});
const testA1 = await documentA.hasPermissionTo('SPACE_MADNESS');
Code.expect(testA1).to.equal(false);
const testA2 = await documentA.hasPermissionTo('UNTAMED_WORLD');
Code.expect(testA2).to.equal(false);
// admin with group membership
const documentB = new Admin({
name: Admin.nameAdapter('Ren B Höek'),
groups: {
sales: 'Sales',
support: 'Support'
}
});
const testB1 = await documentB.hasPermissionTo('SPACE_MADNESS');
Code.expect(testB1).to.equal(true);
const testB2 = await documentB.hasPermissionTo('UNTAMED_WORLD');
Code.expect(testB2).to.equal(false);
});
lab.test('it links and unlinks users', async () => {
let admin = await Admin.create('Guinea Pig');
const user = await User.create('guineapig', 'wheel', 'wood@chips.gov');
Code.expect(admin.user).to.not.exist();
admin = await admin.linkUser(`${user._id}`, user.username);
Code.expect(admin.user).to.be.an.object();
admin = await admin.unlinkUser();
Code.expect(admin.user).to.not.exist();
});
});