Files
threshold-ui/ui/admin/tests/acceptance/credential-store/credentials/delete-test.js
T
Lauren 422cff4547 [ICU-15647] Replace Rose::Dialog with Hds::Modal in admin ui only (#2623)
* sort list

* refactor: 💡 Replace Rose::Dialog with Hds::Modal

✅ Closes: ICU-15647

* refactor targets-host-sources-test

* refactor update-test

* update test selectors

* refactor delete test

* refactor org scope test

* refactor delete scope and auth-method tests

* refactor update host catalogs tests

* refactor credential-stores credential update tests

* refactor credential-stores credentials delete tests

* refactor scopes update tests

* refactor storage buckets delete test

* refactor storage buckets delete test

* refactor credential library update test

* refactor targest workers test

* refactor workers create tags test

* refactor global scope test

* refactor users update test

* refactor host catalogs delete test

* refactor credential-store update tests

* refactor host catalogs, hosts and sets update tests

* remove unused vars

* rename selectors, remove attrs

* add @onDismiss callback
2024-12-20 14:16:43 -05:00

327 lines
12 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
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 { click, currentURL, visit } from '@ember/test-helpers';
import { Response } from 'miragejs';
import * as commonSelectors from 'admin/tests/helpers/selectors';
module(
'Acceptance | credential-stores | credentials | delete',
function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
let getUsernamePasswordCredentialCount;
let getUsernameKeyPairCredentialCount;
let getJSONCredentialCount;
const MANAGE_DROPDOWN_SELECTOR =
'[data-test-manage-credentials-dropdown] button:first-child';
const DELETE_ACTION_SELECTOR =
'[data-test-manage-credentials-dropdown] ul li button';
const instances = {
scopes: {
org: null,
project: null,
},
};
const urls = {
projectScope: null,
credentialStores: null,
staticCredentialStore: null,
credentials: null,
usernamePasswordCredential: null,
usernameKeyPairCredential: null,
jsonCredential: null,
};
hooks.beforeEach(async function () {
// Generate resources
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.staticCredentialStore = this.server.create('credential-store', {
scope: instances.scopes.project,
type: 'static',
});
instances.usernamePasswordCredential = this.server.create('credential', {
scope: instances.scopes.project,
credentialStore: instances.staticCredentialStore,
type: 'username_password',
});
instances.usernameKeyPairCredential = this.server.create('credential', {
scope: instances.scopes.project,
credentialStore: instances.staticCredentialStore,
type: 'ssh_private_key',
});
instances.jsonCredential = this.server.create('credential', {
scope: instances.scopes.project,
credentialStore: instances.staticCredentialStore,
type: 'json',
});
// Generate route URLs for resources
urls.projectScope = `/scopes/${instances.scopes.project.id}`;
urls.credentialStores = `${urls.projectScope}/credential-stores`;
urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`;
urls.credentials = `${urls.staticCredentialStore}/credentials`;
urls.usernamePasswordCredential = `${urls.credentials}/${instances.usernamePasswordCredential.id}`;
urls.usernameKeyPairCredential = `${urls.credentials}/${instances.usernameKeyPairCredential.id}`;
urls.jsonCredential = `${urls.credentials}/${instances.jsonCredential.id}`;
// Generate resource counter
getUsernamePasswordCredentialCount = () => {
return this.server.schema.credentials.where({
type: 'username_password',
}).length;
};
getUsernameKeyPairCredentialCount = () => {
return this.server.schema.credentials.where({ type: 'ssh_private_key' })
.length;
};
getJSONCredentialCount = () => {
return this.server.schema.credentials.where({ type: 'json' }).length;
};
await authenticateSession({});
});
test('can delete username & password credential', async function (assert) {
const usernamePasswordCredentialCount =
getUsernamePasswordCredentialCount();
await visit(urls.usernamePasswordCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getUsernamePasswordCredentialCount(),
usernamePasswordCredentialCount - 1,
);
});
test('can delete username & key pair credential', async function (assert) {
const usernameKeyPairCredentialCount =
getUsernameKeyPairCredentialCount();
await visit(urls.usernameKeyPairCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getUsernameKeyPairCredentialCount(),
usernameKeyPairCredentialCount - 1,
);
});
test('can delete JSON credential', async function (assert) {
const jsonCredentialCount = getJSONCredentialCount();
await visit(urls.jsonCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(getJSONCredentialCount(), jsonCredentialCount - 1);
});
test('cannot delete a username & password credential without proper authorization', async function (assert) {
const usernamePasswordCredentialCount =
getUsernamePasswordCredentialCount();
instances.usernamePasswordCredential.authorized_actions =
instances.usernamePasswordCredential.authorized_actions.filter(
(item) => item !== 'delete',
);
await visit(urls.usernamePasswordCredential);
assert.strictEqual(currentURL(), urls.usernamePasswordCredential);
assert.dom(MANAGE_DROPDOWN_SELECTOR).doesNotExist();
assert.strictEqual(
getUsernamePasswordCredentialCount(),
usernamePasswordCredentialCount,
);
});
test('cannot delete a username & key pair credential without proper authorization', async function (assert) {
const usernameKeyPairCredentialCount =
getUsernameKeyPairCredentialCount();
instances.usernameKeyPairCredential.authorized_actions =
instances.usernameKeyPairCredential.authorized_actions.filter(
(item) => item !== 'delete',
);
await visit(urls.usernameKeyPairCredential);
assert.strictEqual(currentURL(), urls.usernameKeyPairCredential);
assert.dom(MANAGE_DROPDOWN_SELECTOR).doesNotExist();
assert.strictEqual(
getUsernamePasswordCredentialCount(),
usernameKeyPairCredentialCount,
);
});
test('cannot delete a JSON credential without proper authorization', async function (assert) {
const jsonCredentialCount = getJSONCredentialCount();
instances.jsonCredential.authorized_actions =
instances.jsonCredential.authorized_actions.filter(
(item) => item !== 'delete',
);
await visit(urls.jsonCredential);
assert.strictEqual(currentURL(), urls.jsonCredential);
assert.dom(MANAGE_DROPDOWN_SELECTOR).doesNotExist();
assert.strictEqual(getJSONCredentialCount(), jsonCredentialCount);
});
test('can accept delete username & password credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const usernamePasswordCredentialCount =
getUsernamePasswordCredentialCount();
await visit(urls.usernamePasswordCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CONFIRM_BTN);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getUsernamePasswordCredentialCount(),
usernamePasswordCredentialCount - 1,
);
});
test('can accept delete username & key pair credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const usernameKeyPairCredentialCount =
getUsernameKeyPairCredentialCount();
await visit(urls.usernameKeyPairCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CONFIRM_BTN);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getUsernameKeyPairCredentialCount(),
usernameKeyPairCredentialCount - 1,
);
});
test('can accept delete JSON credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const jsonCredentialCount = getJSONCredentialCount();
await visit(urls.jsonCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CONFIRM_BTN);
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(getJSONCredentialCount(), jsonCredentialCount - 1);
});
test('can cancel delete username & password credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const usernamePasswordCredentialCount =
getUsernamePasswordCredentialCount();
await visit(urls.usernamePasswordCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CANCEL_BTN);
assert.strictEqual(currentURL(), urls.usernamePasswordCredential);
assert.strictEqual(
getUsernamePasswordCredentialCount(),
usernamePasswordCredentialCount,
);
});
test('can cancel delete username & key pair credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const usernameKeyPairCredentialCount =
getUsernameKeyPairCredentialCount();
await visit(urls.usernameKeyPairCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CANCEL_BTN);
assert.strictEqual(currentURL(), urls.usernameKeyPairCredential);
assert.strictEqual(
getUsernameKeyPairCredentialCount(),
usernameKeyPairCredentialCount,
);
});
test('can cancel delete JSON credential via dialog', async function (assert) {
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const jsonCredentialCount = getJSONCredentialCount();
await visit(urls.jsonCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
await click(commonSelectors.MODAL_WARNING_CANCEL_BTN);
assert.strictEqual(currentURL(), urls.jsonCredential);
assert.strictEqual(getJSONCredentialCount(), jsonCredentialCount);
});
test('deleting a username & password credential which errors displays error message', async function (assert) {
this.server.del('/credentials/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
},
);
});
await visit(urls.usernamePasswordCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('Oops.');
});
test('deleting a username & key pair credential which errors displays error message', async function (assert) {
this.server.del('/credentials/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
},
);
});
await visit(urls.usernameKeyPairCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('Oops.');
});
test('deleting a JSON credential which errors displays error message', async function (assert) {
this.server.del('/credentials/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
},
);
});
await visit(urls.jsonCredential);
await click(MANAGE_DROPDOWN_SELECTOR);
await click(DELETE_ACTION_SELECTOR);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('Oops.');
});
},
);