Files
threshold-ui/addons/api/tests/unit/abilities/session-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

169 lines
5.2 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
STATUS_SESSION_ACTIVE,
STATUS_SESSION_PENDING,
STATUS_SESSION_CANCELING,
STATUS_SESSION_TERMINATED,
} from 'api/models/session';
module('Unit | Ability | session', function (hooks) {
setupTest(hooks);
let abilitiesService;
let store;
hooks.beforeEach(function () {
abilitiesService = this.owner.lookup('service:abilities');
store = this.owner.lookup('service:store');
});
test('it exists', function (assert) {
const ability = this.owner.lookup('ability:session');
assert.ok(ability);
});
test('can read a session that is active', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['read'],
status: STATUS_SESSION_ACTIVE,
});
assert.true(abilitiesService.can('read session', session));
});
test('can read a session that is pending', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['read'],
status: STATUS_SESSION_PENDING,
});
assert.true(abilitiesService.can('read session', session));
});
test('cannot read a session that is not authorized', function (assert) {
const activeSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_ACTIVE,
});
const pendingSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_PENDING,
});
const cancelingSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_CANCELING,
});
const terminatedSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_TERMINATED,
});
assert.false(abilitiesService.can('read session', activeSession));
assert.false(abilitiesService.can('read session', pendingSession));
assert.false(abilitiesService.can('read session', cancelingSession));
assert.false(abilitiesService.can('read session', terminatedSession));
});
test('cannot read a session that is canceling', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['read'],
status: STATUS_SESSION_CANCELING,
});
assert.false(abilitiesService.can('read session', session));
});
test('cannot read a session that is terminated', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['read'],
status: STATUS_SESSION_TERMINATED,
});
assert.false(abilitiesService.can('read session', session));
});
test('can read a session that has read:self', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['read:self'],
status: STATUS_SESSION_ACTIVE,
});
assert.true(abilitiesService.can('read session', session));
});
test('can cancel a session that is active', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['cancel'],
status: STATUS_SESSION_ACTIVE,
});
assert.true(abilitiesService.can('cancel session', session));
});
test('can cancel a session that has cancel:self', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['cancel:self'],
status: STATUS_SESSION_ACTIVE,
});
assert.true(abilitiesService.can('cancel session', session));
});
test('can cancel a session that is pending', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['cancel'],
status: STATUS_SESSION_PENDING,
});
assert.true(abilitiesService.can('cancel session', session));
});
test('cannot cancel a session that is canceling', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['cancel'],
status: STATUS_SESSION_CANCELING,
});
assert.false(abilitiesService.can('cancel session', session));
});
test('cannot cancel a session that is terminated', function (assert) {
const session = store.createRecord('session', {
authorized_actions: ['cancel'],
status: STATUS_SESSION_TERMINATED,
});
assert.false(abilitiesService.can('cancel session', session));
});
test('cannot cancel a session that is not authorized', function (assert) {
const activeSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_ACTIVE,
});
const pendingSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_PENDING,
});
const cancelingSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_CANCELING,
});
const terminatedSession = store.createRecord('session', {
authorized_actions: [],
status: STATUS_SESSION_TERMINATED,
});
assert.false(abilitiesService.can('cancel session', activeSession));
assert.false(abilitiesService.can('cancel session', pendingSession));
assert.false(abilitiesService.can('cancel session', cancelingSession));
assert.false(abilitiesService.can('cancel session', terminatedSession));
});
});