From f4535c83077e3772be11dc72566aaf653943fba0 Mon Sep 17 00:00:00 2001 From: Cameron Perera Date: Wed, 6 Apr 2022 09:30:49 -0500 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20Add=20tests=20specific?= =?UTF-8?q?=20to=20SSH=20target=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: 💍 Add tests specific to SSH target creation * test: 💍 creating new SSH target when enabled or disabled Verifying the link to create new SSH targets is not present when ssh-targets is not enabled * refactor: update create targets acceptance test names --- ui/admin/config/environment.js | 1 + .../tests/acceptance/targets/create-test.js | 84 +++++++++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/ui/admin/config/environment.js b/ui/admin/config/environment.js index d5478cb6b..a81438a4c 100644 --- a/ui/admin/config/environment.js +++ b/ui/admin/config/environment.js @@ -157,6 +157,7 @@ module.exports = function (environment) { ENV.enableConfirmService = false; // Enable tests for development features + ENV.featureFlags['ssh-target'] = true; } if (environment === 'production') { diff --git a/ui/admin/tests/acceptance/targets/create-test.js b/ui/admin/tests/acceptance/targets/create-test.js index 7e46e243e..371f9e050 100644 --- a/ui/admin/tests/acceptance/targets/create-test.js +++ b/ui/admin/tests/acceptance/targets/create-test.js @@ -1,5 +1,12 @@ import { module, test } from 'qunit'; -import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers'; +import { + visit, + currentURL, + find, + click, + fillIn, + getContext, +} from '@ember/test-helpers'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { Response } from 'miragejs'; @@ -15,6 +22,7 @@ module('Acceptance | targets | create', function (hooks) { setupMirage(hooks); let getTargetCount; + let featuresService; const instances = { scopes: { @@ -34,6 +42,8 @@ module('Acceptance | targets | create', function (hooks) { }; hooks.beforeEach(function () { + const { owner } = getContext(); + featuresService = owner.lookup('service:features'); instances.scopes.global = this.server.create('scope', { id: 'global' }); instances.scopes.org = this.server.create('scope', { type: 'org', @@ -53,6 +63,7 @@ module('Acceptance | targets | create', function (hooks) { urls.targets = `${urls.projectScope}/targets`; urls.target = `${urls.targets}/${instances.target.id}`; urls.unknownTarget = `${urls.targets}/foo`; + urls.newTarget = `${urls.targets}/new`; urls.newTCPTarget = `${urls.targets}/new?type=tcp`; urls.newSSHTarget = `${urls.targets}/new?type=ssh`; // Generate resource couner @@ -60,6 +71,12 @@ module('Acceptance | targets | create', function (hooks) { authenticateSession({}); }); + test('defaults to a new TCP target when no query param provided', async function (assert) { + assert.expect(1); + await visit(urls.newTarget); + assert.equal(find('input[disabled]').value, 'tcp'); + }); + test('can create new targets of type TCP', async function (assert) { assert.expect(1); const count = getTargetCount(); @@ -79,7 +96,7 @@ module('Acceptance | targets | create', function (hooks) { }); test('can navigate to new targets route with proper authorization', async function (assert) { - assert.expect(2); + assert.expect(3); await visit(urls.targets); assert.ok( instances.scopes.project.authorized_collection_actions.targets.includes( @@ -87,10 +104,11 @@ module('Acceptance | targets | create', function (hooks) { ) ); assert.ok(find(`[href="${urls.newTCPTarget}"]`)); + assert.ok(find(`[href="${urls.newSSHTarget}"]`)); }); test('cannot navigate to new targets route without proper authorization', async function (assert) { - assert.expect(2); + assert.expect(3); instances.scopes.project.authorized_collection_actions.targets = []; await visit(urls.targets); assert.notOk( @@ -99,9 +117,22 @@ module('Acceptance | targets | create', function (hooks) { ) ); assert.notOk(find(`[href="${urls.newTCPTarget}"]`)); + assert.notOk(find(`[href="${urls.newSSHTarget}"]`)); }); - test('can cancel create new targets', async function (assert) { + test('cannot navigate to new SSH targets route when ssh feature is disabled', async function (assert) { + featuresService.disable('ssh-target'); + assert.expect(2); + await visit(urls.targets); + assert.ok( + instances.scopes.project.authorized_collection_actions.targets.includes( + 'create' + ) + ); + assert.notOk(find(`[href="${urls.newSSHTarget}"]`)); + }); + + test('can cancel create new TCP target', async function (assert) { assert.expect(2); const count = getTargetCount(); await visit(urls.newTCPTarget); @@ -111,7 +142,17 @@ module('Acceptance | targets | create', function (hooks) { assert.equal(getTargetCount(), count); }); - test('saving a new target with invalid fields displays error messages', async function (assert) { + test('can cancel create new SSH target', async function (assert) { + assert.expect(2); + const count = getTargetCount(); + await visit(urls.newSSHTarget); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.equal(currentURL(), urls.targets); + assert.equal(getTargetCount(), count); + }); + + test('saving a new TCP target with invalid fields displays error messages', async function (assert) { assert.expect(2); this.server.post('/targets', () => { return new Response( @@ -143,4 +184,37 @@ module('Acceptance | targets | create', function (hooks) { 'Name is required.' ); }); + + 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.ok( + find('[role="alert"]').textContent.trim(), + 'The request was invalid.' + ); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.' + ); + }); });