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-03-26 09:41:07 -04:00
|
|
|
import { module, test } from 'qunit';
|
2024-04-12 10:31:32 -07:00
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
|
import { waitUntil } from '@ember/test-helpers';
|
2024-04-22 10:33:06 -07:00
|
|
|
import { visit } from '@ember/test-helpers';
|
|
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
|
|
|
|
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
|
2024-09-06 10:32:30 -07:00
|
|
|
import { setupIntl } from 'ember-intl/test-support';
|
2024-04-22 10:33:06 -07:00
|
|
|
import { authenticateSession } from 'ember-simple-auth/test-support';
|
2024-04-12 10:31:32 -07:00
|
|
|
import {
|
|
|
|
|
TYPE_AUTH_METHOD_PASSWORD,
|
|
|
|
|
TYPE_AUTH_METHOD_OIDC,
|
|
|
|
|
TYPE_AUTH_METHOD_LDAP,
|
|
|
|
|
} from 'api/models/auth-method';
|
2021-03-26 09:41:07 -04:00
|
|
|
|
|
|
|
|
module('Unit | Controller | scopes/scope/auth-methods/index', function (hooks) {
|
|
|
|
|
setupTest(hooks);
|
2024-04-22 10:33:06 -07:00
|
|
|
setupMirage(hooks);
|
|
|
|
|
setupIndexedDb(hooks);
|
2024-09-06 10:32:30 -07:00
|
|
|
setupIntl(hooks, 'en-us');
|
2021-03-26 09:41:07 -04:00
|
|
|
|
2024-04-22 10:33:06 -07:00
|
|
|
let store;
|
|
|
|
|
let controller;
|
|
|
|
|
let getAuthMethodCount;
|
|
|
|
|
|
|
|
|
|
const instances = {
|
|
|
|
|
scope: {
|
|
|
|
|
global: null,
|
|
|
|
|
},
|
|
|
|
|
authMethod: null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hooks.beforeEach(async function () {
|
|
|
|
|
authenticateSession({});
|
|
|
|
|
store = this.owner.lookup('service:store');
|
|
|
|
|
controller = this.owner.lookup(
|
2023-11-22 14:07:41 -05:00
|
|
|
'controller:scopes/scope/auth-methods/index',
|
2021-03-26 09:41:07 -04:00
|
|
|
);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
|
|
|
|
instances.scope.global = this.server.create('scope', { id: 'global' });
|
|
|
|
|
instances.authMethod = this.server.create('auth-method', {
|
2024-11-12 09:27:07 -08:00
|
|
|
scope: instances.scope.global,
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getAuthMethodCount = () =>
|
|
|
|
|
this.server.schema.authMethods.all().models.length;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('controller exists', function (assert) {
|
2021-03-26 09:41:07 -04:00
|
|
|
assert.ok(controller);
|
|
|
|
|
});
|
2024-04-12 10:31:32 -07:00
|
|
|
|
|
|
|
|
test('primaryOptions returns expected object', function (assert) {
|
|
|
|
|
assert.deepEqual(controller.primaryOptions, [
|
|
|
|
|
{ id: 'true', name: 'Yes' },
|
|
|
|
|
{ id: 'false', name: 'No' },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('authMethodTypeOptions returns expected object', function (assert) {
|
|
|
|
|
assert.deepEqual(controller.authMethodTypeOptions, [
|
|
|
|
|
{ id: TYPE_AUTH_METHOD_PASSWORD, name: 'Password' },
|
|
|
|
|
{ id: TYPE_AUTH_METHOD_OIDC, name: 'OIDC' },
|
|
|
|
|
{ id: TYPE_AUTH_METHOD_LDAP, name: 'LDAP' },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('filters returns expected entries', function (assert) {
|
|
|
|
|
assert.ok(controller.filters.allFilters);
|
|
|
|
|
assert.ok(controller.filters.selectedFilters);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('handleSearchInput action sets expected values correctly', async function (assert) {
|
|
|
|
|
const searchValue = 'test';
|
|
|
|
|
controller.handleSearchInput({ target: { value: searchValue } });
|
|
|
|
|
await waitUntil(() => controller.search === searchValue);
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(controller.page, 1);
|
|
|
|
|
assert.strictEqual(controller.search, searchValue);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('applyFilter action sets expected values correctly', async function (assert) {
|
|
|
|
|
const selectedItems = ['true'];
|
|
|
|
|
controller.applyFilter('primary', selectedItems);
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(controller.page, 1);
|
|
|
|
|
assert.deepEqual(controller.primary, selectedItems);
|
|
|
|
|
});
|
2024-04-22 10:33:06 -07:00
|
|
|
|
|
|
|
|
test('cancel action rolls-back changes on the specified model', async function (assert) {
|
|
|
|
|
await visit('/scopes/global/auth-methods');
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
2024-05-15 11:01:54 -07:00
|
|
|
authMethod.name = 'test';
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.strictEqual(authMethod.name, 'test');
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.cancel(authMethod);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.notEqual(authMethod.name, 'test');
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('save action saves changes on the specified model', async function (assert) {
|
|
|
|
|
await visit('/scopes/global/auth-methods');
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
2024-05-15 11:01:54 -07:00
|
|
|
authMethod.name = 'test';
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.save(authMethod);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.strictEqual(authMethod.name, 'test');
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('delete action destroys specified model', async function (assert) {
|
|
|
|
|
await visit('/scopes/global/scopes');
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
const authMethodCount = getAuthMethodCount();
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.delete(authMethod);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
|
|
|
|
assert.strictEqual(getAuthMethodCount(), authMethodCount - 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('makePrimary action updates scope with auth-method id', async function (assert) {
|
|
|
|
|
await visit('/scopes/global/auth-methods');
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.makePrimary(authMethod);
|
|
|
|
|
const scope = await store.findRecord('scope', instances.scope.global.id);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.strictEqual(scope.primary_auth_method_id, instances.authMethod.id);
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('removeAsPrimary action updates scope with null as auth-method id', async function (assert) {
|
|
|
|
|
await visit('/scopes/global/auth-methods');
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.removeAsPrimary(authMethod);
|
|
|
|
|
const scope = await store.findRecord('scope', instances.scope.global.id);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.notOk(scope.primary_auth_method_id);
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('addStringItem action adds item to field and removeItemByIndex removes correct item', async function (assert) {
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
controller.addStringItem(authMethod, 'certificates', 'cert_123');
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.deepEqual(authMethod.certificates, [{ value: 'cert_123' }]);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
controller.addStringItem(authMethod, 'certificates', 'cert_456');
|
|
|
|
|
controller.removeItemByIndex(authMethod, 'certificates', 0);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.deepEqual(authMethod.certificates, [{ value: 'cert_456' }]);
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('addAccountMapItem action adds item to field and removeItemByIndex removes correct item', async function (assert) {
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
controller.addAccountMapItem(
|
2024-05-15 11:01:54 -07:00
|
|
|
authMethod,
|
2024-04-22 10:33:06 -07:00
|
|
|
'account_attribute_maps',
|
|
|
|
|
'name123',
|
|
|
|
|
'fullName',
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.deepEqual(authMethod.account_attribute_maps, [
|
2024-04-22 10:33:06 -07:00
|
|
|
{ from: 'name123', to: 'fullName' },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
controller.addAccountMapItem(
|
2024-05-15 11:01:54 -07:00
|
|
|
authMethod,
|
2024-04-22 10:33:06 -07:00
|
|
|
'account_attribute_maps',
|
|
|
|
|
'name456',
|
|
|
|
|
'fullName',
|
|
|
|
|
);
|
2024-05-15 11:01:54 -07:00
|
|
|
controller.removeItemByIndex(authMethod, 'account_attribute_maps', 0);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.deepEqual(authMethod.account_attribute_maps, [
|
2024-04-22 10:33:06 -07:00
|
|
|
{ from: 'name456', to: 'fullName' },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('edit action updates to a dirty state', async function (assert) {
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.false(authMethod.hasDirtyAttributes);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
controller.edit(authMethod);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.true(authMethod.hasDirtyAttributes);
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('changeState action updates state of auth-method', async function (assert) {
|
|
|
|
|
instances.authMethod.update({ type: TYPE_AUTH_METHOD_LDAP });
|
2024-05-15 11:01:54 -07:00
|
|
|
const authMethod = await store.findRecord(
|
2024-04-22 10:33:06 -07:00
|
|
|
'auth-method',
|
|
|
|
|
instances.authMethod.id,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.notOk(authMethod.state);
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
await controller.changeState(authMethod, 'public');
|
2024-04-22 10:33:06 -07:00
|
|
|
|
2024-05-15 11:01:54 -07:00
|
|
|
assert.strictEqual(authMethod.state, 'public');
|
2024-04-22 10:33:06 -07:00
|
|
|
});
|
2021-03-26 09:41:07 -04:00
|
|
|
});
|