Files
threshold-ui/ui/admin/tests/acceptance/targets/create-test.js
T

355 lines
11 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2020-10-28 13:36:41 -04:00
import { module, test } from 'qunit';
2022-04-06 09:30:49 -05:00
import {
visit,
currentURL,
find,
click,
fillIn,
getContext,
} from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
2020-10-28 13:36:41 -04:00
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { Response } from 'miragejs';
2024-11-22 11:27:53 -08:00
import { authenticateSession } from 'ember-simple-auth/test-support';
2022-12-20 12:07:52 -05:00
import { TYPE_TARGET_TCP, TYPE_TARGET_SSH } from 'api/models/target';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
2020-10-28 13:36:41 -04:00
2021-08-31 23:34:32 -07:00
module('Acceptance | targets | create', function (hooks) {
2020-10-28 13:36:41 -04:00
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
2020-10-28 13:36:41 -04:00
let getTargetCount;
let getTCPTargetCount;
let getSSHTargetCount;
2022-04-06 09:30:49 -05:00
let featuresService;
2020-10-28 13:36:41 -04:00
const SAVE_BTN_SELECTOR = '[type="submit"]';
2020-10-28 13:36:41 -04:00
const instances = {
scopes: {
global: null,
org: null,
project: null,
2020-11-16 15:08:59 -05:00
},
target: null,
2020-10-28 13:36:41 -04:00
};
const urls = {
orgScope: null,
projectScope: null,
targets: null,
target: null,
newTarget: null,
newTCPTarget: null,
newSSHTarget: null,
2020-10-28 13:36:41 -04:00
};
hooks.beforeEach(async function () {
2022-04-06 09:30:49 -05:00
const { owner } = getContext();
featuresService = owner.lookup('service:features');
2020-10-28 13:36:41 -04:00
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,
});
// Generate route URLs for resources
urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`;
urls.projectScope = `/scopes/${instances.scopes.project.id}`;
2020-10-28 14:17:55 -04:00
urls.targets = `${urls.projectScope}/targets`;
urls.target = `${urls.targets}/${instances.target.id}`;
urls.unknownTarget = `${urls.targets}/foo`;
2022-04-06 09:30:49 -05:00
urls.newTarget = `${urls.targets}/new`;
urls.newTCPTarget = `${urls.targets}/new?type=tcp`;
urls.newSSHTarget = `${urls.targets}/new?type=ssh`;
// Generate resource counter
2020-10-28 13:36:41 -04:00
getTargetCount = () => this.server.schema.targets.all().models.length;
getSSHTargetCount = () =>
2022-12-20 12:07:52 -05:00
this.server.schema.targets.where({ type: TYPE_TARGET_SSH }).models.length;
getTCPTargetCount = () =>
2022-12-20 12:07:52 -05:00
this.server.schema.targets.where({ type: TYPE_TARGET_TCP }).models.length;
await authenticateSession({});
2020-10-28 13:36:41 -04:00
});
2022-12-19 16:59:51 -05:00
test('defaults to type `ssh` when no query param provided', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
2022-04-06 09:30:49 -05:00
await visit(urls.newTarget);
assert.strictEqual(find('[name="Type"]:checked').value, TYPE_TARGET_SSH);
2022-04-06 09:30:49 -05:00
});
test('can create a type `ssh` target', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
const targetCount = getTargetCount();
2022-12-19 16:59:51 -05:00
const sshTargetCount = getSSHTargetCount();
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2022-12-19 16:59:51 -05:00
await click('[value="ssh"]');
await fillIn('[name="name"]', 'random string');
await click(SAVE_BTN_SELECTOR);
2022-12-19 16:59:51 -05:00
assert.strictEqual(getSSHTargetCount(), sshTargetCount + 1);
assert.strictEqual(getTargetCount(), targetCount + 1);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1].name,
'random string',
);
2020-10-28 13:36:41 -04:00
});
test('can create a type `tcp` target', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
2023-01-23 14:48:29 -08:00
const targetCount = getTargetCount();
const tcpTargetCount = getTCPTargetCount();
await visit(urls.targets);
2020-10-28 13:36:41 -04:00
2023-01-23 14:48:29 -08:00
await click(`[href="${urls.newTarget}"]`);
await click('[value="tcp"]');
await fillIn('[name="name"]', 'random string');
await click(SAVE_BTN_SELECTOR);
2023-01-23 14:48:29 -08:00
assert.strictEqual(getTargetCount(), targetCount + 1);
assert.strictEqual(getTCPTargetCount(), tcpTargetCount + 1);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1].name,
'random string',
2023-01-23 14:48:29 -08:00
);
});
test('default port is not marked required for SSH targets', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
await visit(urls.newTarget);
assert.dom('[data-test-default-port-label]').includesText('Optional');
});
test('default port is marked required for TCP targets', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
await visit(urls.newTarget);
await click('[value="tcp"]');
assert.dom('[data-test-default-port-label]').includesText('Required');
});
2021-08-31 23:34:32 -07:00
test('can navigate to new targets route with proper authorization', async function (assert) {
await visit(urls.projectScope);
await click(`[href="${urls.targets}"]`);
assert.true(
2021-08-31 23:34:32 -07:00
instances.scopes.project.authorized_collection_actions.targets.includes(
'create',
),
2021-08-31 23:34:32 -07:00
);
assert.dom(`[href="${urls.newTarget}"]`).exists();
2021-08-31 23:34:32 -07:00
});
test('cannot navigate to new targets route without proper authorization', async function (assert) {
instances.scopes.project.authorized_collection_actions.targets =
instances.scopes.project.authorized_collection_actions.targets.filter(
(item) => item !== 'create',
);
await visit(urls.projectScope);
await click(`[href="${urls.targets}"]`);
assert.false(
2021-08-31 23:34:32 -07:00
instances.scopes.project.authorized_collection_actions.targets.includes(
'create',
),
2021-08-31 23:34:32 -07:00
);
assert.dom('.rose-layout-page-actions a').doesNotExist();
2022-04-06 09:30:49 -05:00
});
test('cannot navigate to new SSH targets route when ssh feature is disabled', async function (assert) {
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2023-03-31 17:38:17 -07:00
assert.false(featuresService.isEnabled('ssh-target'));
assert.true(
2022-04-06 09:30:49 -05:00
instances.scopes.project.authorized_collection_actions.targets.includes(
'create',
),
2022-04-06 09:30:49 -05:00
);
assert.dom('.info-field').exists({ count: 1 });
assert.dom('.info-field .hds-form-helper-text').includesText('TCP');
2021-08-31 23:34:32 -07:00
});
2022-04-06 09:30:49 -05:00
test('can cancel create new TCP target', async function (assert) {
const targetCount = getTargetCount();
const tcpTargetCount = getTCPTargetCount();
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2020-10-28 13:36:41 -04:00
await fillIn('[name="name"]', 'random string');
await click('.rose-form-actions [type="button"]');
assert.strictEqual(currentURL(), urls.targets);
assert.strictEqual(getTargetCount(), targetCount);
assert.strictEqual(getTCPTargetCount(), tcpTargetCount);
2020-10-28 13:36:41 -04:00
});
test('can add aliases during target creation', async function (assert) {
const targetCount = getTargetCount();
const tcpTargetCount = getTCPTargetCount();
await visit(urls.targets);
const name = 'target';
await click(`[href="${urls.newTarget}"]`);
await fillIn('[name="name"]', name);
assert.ok(
instances.scopes.global.authorized_collection_actions.aliases.includes(
'create',
),
);
await fillIn(
'[name="with_aliases"] tbody tr:nth-of-type(1) input',
'alias 1',
);
await click('[name="with_aliases"] tbody tr:nth-of-type(1) button');
await fillIn(
'[name="with_aliases"] tbody tr:nth-of-type(2) input',
'alias 2',
);
await click('[name="with_aliases"] tbody tr:nth-of-type(2) button');
await click(SAVE_BTN_SELECTOR);
assert.strictEqual(getTargetCount(), targetCount + 1);
const target = this.server.schema.targets.findBy({ name });
assert.deepEqual(target.withAliases, [
{ value: 'alias 1', scope_id: 'global' },
{ value: 'alias 2', scope_id: 'global' },
]);
assert.strictEqual(getTCPTargetCount(), tcpTargetCount + 1);
});
2022-04-06 09:30:49 -05:00
test('can cancel create new SSH target', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('ssh-target');
const targetCount = getTargetCount();
const sshTargetCount = getSSHTargetCount();
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2022-04-06 09:30:49 -05:00
await fillIn('[name="name"]', 'random string');
await click('[value="ssh"]');
2022-04-06 09:30:49 -05:00
await click('.rose-form-actions [type="button"]');
assert.strictEqual(currentURL(), urls.targets);
assert.strictEqual(getTargetCount(), targetCount);
assert.strictEqual(getSSHTargetCount(), sshTargetCount);
2022-04-06 09:30:49 -05:00
});
test('saving a new TCP target with invalid fields displays error messages', async function (assert) {
2020-10-28 13:36:41 -04:00
this.server.post('/targets', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {
request_fields: [
{
name: 'name',
description: 'Name is required.',
},
],
},
},
2020-10-28 13:36:41 -04:00
);
});
await visit(urls.newTCPTarget);
await click(SAVE_BTN_SELECTOR);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('The request was invalid.');
assert.dom('.hds-form-error__message').hasText('Name is required.');
2020-10-28 13:36:41 -04:00
});
2022-04-06 09:30:49 -05:00
test('saving a new SSH target with invalid fields displays error messages', async function (assert) {
this.server.post('/targets', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {
request_fields: [
{
name: 'name',
description: 'Name is required.',
},
],
},
},
2022-04-06 09:30:49 -05:00
);
});
await visit(urls.newSSHTarget);
await click(SAVE_BTN_SELECTOR);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('The request was invalid.');
assert.dom('.hds-form-error__message').hasText('Name is required.');
2022-04-06 09:30:49 -05:00
});
test('can save address', async function (assert) {
2023-03-31 17:38:17 -07:00
featuresService.enable('target-network-address');
const targetCount = getTargetCount();
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2023-03-31 17:38:17 -07:00
await fillIn('[name="name"]', 'random string');
await fillIn('[name="address"]', '0.0.0.0');
await click(SAVE_BTN_SELECTOR);
assert.strictEqual(getTargetCount(), targetCount + 1);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1].address,
'0.0.0.0',
);
});
test('address field does not exist when target network address feature is disabled', async function (assert) {
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
2023-03-31 17:38:17 -07:00
assert.false(featuresService.isEnabled('target-network-address'));
assert.dom('[name="address"]').doesNotExist();
});
test('users cannot directly navigate to new storage bucket route without proper authorization', async function (assert) {
instances.scopes.project.authorized_collection_actions.targets =
instances.scopes.project.authorized_collection_actions.targets.filter(
(item) => item !== 'create',
);
await visit(urls.newSSHTarget);
assert.false(
instances.scopes.project.authorized_collection_actions.targets.includes(
'create',
),
);
assert.strictEqual(currentURL(), urls.targets);
});
2020-10-28 13:36:41 -04:00
});