Alias clear modal (#2239)

* fix: 🐛 permissions to create aliases within targets
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-04-15 09:15:38 -07:00
1 parent bd65c91ff9
commit 80b633a063
9 files changed
+393 -11

No files matched your search

@@ -1016,6 +1016,8 @@ alias:
title: Welcome to Aliases
description: No Aliases yet
link: Create a new alias
clear-or-delete: Deleting this alias may break any workflows using it. You can also clear the alias of the resource it points to without deleting it and then edit it to point it to a new resource.
delete: Deleting this alias may break any workflows using it.
form:
options:
label: Alias options
+3 -5
View File
@@ -13,10 +13,8 @@ export default class FormAliasComponent extends Component {
*/
@action
handleHostIdChange({ target: { value } }) {
if (value) {
this.args.model.authorize_session_arguments = {
host_id: value,
};
}
this.args.model.authorize_session_arguments = {
host_id: value,
};
}
}
@@ -7,13 +7,18 @@ import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { tracked } from '@glimmer/tracking';
export default class ScopesScopeAliasesIndexController extends Controller {
// =services
@service can;
@service router;
// =attributes
@tracked showModal = false;
@tracked selectedAlias;
@tracked hasDestinationId;
// =actions
/**
@@ -48,16 +53,44 @@ export default class ScopesScopeAliasesIndexController extends Controller {
}
/**
* Delete an alias in current scope
* @param {AliasModel} alias
* Handles modal visibility and sets the destination_id to a tracked variable so we can handle the clear button visibility on the modal
*/
@action
toggleModal(alias) {
if (alias?.destination_id) {
this.hasDestinationId = true;
}
this.showModal = !this.showModal;
this.selectedAlias = alias;
}
/**
* Remove destaination_id from alias
* @param {AliasModel} alias
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.clear-success')
async clearAlias(alias) {
alias.destination_id = '';
this.hasDestinationId = false;
await alias.save();
await this.router.refresh();
this.showModal = false;
}
/**
* Delete an alias
* @param {AliasModel} alias
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(alias) {
async deleteAlias(alias) {
await alias.destroyRecord();
this.showModal = false;
await this.router.refresh();
}
}
+6
View File
@@ -955,6 +955,12 @@
}
}
.alias-deletion-modal {
.hds-button-set {
margin: 0;
}
}
// storage policies
.policy {
margin-bottom: 0;
@@ -96,6 +96,7 @@
}}
{{#if (or perms.canRead perms.canDelete)}}
<Hds::Dropdown as |dd|>
<dd.ToggleIcon
@icon='more-horizontal'
@text={{t 'actions.manage'}}
@@ -116,10 +117,68 @@
<dd.Interactive
@text={{t 'actions.delete'}}
@color='critical'
{{on 'click' (fn this.delete B.data.alias)}}
{{on 'click' (fn this.toggleModal B.data.alias)}}
/>
{{#if this.showModal}}
<Hds::Modal
class='alias-deletion-modal'
@color='warning'
@onClose={{this.toggleModal}}
as |M|
>
<M.Header @icon='alert-triangle'>
{{t 'questions.delete-confirm'}}
</M.Header>
<M.Body>
<p
class='hds-typography-body-300 hds-foreground-primary'
>
{{#if this.hasDestinationId}}
{{t 'resources.alias.messages.clear-or-delete'}}
{{else}}
{{t 'resources.alias.messages.delete'}}
{{/if}}
</p>
</M.Body>
<M.Footer>
<Hds::ButtonSet>
<Hds::Button
type='button'
@text={{t 'actions.delete'}}
{{on
'click'
(fn this.deleteAlias this.selectedAlias)
}}
/>
{{#if this.hasDestinationId}}
<Hds::Button
type='button'
@text={{t 'actions.clear'}}
@color='secondary'
{{on
'click'
(fn this.clearAlias this.selectedAlias)
}}
/>
{{/if}}
<Hds::Button
type='button'
@text={{t 'actions.cancel'}}
@color='secondary'
{{on 'click' this.toggleModal}}
/>
</Hds::ButtonSet>
</M.Footer>
</Hds::Modal>
{{/if}}
{{/if}}
</Hds::Dropdown>
{{/if}}
{{/let}}
</B.Td>
@@ -10,10 +10,13 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { Response } from 'miragejs';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
module('Acceptance | aliases | create', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
let features;
let getAliasCount;
@@ -0,0 +1,118 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { visit, click } from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
module('Acceptance | aliases | delete', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
let aliasCount;
const DROPDOWN_BUTTON_SELECTOR = '.hds-dropdown-toggle-icon';
const DROPDOWN_ITEM_SELECTOR = '.hds-dropdown-list-item button';
const MODAL_SELECTOR = '.alias-deletion-modal';
const DELETE_BUTTON_SELECTOR = '.hds-modal__footer .hds-button:nth-child(1)';
const CLEAR_BUTTON_SELECTOR = '.hds-modal__footer .hds-button:nth-child(2)';
const instances = {
scopes: {
global: null,
org: null,
project: null,
},
alias: null,
target: null,
};
const urls = {
globalScope: null,
aliases: null,
alias: null,
target: null,
};
hooks.beforeEach(function () {
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.scopes.project = this.server.create('scope', {
type: 'project',
scope: { id: instances.scopes.org.id, type: 'org' },
});
instances.target = this.server.create('target', {
scope: instances.scopes.project,
});
instances.alias = this.server.create('alias', {
scope: instances.scopes.global,
destination_id: instances.target.id,
});
urls.globalScope = `/scopes/global`;
urls.aliases = `${urls.globalScope}/aliases`;
aliasCount = () => this.server.schema.aliases.all().models.length;
});
test('users can delete an alias', async function (assert) {
const count = aliasCount();
assert.true(instances.alias.authorized_actions.includes('delete'));
await visit(urls.globalScope);
urls.alias = `${urls.aliases}/${instances.alias.id}`;
await click(`[href="${urls.aliases}"]`);
await click(DROPDOWN_BUTTON_SELECTOR);
assert.dom(DROPDOWN_ITEM_SELECTOR).exists();
assert.dom(DROPDOWN_ITEM_SELECTOR).hasText('Delete');
await click(DROPDOWN_ITEM_SELECTOR);
assert.dom(MODAL_SELECTOR);
await click(DELETE_BUTTON_SELECTOR);
assert.strictEqual(aliasCount(), count - 1);
});
test('users cannot delete an alias without proper authorization', async function (assert) {
instances.alias.authorized_actions =
instances.alias.authorized_actions.filter((item) => item !== 'delete');
await visit(urls.globalScope);
urls.alias = `${urls.aliases}/${instances.alias.id}`;
await click(`[href="${urls.aliases}"]`);
await click(DROPDOWN_BUTTON_SELECTOR);
assert.dom(DROPDOWN_ITEM_SELECTOR).isNotVisible();
});
test('users have the option to clear an alias instead of deleting it', async function (assert) {
const count = aliasCount();
assert.true(instances.alias.authorized_actions.includes('delete'));
await visit(urls.globalScope);
urls.alias = `${urls.aliases}/${instances.alias.id}`;
await click(`[href="${urls.aliases}"]`);
await click(DROPDOWN_BUTTON_SELECTOR);
assert.dom(DROPDOWN_ITEM_SELECTOR).exists();
assert.dom(DROPDOWN_ITEM_SELECTOR).hasText('Delete');
await click(DROPDOWN_ITEM_SELECTOR);
assert.dom(MODAL_SELECTOR);
await click(CLEAR_BUTTON_SELECTOR);
// await click(`[href="${urls.aliases}"]`);
assert.strictEqual(aliasCount(), count);
});
});
@@ -0,0 +1,82 @@
/**
* 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 'admin/tests/helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { authenticateSession } from 'ember-simple-auth/test-support';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
module('Acceptance | aliases | read', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
const MESSAGE_SELECTOR = '.rose-message-subtitle';
const TABLE_LINK_SELECTOR = '.hds-table__tbody tr:first-child a';
const instances = {
scopes: {
global: null,
org: null,
},
alias: null,
};
const urls = {
globalScope: null,
aliases: null,
alias: null,
unknownAlias: null,
};
hooks.beforeEach(function () {
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.alias = this.server.create('alias', {
scope: instances.scopes.global,
});
urls.globalScope = `/scopes/global`;
urls.aliases = `${urls.globalScope}/aliases`;
urls.alias = `${urls.aliases}/${instances.alias.id}`;
urls.unknownAlias = `${urls.aliases}/foo`;
authenticateSession({});
});
test('visiting a alias', async function (assert) {
await visit(urls.globalScope);
await a11yAudit();
await click(`[href="${urls.aliases}"]`);
await a11yAudit();
await click(`[href="${urls.alias}"]`);
await a11yAudit();
assert.strictEqual(currentURL(), urls.alias);
});
test('cannot navigate to a alias without proper authorization', async function (assert) {
await visit(urls.globalScope);
instances.alias.authorized_actions =
instances.alias.authorized_actions.filter((item) => item !== 'read');
await click(`[href="${urls.aliases}"]`);
assert.dom(TABLE_LINK_SELECTOR).doesNotExist();
});
test('visiting an unknown alias displays 404 message', async function (assert) {
await visit(urls.unknownAlias);
await a11yAudit();
assert.dom(MESSAGE_SELECTOR).hasText('Error 404');
});
});
@@ -0,0 +1,81 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { visit, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { authenticateSession } from 'ember-simple-auth/test-support';
module('Acceptance | aliases | update', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
const SAVE_BTN_SELECTOR = '.rose-form-actions [type="submit"]';
const NAME_FIELD_SELECTOR = '[name="name"]';
const NAME_FIELD_TEXT = 'random string';
const BUTTON_SELECTOR = '.rose-form-actions [type="button"]';
const instances = {
scopes: {
global: null,
org: null,
project: null,
},
alias: null,
};
const urls = {
globalScope: null,
aliases: null,
alias: null,
};
hooks.beforeEach(function () {
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.alias = this.server.create('alias', {
scope: instances.scopes.global,
});
urls.globalScope = `/scopes/global`;
urls.aliases = `${urls.globalScope}/aliases`;
urls.alias = `${urls.aliases}/${instances.alias.id}`;
authenticateSession({});
});
test('users can update an exisiting alias', async function (assert) {
await visit(urls.aliases);
await click(`[href="${urls.alias}"]`);
await click(BUTTON_SELECTOR, 'Click edit mode');
await fillIn(NAME_FIELD_SELECTOR, NAME_FIELD_TEXT);
await click(SAVE_BTN_SELECTOR);
assert.strictEqual(instances.alias.name, NAME_FIELD_TEXT);
});
test('can cancel changes to an existing alias', async function (assert) {
const name = instances.alias.name;
await visit(urls.aliases);
await click(`[href="${urls.aliases}"]`);
await click(`[href="${urls.alias}"]`);
await click(BUTTON_SELECTOR, 'Click edit mode');
await fillIn(NAME_FIELD_SELECTOR, NAME_FIELD_TEXT);
await click(BUTTON_SELECTOR, 'Click cancel');
assert.dom(NAME_FIELD_SELECTOR).hasValue(`${name}`);
assert.strictEqual(instances.alias.name, name);
});
});