Files
threshold-ui/ui/admin/tests/acceptance/host-catalogs/host-sets/delete-test.js
T
hashicorp-copywrite[bot]andCameron Perera 3d610427d0 [COMPLIANCE] Add Copyright and License Headers (#1617)
* [COMPLIANCE] Add Copyright and License Headers

* chore: 🤖 update Copyright headers to satisfy our lint rules

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
2023-03-01 09:39:41 -06:00

140 lines
4.6 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { visit, find, click } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { Response } from 'miragejs';
import { resolve, reject } from 'rsvp';
import sinon from 'sinon';
import {
authenticateSession,
// These are left here intentionally for future reference.
//currentSession,
//invalidateSession,
} from 'ember-simple-auth/test-support';
module('Acceptance | host-catalogs | host sets | delete', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
let getHostSetCount;
const instances = {
scopes: {
global: null,
org: null,
project: null,
hostCatalog: null,
host: null,
},
};
const urls = {
globalScope: null,
orgScope: null,
projectScope: null,
hostCatalogs: null,
hostCatalog: null,
hostSets: null,
hostSet: null,
unknownHostSet: null,
newHostSet: null,
};
hooks.beforeEach(function () {
// Generate resources
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.hostCatalog = this.server.create('host-catalog', {
scope: instances.scopes.project,
});
instances.hostSet = this.server.create('host-set', {
scope: instances.scopes.project,
hostCatalog: instances.hostCatalog,
});
// Generate route URLs for resources
urls.globalScope = `/scopes/global/scopes`;
urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`;
urls.projectScope = `/scopes/${instances.scopes.project.id}`;
urls.hostCatalogs = `${urls.projectScope}/host-catalogs`;
urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`;
urls.hostSets = `${urls.hostCatalog}/host-sets`;
urls.hostSet = `${urls.hostSets}/${instances.hostSet.id}`;
urls.unknownHostSet = `${urls.hostSets}/foo`;
urls.newHostSet = `${urls.hostSets}/new`;
// Generate resource couner
getHostSetCount = () => this.server.schema.hostSets.all().models.length;
authenticateSession({});
});
test('can delete host', async function (assert) {
assert.expect(1);
const count = getHostSetCount();
await visit(urls.hostSet);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.strictEqual(getHostSetCount(), count - 1);
});
test('can accept delete host set via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
confirmService.confirm = sinon.fake.returns(resolve());
const count = getHostSetCount();
await visit(urls.hostSet);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.strictEqual(getHostSetCount(), count - 1);
assert.ok(confirmService.confirm.calledOnce);
});
test('cannot delete host set without proper authorization', async function (assert) {
assert.expect(1);
instances.hostSet.authorized_actions =
instances.hostSet.authorized_actions.filter((item) => item !== 'delete');
await visit(urls.hostSet);
assert.notOk(
find('.rose-layout-page-actions .rose-dropdown-button-danger')
);
});
test('cannot cancel delete host set via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
confirmService.confirm = sinon.fake.returns(reject());
const count = getHostSetCount();
await visit(urls.hostSet);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.strictEqual(getHostSetCount(), count);
assert.ok(confirmService.confirm.calledOnce);
});
test('deleting a host set which errors displays error messages', async function (assert) {
assert.expect(1);
this.server.del('/host-sets/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
}
);
});
await visit(urls.hostSet);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.');
});
});