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
|
|
|
*/
|
|
|
|
|
|
2020-10-28 19:00:35 -04:00
|
|
|
import { module, test } from 'qunit';
|
2024-11-04 08:43:41 -08:00
|
|
|
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
|
2024-12-02 12:51:24 -06:00
|
|
|
import { setupApplicationTest } from 'admin/tests/helpers';
|
2020-10-28 19:00:35 -04:00
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
|
|
|
|
import { Response } from 'miragejs';
|
2024-11-04 08:43:41 -08:00
|
|
|
import { authenticateSession } from 'ember-simple-auth/test-support';
|
|
|
|
|
import * as selectors from './selectors';
|
|
|
|
|
import * as commonSelectors from 'admin/tests/helpers/selectors';
|
2020-10-28 19:00:35 -04:00
|
|
|
|
2021-09-02 15:28:40 -07:00
|
|
|
module('Acceptance | host-catalogs | hosts | create', function (hooks) {
|
2020-10-28 19:00:35 -04:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
|
setupMirage(hooks);
|
|
|
|
|
|
|
|
|
|
let getHostCount;
|
|
|
|
|
|
|
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
global: null,
|
|
|
|
|
org: null,
|
|
|
|
|
project: null,
|
|
|
|
|
hostCatalog: null,
|
2020-11-16 15:08:59 -05:00
|
|
|
host: null,
|
|
|
|
|
},
|
2020-10-28 19:00:35 -04:00
|
|
|
};
|
|
|
|
|
const urls = {
|
|
|
|
|
globalScope: null,
|
|
|
|
|
orgScope: null,
|
|
|
|
|
projectScope: null,
|
|
|
|
|
hostCatalogs: null,
|
|
|
|
|
hostCatalog: null,
|
|
|
|
|
hosts: null,
|
|
|
|
|
host: null,
|
|
|
|
|
unknownHost: null,
|
|
|
|
|
newHost: null,
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-03 10:27:27 -08:00
|
|
|
hooks.beforeEach(async function () {
|
2020-10-28 19:00:35 -04:00
|
|
|
// 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', {
|
2023-11-02 16:08:25 -07:00
|
|
|
type: 'static',
|
2020-10-28 19:00:35 -04:00
|
|
|
scope: instances.scopes.project,
|
|
|
|
|
});
|
|
|
|
|
instances.host = this.server.create('host', {
|
|
|
|
|
scope: instances.scopes.project,
|
2020-11-16 15:08:59 -05:00
|
|
|
hostCatalog: instances.hostCatalog,
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|
|
|
|
|
// 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.hosts = `${urls.hostCatalog}/hosts`;
|
|
|
|
|
urls.host = `${urls.hosts}/${instances.host.id}`;
|
|
|
|
|
urls.unknownHost = `${urls.hosts}/foo`;
|
|
|
|
|
urls.newHost = `${urls.hosts}/new`;
|
|
|
|
|
// Generate resource couner
|
|
|
|
|
getHostCount = () => this.server.schema.hosts.all().models.length;
|
2024-12-03 10:27:27 -08:00
|
|
|
await authenticateSession({});
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('can create new host', async function (assert) {
|
|
|
|
|
const count = getHostCount();
|
|
|
|
|
await visit(urls.newHost);
|
2024-11-04 08:43:41 -08:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await fillIn(
|
|
|
|
|
commonSelectors.FIELD_DESCRIPTION,
|
|
|
|
|
commonSelectors.FIELD_DESCRIPTION_VALUE,
|
|
|
|
|
);
|
|
|
|
|
await fillIn(selectors.FIELD_ADDRESS, selectors.FIELD_ADDRESS_VALUE);
|
|
|
|
|
await click(commonSelectors.SAVE_BTN);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(getHostCount(), count + 1);
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|
|
|
|
|
|
2021-09-02 15:28:40 -07:00
|
|
|
test('Users cannot create a new host without proper authorization', async function (assert) {
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts = [];
|
|
|
|
|
await visit(urls.hostCatalog);
|
|
|
|
|
assert.notOk(
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-09-02 15:28:40 -07:00
|
|
|
);
|
2024-11-04 08:43:41 -08:00
|
|
|
|
|
|
|
|
assert.dom(commonSelectors.HREF(urls.newHost)).doesNotExist();
|
2021-09-02 15:28:40 -07:00
|
|
|
});
|
2025-01-08 09:14:29 -08:00
|
|
|
|
2021-11-05 14:23:20 -04:00
|
|
|
test('Users can navigate to new host route with proper authorization', async function (assert) {
|
|
|
|
|
await visit(urls.hosts);
|
2025-01-08 09:14:29 -08:00
|
|
|
|
2021-09-02 15:28:40 -07:00
|
|
|
assert.ok(
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-09-02 15:28:40 -07:00
|
|
|
);
|
2024-12-13 15:57:10 -08:00
|
|
|
assert.dom(selectors.MANAGE_DROPDOWN_HOST_CATALOG).exists();
|
2025-01-08 09:14:29 -08:00
|
|
|
await click(selectors.MANAGE_DROPDOWN_HOST_CATALOG);
|
|
|
|
|
assert
|
|
|
|
|
.dom(selectors.MANAGE_DROPDOWN_HOST_CATALOG_NEW_HOST)
|
|
|
|
|
.hasAttribute('href', urls.newHost);
|
|
|
|
|
await click(selectors.MANAGE_DROPDOWN_HOST_CATALOG_NEW_HOST);
|
|
|
|
|
assert.strictEqual(currentURL(), urls.newHost);
|
2021-09-02 15:28:40 -07:00
|
|
|
});
|
|
|
|
|
|
2021-11-05 14:23:20 -04:00
|
|
|
test('Users cannot navigate to new host route without proper authorization', async function (assert) {
|
2021-09-02 15:28:40 -07:00
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts = [];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.hosts);
|
2021-09-02 15:28:40 -07:00
|
|
|
assert.notOk(
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-09-02 15:28:40 -07:00
|
|
|
);
|
2024-11-04 08:43:41 -08:00
|
|
|
assert.dom(commonSelectors.HREF(urls.newHost)).doesNotExist();
|
2021-09-02 15:28:40 -07:00
|
|
|
});
|
|
|
|
|
|
2020-10-28 19:00:35 -04:00
|
|
|
test('can cancel create new host', async function (assert) {
|
|
|
|
|
const count = getHostCount();
|
|
|
|
|
await visit(urls.newHost);
|
2024-11-04 08:43:41 -08:00
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await click(commonSelectors.CANCEL_BTN);
|
|
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.hosts);
|
|
|
|
|
assert.strictEqual(getHostCount(), count);
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('saving a new host with invalid fields displays error messages', async function (assert) {
|
|
|
|
|
this.server.post('/hosts', () => {
|
|
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'invalid_argument',
|
|
|
|
|
message: 'The request was invalid.',
|
|
|
|
|
details: {
|
|
|
|
|
request_fields: [
|
|
|
|
|
{
|
2024-12-11 08:04:45 -08:00
|
|
|
name: 'address',
|
|
|
|
|
description: 'Address is required.',
|
2020-10-28 19:00:35 -04:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
},
|
2020-10-28 19:00:35 -04:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
await visit(urls.newHost);
|
2024-11-04 08:43:41 -08:00
|
|
|
await click(commonSelectors.SAVE_BTN);
|
|
|
|
|
|
|
|
|
|
assert
|
|
|
|
|
.dom(commonSelectors.ALERT_TOAST_BODY)
|
|
|
|
|
.hasText('The request was invalid.');
|
|
|
|
|
|
2024-12-11 08:04:45 -08:00
|
|
|
assert.dom(selectors.FIELD_ADDRESS_ERROR).hasText('Address is required.');
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|
2023-11-02 16:08:25 -07:00
|
|
|
|
|
|
|
|
test('users cannot directly navigate to new host route without proper authorization', async function (assert) {
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts =
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts.filter(
|
2023-11-22 14:07:41 -05:00
|
|
|
(item) => item !== 'create',
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await visit(urls.newHost);
|
|
|
|
|
|
|
|
|
|
assert.false(
|
|
|
|
|
instances.hostCatalog.authorized_collection_actions.hosts.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
assert.strictEqual(currentURL(), urls.hosts);
|
|
|
|
|
});
|
2020-10-28 19:00:35 -04:00
|
|
|
});
|