Files
threshold-ui/ui/admin/tests/acceptance/auth-methods/create-test.js
T

345 lines
11 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-08-13 15:41:33 -04:00
import { module, test } from 'qunit';
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
module('Acceptance | auth-methods | create', function (hooks) {
2020-08-13 15:41:33 -04:00
setupApplicationTest(hooks);
setupMirage(hooks);
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]';
const NEW_DROPDOWN_SELECTOR = '.hds-dropdown-toggle-button';
let getAuthMethodsCount;
let featuresService;
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,
newLdapAuthMethod: null,
2020-09-21 11:40:33 -04:00
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`;
urls.newAuthMethod = `${urls.authMethods}/new?type=password`;
urls.newLdapAuthMethod = `${urls.authMethods}/new?type=ldap`;
2020-09-21 11:40:33 -04:00
urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`;
getAuthMethodsCount = () =>
this.server.schema.authMethods.all().models.length;
featuresService = this.owner.lookup('service:features');
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);
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)');
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',
];
await visit(urls.orgScope);
await click(`[href="${urls.authMethods}"]`);
assert.true(
2021-08-30 19:16:58 -07:00
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
await click(NEW_DROPDOWN_SELECTOR);
assert.dom(`[href="${urls.newAuthMethod}"]`).exists();
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'] = ['list'];
await visit(urls.orgScope);
await click(`[href="${urls.authMethods}"]`);
assert.false(
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
assert.dom(NEW_DROPDOWN_SELECTOR).doesNotExist();
});
test('Users can navigate to new ldap auth-method route with proper authorization and feature flag enabled', async function (assert) {
assert.expect(2);
instances.orgScope.authorized_collection_actions['auth-methods'] = [
'create',
'list',
];
featuresService.enable('ldap-auth-methods');
await visit(urls.orgScope);
await click(`[href="${urls.authMethods}"]`);
assert.true(
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
await click(NEW_DROPDOWN_SELECTOR);
assert.dom(`[href="${urls.newLdapAuthMethod}"]`).exists();
});
test('Users cannot navigate to new ldap auth-method route when feature flag disabled', async function (assert) {
assert.expect(2);
instances.orgScope.authorized_collection_actions['auth-methods'] = [
'create',
'list',
];
await visit(urls.orgScope);
await click(`[href="${urls.authMethods}"]`);
assert.true(
2021-08-30 19:16:58 -07:00
instances.orgScope.authorized_collection_actions['auth-methods'].includes(
'create'
)
);
await click(NEW_DROPDOWN_SELECTOR);
assert.dom(`[href="${urls.newLdapAuthMethod}"]`).doesNotExist();
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);
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"])');
assert.strictEqual(getAuthMethodsCount(), count);
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);
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'));
});
test('user can make and remove primary auth methods from index', async function (assert) {
assert.expect(4);
assert.notOk(
2021-08-30 19:16:58 -07:00
instances.orgScope.primaryAuthMethodId,
'Primary auth method is not yet set.'
);
await visit(urls.authMethods);
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);
assert.strictEqual(
scope.primaryAuthMethodId,
instances.authMethod.id,
'Primary auth method is set.'
);
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);
assert.notOk(scope.primaryAuthMethodId, 'Primary auth method is unset.');
});
2020-08-13 15:41:33 -04:00
});