From 7bc8fe90464949b2f79cb5591d725989630575d2 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 23 Sep 2020 18:09:57 -0400 Subject: [PATCH 1/2] test: Add hosts acceptance test --- .../acceptance/host-catalogs/hosts-test.js | 32 +++- ui/core/tests/acceptance/hosts-test.js | 180 ++++++++++++++---- 2 files changed, 176 insertions(+), 36 deletions(-) diff --git a/ui/core/tests/acceptance/host-catalogs/hosts-test.js b/ui/core/tests/acceptance/host-catalogs/hosts-test.js index 5b4720a24..565ef41a8 100644 --- a/ui/core/tests/acceptance/host-catalogs/hosts-test.js +++ b/ui/core/tests/acceptance/host-catalogs/hosts-test.js @@ -1,8 +1,9 @@ import { module, test } from 'qunit'; -import { visit, currentURL, fillIn, click, findAll } from '@ember/test-helpers'; +import { visit, currentURL, fillIn, click, find, findAll } from '@ember/test-helpers'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import a11yAudit from 'ember-a11y-testing/test-support/audit'; +import { Response } from 'miragejs'; import { authenticateSession, // These are left here intentionally for future reference. @@ -99,4 +100,33 @@ module('Acceptance | host catalogs | hosts', function (hooks) { assert.equal(currentURL(), urls.hosts); assert.equal(findAll('tbody tr').length, hostsCount); }); + + test('saving a new host with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/hosts', () => { + 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.newHost); + await fillIn('[name="name"]', 'new target'); + await click('form [type="submit"]'); + await a11yAudit(); + assert.ok(find('[role="alert"]')); + assert.ok(find('.rose-form-error-message')); + }); }); diff --git a/ui/core/tests/acceptance/hosts-test.js b/ui/core/tests/acceptance/hosts-test.js index 33ab3f05b..5f6a6214c 100644 --- a/ui/core/tests/acceptance/hosts-test.js +++ b/ui/core/tests/acceptance/hosts-test.js @@ -1,61 +1,171 @@ import { module, test } from 'qunit'; -// import { visit, currentURL } from '@ember/test-helpers'; +import { visit, currentURL, fillIn, click, find } from '@ember/test-helpers'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; -// import a11yAudit from 'ember-a11y-testing/test-support/audit'; -// import { -// authenticateSession, -// // These are left here intentionally for future reference. -// //currentSession, -// //invalidateSession, -// } from 'ember-simple-auth/test-support'; +import a11yAudit from 'ember-a11y-testing/test-support/audit'; +import { Response } from 'miragejs'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; module('Acceptance | hosts', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + hostCatalog: null, + host: null, + }; + const urls = { + orgScope: null, + projects: null, + project: null, + hostCatalogs: null, + hostCatalog: null, + hosts: null, + host: null, + newHost: null + }; + hooks.beforeEach(function () { // Setup Mirage mock resources for this test + authenticateSession(); + 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.host = this.server.create('host', { + scope: instances.scopes.project, + hostCatalog: instances.hostCatalog + }); // Generate route URLs for resources + urls.orgScope = `/scopes/${instances.scopes.org.id}`; + urls.projects = `${urls.orgScope}/projects`; + urls.project = `${urls.projects}/${instances.hostCatalog.scope.id}`; + urls.hostCatalogs = `${urls.project}/host-catalogs`; + urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`; + urls.hosts = `${urls.hostCatalog}/hosts`; + urls.host = `${urls.hosts}/${instances.host.id}`; }); - test('visiting host', async function (assert) { - assert.expect(0); - }); - - test('can navigate to a host form', async function (assert) { - assert.expect(0); + test('visiting a host', async function (assert) { + assert.expect(1); + await visit(urls.host); + await a11yAudit(); + assert.equal(currentURL(), urls.host); }); test('can update a host and save changes', async function (assert) { - assert.expect(0); + assert.expect(4); + await visit(urls.host); + await fillIn('[name="name"]', 'Updated name'); + await fillIn('[name="description"]', 'Updated description'); + await fillIn('[name="address"]', '::1'); + await click('.rose-form-actions [type="submit"]'); + assert.equal(currentURL(), urls.host); + assert.equal(this.server.db.hosts[0].name, 'Updated name'); + assert.equal(this.server.db.hosts[0].description, 'Updated description'); + assert.equal(this.server.db.hosts[0].attributes.address, '::1'); }); test('can update a host and cancel changes', async function (assert) { - assert.expect(0); + assert.expect(1); + await visit(urls.host); + await fillIn('[name="name"]', 'Updated name'); + await click('.rose-form-actions [type="button"]'); + assert.notEqual(find('[name="name"]').value, 'Updated name'); }); - test('can create new host', async function (assert) { - assert.expect(0); - }); - - test('can cancel new host creation', async function (assert) { - assert.expect(0); - }); - - test('saving a new host with invalid fields displays error messages', async function (assert) { - assert.expect(0); - }); - - test('errors are displayed when save on auth method fails', async function (assert) { - assert.expect(0); - }); - - test('errors are displayed when delete on a host fails', async function (assert) { - assert.expect(0); + test('can delete host', async function (assert) { + assert.expect(2); + const hostsCount = this.server.db.hosts.length; + await visit(urls.host); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(currentURL(), urls.hosts); + assert.equal(this.server.db.roles.length, hostsCount - 1); }); test('saving an existing host with invalid fields displays error messages', async function (assert) { - assert.expect(0); + assert.expect(2); + this.server.patch('/hosts/:id', () => { + 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.host); + await fillIn('[name="name"]', 'existing host'); + await click('[type="submit"]'); + await a11yAudit(); + assert.ok(find('[role="alert"]')); + assert.ok(find('.rose-form-error-message')); + }); + + test('errors are displayed when save on host fails', async function (assert) { + assert.expect(1); + this.server.patch('/hosts/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.host); + await fillIn('[name="name"]', 'save host'); + await click('form [type="submit"]'); + await a11yAudit(); + assert.ok(find('[role="alert"]')); + }); + + test('errors are displayed when delete on a host fails', async function (assert) { + assert.expect(1); + this.server.del('/hosts/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.host); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + await a11yAudit(); + assert.ok(find('[role="alert"]')); }); }); From 068e74304d1bb62638e869b69201a3148ac5025b Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 23 Sep 2020 18:33:39 -0400 Subject: [PATCH 2/2] Fix host deletion test --- ui/core/tests/acceptance/hosts-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/core/tests/acceptance/hosts-test.js b/ui/core/tests/acceptance/hosts-test.js index 5f6a6214c..fd8f580f4 100644 --- a/ui/core/tests/acceptance/hosts-test.js +++ b/ui/core/tests/acceptance/hosts-test.js @@ -98,7 +98,7 @@ module('Acceptance | hosts', function (hooks) { await visit(urls.host); await click('.rose-layout-page-actions .rose-dropdown-button-danger'); assert.equal(currentURL(), urls.hosts); - assert.equal(this.server.db.roles.length, hostsCount - 1); + assert.equal(this.server.db.hosts.length, hostsCount - 1); }); test('saving an existing host with invalid fields displays error messages', async function (assert) {