Merge branch 'main' into host-catalog

This commit is contained in:
Dharini Jeeva authored and GitHub committed 2021-08-24 13:31:43 -07:00
commit 1e1387700c
15 files changed
+606 -199

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import { Ability } from 'ember-can';
/**
* Generic ability provides abilities common to all models in the API.
*/
export default class CollectionAbility extends Ability {
// =permissions
/**
* @type {boolean}
*/
get canList() {
return this.hasAuthorizedCollectionAction('list');
}
/**
* @type {boolean}
*/
get canCreate() {
return this.hasAuthorizedCollectionAction('create');
}
// =methods
/**
* Returns true if the given action is contained in the ability model's
* `authorized_collection_actions` array.
* @param {string} action
* @return {boolean}
*/
hasAuthorizedCollectionAction(action) {
const { authorized_collection_actions } = this.model;
const collection = this.collection || {};
return authorized_collection_actions[collection]?.includes(action);
}
}
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/abilities/collection';
@@ -1,10 +1,18 @@
import factory from '../generated/factories/credential-store';
import { random, internet, datatype } from 'faker';
import { trait } from 'ember-cli-mirage';
import permissions from '../helpers/permissions';
const types = ['vault'];
export default factory.extend({
authorized_actions: () =>
permissions.authorizedActionsFor('credential-store') || [
'no-op',
'read',
'update',
'delete',
],
id: (i) => `credential-store-id-${i}`,
type: (i) => types[i % types.length],
@@ -0,0 +1,38 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Abilities | Collection', function (hooks) {
setupTest(hooks);
test('it reflects when a resource may be listed based on authorized_collection_actions', function (assert) {
assert.expect(2);
const service = this.owner.lookup('service:can');
const model = {
authorized_collection_actions: {
foobars: ['list'],
},
};
assert.ok(service.can('list collection', model, { collection: 'foobars' }));
model.authorized_collection_actions.foobars = [];
assert.notOk(
service.can('list collection', model, { collection: 'foobars' })
);
});
test('it reflects when a resource may be created based on authorized_collection_actions', function (assert) {
assert.expect(2);
const service = this.owner.lookup('service:can');
const model = {
authorized_collection_actions: {
foobars: ['create'],
},
};
assert.ok(
service.can('create collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = [];
assert.notOk(
service.can('create collection', model, { collection: 'foobars' })
);
});
});