Files
threshold-ui/ui/admin/tests/acceptance/roles/read-test.js
T
Carlos Alcaide Corvo b64f1876dc Refactor icu 15872 await authenticatesession admin (#2603)
* Add await for all authenticateSession within /tests/acceptance

* Add await for all authenticateSession within /tests/unit
2024-12-03 10:27:27 -08:00

114 lines
3.6 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { visit, currentURL, click, find } from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { authenticateSession } from 'ember-simple-auth/test-support';
module('Acceptance | roles | read', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
const instances = {
scopes: {
global: null,
org: null,
project: null,
},
role: null,
};
const urls = {
orgScope: null,
roles: null,
role: null,
newRole: null,
};
hooks.beforeEach(async function () {
await authenticateSession({ username: 'admin' });
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
// 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',
scope: { id: instances.scopes.org.id, type: instances.scopes.org.type },
});
instances.role = this.server.create(
'role',
{
scope: instances.scopes.org,
},
'withPrincipals',
);
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
urls.roles = `${urls.orgScope}/roles`;
urls.role = `${urls.roles}/${instances.role.id}`;
urls.newRole = `${urls.roles}/new`;
});
test('visiting roles', async function (assert) {
await visit(urls.roles);
await a11yAudit();
assert.strictEqual(currentURL(), urls.roles);
});
test('can navigate to a role form', async function (assert) {
await visit(urls.orgScope);
await click(`[href="${urls.roles}"]`);
await click('main tbody .hds-table__tr:nth-child(1) a');
await a11yAudit();
assert.strictEqual(currentURL(), urls.role);
});
test('cannot navigate to a role form without proper authorization', async function (assert) {
instances.role.authorized_actions =
instances.role.authorized_actions.filter((item) => item !== 'read');
await visit(urls.roles);
assert.notOk(find('main tbody .hds-table__tr:nth-child(1) a'));
});
test('can navigate to a roles and fetches scopes correctly', async function (assert) {
this.server.get('/scopes/:id', ({ scopes }, { params: { id } }) => {
const scope = scopes.find(id);
const response = scope.type === 'project' ? new Response(400) : scope;
return response;
});
await visit(urls.roles);
await click('main tbody .hds-table__tr:nth-child(1) a');
await a11yAudit();
assert.strictEqual(currentURL(), urls.role);
});
test('users can navigate to role and incorrect url autocorrects', async function (assert) {
const orgScope = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
const role = this.server.create('role', {
scope: orgScope,
});
const incorrectUrl = `${urls.roles}/${role.id}/grants`;
const correctUrl = `/scopes/${orgScope.id}/roles/${role.id}/grants`;
await visit(incorrectUrl);
assert.notEqual(currentURL(), incorrectUrl);
assert.strictEqual(currentURL(), correctUrl);
});
});