2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-13 15:41:33 -04:00
|
|
|
import { module, test } from 'qunit';
|
2022-11-23 14:40:07 -05:00
|
|
|
import {
|
|
|
|
|
visit,
|
|
|
|
|
currentURL,
|
|
|
|
|
click,
|
|
|
|
|
find,
|
|
|
|
|
fillIn,
|
|
|
|
|
select,
|
|
|
|
|
} from '@ember/test-helpers';
|
2020-08-13 15:41:33 -04:00
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
|
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
2020-09-21 11:40:33 -04:00
|
|
|
import { Response } from 'miragejs';
|
|
|
|
|
import {
|
|
|
|
|
authenticateSession,
|
2020-11-16 15:08:59 -05:00
|
|
|
// // These are left here intentionally for future reference.
|
|
|
|
|
// //currentSession,
|
|
|
|
|
// //invalidateSession,
|
2020-09-21 11:40:33 -04:00
|
|
|
} from 'ember-simple-auth/test-support';
|
2020-08-13 15:41:33 -04:00
|
|
|
|
2023-05-16 09:37:53 -07:00
|
|
|
module('Acceptance | auth-methods | create', function (hooks) {
|
2020-08-13 15:41:33 -04:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
|
setupMirage(hooks);
|
2023-05-16 09:37:53 -07:00
|
|
|
const DROPDOWN_SELECTOR_ICON =
|
|
|
|
|
'tbody .hds-table__tr:nth-child(1) .hds-table__td:last-child .hds-dropdown-toggle-icon';
|
|
|
|
|
const DROPDOWN_SELECTOR_OPTION =
|
|
|
|
|
'.hds-dropdown__content .hds-dropdown-list-item [type=button]';
|
2022-11-23 14:40:07 -05:00
|
|
|
let getAuthMethodsCount;
|
2020-09-21 11:40:33 -04:00
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
global: null,
|
|
|
|
|
org: null,
|
2021-09-02 09:48:28 -07:00
|
|
|
project: null,
|
2020-09-21 11:40:33 -04:00
|
|
|
},
|
|
|
|
|
authMethod: null,
|
2021-08-30 19:16:58 -07:00
|
|
|
orgScope: null,
|
2020-09-21 11:40:33 -04:00
|
|
|
};
|
2021-08-30 19:16:58 -07:00
|
|
|
|
2020-09-21 11:40:33 -04:00
|
|
|
const urls = {
|
|
|
|
|
orgScope: null,
|
|
|
|
|
authMethods: null,
|
|
|
|
|
newAuthMethod: null,
|
|
|
|
|
authMethod: null,
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-13 15:41:33 -04:00
|
|
|
hooks.beforeEach(function () {
|
2020-09-21 11:40:33 -04:00
|
|
|
authenticateSession({});
|
2021-09-02 09:48:28 -07:00
|
|
|
instances.orgScope = this.server.create(
|
|
|
|
|
'scope',
|
|
|
|
|
{
|
|
|
|
|
type: 'org',
|
|
|
|
|
scope: { id: 'global', type: 'global' },
|
|
|
|
|
},
|
|
|
|
|
'withChildren'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
instances.scopes.project = this.server.create('scope', {
|
|
|
|
|
type: 'project',
|
|
|
|
|
scope: { id: instances.orgScope.id, type: instances.orgScope.type },
|
2020-09-21 11:40:33 -04:00
|
|
|
});
|
2021-08-30 19:16:58 -07:00
|
|
|
|
2020-09-21 11:40:33 -04:00
|
|
|
instances.authMethod = this.server.create('auth-method', {
|
2021-08-30 19:16:58 -07:00
|
|
|
scope: instances.orgScope,
|
2020-09-21 11:40:33 -04:00
|
|
|
});
|
|
|
|
|
|
2020-08-13 15:41:33 -04:00
|
|
|
// Generate route URLs for resources
|
2021-09-02 09:48:28 -07:00
|
|
|
urls.orgScope = `/scopes/${instances.orgScope.id}`;
|
|
|
|
|
urls.authMethods = `${urls.orgScope}/auth-methods`;
|
2021-01-05 17:41:02 -05:00
|
|
|
urls.newAuthMethod = `${urls.authMethods}/new?type=password`;
|
2020-09-21 11:40:33 -04:00
|
|
|
urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`;
|
2022-11-23 14:40:07 -05:00
|
|
|
getAuthMethodsCount = () =>
|
|
|
|
|
this.server.schema.authMethods.all().models.length;
|
2020-08-13 15:41:33 -04:00
|
|
|
});
|
|
|
|
|
|
2021-09-02 08:15:04 -07:00
|
|
|
test('Users can create new auth method', async function (assert) {
|
2020-09-21 11:40:33 -04:00
|
|
|
assert.expect(1);
|
2022-11-23 14:40:07 -05:00
|
|
|
const count = getAuthMethodsCount();
|
|
|
|
|
|
2020-09-21 11:40:33 -04:00
|
|
|
await visit(urls.newAuthMethod);
|
|
|
|
|
await fillIn('[name="name"]', 'AuthMethod name');
|
|
|
|
|
await fillIn('[name="description"]', 'description');
|
|
|
|
|
await click('form [type="submit"]:not(:disabled)');
|
2022-11-23 14:40:07 -05:00
|
|
|
assert.strictEqual(getAuthMethodsCount(), count + 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Users can create new oidc auth method', async function (assert) {
|
|
|
|
|
assert.expect(13);
|
|
|
|
|
const count = getAuthMethodsCount();
|
|
|
|
|
|
|
|
|
|
await visit(`${urls.authMethods}/new?type=oidc`);
|
|
|
|
|
const name = 'oidc name';
|
|
|
|
|
await fillIn('[name="name"]', name);
|
|
|
|
|
await fillIn('[name="description"]', 'description');
|
|
|
|
|
await fillIn('[name="issuer"]', 'issuer');
|
|
|
|
|
await fillIn('[name="client_id"]', 'client_id');
|
|
|
|
|
await fillIn('[name="client_secret"]', 'client_secret');
|
|
|
|
|
await select('form fieldset:nth-of-type(1) select', 'RS384');
|
|
|
|
|
await click('form fieldset:nth-of-type(1) [title="Add"]');
|
|
|
|
|
await fillIn('[name="allowed_audiences"]', 'allowed_audiences');
|
|
|
|
|
await click('form fieldset:nth-of-type(2) [title="Add"]');
|
|
|
|
|
await fillIn('[name="claims_scopes"]', 'claims_scopes');
|
|
|
|
|
await click('form fieldset:nth-of-type(3) [title="Add"]');
|
|
|
|
|
await fillIn('[name="from_claim"]', 'from_claim');
|
|
|
|
|
await select('form fieldset:nth-of-type(4) select', 'email');
|
|
|
|
|
await click('form fieldset:nth-of-type(4) [title="Add"]');
|
|
|
|
|
await fillIn('form fieldset:nth-of-type(5) textarea', 'certificates');
|
|
|
|
|
await click('form fieldset:nth-of-type(5) [title="Add"]');
|
|
|
|
|
await fillIn('[name="max_age"]', '5');
|
|
|
|
|
await fillIn('[name="api_url_prefix"]', 'api_url_prefix');
|
|
|
|
|
await click('form [type="submit"]:not(:disabled)');
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(getAuthMethodsCount(), count + 1);
|
|
|
|
|
const authMethod = this.server.schema.authMethods.findBy({ name });
|
|
|
|
|
assert.strictEqual(authMethod.name, name);
|
|
|
|
|
assert.strictEqual(authMethod.description, 'description');
|
|
|
|
|
assert.strictEqual(authMethod.attributes.issuer, 'issuer');
|
|
|
|
|
assert.strictEqual(authMethod.attributes.client_id, 'client_id');
|
|
|
|
|
assert.strictEqual(authMethod.attributes.client_secret, 'client_secret');
|
|
|
|
|
assert.deepEqual(authMethod.attributes.signing_algorithms, ['RS384']);
|
|
|
|
|
assert.deepEqual(authMethod.attributes.allowed_audiences, [
|
|
|
|
|
'allowed_audiences',
|
|
|
|
|
]);
|
|
|
|
|
assert.deepEqual(authMethod.attributes.claims_scopes, ['claims_scopes']);
|
|
|
|
|
assert.deepEqual(authMethod.attributes.account_claim_maps, [
|
|
|
|
|
'from_claim=email',
|
|
|
|
|
]);
|
|
|
|
|
assert.deepEqual(authMethod.attributes.idp_ca_certs, ['certificates']);
|
|
|
|
|
assert.strictEqual(authMethod.attributes.max_age, 5);
|
|
|
|
|
assert.strictEqual(authMethod.attributes.api_url_prefix, 'api_url_prefix');
|
2020-08-13 16:08:45 -04:00
|
|
|
});
|
|
|
|
|
|
2021-09-02 08:15:04 -07:00
|
|
|
test('Users can navigate to new auth-methods route with proper authorization', async function (assert) {
|
2021-08-30 19:16:58 -07:00
|
|
|
assert.expect(2);
|
|
|
|
|
instances.orgScope.authorized_collection_actions['auth-methods'] = [
|
|
|
|
|
'create',
|
|
|
|
|
'list',
|
|
|
|
|
];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.authMethods);
|
2021-08-30 19:16:58 -07:00
|
|
|
assert.ok(
|
|
|
|
|
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
|
|
|
|
|
'create'
|
|
|
|
|
)
|
|
|
|
|
);
|
2021-11-05 14:23:20 -04:00
|
|
|
assert.ok(find(`[href="${urls.newAuthMethod}"]`));
|
2021-08-30 19:16:58 -07:00
|
|
|
});
|
|
|
|
|
|
2021-09-02 08:15:04 -07:00
|
|
|
test('Users cannot navigate to new auth-methods route without proper authorization', async function (assert) {
|
2021-08-30 19:16:58 -07:00
|
|
|
assert.expect(2);
|
|
|
|
|
instances.orgScope.authorized_collection_actions['auth-methods'] = [];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.authMethods);
|
2021-08-30 19:16:58 -07:00
|
|
|
assert.notOk(
|
|
|
|
|
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
|
|
|
|
|
'create'
|
|
|
|
|
)
|
|
|
|
|
);
|
2021-11-05 14:23:20 -04:00
|
|
|
assert.notOk(find(`[href="${urls.newAuthMethod}"]`));
|
2021-08-30 19:16:58 -07:00
|
|
|
});
|
|
|
|
|
|
2020-08-13 16:08:45 -04:00
|
|
|
test('can cancel new auth method creation', async function (assert) {
|
2020-09-21 11:40:33 -04:00
|
|
|
assert.expect(2);
|
2022-11-23 14:40:07 -05:00
|
|
|
const count = getAuthMethodsCount();
|
2020-09-21 11:40:33 -04:00
|
|
|
await visit(urls.newAuthMethod);
|
|
|
|
|
await fillIn('[name="name"]', 'AuthMethod name');
|
|
|
|
|
await fillIn('[name="description"]', 'description');
|
|
|
|
|
await click('form button:not([type="submit"])');
|
2022-11-23 14:40:07 -05:00
|
|
|
assert.strictEqual(getAuthMethodsCount(), count);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.authMethods);
|
2020-08-13 16:08:45 -04:00
|
|
|
});
|
|
|
|
|
|
2021-03-26 14:55:19 -04:00
|
|
|
test('user can make primary an auth method', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
assert.notOk(
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.primaryAuthMethodId,
|
2021-03-26 14:55:19 -04:00
|
|
|
'Primary auth method is not yet set.'
|
|
|
|
|
);
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await click(
|
|
|
|
|
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
|
|
|
|
|
);
|
2021-08-30 19:16:58 -07:00
|
|
|
const scope = this.server.schema.scopes.find(instances.orgScope.id);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(
|
2021-03-26 14:55:19 -04:00
|
|
|
scope.primaryAuthMethodId,
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
'Primary auth method is set.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('user is notified of error on make primary an auth method', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
this.server.patch('/scopes/:id', () => {
|
|
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'fail',
|
|
|
|
|
message: 'Sorry!',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
assert.notOk(
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.primaryAuthMethodId,
|
2021-03-26 14:55:19 -04:00
|
|
|
'Primary auth method is not yet set.'
|
|
|
|
|
);
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await click(
|
|
|
|
|
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
|
|
|
|
|
);
|
|
|
|
|
assert.ok(find('.rose-notification'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('user can remove as primary an auth method', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.update({
|
2021-03-26 14:55:19 -04:00
|
|
|
primaryAuthMethodId: instances.authMethod.id,
|
|
|
|
|
});
|
|
|
|
|
assert.ok(
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.primaryAuthMethodId,
|
2021-03-26 14:55:19 -04:00
|
|
|
'Primary auth method is set.'
|
|
|
|
|
);
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await click(
|
|
|
|
|
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
|
|
|
|
|
);
|
2021-08-30 19:16:58 -07:00
|
|
|
const scope = this.server.schema.scopes.find(instances.orgScope.id);
|
2021-03-26 14:55:19 -04:00
|
|
|
assert.notOk(scope.primaryAuthMethodId, 'Primary auth method is unset.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('user is notified of error on remove as primary an auth method', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
this.server.patch('/scopes/:id', () => {
|
|
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'fail',
|
|
|
|
|
message: 'Sorry!',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.update({
|
2021-03-26 14:55:19 -04:00
|
|
|
primaryAuthMethodId: instances.authMethod.id,
|
|
|
|
|
});
|
|
|
|
|
assert.ok(
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.primaryAuthMethodId,
|
2021-03-26 14:55:19 -04:00
|
|
|
'Primary auth method is set.'
|
|
|
|
|
);
|
|
|
|
|
await visit(urls.authMethod);
|
|
|
|
|
await click(
|
|
|
|
|
'.rose-layout-page-actions .rose-dropdown-content [type="button"]:first-child'
|
|
|
|
|
);
|
|
|
|
|
assert.ok(find('.rose-notification'));
|
|
|
|
|
});
|
2021-03-29 19:24:21 -04:00
|
|
|
|
|
|
|
|
test('user can make and remove primary auth methods from index', async function (assert) {
|
2023-05-16 09:37:53 -07:00
|
|
|
assert.expect(4);
|
2021-03-29 19:24:21 -04:00
|
|
|
assert.notOk(
|
2021-08-30 19:16:58 -07:00
|
|
|
instances.orgScope.primaryAuthMethodId,
|
2021-03-29 19:24:21 -04:00
|
|
|
'Primary auth method is not yet set.'
|
|
|
|
|
);
|
|
|
|
|
await visit(urls.authMethods);
|
2023-05-16 09:37:53 -07:00
|
|
|
|
|
|
|
|
await click(DROPDOWN_SELECTOR_ICON);
|
|
|
|
|
assert.dom(DROPDOWN_SELECTOR_OPTION).exists();
|
|
|
|
|
await click(DROPDOWN_SELECTOR_OPTION);
|
|
|
|
|
|
2021-08-30 19:16:58 -07:00
|
|
|
let scope = this.server.schema.scopes.find(instances.orgScope.id);
|
2023-05-16 09:37:53 -07:00
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(
|
2021-03-29 19:24:21 -04:00
|
|
|
scope.primaryAuthMethodId,
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
'Primary auth method is set.'
|
|
|
|
|
);
|
2023-05-16 09:37:53 -07:00
|
|
|
await click(DROPDOWN_SELECTOR_ICON);
|
|
|
|
|
await click(DROPDOWN_SELECTOR_OPTION);
|
|
|
|
|
|
2021-08-30 19:16:58 -07:00
|
|
|
scope = this.server.schema.scopes.find(instances.orgScope.id);
|
2021-03-29 19:24:21 -04:00
|
|
|
assert.notOk(scope.primaryAuthMethodId, 'Primary auth method is unset.');
|
|
|
|
|
});
|
2020-08-13 15:41:33 -04:00
|
|
|
});
|