Last active
August 29, 2015 13:57
-
-
Save szmeku/9620685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
describe('RouteConfig', function () | |
{ | |
// load the service's module | |
beforeEach(module('govipAdminApp')); | |
// instantiate service | |
var RouteConfig; | |
beforeEach(inject(function (_RouteConfig_) | |
{ | |
RouteConfig = _RouteConfig_; | |
})); | |
describe('buildRoles', function () | |
{ | |
using('role declarations, roles', [ | |
[ | |
['admin', 'manager', 'anonymous'], | |
{admin: parseInt("001", 2), manager: parseInt("010", 2), anonymous: parseInt("100", 2)} | |
], | |
[ | |
['admin', 'anonymous'], | |
{admin: parseInt("001", 2), anonymous: parseInt("010", 2)} | |
] | |
], function (roleDeclarations, roles) | |
{ | |
it('should build roles', function () | |
{ | |
expect(RouteConfig.buildRoles(roleDeclarations)).toEqual(roles); | |
}); | |
}); | |
}); | |
describe('buildAccessLevels', function () | |
{ | |
using('access level declarations, roles, access levels', [ | |
[ | |
{public: '*', admin: ['admin'], user: ['user', 'admin']}, | |
{anon: parseInt('001', 2), user: parseInt('010', 2), admin: parseInt('100', 2)}, | |
{public: parseInt('1111111', 2), admin: parseInt('100', 2), user: parseInt('110', 2)} | |
] | |
], function (accessLevelDeclarations, roles, accessLevels) | |
{ | |
it('should build access levels', function () | |
{ | |
expect(RouteConfig.buildAccessLevels(accessLevelDeclarations, roles)).toEqual(accessLevels); | |
}) | |
}); | |
it('using invalid level declaration should throw error', function () | |
{ | |
expect(function () | |
{ | |
RouteConfig.buildAccessLevels({public: 'dudnik'}, {admin: '', user: '' }); | |
}).toThrow("Access Control Error: could not parse 'dudnik' as access definition for level public"); | |
}); | |
it('using invalid role in level declaration should throw error', function () | |
{ | |
expect(function () | |
{ | |
RouteConfig.buildAccessLevels({public: ['adminix']}, {admin: '' }); | |
}).toThrow("Access Control Error: could not find user role 'adminix'"); | |
}); | |
}); | |
describe('Access Control', function () | |
{ | |
var config, roles, accessLevels; | |
beforeEach(function () | |
{ | |
config = { | |
roles: ['admin', 'user', 'anonymous'], | |
accessLevels: { | |
public: '*', | |
admin: ['admin'], | |
user: ['user', 'admin'] | |
} | |
} | |
roles = RouteConfig.buildRoles(config.roles); | |
accessLevels = RouteConfig.buildAccessLevels(config.accessLevels, roles); | |
}) | |
using('authorized role, access level', [ | |
['admin', 'public'], | |
['admin', 'admin'], | |
['admin', 'user'], | |
['user', 'admin'], | |
['user', 'public'] | |
], function (role, accessLevel) | |
{ | |
it('should allow authorized roles access to access levels', function () | |
{ | |
expect(roles[role] & accessLevels[accessLevel]).toBeTruthy(); | |
}) | |
}); | |
using('unauthorized roles, access level', [ | |
['user', 'admin'], | |
['anonymous', 'admin'], | |
['anonymous', 'user'], | |
], function (role, accessLevel) | |
{ | |
it('should deny unauthorized roles access to access levels', function () | |
{ | |
expect(roles[role] & accessLevels[accessLevel]).toBeFalsy(); | |
}) | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment