Files
threshold-ui/addons/api/tests/unit/abilities/model-test.js
T
hashicc b5d33948dc chore: 🤖 bump ember-can (#3093)
* chore: 🤖 bump ember-can

* chore: 🤖 rename "can" service (deprecated) to abilities

See https://github.com/minutebase/ember-can/pull/152

* chore: 🤖 bump to ember-can@^8

* chore: 🤖 extend resolver for ember-can v7 breaking change

https://github.com/minutebase/ember-can/pull/186
2025-12-08 10:41:38 -05:00

96 lines
3.2 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* 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' }));
});
});