Files
threshold-ui/addons/api/tests/unit/abilities/model-test.js
T

95 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
* 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) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2021-08-05 13:21:03 -04:00
const model = {
authorized_actions: ['read'],
2021-08-05 13:21:03 -04:00
};
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) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2021-08-05 13:21:03 -04:00
const model = {
authorized_actions: ['update'],
2021-08-05 13:21:03 -04:00
};
assert.ok(service.can('update model', model));
model.authorized_actions = [];
assert.notOk(service.can('update model', model));
});
2021-08-05 13:21:03 -04:00
test('it reflects when a given model may be saved based on isNew and authorized_actions', function (assert) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2021-08-05 13:21:03 -04:00
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) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2021-08-05 13:21:03 -04:00
const model = {
authorized_actions: ['delete'],
2021-08-05 13:21:03 -04:00
};
assert.ok(service.can('delete model', model));
model.authorized_actions = [];
assert.notOk(service.can('delete model', model));
});
2022-01-31 12:02:37 -05:00
// =collections
test('it reflects when a resource may be listed based on authorized_collection_actions', function (assert) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2022-01-31 12:02:37 -05:00
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) {
2025-12-08 18:20:55 -05:00
const service = this.owner.lookup('service:can');
2022-01-31 12:02:37 -05:00
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' }));
});
});