Repository navigation
Expand file tree
/
Copy pathmanager.spec.js
More file actions
411 lines (333 loc) · 11.2 KB
/
Copy pathmanager.spec.js
File metadata and controls
411 lines (333 loc) · 11.2 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var EventEmitter = require('events').EventEmitter;
var chai = require('chai');
var authorized = require('../../lib/authorized');
var ConfigError = authorized.ConfigError;
var Manager = authorized.Manager;
var Role = authorized.Role;
var UnauthorizedError = authorized.UnauthorizedError;
/** @type {boolean} */
chai.Assertion.includeStack = true;
var assert = chai.assert;
describe('Manager', function() {
describe('constructor', function() {
it('creates a new authorization manager with the defaults', function() {
var auth = new Manager();
assert.instanceOf(auth, Manager);
assert.isTrue(auth.options.pauseStream);
});
it('allows options to be set', function() {
var auth = new Manager({pauseStream: false});
assert.instanceOf(auth, Manager);
assert.isFalse(auth.options.pauseStream);
});
it('instance exported by main module', function() {
var auth = require('../../lib/authorized');
assert.instanceOf(auth, Manager);
});
});
describe('#action()', function() {
var auth;
beforeEach(function() {
auth = new Manager();
});
it('defines roles required for specific actions', function() {
auth.role('admin', function(req, done) {
// pretend everybody is admin
done(null, true);
});
assert.doesNotThrow(function() {
auth.action('can view passwords', ['admin']);
});
});
it('allows multiple roles to perform an action', function() {
auth.role('admin', function(req, done) {
// pretend ~1/2 users are admin
done(null, Math.random() > 0.5);
});
auth.role('page.author', function(page, req, done) {
// pretend everybody is author
done(null, true);
});
auth.entity('page', function(req, done) {
// mock page
done(null, {});
});
assert.doesNotThrow(function() {
auth.action('can edit page', ['admin', 'page.author']);
});
});
it('accepts a single role string instead of an array', function() {
auth.role('admin', function(req, done) {
// pretend everybody is admin
done(null, true);
});
assert.doesNotThrow(function() {
auth.action('can view passwords', 'admin');
});
});
it('does not accept an action with no roles', function() {
assert.throws(function() {
auth.action('can do nothing', []);
});
});
it('requires that all roles have been defined', function() {
auth.role('admin', function(req, done) {
// pretend everybody is admin
done(null, true);
});
assert.throws(function() {
auth.action('can view passwords', ['admin', 'foo']);
});
});
it('requires that all entities have been defined', function() {
auth.role('admin', function(req, done) {
// pretend nobody is admin
done(null, false);
});
auth.role('page.author', function(page, req, done) {
// pretend everybody is author
done(null, true);
});
assert.throws(function() {
auth.action('can edit page', ['admin', 'page.author']);
});
});
});
describe('#can()', function() {
var auth = new Manager();
var organizations = {
'org.1': {
owners: ['user.1']
}
};
auth.entity('organization', function(req, done) {
// assume url like /:orgId
var orgId = req.url.substring(1);
// pretend we're going to the db for the organization
process.nextTick(function() {
done(null, organizations[orgId]);
});
});
auth.role('organization.owner', function(org, req, done) {
if (!req.user) {
done();
} else {
done(null, ~org.owners.indexOf(req.user.id));
}
});
auth.role('admin', function(req, done) {
done(null, req.user && req.user.admin);
});
auth.action('add members to organization', ['admin', 'organization.owner']);
auth.action('delete organization', ['admin']);
it('creates a middleware function', function() {
var middleware = auth.can('add members to organization');
assert.isFunction(middleware, 'is a function');
assert.lengthOf(middleware, 3, 'takes three args');
});
it('calls next with no args if action is allowed', function(done) {
var middleware = auth.can('add members to organization');
var req = new EventEmitter();
req.url = '/org.1';
req.user = {
id: 'user.1'
};
middleware(req, {}, function(err) {
assert.lengthOf(arguments, 0, 'next called with no arguments');
var view = auth.view(req);
assert.strictEqual(view.entities.organization, organizations['org.1'],
'got organization');
assert.strictEqual(view.roles['organization.owner'], true,
'is organization.owner');
assert.strictEqual(view.roles.admin, false,
'is not admin');
assert.strictEqual(view.actions['add members to organization'], true,
'can add members to organization');
done();
});
});
it('calls next if any of the actions can be performed', function(done) {
var middleware = auth.can(
'add members to organization', 'delete organization');
var req = new EventEmitter();
req.url = '/org.1';
req.user = {
admin: true
};
middleware(req, {}, function(err) {
assert.lengthOf(arguments, 0, 'next called with no arguments');
var view = auth.view(req);
assert.strictEqual(view.entities.organization, organizations['org.1'],
'got organization');
assert.strictEqual(view.roles['organization.owner'], false,
'is organization.owner');
assert.strictEqual(view.roles.admin, true,
'is not admin');
assert.strictEqual(view.actions['add members to organization'], true,
'can add members to organization');
assert.strictEqual(view.actions['delete organization'], true,
'can delete organization');
done();
});
});
it('adds all actions to the view', function(done) {
var middleware = auth.can(
'add members to organization', 'delete organization');
var req = new EventEmitter();
req.url = '/org.1';
req.user = {
id: 'user.1'
};
middleware(req, {}, function(err) {
assert.lengthOf(arguments, 0, 'next called with no arguments');
var view = auth.view(req);
assert.strictEqual(view.entities.organization, organizations['org.1'],
'got organization');
assert.strictEqual(view.roles['organization.owner'], true,
'is organization.owner');
assert.strictEqual(view.roles.admin, false,
'is not admin');
assert.strictEqual(view.actions['add members to organization'], true,
'can add members to organization');
assert.strictEqual(view.actions['delete organization'], false,
'can delete organization');
done();
});
});
it('calls next error if action is not allowed', function(done) {
var middleware = auth.can('add members to organization');
var req = new EventEmitter();
req.url = '/org.1';
req.user = {
id: 'user.2'
};
middleware(req, {}, function(err) {
assert.lengthOf(arguments, 1, 'next called with one argument');
assert.instanceOf(err, UnauthorizedError, 'UnauthorizedError');
var view = auth.view(req);
assert.strictEqual(view.entities.organization, organizations['org.1'],
'got organization');
assert.strictEqual(view.roles['organization.owner'], false,
'is organization.owner');
assert.strictEqual(view.roles.admin, false,
'is not admin');
assert.strictEqual(view.actions['add members to organization'], false,
'can add members to organization');
done();
});
});
it('requires that an action has been defined', function() {
assert.throws(function() {
auth.can('do things we know nothing about');
});
});
});
describe('#entity()', function() {
var auth;
beforeEach(function() {
auth = new Manager();
});
it('registers an entity getter', function() {
function getter(req, done) {
done(null, {});
}
assert.doesNotThrow(function() {
auth.entity('page', getter);
});
});
it('throws ConfigError if type is not a string', function() {
function getter(req, done) {
done(null, {});
}
assert.throws(function() {
auth.entity(10, getter);
}, ConfigError);
});
it('throws ConfigError if getter is not a function', function() {
assert.throws(function() {auth.entity('page', 12);}, ConfigError);
});
it('throws ConfigError if role getter arity is not 2', function() {
function getter(err) {
return;
}
assert.throws(function() {
auth.entity('page', getter);
}, ConfigError);
});
});
describe('#role()', function() {
var auth;
beforeEach(function() {
auth = new Manager();
});
it('registers a role getter', function() {
function getter(req, done) {
return done(null, true);
}
assert.doesNotThrow(function() {
auth.role('admin', getter);
});
});
it('accepts a Role instance', function() {
function getter(req, done) {
return done(null, true);
}
assert.doesNotThrow(function() {
auth.role(new Role('admin'), getter);
});
});
it('expects a getter of arity 2 for simple roles', function() {
function getter(req, done) {
return done(null, true);
}
assert.doesNotThrow(function() {
auth.role('admin', getter);
});
});
it('expects a getter of arity 3 for entity roles', function() {
function getter(organization, req, done) {
return done(null, true);
}
assert.doesNotThrow(function() {
auth.role('organization.admin', getter);
});
});
it('throws ConfigError if role is not a string or Role', function() {
function getter(req, done) {
return done(null, true);
}
assert.throws(function() {
auth.role(10, getter);
}, ConfigError);
});
it('throws ConfigError if role cannot be created from string', function() {
function getter(req, done) {
return done(null, true);
}
assert.throws(function() {
auth.role('foo.bar.bam', getter);
}, ConfigError);
});
it('throws ConfigError if getter is not a function', function() {
assert.throws(function() {
auth.role('admin', 12);
}, ConfigError);
});
it('throws ConfigError if simple role getter arity is not 2', function() {
function getter(err) {
return;
}
assert.throws(function() {
auth.role('adin', getter);
}, ConfigError);
});
it('throws ConfigError if entity role getter arity is not 2', function() {
function getter(err, done) {
return;
}
assert.throws(function() {
auth.role('page.author', getter);
}, ConfigError);
});
});
});