diff --git a/addons/api/mirage/factories/credential-library.js b/addons/api/mirage/factories/credential-library.js index 06d8c4f6c..621ae6410 100644 --- a/addons/api/mirage/factories/credential-library.js +++ b/addons/api/mirage/factories/credential-library.js @@ -1,5 +1,6 @@ import factory from '../generated/factories/credential-library'; import { random, system } from 'faker'; +import permissions from '../helpers/permissions'; const types = ['vault']; @@ -7,6 +8,14 @@ export default factory.extend({ id: (i) => `credential-library-id-${i}`, type: (i) => types[i % types.length], + authorized_actions: () => + permissions.authorizedActionsFor('credential-library') || [ + 'no-op', + 'read', + 'update', + 'delete', + ], + attributes() { switch (this.type) { case 'vault': diff --git a/ui/admin/app/components/form/credential-library/index.hbs b/ui/admin/app/components/form/credential-library/index.hbs index 3e3add0ee..29d016e7c 100644 --- a/ui/admin/app/components/form/credential-library/index.hbs +++ b/ui/admin/app/components/form/credential-library/index.hbs @@ -118,10 +118,13 @@ {{/if}} - + {{#if (can 'save model' @model)}} + + {{/if}} + \ No newline at end of file diff --git a/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/-actions.hbs b/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/-actions.hbs index 759b187d5..4399f847b 100644 --- a/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/-actions.hbs +++ b/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/-actions.hbs @@ -1,13 +1,15 @@ - - - {{t 'resources.credential-library.actions.delete'}} - - \ No newline at end of file + + {{t 'resources.credential-library.actions.delete'}} + + +{{/if}} \ No newline at end of file diff --git a/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/index.hbs b/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/index.hbs index e97d52411..2bb3101c2 100644 --- a/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/index.hbs +++ b/ui/admin/app/templates/scopes/scope/credential-stores/credential-store/credential-libraries/index.hbs @@ -12,12 +12,16 @@ {{#each @model as |credentialLibrary|}} - + {{#if (can 'read model' credentialLibrary)}} + + {{credentialLibrary.displayName}} + + {{else}} {{credentialLibrary.displayName}} - + {{/if}}

{{credentialLibrary.description}}

diff --git a/ui/admin/tests/acceptance/accounts/update-test.js b/ui/admin/tests/acceptance/accounts/update-test.js index ecce31da0..163b7ac9e 100644 --- a/ui/admin/tests/acceptance/accounts/update-test.js +++ b/ui/admin/tests/acceptance/accounts/update-test.js @@ -59,7 +59,7 @@ module('Acceptance | accounts | update', function (hooks) { } }); - test('can update an account and save changes', async function (assert) { + test('can update resource and save changes', async function (assert) { assert.expect(1); await visit(urls.account); await click('form [type="button"]', 'Activate edit mode'); @@ -68,6 +68,14 @@ module('Acceptance | accounts | update', function (hooks) { assert.equal(this.server.db.accounts[0].name, 'update name'); }); + test('cannot update resource without proper authorization', async function (assert) { + assert.expect(1); + instances.account.authorized_actions = + instances.account.authorized_actions.filter((item) => item !== 'update'); + await visit(urls.account); + assert.notOk(find('form [type="button"]')); + }); + test('can update an account and cancel changes', async function (assert) { assert.expect(1); await visit(urls.account); diff --git a/ui/admin/tests/acceptance/credential-libraries-test.js b/ui/admin/tests/acceptance/credential-libraries-test.js new file mode 100644 index 000000000..d4dcce482 --- /dev/null +++ b/ui/admin/tests/acceptance/credential-libraries-test.js @@ -0,0 +1,138 @@ +import { module, test } from 'qunit'; +import { visit, click, fillIn, currentURL, 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 } from 'ember-simple-auth/test-support'; +import { Response } from 'miragejs'; + +module('Acceptance | credential-libraries', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + let getCredentialLibraryCount; + + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + }; + + const urls = { + globalScope: null, + orgScope: null, + projectScope: null, + credentialStores: null, + credentialStore: null, + credentialLibrary: null, + credentialLibraries: null, + newCredentialLibrary: null, + unknownCredentialLibrary: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.credentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + }); + instances.credentialLibrary = this.server.create('credential-library', { + scope: instances.scopes.project, + credentialStore: instances.credentialStore, + }); + // Generate route URLs for resources + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.credentialStore = `${urls.credentialStores}/${instances.credentialStore.id}`; + urls.credentialLibraries = `${urls.credentialStore}/credential-libraries`; + urls.credentialLibrary = `${urls.credentialLibraries}/${instances.credentialLibrary.id}`; + urls.newCredentialLibrary = `${urls.credentialLibraries}/new`; + urls.unknownCredentialLibrary = `${urls.credentialLibraries}/foo`; + // Generate resource counter + getCredentialLibraryCount = () => + this.server.schema.credentialLibraries.all().models.length; + authenticateSession({}); + }); + + test('visiting credential libraries', async function (assert) { + assert.expect(2); + await visit(urls.credentialLibraries); + await a11yAudit(); + assert.equal(currentURL(), urls.credentialLibraries); + await visit(urls.credentialLibrary); + await a11yAudit(); + assert.equal(currentURL(), urls.credentialLibrary); + }); + + test('visiting an unknown credential library displays 404 message', async function (assert) { + assert.expect(1); + await visit(urls.unknownCredentialLibrary); + await a11yAudit(); + console.debug(find('.rose-message-subtitle')); + assert.ok(find('.rose-message-subtitle').textContent.trim(), 'Error 404'); + }); + + test('can create a credential library', async function (assert) { + assert.expect(1); + const count = getCredentialLibraryCount(); + await visit(urls.newCredentialLibrary); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.equal(getCredentialLibraryCount(), count + 1); + }); + + test('can cancel create a new credential library', async function (assert) { + assert.expect(2); + const count = getCredentialLibraryCount(); + await visit(urls.newCredentialLibrary); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.equal(currentURL(), urls.credentialLibraries); + assert.equal(getCredentialLibraryCount(), count); + }); + + test('saving a new credential library with invalid fields displays error messasges', async function (assert) { + assert.expect(2); + this.server.post('/credential-libraries', () => { + 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.newCredentialLibrary); + 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.' + ); + }); +}); diff --git a/ui/admin/tests/acceptance/credential-library/delete-test.js b/ui/admin/tests/acceptance/credential-library/delete-test.js new file mode 100644 index 000000000..e539d00e9 --- /dev/null +++ b/ui/admin/tests/acceptance/credential-library/delete-test.js @@ -0,0 +1,131 @@ +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 { authenticateSession } from 'ember-simple-auth/test-support'; +import { Response } from 'miragejs'; +import { resolve, reject } from 'rsvp'; +import sinon from 'sinon'; + +module('Acceptance | credential-libraries | delete', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + let getCredentialLibraryCount; + + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + }; + + const urls = { + globalScope: null, + orgScope: null, + projectScope: null, + credentialStores: null, + credentialStore: null, + credentialLibrary: null, + credentialLibraries: null, + newCredentialLibrary: null, + unknownCredentialLibrary: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.credentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + }); + instances.credentialLibrary = this.server.create('credential-library', { + scope: instances.scopes.project, + credentialStore: instances.credentialStore, + }); + // Generate route URLs for resources + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.credentialStore = `${urls.credentialStores}/${instances.credentialStore.id}`; + urls.credentialLibraries = `${urls.credentialStore}/credential-libraries`; + urls.credentialLibrary = `${urls.credentialLibraries}/${instances.credentialLibrary.id}`; + urls.newCredentialLibrary = `${urls.credentialLibraries}/new`; + urls.unknownCredentialLibrary = `${urls.credentialLibraries}/foo`; + // Generate resource counter + getCredentialLibraryCount = () => + this.server.schema.credentialLibraries.all().models.length; + authenticateSession({}); + }); + + test('can delete resource', async function (assert) { + assert.expect(1); + const count = getCredentialLibraryCount(); + await visit(urls.credentialLibrary); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getCredentialLibraryCount(), count - 1); + }); + + test('cannot delete resource without proper authorization', async function (assert) { + assert.expect(1); + instances.credentialLibrary.authorized_actions = + instances.credentialLibrary.authorized_actions.filter( + (item) => item !== 'delete' + ); + await visit(urls.credentialLibrary); + assert.notOk( + find('.rose-layout-page-actions .rose-dropdown-button-danger') + ); + }); + + test('can accept delete credential library via dialog', async function (assert) { + assert.expect(2); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + confirmService.confirm = sinon.fake.returns(resolve()); + const count = getCredentialLibraryCount(); + await visit(urls.credentialLibrary); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getCredentialLibraryCount(), count - 1); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('cannot cancel delete credential library via dialog', async function (assert) { + assert.expect(2); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + confirmService.confirm = sinon.fake.returns(reject()); + const count = getCredentialLibraryCount(); + await visit(urls.credentialLibrary); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.equal(getCredentialLibraryCount(), count); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('deleting a credential library which errors displays error messages', async function (assert) { + assert.expect(1); + this.server.del('/credential-libraries/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.credentialLibrary); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.'); + }); +}); diff --git a/ui/admin/tests/acceptance/credential-library/read-test.js b/ui/admin/tests/acceptance/credential-library/read-test.js new file mode 100644 index 000000000..6e94637da --- /dev/null +++ b/ui/admin/tests/acceptance/credential-library/read-test.js @@ -0,0 +1,80 @@ +import { module, test } from 'qunit'; +import { visit, click, currentURL, 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 } from 'ember-simple-auth/test-support'; + +module('Acceptance | credential-libraries | read', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + }; + + const urls = { + globalScope: null, + orgScope: null, + projectScope: null, + credentialStores: null, + credentialStore: null, + credentialLibrary: null, + credentialLibraries: null, + newCredentialLibrary: null, + unknownCredentialLibrary: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.credentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + }); + instances.credentialLibrary = this.server.create('credential-library', { + scope: instances.scopes.project, + credentialStore: instances.credentialStore, + }); + // Generate route URLs for resources + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.credentialStore = `${urls.credentialStores}/${instances.credentialStore.id}`; + urls.credentialLibraries = `${urls.credentialStore}/credential-libraries`; + urls.credentialLibrary = `${urls.credentialLibraries}/${instances.credentialLibrary.id}`; + urls.newCredentialLibrary = `${urls.credentialLibraries}/new`; + urls.unknownCredentialLibrary = `${urls.credentialLibraries}/foo`; + authenticateSession({}); + }); + + test('can navigate to resource', async function (assert) { + assert.expect(1); + await visit(urls.credentialLibraries); + await click('main tbody .rose-table-header-cell a'); + await a11yAudit(); + assert.equal(currentURL(), urls.credentialLibrary); + }); + + test('cannot navigate to resource without proper authorization', async function (assert) { + assert.expect(1); + instances.credentialLibrary.authorized_actions = + instances.credentialLibrary.authorized_actions.filter( + (item) => item !== 'read' + ); + await visit(urls.credentialLibraries); + assert.notOk(find('main tbody .rose-table-header-cell a')); + }); +}); diff --git a/ui/admin/tests/acceptance/credential-library/credential-library-test.js b/ui/admin/tests/acceptance/credential-library/update-test.js similarity index 56% rename from ui/admin/tests/acceptance/credential-library/credential-library-test.js rename to ui/admin/tests/acceptance/credential-library/update-test.js index c2b71f8f1..7958ccc6f 100644 --- a/ui/admin/tests/acceptance/credential-library/credential-library-test.js +++ b/ui/admin/tests/acceptance/credential-library/update-test.js @@ -2,18 +2,13 @@ import { module, test } from 'qunit'; import { visit, click, fillIn, currentURL, 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 } from 'ember-simple-auth/test-support'; import { Response } from 'miragejs'; -import { resolve, reject } from 'rsvp'; -import sinon from 'sinon'; -module('Acceptance | credential-library', function (hooks) { +module('Acceptance | credential-libraries | update', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); - let getCredentialLibraryCount; - const instances = { scopes: { global: null, @@ -50,6 +45,7 @@ module('Acceptance | credential-library', function (hooks) { }); instances.credentialLibrary = this.server.create('credential-library', { scope: instances.scopes.project, + credentialStore: instances.credentialStore, }); // Generate route URLs for resources urls.globalScope = `/scopes/global/scopes`; @@ -61,83 +57,10 @@ module('Acceptance | credential-library', function (hooks) { urls.credentialLibrary = `${urls.credentialLibraries}/${instances.credentialLibrary.id}`; urls.newCredentialLibrary = `${urls.credentialLibraries}/new`; urls.unknownCredentialLibrary = `${urls.credentialLibraries}/foo`; - // Generate resource counter - getCredentialLibraryCount = () => - this.server.schema.credentialLibraries.all().models.length; authenticateSession({}); }); - test('visiting credential libraries', async function (assert) { - assert.expect(2); - await visit(urls.credentialLibraries); - await a11yAudit(); - assert.equal(currentURL(), urls.credentialLibraries); - await visit(urls.credentialLibrary); - await a11yAudit(); - assert.equal(currentURL(), urls.credentialLibrary); - }); - - test('visiting an unknown credential library displays 404 message', async function (assert) { - assert.expect(1); - await visit(urls.unknownCredentialLibrary); - await a11yAudit(); - console.debug(find('.rose-message-subtitle')); - assert.ok(find('.rose-message-subtitle').textContent.trim(), 'Error 404'); - }); - - test('can create a credential library', async function (assert) { - assert.expect(1); - const count = getCredentialLibraryCount(); - await visit(urls.newCredentialLibrary); - await fillIn('[name="name"]', 'random string'); - await click('[type="submit"]'); - assert.equal(getCredentialLibraryCount(), count + 1); - }); - - test('can cancel create a new credential library', async function (assert) { - assert.expect(2); - const count = getCredentialLibraryCount(); - await visit(urls.newCredentialLibrary); - await fillIn('[name="name"]', 'random string'); - await click('.rose-form-actions [type="button"]'); - assert.equal(currentURL(), urls.credentialLibraries); - assert.equal(getCredentialLibraryCount(), count); - }); - - test('saving a new credential library with invalid fields displays error messasges', async function (assert) { - assert.expect(2); - this.server.post('/credential-libraries', () => { - 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.newCredentialLibrary); - 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.' - ); - }); - - test('can save changes to existing credential library', async function (assert) { + test('can update resource and save changes', async function (assert) { assert.expect(3); assert.notEqual(instances.credentialLibrary.name, 'random string'); await visit(urls.credentialLibrary); @@ -151,7 +74,17 @@ module('Acceptance | credential-library', function (hooks) { ); }); - test('can cancel changes to existing credential library', async function (assert) { + test('cannot update resource without proper authorization', async function (assert) { + assert.expect(1); + instances.credentialLibrary.authorized_actions = + instances.credentialLibrary.authorized_actions.filter( + (item) => item !== 'update' + ); + await visit(urls.credentialLibrary); + assert.notOk(find('form [type="button"]')); + }); + + test('can update a credential library and cancel changes', async function (assert) { assert.expect(2); await visit(urls.credentialLibrary); await click('form [type="button"]', 'Activate edit mode'); @@ -245,54 +178,4 @@ module('Acceptance | credential-library', function (hooks) { ); } }); - - test('can delete credential library', async function (assert) { - assert.expect(1); - const count = getCredentialLibraryCount(); - await visit(urls.credentialLibrary); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.equal(getCredentialLibraryCount(), count - 1); - }); - - test('can accept delete credential library via dialog', async function (assert) { - assert.expect(2); - const confirmService = this.owner.lookup('service:confirm'); - confirmService.enabled = true; - confirmService.confirm = sinon.fake.returns(resolve()); - const count = getCredentialLibraryCount(); - await visit(urls.credentialLibrary); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.equal(getCredentialLibraryCount(), count - 1); - assert.ok(confirmService.confirm.calledOnce); - }); - - test('cannot cancel delete credential library via dialog', async function (assert) { - assert.expect(2); - const confirmService = this.owner.lookup('service:confirm'); - confirmService.enabled = true; - confirmService.confirm = sinon.fake.returns(reject()); - const count = getCredentialLibraryCount(); - await visit(urls.credentialLibrary); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.equal(getCredentialLibraryCount(), count); - assert.ok(confirmService.confirm.calledOnce); - }); - - test('deleting a credential library which errors displays error messages', async function (assert) { - assert.expect(1); - this.server.del('/credential-libraries/:id', () => { - return new Response( - 490, - {}, - { - status: 490, - code: 'error', - message: 'Oops.', - } - ); - }); - await visit(urls.credentialLibrary); - await click('.rose-layout-page-actions .rose-dropdown-button-danger'); - assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.'); - }); });