/
roles.test.js
309 lines (256 loc) · 10 KB
/
roles.test.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict';
const
rewire = require('rewire'),
should = require('should'),
Bluebird = require('bluebird'),
sinon = require('sinon'),
KuzzleMock = require('../../../mocks/kuzzle.mock'),
{
Request,
errors: {
BadRequestError,
SizeLimitError
}
} = require('kuzzle-common-objects'),
SecurityController = rewire('../../../../lib/api/controllers/security');
describe('Test: security controller - roles', () => {
let
kuzzle,
request,
securityController;
beforeEach(() => {
kuzzle = new KuzzleMock();
securityController = new SecurityController(kuzzle);
request = new Request({controller: 'security'});
kuzzle.internalIndex.get.resolves({});
kuzzle.internalIndex.getMapping.resolves({internalIndex: {mappings: {roles: {properties: {}}}}});
});
describe('#updateRoleMapping', () => {
const foo = {foo: 'bar'};
it('should throw a BadRequestError if the body is missing', () => {
return should(() => {
securityController.updateRoleMapping(request);
}).throw(BadRequestError, { id: 'api.assert.body_required' });
});
it('should update the role mapping', () => {
request.input.body = foo;
kuzzle.internalIndex.updateMapping.resolves(foo);
return securityController.updateRoleMapping(request)
.then(response => {
should(kuzzle.internalIndex.updateMapping).be.calledOnce();
should(kuzzle.internalIndex.updateMapping).be.calledWith('roles', request.input.body);
should(response).be.instanceof(Object);
should(response).match(foo);
});
});
});
describe('#getRoleMapping', () => {
it('should fulfill with a response object', () => {
kuzzle.internalIndex.getMapping.resolves({ properties: { foo: 'bar' } });
return securityController.getRoleMapping(request)
.then(response => {
should(kuzzle.internalIndex.getMapping)
.be.calledOnce()
.be.calledWith('roles');
should(response).be.instanceof(Object);
should(response).match({ mapping: { foo: 'bar' } });
});
});
});
describe('#createOrReplaceRole', () => {
it('should resolve to an object on a createOrReplaceRole call', () => {
kuzzle.repositories.role.validateAndSaveRole.resolves({_id: 'test'});
return securityController.createOrReplaceRole(new Request({_id: 'test', body: {controllers: {}}}))
.then(response => {
should(response).be.instanceof(Object);
should(response._id).be.exactly('test');
});
});
it('should reject an error in case of error', () => {
kuzzle.repositories.role.validateAndSaveRole.rejects(new Error('Mocked error'));
return should(securityController.createOrReplaceRole(new Request({_id: 'alreadyExists', body: {indexes: {}}})))
.be.rejectedWith(new Error('Mocked error'));
});
it('should forward refresh option', () => {
kuzzle.repositories.role.validateAndSaveRole.resolves({_id: 'test'});
return securityController.createOrReplaceRole(new Request({
_id: 'test',
body: {
controllers: {}
},
refresh: 'wait_for'
}))
.then(() => {
const options = kuzzle.repositories.role.validateAndSaveRole.firstCall.args[1];
should(options).match({
refresh: 'wait_for'
});
});
});
});
describe('#createRole', () => {
it('should resolve to an object on a createRole call', () => {
kuzzle.repositories.role.validateAndSaveRole.resolves({_id: 'test'});
return should(securityController.createRole(new Request({_id: 'test', body: {controllers: {}}})))
.be.fulfilled();
});
});
describe('#getRole', () => {
it('should resolve to an object on a getRole call', () => {
kuzzle.repositories.role.load.resolves({
_id: 'test'
});
return securityController.getRole(new Request({_id: 'test'}))
.then(response => {
should(response).be.instanceof(Object);
should(response._id).be.exactly('test');
});
});
it('should reject NotFoundError on a getRole call with a bad id', () => {
kuzzle.repositories.role.load.resolves(null);
return should(securityController.getRole(new Request({_id: 'badId'}))).be.rejected();
});
});
describe('#mGetRoles', () => {
it('should throw an error if no ids is provided', () => {
return should(() => {
securityController.mGetRoles(new Request({body: {}}));
}).throw(BadRequestError, { id: 'api.assert.missing_argument' });
});
it('should reject an error if loading roles fails', () => {
kuzzle.repositories.role.loadMultiFromDatabase.rejects(new Error('foobar'));
return should(securityController.mGetRoles(new Request({body: {ids: ['test']}}))).be.rejected();
});
it('should resolve to an object', () => {
kuzzle.repositories.role.loadMultiFromDatabase.resolves([
{ _id: 'test', _source: null }
]);
return securityController.mGetRoles(new Request({body: {ids: ['test']}}))
.then(response => {
should(response).be.instanceof(Object);
should(response.hits).be.an.Array();
should(response.hits).not.be.empty();
});
});
});
describe('#searchRoles', () => {
it('should return response with an array of roles on searchRole call', () => {
kuzzle.repositories.role.searchRole.resolves({
hits: [{_id: 'test'}],
total: 1
});
return securityController.searchRoles(new Request({body: {controllers: ['foo', 'bar']}}))
.then(response => {
should(response).be.instanceof(Object);
should(response.hits).be.an.Array();
should(response.hits[0]._id).be.exactly('test');
should(kuzzle.repositories.role.searchRole)
.be.calledOnce()
.be.calledWith(['foo', 'bar']);
});
});
it('should throw an error if the number of documents per page exceeds server limits', () => {
kuzzle.config.limits.documentsFetchCount = 1;
request = new Request({body: {policies: ['role1']}});
request.input.args.from = 0;
request.input.args.size = 10;
return should(() => securityController.searchRoles(request))
.throw(SizeLimitError, { id: 'services.storage.get_limit_exceeded' });
});
it('should reject an error in case of error', () => {
kuzzle.repositories.role.searchRole.rejects(new Error());
return should(securityController.searchRoles(new Request({body: {controllers: ['foo', 'bar']}}))).be.rejected();
});
});
describe('#updateRole', () => {
it('should return a valid response', () => {
kuzzle.repositories.role.load.resolves({_id: 'test'});
kuzzle.repositories.role.roles = [];
kuzzle.repositories.role.validateAndSaveRole = role => {
if (role._id === 'alreadyExists') {
return Bluebird.reject();
}
return Bluebird.resolve(role);
};
return securityController.updateRole(new Request({_id: 'test', body: {foo: 'bar'}}))
.then(response => {
should(response).be.instanceof(Object);
should(response._id).be.exactly('test');
});
});
it('should throw an error if no id is given', () => {
return should(() => {
securityController.updateRole(new Request({body: {}}));
}).throw(BadRequestError, { id: 'api.assert.missing_argument' });
});
it('should reject the promise if the role cannot be found in the database', () => {
kuzzle.repositories.role.load.resolves(null);
return should(securityController.updateRole(new Request({_id: 'badId', body: {}, context: {action: 'updateRole'}}))).be.rejected();
});
it('should forward refresh option', () => {
kuzzle.repositories.role.load.resolves({_id: 'test'});
kuzzle.repositories.role.roles = [];
kuzzle.repositories.role.validateAndSaveRole = sinon.stub().returnsArg(0);
return securityController.updateRole(new Request({
_id: 'test',
body: {
foo: 'bar'
},
refresh: 'wait_for'
}))
.then(() => {
const options = kuzzle.repositories.role.validateAndSaveRole.firstCall.args[1];
should(options).match({
refresh: 'wait_for'
});
});
});
});
describe('#deleteRole', () => {
it('should return response with on deleteRole call', done => {
const role = {_id: 'role'};
kuzzle.repositories.role.load.resolves(role);
kuzzle.repositories.role.delete.resolves();
securityController.deleteRole(new Request({_id: 'test',body: {}}))
.then(() => {
should(kuzzle.repositories.role.delete.calledWith(role)).be.true();
done();
});
});
it('should reject the promise if attempting to delete one of the core roles', () => {
kuzzle.repositories.role.delete
.rejects(new Error('admin is one of the basic roles of Kuzzle, you cannot delete it, but you can edit it.'));
return should(securityController.deleteRole(new Request({_id: 'admin',body: {}}))).be.rejected();
});
it('should forward refresh option', () => {
const role = {_id: 'role'};
kuzzle.repositories.role.load.resolves(role);
kuzzle.repositories.role.getRoleFromRequest.resolves(role);
kuzzle.repositories.role.delete.resolves();
return securityController.deleteRole(new Request({
_id: 'test',
body: {},
refresh: 'wait_for'
}))
.then(() => {
const options = kuzzle.repositories.role.delete.firstCall.args[1];
should(options).match({
refresh: 'wait_for'
});
});
});
});
describe('#mDeleteRoles', () => {
it('should forward its args to mDelete', () => {
const spy = sinon.spy();
SecurityController.__with__({
mDelete: spy
})(() => {
securityController.mDeleteRoles(request);
should(spy)
.be.calledOnce()
.be.calledWith(kuzzle, 'role', request);
});
});
});
});