From 41688ad80ca3f39bb1f6dbc3de00550345881566 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Sun, 20 Sep 2020 16:15:13 -0400 Subject: [PATCH 1/6] refactor: Refactor role acceptance test to config objects --- ui/core/tests/acceptance/roles-test.js | 74 ++++++++++++++------------ 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js index df68d06eb..fb1eccf2b 100644 --- a/ui/core/tests/acceptance/roles-test.js +++ b/ui/core/tests/acceptance/roles-test.js @@ -15,49 +15,53 @@ module('Acceptance | roles', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); - let orgScope; - let rolesURL; - let roleURL; - let newRoleURL; + const instances = { + scopes: { + global: null, + org: null, + }, + role: null, + }; + const urls = { + orgScope: null, + roles: null, + role: null, + newRole: null, + }; hooks.beforeEach(function () { - orgScope = this.server.create( - 'scope', - { - type: 'org', - }, - 'withChildren' - ); - - const role = this.server.create('role', { - scope: orgScope, - }); - - rolesURL = `/scopes/${orgScope.id}/roles`; - roleURL = `${rolesURL}/${role.id}`; - newRoleURL = `${rolesURL}/new`; - 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.role = this.server.create('role', { + scope: instances.scopes.org, + }); + urls.roles = `/scopes/${instances.scopes.org.id}/roles`; + urls.role = `${urls.roles}/${instances.role.id}`; + urls.newRole = `${urls.roles}/new`; }); test('visiting roles', async function (assert) { assert.expect(1); - await visit(rolesURL); + await visit(urls.roles); await a11yAudit(); - assert.equal(currentURL(), rolesURL); + assert.equal(currentURL(), urls.roles); }); test('visiting a role', async function (assert) { assert.expect(1); - await visit(newRoleURL); + await visit(urls.newRole); await a11yAudit(); - assert.equal(currentURL(), newRoleURL); + assert.equal(currentURL(), urls.newRole); }); test('can create new role', async function (assert) { assert.expect(1); const rolesCount = this.server.db.roles.length; - await visit(newRoleURL); + await visit(urls.newRole); await fillIn('[name="name"]', 'role name'); await click('[type="submit"]'); assert.equal(this.server.db.roles.length, rolesCount + 1); @@ -66,10 +70,10 @@ module('Acceptance | roles', function (hooks) { test('can cancel new role creation', async function (assert) { assert.expect(2); const rolesCount = this.server.db.roles.length; - await visit(newRoleURL); + await visit(urls.newRole); await fillIn('[name="name"]', 'role name'); await click('.rose-form-actions [type="button"]'); - assert.equal(currentURL(), rolesURL); + assert.equal(currentURL(), urls.roles); assert.equal(this.server.db.roles.length, rolesCount); }); @@ -94,7 +98,7 @@ module('Acceptance | roles', function (hooks) { } ); }); - await visit(newRoleURL); + await visit(urls.newRole); await fillIn('[name="name"]', 'role name'); await click('[type="submit"]'); assert.ok( @@ -111,16 +115,16 @@ module('Acceptance | roles', function (hooks) { test('can save changes to an existing role', async function (assert) { assert.expect(2); - await visit(roleURL); + await visit(urls.role); await fillIn('[name="name"]', 'Updated admin role'); await click('.rose-form-actions [type="submit"]'); - assert.equal(currentURL(), roleURL); + assert.equal(currentURL(), urls.role); assert.equal(this.server.db.roles[0].name, 'Updated admin role'); }); test('can cancel changes to an existing role', async function (assert) { assert.expect(1); - await visit(roleURL); + await visit(urls.role); await fillIn('[name="name"]', 'Updated admin role'); await click('.rose-form-actions [type="button"]'); assert.notEqual(find('[name="name"]').value, 'Updated admin role'); @@ -129,7 +133,7 @@ module('Acceptance | roles', function (hooks) { test('can delete a role', async function (assert) { assert.expect(1); const rolesCount = this.server.db.roles.length; - await visit(roleURL); + await visit(urls.role); await click('.rose-layout-page-actions .rose-dropdown-button-danger'); assert.equal(this.server.db.roles.length, rolesCount - 1); }); @@ -155,7 +159,7 @@ module('Acceptance | roles', function (hooks) { } ); }); - await visit(roleURL); + await visit(urls.role); await fillIn('[name="name"]', 'random string'); await click('[type="submit"]'); assert.ok( @@ -183,7 +187,7 @@ module('Acceptance | roles', function (hooks) { } ); }); - await visit(roleURL); + await visit(urls.role); await fillIn('[name="name"]', 'Role name'); await click('[type="submit"]'); assert.ok( @@ -206,7 +210,7 @@ module('Acceptance | roles', function (hooks) { } ); }); - await visit(roleURL); + await visit(urls.role); await click('.rose-layout-page-actions .rose-dropdown-button-danger'); assert.ok( find('[role="alert"]').textContent.trim(), From a8efd3768d718902189ed9d325a5e594d97c28fa Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 21 Sep 2020 10:36:59 -0400 Subject: [PATCH 2/6] test: Add grants association test to role acceptance test --- ui/core/tests/acceptance/roles-test.js | 145 ++++++++++++++++++++----- 1 file changed, 118 insertions(+), 27 deletions(-) diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js index fb1eccf2b..734ab23fe 100644 --- a/ui/core/tests/acceptance/roles-test.js +++ b/ui/core/tests/acceptance/roles-test.js @@ -1,5 +1,5 @@ import { module, test } from 'qunit'; -import { visit, currentURL, click, fillIn, find } from '@ember/test-helpers'; +import { visit, currentURL, click, fillIn, 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'; @@ -38,7 +38,7 @@ module('Acceptance | roles', function (hooks) { }); instances.role = this.server.create('role', { scope: instances.scopes.org, - }); + }, 'withPrincipals'); urls.roles = `/scopes/${instances.scopes.org.id}/roles`; urls.role = `${urls.roles}/${instances.role.id}`; urls.newRole = `${urls.roles}/new`; @@ -219,39 +219,130 @@ module('Acceptance | roles', function (hooks) { ); }); - test('principals may be viewed in principals tab', async function (assert) { - assert.expect(0); + module('principals', function(hooks) { + let principalsCount; + + hooks.beforeEach(function() { + principalsCount = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; + instances.addPrincipals = urls.addPrincipals = `${urls.role}/add-principals`; + urls.rolePrincipals = `${urls.role}/principals`; + this.server.create('user', { scope: instances.scopes.org }); + }); + + test('visiting role principals', async function (assert) { + assert.expect(1); + await visit(urls.rolePrincipals); + await a11yAudit(); + assert.equal(currentURL(), urls.rolePrincipals); + }); + + test('principal can be removed from a role', async function (assert) { + assert.expect(1); + await visit(urls.rolePrincipals); + await click('main tbody .rose-table-row:nth-child(1) .rose-table-cell:nth-child(4) .rose-dropdown-button-danger'); + const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; + assert.equal(count, principalsCount - 1); + }); + + test('select and save principals to add', async function (assert) { + assert.expect(1); + await visit(urls.addPrincipals); + await click('main tbody .rose-table-cell:nth-child(1) [type="checkbox"]'); + await click('form [type="submit"]:not(:disabled)'); + const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; + assert.equal(count, principalsCount + 1); + }); + + test('select and cancel principals to add', async function (assert) { + assert.expect(1); + await visit(urls.addPrincipals); + await click('main tbody .rose-table-cell:nth-child(1) [type="checkbox"]'); + await click('form button:not([type="submit"])'); + const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; + assert.equal(count, principalsCount); + }); }); - test('principal can be removed from a role', async function (assert) { - assert.expect(0); - }); + module('grants', function(hooks) { + let grantsCount; - test('grants can be updated and saved or canceled', async function (assert) { - assert.expect(0); - }); + hooks.beforeEach(function() { + grantsCount = this.server.db.roles[0].grant_strings.length; + urls.grants = `${urls.role}/grants`; + }); - test('grants can be removed and saved or canceled', async function (assert) { - assert.expect(0); - }); + test('visiting role grants', async function (assert) { + assert.expect(1); + await visit(urls.grants); + await a11yAudit(); + assert.equal(currentURL(), urls.grants); + }); - test('new grants can be added and saved or canceled', async function (assert) { - assert.expect(0); - }); + test('update a grant', async function(assert) { + assert.expect(1); + this.server.post('/roles/:id', (_, { requestBody }) => { + const attrs = JSON.parse(requestBody); + assert.equal(attrs.grant_strings[0], 'id=123,action=delete', "attributes don't match the expected ones"); + }); + await visit(urls.grants); + const grant = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; + await fillIn(grant, 'id=123,action=delete'); + await click('form [type="submit"]:not(:disabled)'); + }); - test('viewing role principals', async function (assert) { - assert.expect(0); - }); + test('cancel a grant update', async function(assert) { + assert.expect(1); + await visit(urls.grants); + const grant = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; + await fillIn(grant, 'id=123,action=delete'); + await click('form button:not([type="submit"])'); + const grantAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; + assert.notEqual(grant.value, grantAfter.value); + }); - test('can delete a principal', async function (assert) { - assert.expect(0); - }); + test('create a grant', async function(assert) { + assert.expect(1); + this.server.post('/roles/:id', (_, { requestBody }) => { + const attrs = JSON.parse(requestBody); + assert.equal(attrs.grant_strings.length, grantsCount + 1, "grants don't match the expected ones"); + }); + await visit(urls.grants); + await fillIn('.rose-list-key-value:nth-child(1) [name="grant"]', 'id=123,action=delete'); + await click('.rose-list-key-value:nth-child(1) [type="submit"]:not(:disabled)'); + await click('form [type="submit"]:not(:disabled)'); + }); - test('select and save principals to add', async function (assert) { - assert.expect(0); - }); + test('cancel a grant creation', async function(assert) { + assert.expect(1); + await visit(urls.grants); + const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + await fillIn('.rose-list-key-value:nth-child(1) [name="grant"]', 'id=123,action=delete'); + await click('.rose-list-key-value:nth-child(1) [type="submit"]:not(:disabled)'); + await click('form button:not([type="submit"])'); + const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + assert.equal(grantsBefore, grantsAfter); + }); - test('select and cancel principals to add', async function (assert) { - assert.expect(0); + test('delete a grant', async function(assert) { + assert.expect(1); + await visit(urls.grants); + const grant = findAll('.rose-list-key-value:nth-child(1) button:not([type="submit"])')[0]; + const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + await click(grant); + await click('form [type="submit"]:not(:disabled)'); + const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + assert.equal(grantsBefore - 1, grantsAfter); + }); + + test('cancel a grant deletion', async function(assert) { + assert.expect(1); + await visit(urls.grants); + const grant = findAll('.rose-list-key-value:nth-child(1) button:not([type="submit"])')[0]; + const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + await click(grant); + await click('.rose-form-actions button:not([type="submit"])'); + const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; + assert.equal(grantsBefore, grantsAfter); + }); }); }); From c014003d1c0b69149a38b84988103157827ce18c Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 22 Sep 2020 17:52:56 -0400 Subject: [PATCH 3/6] test: Refactor grants and principals tests in role test --- ui/core/tests/acceptance/roles-test.js | 283 ++++++++++++++++++------- 1 file changed, 212 insertions(+), 71 deletions(-) diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js index 734ab23fe..8759b0433 100644 --- a/ui/core/tests/acceptance/roles-test.js +++ b/ui/core/tests/acceptance/roles-test.js @@ -51,30 +51,12 @@ module('Acceptance | roles', function (hooks) { assert.equal(currentURL(), urls.roles); }); - test('visiting a role', async function (assert) { + test('can navigate to a role form', async function (assert) { assert.expect(1); - await visit(urls.newRole); + await visit(urls.roles); + await click('main tbody .rose-table-header-cell:nth-child(1) a'); await a11yAudit(); - assert.equal(currentURL(), urls.newRole); - }); - - test('can create new role', async function (assert) { - assert.expect(1); - const rolesCount = this.server.db.roles.length; - await visit(urls.newRole); - await fillIn('[name="name"]', 'role name'); - await click('[type="submit"]'); - assert.equal(this.server.db.roles.length, rolesCount + 1); - }); - - test('can cancel new role creation', async function (assert) { - assert.expect(2); - const rolesCount = this.server.db.roles.length; - await visit(urls.newRole); - await fillIn('[name="name"]', 'role name'); - await click('.rose-form-actions [type="button"]'); - assert.equal(currentURL(), urls.roles); - assert.equal(this.server.db.roles.length, rolesCount); + assert.equal(currentURL(), urls.role); }); test('saving a new role with invalid fields displays error messages', async function (assert) { @@ -130,6 +112,25 @@ module('Acceptance | roles', function (hooks) { assert.notEqual(find('[name="name"]').value, 'Updated admin role'); }); + test('can create new role', async function (assert) { + assert.expect(1); + const rolesCount = this.server.db.roles.length; + await visit(urls.newRole); + await fillIn('[name="name"]', 'role name'); + await click('[type="submit"]'); + assert.equal(this.server.db.roles.length, rolesCount + 1); + }); + + test('can cancel new role creation', async function (assert) { + assert.expect(2); + const rolesCount = this.server.db.roles.length; + await visit(urls.newRole); + await fillIn('[name="name"]', 'role name'); + await click('.rose-form-actions [type="button"]'); + assert.equal(currentURL(), urls.roles); + assert.equal(this.server.db.roles.length, rolesCount); + }); + test('can delete a role', async function (assert) { assert.expect(1); const rolesCount = this.server.db.roles.length; @@ -138,6 +139,35 @@ module('Acceptance | roles', function (hooks) { assert.equal(this.server.db.roles.length, rolesCount - 1); }); + test('saving a new role with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/roles', () => { + 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.newRole); + 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')); + }); + test('saving an existing role with invalid fields displays error messages', async function (assert) { assert.expect(2); this.server.patch('/roles/:id', () => { @@ -224,47 +254,100 @@ module('Acceptance | roles', function (hooks) { hooks.beforeEach(function() { principalsCount = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; - instances.addPrincipals = urls.addPrincipals = `${urls.role}/add-principals`; urls.rolePrincipals = `${urls.role}/principals`; - this.server.create('user', { scope: instances.scopes.org }); + urls.addPrincipals = `${urls.role}/add-principals`; }); test('visiting role principals', async function (assert) { - assert.expect(1); + assert.expect(2); await visit(urls.rolePrincipals); await a11yAudit(); assert.equal(currentURL(), urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); }); test('principal can be removed from a role', async function (assert) { - assert.expect(1); + assert.expect(2); await visit(urls.rolePrincipals); - await click('main tbody .rose-table-row:nth-child(1) .rose-table-cell:nth-child(4) .rose-dropdown-button-danger'); - const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; - assert.equal(count, principalsCount - 1); + assert.equal(findAll('tbody tr').length, principalsCount); + await click('tbody tr .rose-dropdown-button-danger'); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + }); + + test('shows error message on principal remove', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + await click('tbody tr .rose-dropdown-button-danger'); + assert.ok(find('[role="alert"]')); }); test('select and save principals to add', async function (assert) { - assert.expect(1); - await visit(urls.addPrincipals); - await click('main tbody .rose-table-cell:nth-child(1) [type="checkbox"]'); - await click('form [type="submit"]:not(:disabled)'); - const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; - assert.equal(count, principalsCount + 1); + assert.expect(3); + instances.role.update({ userIds: [], groupIds: [] }); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, 0); + await click('.rose-layout-page-actions a') + assert.equal(currentURL(), urls.addPrincipals); + await click('tbody label'); + await click('form [type="submit"]'); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, 1); }); test('select and cancel principals to add', async function (assert) { + assert.expect(4); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + // Remove a principal to populate association view + await click('tbody tr .rose-dropdown-button-danger'); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + await click('.rose-layout-page-actions a') + assert.equal(currentURL(), urls.addPrincipals); + await click('tbody label'); + await click('form [type="button"]'); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + }); + + test('shows error message on principal add', async function (assert) { assert.expect(1); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + instances.role.update({ userIds: [], groupIds: [] }); await visit(urls.addPrincipals); - await click('main tbody .rose-table-cell:nth-child(1) [type="checkbox"]'); - await click('form button:not([type="submit"])'); - const count = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; - assert.equal(count, principalsCount); + await click('tbody label'); + await click('form [type="submit"]'); + assert.ok(find('[role="alert"]')); }); }); module('grants', function(hooks) { let grantsCount; + const newGrantForm = 'form:nth-child(1)', + grantsForm = 'form:nth-child(2)'; hooks.beforeEach(function() { grantsCount = this.server.db.roles[0].grant_strings.length; @@ -272,77 +355,135 @@ module('Acceptance | roles', function (hooks) { }); test('visiting role grants', async function (assert) { - assert.expect(1); + assert.expect(2); await visit(urls.grants); await a11yAudit(); assert.equal(currentURL(), urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); }); test('update a grant', async function(assert) { assert.expect(1); this.server.post('/roles/:id', (_, { requestBody }) => { const attrs = JSON.parse(requestBody); - assert.equal(attrs.grant_strings[0], 'id=123,action=delete', "attributes don't match the expected ones"); + assert.equal(attrs.grant_strings[0], 'id=123,action=delete', "A grant is updated"); }); await visit(urls.grants); - const grant = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; - await fillIn(grant, 'id=123,action=delete'); - await click('form [type="submit"]:not(:disabled)'); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); }); test('cancel a grant update', async function(assert) { assert.expect(1); await visit(urls.grants); - const grant = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; - await fillIn(grant, 'id=123,action=delete'); - await click('form button:not([type="submit"])'); - const grantAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]')[1]; - assert.notEqual(grant.value, grantAfter.value); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions button:not([type="submit"])'); + assert.notEqual(find(`${grantsForm} [name="grant"]`).value, 'id=123,action=delete'); + }); + + test('shows error message on grant update', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); }); test('create a grant', async function(assert) { assert.expect(1); this.server.post('/roles/:id', (_, { requestBody }) => { const attrs = JSON.parse(requestBody); - assert.equal(attrs.grant_strings.length, grantsCount + 1, "grants don't match the expected ones"); + assert.equal(attrs.grant_strings.length, grantsCount + 1, "A grant is created"); }); await visit(urls.grants); - await fillIn('.rose-list-key-value:nth-child(1) [name="grant"]', 'id=123,action=delete'); - await click('.rose-list-key-value:nth-child(1) [type="submit"]:not(:disabled)'); - await click('form [type="submit"]:not(:disabled)'); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); }); test('cancel a grant creation', async function(assert) { assert.expect(1); await visit(urls.grants); - const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - await fillIn('.rose-list-key-value:nth-child(1) [name="grant"]', 'id=123,action=delete'); - await click('.rose-list-key-value:nth-child(1) [type="submit"]:not(:disabled)'); - await click('form button:not([type="submit"])'); - const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - assert.equal(grantsBefore, grantsAfter); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions button:not([type="submit"])'); + assert.notOk(find(`${newGrantForm} [name="grant"]`).value); + }); + + test('shows error message on grant create', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); }); test('delete a grant', async function(assert) { assert.expect(1); await visit(urls.grants); - const grant = findAll('.rose-list-key-value:nth-child(1) button:not([type="submit"])')[0]; - const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - await click(grant); - await click('form [type="submit"]:not(:disabled)'); - const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - assert.equal(grantsBefore - 1, grantsAfter); + await click(`${grantsForm} button:not([type="submit"])`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount - 1); }); - test('cancel a grant deletion', async function(assert) { + test('cancel a grant remove', async function(assert) { assert.expect(1); await visit(urls.grants); - const grant = findAll('.rose-list-key-value:nth-child(1) button:not([type="submit"])')[0]; - const grantsBefore = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - await click(grant); + await click(`${grantsForm} button`); await click('.rose-form-actions button:not([type="submit"])'); - const grantsAfter = findAll('.rose-list-key-value:nth-child(1) [name="grant"]').length; - assert.equal(grantsBefore, grantsAfter); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); + }); + + test('shows error message on grant remove', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await click(`${grantsForm} button:not([type="submit"])`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); }); }); }); From 09937e43b7af30cbd2614cda3f0508d5190b6320 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 23 Sep 2020 12:16:31 -0400 Subject: [PATCH 4/6] refactor: Breakout role tests into role, principal, and grant --- ui/core/tests/acceptance/roles-test.js | 489 ------------------ ui/core/tests/acceptance/roles/grants-test.js | 182 +++++++ .../tests/acceptance/roles/principals-test.js | 134 +++++ ui/core/tests/acceptance/roles/roles-test.js | 251 +++++++++ 4 files changed, 567 insertions(+), 489 deletions(-) delete mode 100644 ui/core/tests/acceptance/roles-test.js create mode 100644 ui/core/tests/acceptance/roles/grants-test.js create mode 100644 ui/core/tests/acceptance/roles/principals-test.js create mode 100644 ui/core/tests/acceptance/roles/roles-test.js diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js deleted file mode 100644 index 8759b0433..000000000 --- a/ui/core/tests/acceptance/roles-test.js +++ /dev/null @@ -1,489 +0,0 @@ -import { module, test } from 'qunit'; -import { visit, currentURL, click, fillIn, 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. - //currentSession, - //invalidateSession, -} from 'ember-simple-auth/test-support'; - -module('Acceptance | roles', function (hooks) { - setupApplicationTest(hooks); - setupMirage(hooks); - - const instances = { - scopes: { - global: null, - org: null, - }, - role: null, - }; - const urls = { - orgScope: null, - roles: null, - role: null, - newRole: null, - }; - - hooks.beforeEach(function () { - 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.role = this.server.create('role', { - scope: instances.scopes.org, - }, 'withPrincipals'); - urls.roles = `/scopes/${instances.scopes.org.id}/roles`; - urls.role = `${urls.roles}/${instances.role.id}`; - urls.newRole = `${urls.roles}/new`; - }); - - test('visiting roles', async function (assert) { - assert.expect(1); - await visit(urls.roles); - await a11yAudit(); - assert.equal(currentURL(), urls.roles); - }); - - test('can navigate to a role form', async function (assert) { - assert.expect(1); - await visit(urls.roles); - await click('main tbody .rose-table-header-cell:nth-child(1) a'); - await a11yAudit(); - assert.equal(currentURL(), urls.role); - }); - - test('saving a new role with invalid fields displays error messages', async function (assert) { - assert.expect(2); - this.server.post('/roles', () => { - 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.newRole); - await fillIn('[name="name"]', 'role name'); - await click('[type="submit"]'); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'The request was invalid.', - 'Displays primary error message.' - ); - assert.ok( - find('.rose-form-error-message').textContent.trim(), - 'Name is required.', - 'Displays field-level errors.' - ); - }); - - test('can save changes to an existing role', async function (assert) { - assert.expect(2); - await visit(urls.role); - await fillIn('[name="name"]', 'Updated admin role'); - await click('.rose-form-actions [type="submit"]'); - assert.equal(currentURL(), urls.role); - assert.equal(this.server.db.roles[0].name, 'Updated admin role'); - }); - - test('can cancel changes to an existing role', async function (assert) { - assert.expect(1); - await visit(urls.role); - await fillIn('[name="name"]', 'Updated admin role'); - await click('.rose-form-actions [type="button"]'); - assert.notEqual(find('[name="name"]').value, 'Updated admin role'); - }); - - test('can create new role', async function (assert) { - assert.expect(1); - const rolesCount = this.server.db.roles.length; - await visit(urls.newRole); - await fillIn('[name="name"]', 'role name'); - await click('[type="submit"]'); - assert.equal(this.server.db.roles.length, rolesCount + 1); - }); - - test('can cancel new role creation', async function (assert) { - assert.expect(2); - const rolesCount = this.server.db.roles.length; - await visit(urls.newRole); - await fillIn('[name="name"]', 'role name'); - await click('.rose-form-actions [type="button"]'); - assert.equal(currentURL(), urls.roles); - assert.equal(this.server.db.roles.length, rolesCount); - }); - - test('can delete a role', async function (assert) { - assert.expect(1); - const rolesCount = this.server.db.roles.length; - await visit(urls.role); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.equal(this.server.db.roles.length, rolesCount - 1); - }); - - test('saving a new role with invalid fields displays error messages', async function (assert) { - assert.expect(2); - this.server.post('/roles', () => { - 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.newRole); - 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')); - }); - - test('saving an existing role with invalid fields displays error messages', async function (assert) { - assert.expect(2); - this.server.patch('/roles/: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.role); - await fillIn('[name="name"]', 'random string'); - await click('[type="submit"]'); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'The request was invalid.', - 'Displays primary error message.' - ); - assert.ok( - find('.rose-form-error-message').textContent.trim(), - 'Name is required.', - 'Displays field-level errors.' - ); - }); - - test('errors are displayed when save project fails', async function (assert) { - assert.expect(1); - this.server.patch('/roles/:id', () => { - return new Response( - 490, - {}, - { - status: 490, - code: 'error', - message: 'Oops.', - } - ); - }); - await visit(urls.role); - await fillIn('[name="name"]', 'Role name'); - await click('[type="submit"]'); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'Oops.', - 'Displays primary error message.' - ); - }); - - test('errors are displayed when delete project fails', async function (assert) { - assert.expect(1); - this.server.del('/roles/:id', () => { - return new Response( - 490, - {}, - { - status: 490, - code: 'error', - message: 'Oops.', - } - ); - }); - await visit(urls.role); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'Oops.', - 'Displays primary error message.' - ); - }); - - module('principals', function(hooks) { - let principalsCount; - - hooks.beforeEach(function() { - principalsCount = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; - urls.rolePrincipals = `${urls.role}/principals`; - urls.addPrincipals = `${urls.role}/add-principals`; - }); - - test('visiting role principals', async function (assert) { - assert.expect(2); - await visit(urls.rolePrincipals); - await a11yAudit(); - assert.equal(currentURL(), urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, principalsCount); - }); - - test('principal can be removed from a role', async function (assert) { - assert.expect(2); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, principalsCount); - await click('tbody tr .rose-dropdown-button-danger'); - assert.equal(findAll('tbody tr').length, principalsCount - 1); - }); - - test('shows error message on principal remove', async function (assert) { - assert.expect(2); - this.server.post('/roles/:idMethod', () => { - return new Response( - 400, - {}, - { - status: 400, - code: 'invalid_argument', - message: 'The request was invalid.', - details: {}, - } - ); - }); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, principalsCount); - await click('tbody tr .rose-dropdown-button-danger'); - assert.ok(find('[role="alert"]')); - }); - - test('select and save principals to add', async function (assert) { - assert.expect(3); - instances.role.update({ userIds: [], groupIds: [] }); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, 0); - await click('.rose-layout-page-actions a') - assert.equal(currentURL(), urls.addPrincipals); - await click('tbody label'); - await click('form [type="submit"]'); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, 1); - }); - - test('select and cancel principals to add', async function (assert) { - assert.expect(4); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, principalsCount); - // Remove a principal to populate association view - await click('tbody tr .rose-dropdown-button-danger'); - assert.equal(findAll('tbody tr').length, principalsCount - 1); - await click('.rose-layout-page-actions a') - assert.equal(currentURL(), urls.addPrincipals); - await click('tbody label'); - await click('form [type="button"]'); - await visit(urls.rolePrincipals); - assert.equal(findAll('tbody tr').length, principalsCount - 1); - }); - - test('shows error message on principal add', async function (assert) { - assert.expect(1); - this.server.post('/roles/:idMethod', () => { - return new Response( - 400, - {}, - { - status: 400, - code: 'invalid_argument', - message: 'The request was invalid.', - details: {}, - } - ); - }); - instances.role.update({ userIds: [], groupIds: [] }); - await visit(urls.addPrincipals); - await click('tbody label'); - await click('form [type="submit"]'); - assert.ok(find('[role="alert"]')); - }); - }); - - module('grants', function(hooks) { - let grantsCount; - const newGrantForm = 'form:nth-child(1)', - grantsForm = 'form:nth-child(2)'; - - hooks.beforeEach(function() { - grantsCount = this.server.db.roles[0].grant_strings.length; - urls.grants = `${urls.role}/grants`; - }); - - test('visiting role grants', async function (assert) { - assert.expect(2); - await visit(urls.grants); - await a11yAudit(); - assert.equal(currentURL(), urls.grants); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); - }); - - test('update a grant', async function(assert) { - assert.expect(1); - this.server.post('/roles/:id', (_, { requestBody }) => { - const attrs = JSON.parse(requestBody); - assert.equal(attrs.grant_strings[0], 'id=123,action=delete', "A grant is updated"); - }); - await visit(urls.grants); - await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - }); - - test('cancel a grant update', async function(assert) { - assert.expect(1); - await visit(urls.grants); - await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); - await click('.rose-form-actions button:not([type="submit"])'); - assert.notEqual(find(`${grantsForm} [name="grant"]`).value, 'id=123,action=delete'); - }); - - test('shows error message on grant update', async function (assert) { - assert.expect(2); - this.server.post('/roles/:idMethod', () => { - return new Response( - 400, - {}, - { - status: 400, - code: 'invalid_argument', - message: 'The request was invalid.', - details: {}, - } - ); - }); - await visit(urls.grants); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, - grantsCount); - await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - assert.ok(find('[role="alert"]')); - }); - - test('create a grant', async function(assert) { - assert.expect(1); - this.server.post('/roles/:id', (_, { requestBody }) => { - const attrs = JSON.parse(requestBody); - assert.equal(attrs.grant_strings.length, grantsCount + 1, "A grant is created"); - }); - await visit(urls.grants); - await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); - await click(`${newGrantForm} [type="submit"]:not(:disabled)`); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - }); - - test('cancel a grant creation', async function(assert) { - assert.expect(1); - await visit(urls.grants); - await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); - await click(`${newGrantForm} [type="submit"]:not(:disabled)`); - await click('.rose-form-actions button:not([type="submit"])'); - assert.notOk(find(`${newGrantForm} [name="grant"]`).value); - }); - - test('shows error message on grant create', async function (assert) { - assert.expect(2); - this.server.post('/roles/:idMethod', () => { - return new Response( - 400, - {}, - { - status: 400, - code: 'invalid_argument', - message: 'The request was invalid.', - details: {}, - } - ); - }); - await visit(urls.grants); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, - grantsCount); - await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); - await click(`${newGrantForm} [type="submit"]:not(:disabled)`); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - assert.ok(find('[role="alert"]')); - }); - - test('delete a grant', async function(assert) { - assert.expect(1); - await visit(urls.grants); - await click(`${grantsForm} button:not([type="submit"])`); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, - grantsCount - 1); - }); - - test('cancel a grant remove', async function(assert) { - assert.expect(1); - await visit(urls.grants); - await click(`${grantsForm} button`); - await click('.rose-form-actions button:not([type="submit"])'); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); - }); - - test('shows error message on grant remove', async function (assert) { - assert.expect(2); - this.server.post('/roles/:idMethod', () => { - return new Response( - 400, - {}, - { - status: 400, - code: 'invalid_argument', - message: 'The request was invalid.', - details: {}, - } - ); - }); - await visit(urls.grants); - assert.equal(findAll(`${grantsForm} [name="grant"]`).length, - grantsCount); - await click(`${grantsForm} button:not([type="submit"])`); - await click('.rose-form-actions [type="submit"]:not(:disabled)'); - assert.ok(find('[role="alert"]')); - }); - }); -}); diff --git a/ui/core/tests/acceptance/roles/grants-test.js b/ui/core/tests/acceptance/roles/grants-test.js new file mode 100644 index 000000000..e767928fb --- /dev/null +++ b/ui/core/tests/acceptance/roles/grants-test.js @@ -0,0 +1,182 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, click, fillIn, 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. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | roles | grants', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + role: null, + }; + const urls = { + orgScope: null, + roles: null, + role: null, + newRole: null, + }; + const newGrantForm = 'form:nth-child(1)'; + const grantsForm = 'form:nth-child(2)'; + let grantsCount; + + hooks.beforeEach(function () { + 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.role = this.server.create('role', { + scope: instances.scopes.org, + }); + grantsCount = this.server.db.roles[0].grant_strings.length; + urls.roles = `/scopes/${instances.scopes.org.id}/roles`; + urls.role = `${urls.roles}/${instances.role.id}`; + urls.grants = `${urls.role}/grants`; + }); + + test('visiting role grants', async function (assert) { + assert.expect(2); + await visit(urls.grants); + await a11yAudit(); + assert.equal(currentURL(), urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); + }); + + test('update a grant', async function(assert) { + assert.expect(1); + this.server.post('/roles/:id', (_, { requestBody }) => { + const attrs = JSON.parse(requestBody); + assert.equal(attrs.grant_strings[0], 'id=123,action=delete', "A grant is updated"); + }); + await visit(urls.grants); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + }); + + test('cancel a grant update', async function(assert) { + assert.expect(1); + await visit(urls.grants); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions button:not([type="submit"])'); + assert.notEqual(find(`${grantsForm} [name="grant"]`).value, 'id=123,action=delete'); + }); + + test('shows error message on grant update', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await fillIn(`${grantsForm} [name="grant"]`, 'id=123,action=delete'); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); + }); + + test('create a grant', async function(assert) { + assert.expect(1); + this.server.post('/roles/:id', (_, { requestBody }) => { + const attrs = JSON.parse(requestBody); + assert.equal(attrs.grant_strings.length, grantsCount + 1, "A grant is created"); + }); + await visit(urls.grants); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + }); + + test('cancel a grant creation', async function(assert) { + assert.expect(1); + await visit(urls.grants); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions button:not([type="submit"])'); + assert.notOk(find(`${newGrantForm} [name="grant"]`).value); + }); + + test('shows error message on grant create', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await fillIn(`${newGrantForm} [name="grant"]`, 'id=123,action=delete'); + await click(`${newGrantForm} [type="submit"]:not(:disabled)`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); + }); + + test('delete a grant', async function(assert) { + assert.expect(1); + await visit(urls.grants); + await click(`${grantsForm} button:not([type="submit"])`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount - 1); + }); + + test('cancel a grant remove', async function(assert) { + assert.expect(1); + await visit(urls.grants); + await click(`${grantsForm} button`); + await click('.rose-form-actions button:not([type="submit"])'); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, grantsCount); + }); + + test('shows error message on grant remove', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.grants); + assert.equal(findAll(`${grantsForm} [name="grant"]`).length, + grantsCount); + await click(`${grantsForm} button:not([type="submit"])`); + await click('.rose-form-actions [type="submit"]:not(:disabled)'); + assert.ok(find('[role="alert"]')); + }); +}); diff --git a/ui/core/tests/acceptance/roles/principals-test.js b/ui/core/tests/acceptance/roles/principals-test.js new file mode 100644 index 000000000..493a7a9f4 --- /dev/null +++ b/ui/core/tests/acceptance/roles/principals-test.js @@ -0,0 +1,134 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, 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. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | roles | principals', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + role: null, + }; + const urls = { + orgScope: null, + roles: null, + role: null, + newRole: null, + }; + let principalsCount; + + hooks.beforeEach(function () { + 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.role = this.server.create('role', { + scope: instances.scopes.org, + }, 'withPrincipals'); + principalsCount = this.server.db.roles[0].userIds.length + this.server.db.roles[0].groupIds.length; + urls.roles = `/scopes/${instances.scopes.org.id}/roles`; + urls.role = `${urls.roles}/${instances.role.id}`; + urls.rolePrincipals = `${urls.role}/principals`; + urls.addPrincipals = `${urls.role}/add-principals`; + }); + + test('visiting role principals', async function (assert) { + assert.expect(2); + await visit(urls.rolePrincipals); + await a11yAudit(); + assert.equal(currentURL(), urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + }); + + test('principal can be removed from a role', async function (assert) { + assert.expect(2); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + await click('tbody tr .rose-dropdown-button-danger'); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + }); + + test('shows error message on principal remove', async function (assert) { + assert.expect(2); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + await click('tbody tr .rose-dropdown-button-danger'); + assert.ok(find('[role="alert"]')); + }); + + test('select and save principals to add', async function (assert) { + assert.expect(3); + instances.role.update({ userIds: [], groupIds: [] }); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, 0); + await click('.rose-layout-page-actions a') + assert.equal(currentURL(), urls.addPrincipals); + await click('tbody label'); + await click('form [type="submit"]'); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, 1); + }); + + test('select and cancel principals to add', async function (assert) { + assert.expect(4); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount); + // Remove a principal to populate association view + await click('tbody tr .rose-dropdown-button-danger'); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + await click('.rose-layout-page-actions a') + assert.equal(currentURL(), urls.addPrincipals); + await click('tbody label'); + await click('form [type="button"]'); + await visit(urls.rolePrincipals); + assert.equal(findAll('tbody tr').length, principalsCount - 1); + }); + + test('shows error message on principal add', async function (assert) { + assert.expect(1); + this.server.post('/roles/:idMethod', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: {}, + } + ); + }); + instances.role.update({ userIds: [], groupIds: [] }); + await visit(urls.addPrincipals); + await click('tbody label'); + await click('form [type="submit"]'); + assert.ok(find('[role="alert"]')); + }); +}); diff --git a/ui/core/tests/acceptance/roles/roles-test.js b/ui/core/tests/acceptance/roles/roles-test.js new file mode 100644 index 000000000..a9212b06f --- /dev/null +++ b/ui/core/tests/acceptance/roles/roles-test.js @@ -0,0 +1,251 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, click, fillIn, 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 { Response } from 'miragejs'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | roles', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + role: null, + }; + const urls = { + orgScope: null, + roles: null, + role: null, + newRole: null, + }; + + hooks.beforeEach(function () { + 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.role = this.server.create('role', { + scope: instances.scopes.org, + }, 'withPrincipals'); + urls.roles = `/scopes/${instances.scopes.org.id}/roles`; + urls.role = `${urls.roles}/${instances.role.id}`; + urls.newRole = `${urls.roles}/new`; + }); + + test('visiting roles', async function (assert) { + assert.expect(1); + await visit(urls.roles); + await a11yAudit(); + assert.equal(currentURL(), urls.roles); + }); + + test('can navigate to a role form', async function (assert) { + assert.expect(1); + await visit(urls.roles); + await click('main tbody .rose-table-header-cell:nth-child(1) a'); + await a11yAudit(); + assert.equal(currentURL(), urls.role); + }); + + test('saving a new role with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/roles', () => { + 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.newRole); + await fillIn('[name="name"]', 'role name'); + await click('[type="submit"]'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'The request was invalid.', + 'Displays primary error message.' + ); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.', + 'Displays field-level errors.' + ); + }); + + test('can save changes to an existing role', async function (assert) { + assert.expect(2); + await visit(urls.role); + await fillIn('[name="name"]', 'Updated admin role'); + await click('.rose-form-actions [type="submit"]'); + assert.equal(currentURL(), urls.role); + assert.equal(this.server.db.roles[0].name, 'Updated admin role'); + }); + + test('can cancel changes to an existing role', async function (assert) { + assert.expect(1); + await visit(urls.role); + await fillIn('[name="name"]', 'Updated admin role'); + await click('.rose-form-actions [type="button"]'); + assert.notEqual(find('[name="name"]').value, 'Updated admin role'); + }); + + test('can create new role', async function (assert) { + assert.expect(1); + const rolesCount = this.server.db.roles.length; + await visit(urls.newRole); + await fillIn('[name="name"]', 'role name'); + await click('[type="submit"]'); + assert.equal(this.server.db.roles.length, rolesCount + 1); + }); + + test('can cancel new role creation', async function (assert) { + assert.expect(2); + const rolesCount = this.server.db.roles.length; + await visit(urls.newRole); + await fillIn('[name="name"]', 'role name'); + await click('.rose-form-actions [type="button"]'); + assert.equal(currentURL(), urls.roles); + assert.equal(this.server.db.roles.length, rolesCount); + }); + + test('can delete a role', async function (assert) { + assert.expect(1); + const rolesCount = this.server.db.roles.length; + await visit(urls.role); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(this.server.db.roles.length, rolesCount - 1); + }); + + test('saving a new role with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/roles', () => { + 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.newRole); + 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')); + }); + + test('saving an existing role with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.patch('/roles/: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.role); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'The request was invalid.', + 'Displays primary error message.' + ); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.', + 'Displays field-level errors.' + ); + }); + + test('errors are displayed when save project fails', async function (assert) { + assert.expect(1); + this.server.patch('/roles/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.role); + await fillIn('[name="name"]', 'Role name'); + await click('[type="submit"]'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Oops.', + 'Displays primary error message.' + ); + }); + + test('errors are displayed when delete project fails', async function (assert) { + assert.expect(1); + this.server.del('/roles/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.role); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Oops.', + 'Displays primary error message.' + ); + }); +}); From 296580e604bf845a0cddbcc3696ce6e0a2241b8b Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 23 Sep 2020 12:53:09 -0400 Subject: [PATCH 5/6] refactor: Move roles acceptance test to root folder --- ui/core/tests/acceptance/{roles => }/roles-test.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ui/core/tests/acceptance/{roles => }/roles-test.js (100%) diff --git a/ui/core/tests/acceptance/roles/roles-test.js b/ui/core/tests/acceptance/roles-test.js similarity index 100% rename from ui/core/tests/acceptance/roles/roles-test.js rename to ui/core/tests/acceptance/roles-test.js From 7901f561d85bd82406762f7259bd41ac895e571a Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 23 Sep 2020 16:59:13 -0400 Subject: [PATCH 6/6] test: Remove duplicated new role test --- ui/core/tests/acceptance/roles-test.js | 36 -------------------------- 1 file changed, 36 deletions(-) diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js index a9212b06f..eee3ffa50 100644 --- a/ui/core/tests/acceptance/roles-test.js +++ b/ui/core/tests/acceptance/roles-test.js @@ -59,42 +59,6 @@ module('Acceptance | roles', function (hooks) { assert.equal(currentURL(), urls.role); }); - test('saving a new role with invalid fields displays error messages', async function (assert) { - assert.expect(2); - this.server.post('/roles', () => { - 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.newRole); - await fillIn('[name="name"]', 'role name'); - await click('[type="submit"]'); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'The request was invalid.', - 'Displays primary error message.' - ); - assert.ok( - find('.rose-form-error-message').textContent.trim(), - 'Name is required.', - 'Displays field-level errors.' - ); - }); - test('can save changes to an existing role', async function (assert) { assert.expect(2); await visit(urls.role);