2019-01-18 22:04:40 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
import Pretender from 'pretender';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
|
|
|
|
const PERMISSIONS_RESPONSE = {
|
|
|
|
data: {
|
|
|
|
exact_paths: {
|
|
|
|
foo: {
|
|
|
|
capabilities: ['read'],
|
|
|
|
},
|
|
|
|
'bar/bee': {
|
2019-01-28 22:49:25 +00:00
|
|
|
capabilities: ['create', 'list'],
|
2019-01-18 22:04:40 +00:00
|
|
|
},
|
|
|
|
boo: {
|
|
|
|
capabilities: ['deny'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
glob_paths: {
|
|
|
|
'baz/biz': {
|
|
|
|
capabilities: ['read'],
|
|
|
|
},
|
2019-03-01 21:16:53 +00:00
|
|
|
'ends/in/slash/': {
|
|
|
|
capabilities: ['list'],
|
|
|
|
},
|
2019-01-18 22:04:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Unit | Service | permissions', function (hooks) {
|
2019-01-18 22:04:40 +00:00
|
|
|
setupTest(hooks);
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.beforeEach(function () {
|
2019-01-18 22:04:40 +00:00
|
|
|
this.server = new Pretender();
|
|
|
|
this.server.get('/v1/sys/internal/ui/resultant-acl', () => {
|
|
|
|
return [200, { 'Content-Type': 'application/json' }, JSON.stringify(PERMISSIONS_RESPONSE)];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
hooks.afterEach(function () {
|
2019-01-18 22:04:40 +00:00
|
|
|
this.server.shutdown();
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('sets paths properly', async function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
await service.getPaths.perform();
|
|
|
|
assert.deepEqual(service.get('exactPaths'), PERMISSIONS_RESPONSE.data.exact_paths);
|
|
|
|
assert.deepEqual(service.get('globPaths'), PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('returns true if a policy includes access to an exact path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
|
|
|
|
assert.equal(service.hasPermission('foo'), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('returns true if a paths prefix is included in the policys exact paths', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
|
|
|
|
assert.equal(service.hasPermission('bar'), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns true if a policy includes access to a glob path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
assert.equal(service.hasPermission('baz/biz/hi'), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns true if a policy includes access to the * glob path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
const splatPath = { '': {} };
|
|
|
|
service.set('globPaths', splatPath);
|
|
|
|
assert.equal(service.hasPermission('hi'), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns false if the matched path includes the deny capability', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
assert.equal(service.hasPermission('boo'), false);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns true if passed path does not end in a slash but globPath does', function (assert) {
|
2019-03-01 21:16:53 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
assert.equal(service.hasPermission('ends/in/slash'), true, 'matches without slash');
|
|
|
|
assert.equal(service.hasPermission('ends/in/slash/'), true, 'matches with slash');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns false if a policy does not includes access to a path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
assert.equal(service.hasPermission('danger'), false);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('sets the root token', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.setPaths({ data: { root: true } });
|
|
|
|
assert.equal(service.canViewAll, true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('returns true with the root token', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('canViewAll', true);
|
|
|
|
assert.equal(service.hasPermission('hi'), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns true if a policy has the specified capabilities on a path', function (assert) {
|
2019-01-28 22:49:25 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
|
|
|
|
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
assert.equal(service.hasPermission('bar/bee', ['create', 'list']), true);
|
|
|
|
assert.equal(service.hasPermission('baz/biz', ['read']), true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns false if a policy does not have the specified capabilities on a path', function (assert) {
|
2019-01-28 22:49:25 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
|
|
|
|
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
|
|
|
|
assert.equal(service.hasPermission('bar/bee', ['create', 'delete']), false);
|
|
|
|
assert.equal(service.hasPermission('foo', ['create']), false);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('defaults to show all items when policy cannot be found', async function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
this.server.get('/v1/sys/internal/ui/resultant-acl', () => {
|
|
|
|
return [403, { 'Content-Type': 'application/json' }];
|
|
|
|
});
|
|
|
|
await service.getPaths.perform();
|
|
|
|
assert.equal(service.canViewAll, true);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('returns the first allowed nav route for policies', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
const policyPaths = {
|
|
|
|
'sys/policies/acl': {
|
|
|
|
capabilities: ['deny'],
|
|
|
|
},
|
|
|
|
'sys/policies/rgp': {
|
|
|
|
capabilities: ['read'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
service.set('exactPaths', policyPaths);
|
|
|
|
assert.equal(service.navPathParams('policies'), 'rgp');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('returns the first allowed nav route for access', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
const accessPaths = {
|
|
|
|
'sys/auth': {
|
|
|
|
capabilities: ['deny'],
|
|
|
|
},
|
2019-07-18 21:24:30 +00:00
|
|
|
'identity/entity/id': {
|
2019-01-18 22:04:40 +00:00
|
|
|
capabilities: ['read'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const expected = ['vault.cluster.access.identity', 'entities'];
|
|
|
|
service.set('exactPaths', accessPaths);
|
|
|
|
assert.deepEqual(service.navPathParams('access'), expected);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('hasNavPermission returns true if a policy includes the required capabilities for at least one path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
const accessPaths = {
|
|
|
|
'sys/auth': {
|
|
|
|
capabilities: ['deny'],
|
|
|
|
},
|
2019-10-30 18:39:51 +00:00
|
|
|
'identity/group/id': {
|
|
|
|
capabilities: ['list', 'read'],
|
2019-01-18 22:04:40 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
service.set('exactPaths', accessPaths);
|
2019-10-30 18:39:51 +00:00
|
|
|
assert.equal(service.hasNavPermission('access', 'groups'), true);
|
2019-01-18 22:04:40 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('hasNavPermission returns false if a policy does not include the required capabilities for at least one path', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
let service = this.owner.lookup('service:permissions');
|
2019-10-30 18:39:51 +00:00
|
|
|
const accessPaths = {
|
|
|
|
'sys/auth': {
|
|
|
|
capabilities: ['deny'],
|
|
|
|
},
|
|
|
|
'identity/group/id': {
|
|
|
|
capabilities: ['read'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
service.set('exactPaths', accessPaths);
|
|
|
|
assert.equal(service.hasNavPermission('access', 'groups'), false);
|
2019-01-18 22:04:40 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('appends the namespace to the path if there is one', function (assert) {
|
2019-01-18 22:04:40 +00:00
|
|
|
const namespaceService = Service.extend({
|
|
|
|
path: 'marketing',
|
|
|
|
});
|
|
|
|
this.owner.register('service:namespace', namespaceService);
|
|
|
|
let service = this.owner.lookup('service:permissions');
|
|
|
|
assert.equal(service.pathNameWithNamespace('sys/auth'), 'marketing/sys/auth');
|
|
|
|
});
|
|
|
|
});
|