2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2021-06-01 22:29:19 -04:00
|
|
|
import { module, test } from 'qunit';
|
2024-11-22 11:27:53 -08:00
|
|
|
import { visit, currentURL, click, find } from '@ember/test-helpers';
|
2024-12-02 12:51:24 -06:00
|
|
|
import { setupApplicationTest } from 'admin/tests/helpers';
|
2021-06-01 22:29:19 -04:00
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
|
|
|
|
import a11yAudit from 'ember-a11y-testing/test-support/audit';
|
2021-06-02 14:33:31 -04:00
|
|
|
//import { Response } from 'miragejs';
|
2024-11-22 11:27:53 -08:00
|
|
|
import { authenticateSession } from 'ember-simple-auth/test-support';
|
2021-06-01 22:29:19 -04:00
|
|
|
|
2023-05-11 15:15:31 -07:00
|
|
|
import { TYPE_AUTH_METHOD_OIDC } from 'api/models/auth-method';
|
|
|
|
|
|
2024-11-19 13:03:29 -08:00
|
|
|
const CHANGE_STATE_SELECTOR = '[data-test-change-state] button:first-child';
|
|
|
|
|
const CHANGE_STATE_INPUT_CHECKED = '[data-test-change-state] input:checked';
|
|
|
|
|
|
|
|
|
|
const CHANGE_STATE_INPUT_SELECTOR = '[data-test-change-state]';
|
|
|
|
|
|
2023-11-02 16:08:25 -07:00
|
|
|
module('Acceptance | auth-methods | oidc', function (hooks) {
|
2021-06-01 22:29:19 -04:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
|
setupMirage(hooks);
|
|
|
|
|
|
|
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
global: null,
|
|
|
|
|
org: null,
|
|
|
|
|
},
|
|
|
|
|
authMethod: null,
|
|
|
|
|
};
|
|
|
|
|
const urls = {
|
|
|
|
|
orgScope: null,
|
|
|
|
|
authMethods: null,
|
|
|
|
|
authMethod: null,
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-03 10:27:27 -08:00
|
|
|
hooks.beforeEach(async function () {
|
|
|
|
|
await authenticateSession({ username: 'admin' });
|
2021-06-01 22:29:19 -04:00
|
|
|
instances.scopes.global = this.server.create('scope', { id: 'global' });
|
|
|
|
|
instances.scopes.org = this.server.create('scope', {
|
|
|
|
|
type: 'org',
|
|
|
|
|
scope: { id: 'global', type: 'global' },
|
|
|
|
|
});
|
|
|
|
|
instances.authMethod = this.server.create('auth-method', {
|
|
|
|
|
scope: instances.scopes.org,
|
2023-05-11 15:15:31 -07:00
|
|
|
type: TYPE_AUTH_METHOD_OIDC,
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
|
|
|
|
|
urls.authMethods = `${urls.orgScope}/auth-methods`;
|
|
|
|
|
urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('visiting oidc auth method', async function (assert) {
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await a11yAudit();
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.authMethod);
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-02 14:03:53 -04:00
|
|
|
test('can view oidc state', async function (assert) {
|
2021-06-01 22:29:19 -04:00
|
|
|
await visit(urls.authMethod);
|
2024-11-19 13:03:29 -08:00
|
|
|
await click(CHANGE_STATE_SELECTOR);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(
|
2024-11-19 13:03:29 -08:00
|
|
|
find(CHANGE_STATE_INPUT_CHECKED).value,
|
2023-11-22 14:07:41 -05:00
|
|
|
instances.authMethod.attributes.state,
|
2021-06-01 22:29:19 -04:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-02 14:03:53 -04:00
|
|
|
test('can update oidc state', async function (assert) {
|
2021-06-01 22:29:19 -04:00
|
|
|
const updateValue = 'inactive';
|
|
|
|
|
await visit(urls.authMethod);
|
2024-11-19 13:03:29 -08:00
|
|
|
await click(CHANGE_STATE_SELECTOR);
|
|
|
|
|
await click(`${CHANGE_STATE_INPUT_SELECTOR} input[value="${updateValue}"]`);
|
|
|
|
|
const authMethod = this.server.schema.authMethods.findBy({
|
|
|
|
|
id: instances.authMethod.id,
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(authMethod.attributes.state, updateValue);
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-02 14:03:53 -04:00
|
|
|
test('can update oidc state to active-private', async function (assert) {
|
2021-06-01 22:29:19 -04:00
|
|
|
const updateValue = 'active-private';
|
|
|
|
|
await visit(urls.authMethod);
|
2024-11-19 13:03:29 -08:00
|
|
|
await click(CHANGE_STATE_SELECTOR);
|
|
|
|
|
await click(`${CHANGE_STATE_INPUT_SELECTOR} input[value="${updateValue}"]`);
|
|
|
|
|
const authMethod = this.server.schema.authMethods.findBy({
|
|
|
|
|
id: instances.authMethod.id,
|
|
|
|
|
});
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
find(CHANGE_STATE_INPUT_CHECKED).value,
|
|
|
|
|
instances.authMethod.attributes.state,
|
|
|
|
|
);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(authMethod.attributes.state, updateValue);
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-02 14:03:53 -04:00
|
|
|
test('can update oidc state to active-public', async function (assert) {
|
2021-06-01 22:29:19 -04:00
|
|
|
instances.authMethod.attributes.state = 'inactive';
|
|
|
|
|
const updateValue = 'active-public';
|
|
|
|
|
await visit(urls.authMethod);
|
2024-11-19 13:03:29 -08:00
|
|
|
await click(CHANGE_STATE_SELECTOR);
|
|
|
|
|
await click(`${CHANGE_STATE_INPUT_SELECTOR} input[value="${updateValue}"]`);
|
|
|
|
|
const authMethod = this.server.schema.authMethods.findBy({
|
|
|
|
|
id: instances.authMethod.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
find(CHANGE_STATE_INPUT_CHECKED).value,
|
|
|
|
|
instances.authMethod.attributes.state,
|
|
|
|
|
);
|
|
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(authMethod.attributes.state, updateValue);
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-02 22:44:40 -04:00
|
|
|
// FIXME: How to mock just one request routed to /auth-methods and let everything else passthrough?
|
2021-10-23 11:48:10 -04:00
|
|
|
test.skip('errors are displayed when state update fails', async function (assert) {
|
|
|
|
|
this.server.post('/auth-methods/:idMethod', (_, request) => {
|
|
|
|
|
// Only respond to state update with error
|
|
|
|
|
if (request.params.idMethod.match(/(change-state)/i)) {
|
|
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'error',
|
|
|
|
|
message: 'Sorry!',
|
2023-11-22 14:07:41 -05:00
|
|
|
},
|
2021-10-23 11:48:10 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return request;
|
|
|
|
|
});
|
|
|
|
|
const newState = 'inactive';
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await click('.rose-layout-page-actions .rose-dropdown-trigger');
|
|
|
|
|
await click(`.rose-dropdown[open] input[value="${newState}"]`);
|
|
|
|
|
const authMethod = this.server.db.authMethods.find(instances.authMethod.id);
|
|
|
|
|
assert.notEqual(
|
|
|
|
|
newState,
|
|
|
|
|
authMethod.attributes.state,
|
2023-11-22 14:07:41 -05:00
|
|
|
'Auth method state is not be updated.',
|
2021-10-23 11:48:10 -04:00
|
|
|
);
|
2024-11-19 15:18:18 -08:00
|
|
|
assert
|
|
|
|
|
.dom('[data-test-toast-notification] .hds-alert__description')
|
|
|
|
|
.hasText('Sorry!');
|
2021-10-23 11:48:10 -04:00
|
|
|
});
|
2021-06-01 22:29:19 -04:00
|
|
|
});
|