Files
threshold-ui/addons/api/tests/unit/abilities/model-test.js
T
hashiccandLisbet Alvarez 3a847605ed Reapply ember-can upgrade (#3111)
* chore: 🤖 reapply previous ember-can upgrade from #3093

* chore: 🤖 add missing ember-inflector dependency

* chore: 🤖 move @ember/string to be a dependency in desktop

* chore: 🤖 re-run pnpm install to resolve lockfile dupe key error

* chore: 🤖 remove unused abilities service injections

Co-Authored-By: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com>

---------

Co-authored-by: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com>
2026-02-24 13:48:34 -05:00

96 lines
3.2 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Abilities | Model', function (hooks) {
setupTest(hooks);
test('it reflects when a given model may be read based on authorized_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
const model = {
authorized_actions: ['read'],
};
assert.ok(service.can('read model', model));
model.authorized_actions = [];
assert.notOk(service.can('read model', model));
});
test('it reflects when a given model may be updated based on authorized_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
const model = {
authorized_actions: ['update'],
};
assert.ok(service.can('update model', model));
model.authorized_actions = [];
assert.notOk(service.can('update model', model));
});
test('it reflects when a given model may be saved based on isNew and authorized_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
let model = {
isNew: false,
authorized_actions: ['update'],
};
assert.ok(service.can('save model', model));
model = {
isNew: false,
authorized_actions: [],
};
assert.notOk(service.can('save model', model));
// Model instances are always saveable at this level when isNew is true.
// In practice, the ability to _create_ a resource depends not on instance
// authorization, but on collection authorization, which is
// tested separately.
model = {
isNew: true,
authorized_actions: ['update'],
};
assert.ok(service.can('save model', model));
model = {
isNew: true,
authorized_actions: [],
};
assert.ok(service.can('save model', model));
});
test('it reflects when a given model may be deleted based on authorized_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
const model = {
authorized_actions: ['delete'],
};
assert.ok(service.can('delete model', model));
model.authorized_actions = [];
assert.notOk(service.can('delete model', model));
});
// =collections
test('it reflects when a resource may be listed based on authorized_collection_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
const model = {
authorized_collection_actions: {
foobars: ['list'],
},
};
assert.ok(service.can('list model', model, { collection: 'foobars' }));
model.authorized_collection_actions.foobars = [];
assert.notOk(service.can('list model', model, { collection: 'foobars' }));
});
test('it reflects when a resource may be created based on authorized_collection_actions', function (assert) {
const service = this.owner.lookup('service:abilities');
const model = {
authorized_collection_actions: {
foobars: ['create'],
},
};
assert.ok(service.can('create model', model, { collection: 'foobars' }));
model.authorized_collection_actions.foobars = [];
assert.notOk(service.can('create model', model, { collection: 'foobars' }));
});
});