From 74df9237753a61afddbf1a7eb03ae9c4d6415386 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Thu, 5 Aug 2021 13:29:33 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20apply=20base=20singular?= =?UTF-8?q?=20capabilities=20to=20account?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/form/account/oidc/index.hbs | 14 +- .../form/account/password/index.hbs | 14 +- .../auth-method/accounts/account/-actions.hbs | 26 ++-- .../auth-method/accounts/index.hbs | 32 ++-- ui/admin/tests/acceptance/accounts-test.js | 120 --------------- .../tests/acceptance/accounts/delete-test.js | 102 +++++++++++++ .../tests/acceptance/accounts/read-test.js | 76 ++++++++++ .../tests/acceptance/accounts/update-test.js | 142 ++++++++++++++++++ 8 files changed, 370 insertions(+), 156 deletions(-) create mode 100644 ui/admin/tests/acceptance/accounts/delete-test.js create mode 100644 ui/admin/tests/acceptance/accounts/read-test.js create mode 100644 ui/admin/tests/acceptance/accounts/update-test.js diff --git a/ui/admin/app/components/form/account/oidc/index.hbs b/ui/admin/app/components/form/account/oidc/index.hbs index b3a2dd8a7..16cc9c0b8 100644 --- a/ui/admin/app/components/form/account/oidc/index.hbs +++ b/ui/admin/app/components/form/account/oidc/index.hbs @@ -117,11 +117,13 @@ {{/if}} - + {{#if (can 'save model' @model)}} + + {{/if}} \ No newline at end of file diff --git a/ui/admin/app/components/form/account/password/index.hbs b/ui/admin/app/components/form/account/password/index.hbs index 6c4ac649a..cdfc1ca78 100644 --- a/ui/admin/app/components/form/account/password/index.hbs +++ b/ui/admin/app/components/form/account/password/index.hbs @@ -91,11 +91,13 @@ {{/if}} - + {{#if (can 'save model' @model)}} + + {{/if}} \ No newline at end of file diff --git a/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/account/-actions.hbs b/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/account/-actions.hbs index 108a22afd..aa7db7b12 100644 --- a/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/account/-actions.hbs +++ b/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/account/-actions.hbs @@ -1,13 +1,15 @@ - - - {{t 'resources.account.actions.delete'}} - - \ No newline at end of file + + {{t 'resources.account.actions.delete'}} + + +{{/if}} \ No newline at end of file diff --git a/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/index.hbs b/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/index.hbs index d1ee9dde5..c35071bf7 100644 --- a/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/index.hbs +++ b/ui/admin/app/templates/scopes/scope/auth-methods/auth-method/accounts/index.hbs @@ -18,13 +18,17 @@ {{#each @model as |account|}} - + {{#if (can 'read model' account)}} + + {{account.accountName}} + + {{else}} {{account.accountName}} -

{{account.description}}

-
+ {{/if}} +

{{account.description}}

{{account.attributes.issuer}} @@ -68,13 +72,17 @@ {{#each @model as |account|}} - + {{#if (can 'read model' account)}} + + {{account.accountName}} + + {{else}} {{account.accountName}} -

{{account.description}}

-
+ {{/if}} +

{{account.description}}

{{account.attributes.login_name}} diff --git a/ui/admin/tests/acceptance/accounts-test.js b/ui/admin/tests/acceptance/accounts-test.js index 5b16765f8..5430e36db 100644 --- a/ui/admin/tests/acceptance/accounts-test.js +++ b/ui/admin/tests/acceptance/accounts-test.js @@ -68,32 +68,6 @@ module('Acceptance | accounts', function (hooks) { assert.equal(currentURL(), urls.accounts); }); - test('can navigate to an account form', async function (assert) { - assert.expect(1); - await visit(urls.accounts); - await click('main tbody .rose-table-header-cell:nth-child(1) a'); - await a11yAudit(); - assert.equal(currentURL(), urls.account); - }); - - test('can update an account and save changes', async function (assert) { - assert.expect(1); - await visit(urls.account); - await click('form [type="button"]', 'Activate edit mode'); - await fillIn('[name="name"]', 'update name'); - await click('form [type="submit"]:not(:disabled)'); - assert.equal(this.server.db.accounts[0].name, 'update name'); - }); - - test('can update an account and cancel changes', async function (assert) { - assert.expect(1); - await visit(urls.account); - await click('form [type="button"]', 'Activate edit mode'); - await fillIn('[name="name"]', 'update name'); - await click('form button:not([type="submit"])'); - assert.notEqual(this.server.db.accounts[0].name, 'update name'); - }); - test('can create a new account', async function (assert) { assert.expect(1); const accountsCount = this.server.db.accounts.length; @@ -116,14 +90,6 @@ module('Acceptance | accounts', function (hooks) { assert.equal(currentURL(), urls.accounts); }); - test('can delete an account', async function (assert) { - assert.expect(1); - const accountsCount = this.server.db.accounts.length; - await visit(urls.account); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.equal(this.server.db.accounts.length, accountsCount - 1); - }); - test('saving a new account with invalid fields displays error messages', async function (assert) { assert.expect(2); this.server.post('/accounts', () => { @@ -160,90 +126,4 @@ module('Acceptance | accounts', function (hooks) { 'Displays field-level errors.' ); }); - - test('errors are displayed when save on account fails', async function (assert) { - assert.expect(1); - this.server.patch('/accounts/:id', () => { - return new Response( - 490, - {}, - { - status: 490, - code: 'error', - message: 'Oops.', - } - ); - }); - await visit(urls.account); - await click('form [type="button"]', 'Activate edit mode'); - await fillIn('[name="name"]', 'save account'); - await click('form [type="submit"]'); - await a11yAudit(); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'Oops.', - 'Displays primary error message.' - ); - }); - - test('errors are displayed when delete on account fails', async function (assert) { - assert.expect(1); - this.server.del('/accounts/:id', () => { - return new Response( - 490, - {}, - { - status: 490, - code: 'error', - message: 'Oops.', - } - ); - }); - await visit(urls.account); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - await a11yAudit(); - assert.ok( - find('[role="alert"]').textContent.trim(), - 'Oops.', - 'Displays primary error message.' - ); - }); - - test('saving an existing account with invalid fields displays error messages', async function (assert) { - assert.expect(2); - this.server.patch('/accounts/: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.account); - await click('form [type="button"]', 'Activate edit mode'); - await fillIn('[name="name"]', 'existing account'); - await click('form [type="submit"]'); - await a11yAudit(); - 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.' - ); - }); }); diff --git a/ui/admin/tests/acceptance/accounts/delete-test.js b/ui/admin/tests/acceptance/accounts/delete-test.js new file mode 100644 index 000000000..8b78b1834 --- /dev/null +++ b/ui/admin/tests/acceptance/accounts/delete-test.js @@ -0,0 +1,102 @@ +import { module, test } from 'qunit'; +import { visit, 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 { Response } from 'miragejs'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | accounts | delete', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + authMethods: null, + account: null, + }; + const urls = { + orgScope: null, + authMethods: null, + accounts: null, + account: 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.authMethod = this.server.create('auth-method', { + scope: instances.scopes.org, + }); + instances.account = this.server.create('account', { + scope: instances.scopes.org, + authMethod: instances.authMethod, + }); + // Generate route URLs for resources + urls.orgScope = `/scopes/${instances.scopes.org.id}`; + urls.authMethods = `${urls.orgScope}/auth-methods`; + urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`; + urls.accounts = `${urls.authMethod}/accounts`; + urls.account = `${urls.accounts}/${instances.account.id}`; + }); + + hooks.afterEach(async function () { + const notification = find('.rose-notification'); + if (notification) { + await click('.rose-notification-dismiss'); + } + }); + + test('can delete an account', async function (assert) { + assert.expect(1); + const accountsCount = this.server.db.accounts.length; + await visit(urls.account); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(this.server.db.accounts.length, accountsCount - 1); + }); + + test('cannot delete an account without proper authorization', async function (assert) { + assert.expect(1); + instances.account.authorized_actions = + instances.account.authorized_actions.filter((item) => item !== 'delete'); + await visit(urls.account); + assert.notOk( + find('.rose-layout-page-actions .rose-dropdown-button-danger') + ); + }); + + test('errors are displayed when delete on account fails', async function (assert) { + assert.expect(1); + this.server.del('/accounts/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.account); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + await a11yAudit(); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Oops.', + 'Displays primary error message.' + ); + }); +}); diff --git a/ui/admin/tests/acceptance/accounts/read-test.js b/ui/admin/tests/acceptance/accounts/read-test.js new file mode 100644 index 000000000..b32e5e865 --- /dev/null +++ b/ui/admin/tests/acceptance/accounts/read-test.js @@ -0,0 +1,76 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, 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'; + +module('Acceptance | accounts | read', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + authMethods: null, + account: null, + }; + const urls = { + orgScope: null, + authMethods: null, + accounts: null, + account: 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.authMethod = this.server.create('auth-method', { + scope: instances.scopes.org, + }); + instances.account = this.server.create('account', { + scope: instances.scopes.org, + authMethod: instances.authMethod, + }); + // Generate route URLs for resources + urls.orgScope = `/scopes/${instances.scopes.org.id}`; + urls.authMethods = `${urls.orgScope}/auth-methods`; + urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`; + urls.accounts = `${urls.authMethod}/accounts`; + urls.account = `${urls.accounts}/${instances.account.id}`; + }); + + hooks.afterEach(async function () { + const notification = find('.rose-notification'); + if (notification) { + await click('.rose-notification-dismiss'); + } + }); + + test('can navigate to an account form', async function (assert) { + assert.expect(1); + await visit(urls.accounts); + await click('main tbody .rose-table-header-cell:nth-child(1) a'); + await a11yAudit(); + assert.equal(currentURL(), urls.account); + }); + + test('cannot navigate to an account form without proper authorization', async function (assert) { + assert.expect(1); + instances.account.authorized_actions = + instances.account.authorized_actions.filter((item) => item !== 'read'); + await visit(urls.accounts); + assert.notOk(find('main tbody .rose-table-header-cell:nth-child(1) a')); + }); +}); diff --git a/ui/admin/tests/acceptance/accounts/update-test.js b/ui/admin/tests/acceptance/accounts/update-test.js new file mode 100644 index 000000000..ecce31da0 --- /dev/null +++ b/ui/admin/tests/acceptance/accounts/update-test.js @@ -0,0 +1,142 @@ +import { module, test } from 'qunit'; +import { visit, click, find, fillIn } 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 | accounts | update', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + }, + authMethods: null, + account: null, + }; + const urls = { + orgScope: null, + authMethods: null, + accounts: null, + account: 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.authMethod = this.server.create('auth-method', { + scope: instances.scopes.org, + }); + instances.account = this.server.create('account', { + scope: instances.scopes.org, + authMethod: instances.authMethod, + }); + // Generate route URLs for resources + urls.orgScope = `/scopes/${instances.scopes.org.id}`; + urls.authMethods = `${urls.orgScope}/auth-methods`; + urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`; + urls.accounts = `${urls.authMethod}/accounts`; + urls.account = `${urls.accounts}/${instances.account.id}`; + }); + + hooks.afterEach(async function () { + const notification = find('.rose-notification'); + if (notification) { + await click('.rose-notification-dismiss'); + } + }); + + test('can update an account and save changes', async function (assert) { + assert.expect(1); + await visit(urls.account); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'update name'); + await click('form [type="submit"]:not(:disabled)'); + assert.equal(this.server.db.accounts[0].name, 'update name'); + }); + + test('can update an account and cancel changes', async function (assert) { + assert.expect(1); + await visit(urls.account); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'update name'); + await click('form button:not([type="submit"])'); + assert.notEqual(this.server.db.accounts[0].name, 'update name'); + }); + + test('errors are displayed when save on account fails', async function (assert) { + assert.expect(1); + this.server.patch('/accounts/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.account); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'save account'); + await click('form [type="submit"]'); + await a11yAudit(); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Oops.', + 'Displays primary error message.' + ); + }); + + test('saving an existing account with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.patch('/accounts/: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.account); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'existing account'); + await click('form [type="submit"]'); + await a11yAudit(); + 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.' + ); + }); +});