2023-03-01 09:39:41 -06:00
|
|
|
/**
|
2026-01-28 17:09:06 -08:00
|
|
|
* Copyright IBM Corp. 2021, 2026
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2020-09-23 12:16:31 -04:00
|
|
|
import { module, test } from 'qunit';
|
2025-03-20 14:08:33 -05:00
|
|
|
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
|
2024-12-02 12:51:24 -06:00
|
|
|
import { setupApplicationTest } from 'admin/tests/helpers';
|
2025-08-19 14:12:46 -04:00
|
|
|
import { setupSqlite } from 'api/test-support/helpers/sqlite';
|
2020-09-23 12:16:31 -04:00
|
|
|
import { Response } from 'miragejs';
|
2025-03-20 14:08:33 -05:00
|
|
|
import * as selectors from './selectors';
|
2025-02-13 15:54:40 -05:00
|
|
|
import * as commonSelectors from 'admin/tests/helpers/selectors';
|
2020-09-23 12:16:31 -04:00
|
|
|
|
2021-08-29 22:57:16 -07:00
|
|
|
module('Acceptance | roles | create', function (hooks) {
|
2020-09-23 12:16:31 -04:00
|
|
|
setupApplicationTest(hooks);
|
2025-08-19 14:12:46 -04:00
|
|
|
setupSqlite(hooks);
|
2020-09-23 12:16:31 -04:00
|
|
|
|
2025-03-20 14:08:33 -05:00
|
|
|
let getRolesCount;
|
|
|
|
|
|
2020-09-23 12:16:31 -04:00
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
org: null,
|
2020-11-03 12:01:46 -05:00
|
|
|
project: null,
|
2020-09-23 12:16:31 -04:00
|
|
|
},
|
|
|
|
|
role: null,
|
|
|
|
|
};
|
|
|
|
|
const urls = {
|
|
|
|
|
roles: null,
|
|
|
|
|
role: null,
|
2021-09-01 14:17:00 -07:00
|
|
|
newRole: null,
|
2020-09-23 12:16:31 -04:00
|
|
|
};
|
|
|
|
|
|
2024-12-03 10:27:27 -08:00
|
|
|
hooks.beforeEach(async function () {
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org = this.server.create(
|
2021-08-29 22:57:16 -07:00
|
|
|
'scope',
|
|
|
|
|
{
|
|
|
|
|
type: 'org',
|
|
|
|
|
scope: { id: 'global', type: 'global' },
|
|
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
'withChildren',
|
2021-08-29 22:57:16 -07:00
|
|
|
);
|
|
|
|
|
|
2020-11-03 12:01:46 -05:00
|
|
|
// The project scope is not yet used for role tests (though it will be
|
|
|
|
|
// in the future). This is created simply to test the grant scope loading
|
|
|
|
|
// mechanism.
|
|
|
|
|
instances.scopes.project = this.server.create('scope', {
|
|
|
|
|
type: 'project',
|
2023-11-02 16:08:25 -07:00
|
|
|
scope: { id: instances.scopes.org.id, type: instances.scopes.org.type },
|
2020-11-03 12:01:46 -05:00
|
|
|
});
|
2020-11-16 15:08:59 -05:00
|
|
|
instances.role = this.server.create(
|
|
|
|
|
'role',
|
|
|
|
|
{
|
2023-11-02 16:08:25 -07:00
|
|
|
scope: instances.scopes.org,
|
2020-11-16 15:08:59 -05:00
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
'withPrincipals',
|
2020-11-16 15:08:59 -05:00
|
|
|
);
|
2023-11-02 16:08:25 -07:00
|
|
|
urls.roles = `/scopes/${instances.scopes.org.id}/roles`;
|
2020-09-23 12:16:31 -04:00
|
|
|
urls.role = `${urls.roles}/${instances.role.id}`;
|
2021-09-01 14:17:00 -07:00
|
|
|
urls.newRole = `${urls.roles}/new`;
|
2025-03-20 14:08:33 -05:00
|
|
|
getRolesCount = () => this.server.schema.roles.all().models.length;
|
2020-09-23 12:16:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('can create new role', async function (assert) {
|
2025-03-20 14:08:33 -05:00
|
|
|
const rolesCount = getRolesCount();
|
2021-09-01 14:17:00 -07:00
|
|
|
await visit(urls.newRole);
|
2025-03-20 14:08:33 -05:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await click(commonSelectors.SAVE_BTN);
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(getRolesCount(), rolesCount + 1);
|
2020-09-23 12:16:31 -04:00
|
|
|
});
|
|
|
|
|
|
2021-09-01 14:26:25 -07:00
|
|
|
test('Users can navigate to new roles route with proper authorization', async function (assert) {
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.roles);
|
2025-03-20 14:08:33 -05:00
|
|
|
|
2021-08-31 08:17:25 -07:00
|
|
|
assert.ok(
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.roles.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-08-31 08:17:25 -07:00
|
|
|
);
|
2025-03-20 14:08:33 -05:00
|
|
|
assert.dom(selectors.NEW_ROLE_BTN).isVisible();
|
2025-04-14 14:27:02 -07:00
|
|
|
assert.dom(commonSelectors.SIDEBAR_NAV_LINK(urls.roles)).isVisible();
|
2025-03-20 14:08:33 -05:00
|
|
|
assert.dom(commonSelectors.HREF(urls.newRole)).isVisible();
|
2021-08-29 22:57:16 -07:00
|
|
|
});
|
|
|
|
|
|
2021-09-01 14:26:25 -07:00
|
|
|
test('Users cannot navigate to new roles route without proper authorization', async function (assert) {
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.roles = [];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.roles);
|
2025-03-20 14:08:33 -05:00
|
|
|
|
2021-08-29 22:57:16 -07:00
|
|
|
assert.notOk(
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.roles.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-08-29 22:57:16 -07:00
|
|
|
);
|
2025-03-20 14:08:33 -05:00
|
|
|
assert.dom(selectors.NEW_ROLE_BTN).doesNotExist();
|
2025-04-14 14:27:02 -07:00
|
|
|
assert.dom(commonSelectors.SIDEBAR_NAV_LINK(urls.roles)).doesNotExist();
|
2025-03-20 14:08:33 -05:00
|
|
|
assert.dom(commonSelectors.HREF(urls.newRole)).doesNotExist();
|
2021-08-29 22:57:16 -07:00
|
|
|
});
|
|
|
|
|
|
2020-09-23 12:16:31 -04:00
|
|
|
test('can cancel new role creation', async function (assert) {
|
2025-03-20 14:08:33 -05:00
|
|
|
const rolesCount = getRolesCount();
|
2021-09-01 14:17:00 -07:00
|
|
|
await visit(urls.newRole);
|
2025-03-20 14:08:33 -05:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await click(commonSelectors.CANCEL_BTN);
|
|
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.roles);
|
2025-03-20 14:08:33 -05:00
|
|
|
assert.strictEqual(getRolesCount(), rolesCount);
|
2020-09-23 12:16:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('saving a new role with invalid fields displays error messages', async function (assert) {
|
2025-03-20 14:08:33 -05:00
|
|
|
const errorMsg =
|
|
|
|
|
'Invalid request. Request attempted to make second resource with the same field value that must be unique.';
|
2020-09-23 12:16:31 -04:00
|
|
|
this.server.post('/roles', () => {
|
|
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'invalid_argument',
|
2025-03-20 14:08:33 -05:00
|
|
|
message: errorMsg,
|
2020-09-23 12:16:31 -04:00
|
|
|
details: {
|
|
|
|
|
request_fields: [
|
|
|
|
|
{
|
|
|
|
|
name: 'name',
|
2025-03-20 14:08:33 -05:00
|
|
|
description: errorMsg,
|
2020-09-23 12:16:31 -04:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
},
|
2020-09-23 12:16:31 -04:00
|
|
|
);
|
|
|
|
|
});
|
2021-09-01 14:17:00 -07:00
|
|
|
await visit(urls.newRole);
|
2025-03-20 14:08:33 -05:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await click(commonSelectors.SAVE_BTN);
|
|
|
|
|
|
|
|
|
|
assert.dom(commonSelectors.ALERT_TOAST_BODY).hasText(errorMsg);
|
2020-09-23 12:16:31 -04:00
|
|
|
});
|
2023-11-02 16:08:25 -07:00
|
|
|
|
|
|
|
|
test('users cannot directly navigate to new role route without proper authorization', async function (assert) {
|
|
|
|
|
instances.scopes.org.authorized_collection_actions.roles =
|
|
|
|
|
instances.scopes.org.authorized_collection_actions.roles.filter(
|
2023-11-22 14:07:41 -05:00
|
|
|
(item) => item !== 'create',
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await visit(urls.newRole);
|
|
|
|
|
|
|
|
|
|
assert.false(
|
|
|
|
|
instances.scopes.org.authorized_collection_actions.roles.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
assert.strictEqual(currentURL(), urls.roles);
|
|
|
|
|
});
|
2020-09-23 12:16:31 -04:00
|
|
|
});
|