Files
threshold-ui/ui/admin/tests/acceptance/auth-methods/read-test.js
T
0129784322 fix url when user tries to access resource with incorrect url (#1958)
* refactor: 💡 auth-method, account, & managed abilities & routes

* refactor: 💡 credential store resources abilities & routes

* refactor: 💡 group abilities & routes

* refactor: 💡 host-catalog resources abilities & routes

* refactor: 💡 role abilities & routes

* test: 💍 fix route-info-test

* test: 💍 fix host serializer tests

* test: 💍 fix create-test for host-sets

* refactor: 💡 session recording resources abilities & routes

* refactor: 💡 storage bucket abilities & routes

* refactor: 💡 target abilities & routes

* refactor: 💡 user abilities & routes

* refactor: 💡 worker abilities & routes

* refactor: 💡 remove ability changes and add comments

* Update ui/admin/app/utils/route-info.js

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

* test: 💍 add test cases and rename util file

* refactor: 💡 remove dup file and update imports

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11282

* docs: ✏️ update comments & remove console.log

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11282

* Update ui/admin/app/routes/scopes/scope/auth-methods/auth-method.js

Co-authored-by: Dharini Jeeva <dharini@hashicorp.com>

---------

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
Co-authored-by: Dharini Jeeva <dharini@hashicorp.com>
2023-11-02 16:08:25 -07:00

205 lines
6.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { visit, currentURL, click } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import {
authenticateSession,
// // These are left here intentionally for future reference.
// //currentSession,
// //invalidateSession,
} from 'ember-simple-auth/test-support';
import {
TYPE_AUTH_METHOD_LDAP,
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_PASSWORD,
} from 'api/models/auth-method';
module('Acceptance | auth-methods | read', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
const AUTH_LINK_SELECTOR = 'main tbody .hds-table__td:nth-child(1) a';
const AUTH_TYPE_SELECTOR =
'tbody .hds-table__tr:nth-child(1) .hds-table__td:nth-child(2)';
const AUTH_ACTIONS_SELECTOR =
'tbody .hds-table__tr:nth-child(1) .hds-table__td:last-child .hds-dropdown';
const LDAP_AUTH_LINK_SELECTOR = 'tbody .hds-table__tr:nth-child(2) a';
const LDAP_AUTH_TYPE_SELECTOR =
'tbody .hds-table__tr:nth-child(2) .hds-table__td:nth-child(2)';
const LDAP_AUTH_ACTIONS_SELECTOR =
'tbody .hds-table__tr:nth-child(2) .hds-table__td:last-child .hds-dropdown';
const instances = {
scopes: {
global: null,
org: null,
},
passwordAuthMethodOrg: null,
oidcAuthMethodGlobal: null,
ldapAuthMethod: null,
};
const urls = {
globalScope: null,
orgScope: null,
orgAuthMethods: null,
globalAuthMethods: null,
passwordAuthMethodOrg: null,
oidcAuthMethodGlobal: null,
};
hooks.beforeEach(function () {
// Setup Mirage mock resources for this test
authenticateSession({});
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.passwordAuthMethodOrg = this.server.create('auth-method', {
scope: instances.scopes.org,
type: TYPE_AUTH_METHOD_PASSWORD,
});
instances.oidcAuthMethodGlobal = this.server.create('auth-method', {
scope: instances.scopes.global,
type: TYPE_AUTH_METHOD_OIDC,
});
// Generate route URLs for resources
urls.globalScope = `/scopes/global`;
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
urls.orgAuthMethods = `${urls.orgScope}/auth-methods`;
urls.globalAuthMethods = `${urls.globalScope}/auth-methods`;
urls.passwordAuthMethodOrg = `${urls.orgAuthMethods}/${instances.passwordAuthMethodOrg.id}`;
urls.oidcAuthMethodGlobal = `${urls.globalAuthMethods}/${instances.oidcAuthMethodGlobal.id}`;
});
test('visiting auth methods in org scope', async function (assert) {
assert.expect(1);
await visit(urls.orgScope);
await click(`[href="${urls.orgAuthMethods}"]`);
await a11yAudit();
assert.strictEqual(currentURL(), urls.orgAuthMethods);
});
test('visiting auth methods in global scope', async function (assert) {
assert.expect(1);
await visit(urls.globalScope);
await click(`[href="${urls.globalAuthMethods}"]`);
await a11yAudit();
assert.strictEqual(currentURL(), urls.globalAuthMethods);
});
test('can navigate to an auth method form in org scope', async function (assert) {
assert.expect(3);
await visit(urls.orgScope);
await click(`[href="${urls.orgAuthMethods}"]`);
assert.dom(AUTH_TYPE_SELECTOR).hasText('Password');
assert.dom(AUTH_ACTIONS_SELECTOR).exists();
await click(AUTH_LINK_SELECTOR);
await a11yAudit();
assert.strictEqual(currentURL(), urls.passwordAuthMethodOrg);
});
test('can navigate to an auth method form in global scope', async function (assert) {
assert.expect(3);
await visit(urls.globalScope);
await click(`[href="${urls.globalAuthMethods}"]`);
assert.dom(AUTH_TYPE_SELECTOR).hasText('OIDC');
assert.dom(AUTH_ACTIONS_SELECTOR).exists();
await click(AUTH_LINK_SELECTOR);
await a11yAudit();
assert.strictEqual(currentURL(), urls.oidcAuthMethodGlobal);
});
test('cannot navigate to an auth method form without proper authorization in org scope', async function (assert) {
assert.expect(1);
instances.passwordAuthMethodOrg.authorized_actions =
instances.passwordAuthMethodOrg.authorized_actions.filter(
(item) => item !== 'read'
);
await visit(urls.orgScope);
await click(`[href="${urls.orgAuthMethods}"]`);
assert.dom(AUTH_LINK_SELECTOR).doesNotExist();
});
test('cannot navigate to an auth method form without proper authorization in global scope', async function (assert) {
assert.expect(1);
instances.oidcAuthMethodGlobal.authorized_actions =
instances.oidcAuthMethodGlobal.authorized_actions.filter(
(item) => item !== 'read'
);
await visit(urls.globalScope);
await click(`[href="${urls.globalAuthMethods}"]`);
assert.dom(AUTH_LINK_SELECTOR).doesNotExist();
});
test('cannot navigate to an ldap auth method form in global scope', async function (assert) {
assert.expect(3);
instances.ldapAuthMethod = this.server.create('auth-method', {
scope: instances.scopes.global,
type: TYPE_AUTH_METHOD_LDAP,
});
await visit(urls.globalScope);
await click(`[href="${urls.globalAuthMethods}"]`);
assert.dom(LDAP_AUTH_LINK_SELECTOR).doesNotExist();
assert.dom(LDAP_AUTH_TYPE_SELECTOR).hasText('LDAP');
assert.dom(LDAP_AUTH_ACTIONS_SELECTOR).doesNotExist();
});
test('cannot navigate to an ldap auth method form in org scope', async function (assert) {
assert.expect(3);
instances.ldapAuthMethod = this.server.create('auth-method', {
scope: instances.scopes.org,
type: TYPE_AUTH_METHOD_LDAP,
});
await visit(urls.orgScope);
await click(`[href="${urls.orgAuthMethods}"]`);
assert.dom(LDAP_AUTH_LINK_SELECTOR).doesNotExist();
assert.dom(LDAP_AUTH_TYPE_SELECTOR).hasText('LDAP');
assert.dom(LDAP_AUTH_ACTIONS_SELECTOR).doesNotExist();
});
test('users can navigate to auth method and incorrect url autocorrects', async function (assert) {
assert.expect(2);
const orgScope = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
const authMethod = this.server.create('auth-method', {
scope: orgScope,
});
const incorrectUrl = `${urls.globalAuthMethods}/${authMethod.id}/accounts`;
const correctUrl = `/scopes/${orgScope.id}/auth-methods/${authMethod.id}/accounts`;
await visit(incorrectUrl);
assert.notEqual(currentURL(), incorrectUrl);
assert.strictEqual(currentURL(), correctUrl);
});
});