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

270 lines
8.3 KiB
JavaScript
Raw Normal View History

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';
2020-10-28 13:36:41 -04:00
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { Response } from 'miragejs';
import {
authenticateSession,
// These are left here intentionally for future reference.
//currentSession,
//invalidateSession,
} from 'ember-simple-auth/test-support';
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);
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 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(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 = () =>
this.server.schema.targets.where({ type: 'ssh' }).models.length;
getTCPTargetCount = () =>
this.server.schema.targets.where({ type: 'tcp' }).models.length;
2020-10-28 13:36:41 -04:00
authenticateSession({});
});
2022-04-06 09:30:49 -05:00
test('defaults to a new TCP target when no query param provided', async function (assert) {
assert.expect(1);
await visit(urls.newTarget);
assert.strictEqual(find('input[name=tcp]:checked').value, 'tcp');
2022-04-06 09:30:49 -05:00
});
test('can create new targets of type TCP', async function (assert) {
assert.expect(4);
const targetCount = getTargetCount();
const tcpTargetCount = getTCPTargetCount();
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
await fillIn('[name="name"]', 'random string');
await fillIn('[name="worker_filter"]', 'random filter');
await click('[type="submit"]');
assert.strictEqual(getTargetCount(), targetCount + 1);
assert.strictEqual(getTCPTargetCount(), tcpTargetCount + 1);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1].name,
'random string'
);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1]
.workerFilter,
'random filter'
);
});
test('can create new targets of type SSH', async function (assert) {
assert.expect(4);
const targetCount = getTargetCount();
const sshTargetCount = getSSHTargetCount();
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('[value="ssh"]');
await fillIn('[name="worker_filter"]', 'random filter');
2020-10-28 13:36:41 -04:00
await click('[type="submit"]');
assert.strictEqual(getSSHTargetCount(), sshTargetCount + 1);
assert.strictEqual(getTargetCount(), targetCount + 1);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1].name,
'random string'
);
assert.strictEqual(
this.server.schema.targets.all().models[getTargetCount() - 1]
.workerFilter,
'random filter'
);
2020-10-28 13:36:41 -04:00
});
2021-08-31 23:34:32 -07:00
test('can navigate to new targets route with proper authorization', async function (assert) {
2022-04-12 14:05:16 -07:00
assert.expect(2);
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'
)
);
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) {
2022-04-12 14:05:16 -07:00
assert.expect(2);
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'
)
);
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) {
assert.expect(3);
2022-04-06 09:30:49 -05:00
featuresService.disable('ssh-target');
await visit(urls.targets);
await click(`[href="${urls.newTarget}"]`);
assert.true(
2022-04-06 09:30:49 -05:00
instances.scopes.project.authorized_collection_actions.targets.includes(
'create'
)
);
assert.dom('.info-field').exists({ count: 1 });
assert.dom('.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) {
assert.expect(3);
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
});
2022-04-06 09:30:49 -05:00
test('can cancel create new SSH target', async function (assert) {
assert.expect(3);
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
assert.expect(2);
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.',
},
],
},
}
);
});
await visit(urls.newTCPTarget);
2020-10-28 13:36:41 -04:00
await click('[type="submit"]');
assert.dom('[role="alert"] div').hasText('The request was invalid.');
assert.dom('.rose-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) {
assert.expect(2);
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.',
},
],
},
}
);
});
await visit(urls.newSSHTarget);
await click('[type="submit"]');
assert.dom('[role="alert"] div').hasText('The request was invalid.');
assert.dom('.rose-form-error-message').hasText('Name is required.');
2022-04-06 09:30:49 -05:00
});
2020-10-28 13:36:41 -04:00
});