feat: 🎸 credential library read/update/delete capabilities

This commit is contained in:
Randall Morey committed 2021-08-16 16:12:55 -04:00
1 parent 8bfd9bd7a3
commit 9abb540d7f
9 files changed
+413 -155

No files matched your search

@@ -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':
@@ -118,10 +118,13 @@
</form.textarea>
{{/if}}
<form.actions
@disabled={{if @model.cannotSave @model.cannotSave}}
@enableEditText={{t 'actions.edit-form'}}
@submitText={{t 'actions.save'}}
@cancelText={{t 'actions.cancel'}}
/>
{{#if (can 'save model' @model)}}
<form.actions
@disabled={{if @model.cannotSave @model.cannotSave}}
@enableEditText={{t 'actions.edit-form'}}
@submitText={{t 'actions.save'}}
@cancelText={{t 'actions.cancel'}}
/>
{{/if}}
</Rose::Form>
@@ -1,13 +1,15 @@
<Rose::Dropdown
@text={{t 'actions.manage'}}
@dropdownRight={{true}}
as |dropdown|
>
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
{{#if (can 'delete model' @model)}}
<Rose::Dropdown
@text={{t 'actions.manage'}}
@dropdownRight={{true}}
as |dropdown|
>
{{t 'resources.credential-library.actions.delete'}}
</dropdown.button>
</Rose::Dropdown>
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
>
{{t 'resources.credential-library.actions.delete'}}
</dropdown.button>
</Rose::Dropdown>
{{/if}}
@@ -12,12 +12,16 @@
{{#each @model as |credentialLibrary|}}
<body.row as |row|>
<row.headerCell>
<LinkTo
@route='scopes.scope.credential-stores.credential-store.credential-libraries.credential-library'
@model={{credentialLibrary.id}}
>
{{#if (can 'read model' credentialLibrary)}}
<LinkTo
@route='scopes.scope.credential-stores.credential-store.credential-libraries.credential-library'
@model={{credentialLibrary.id}}
>
{{credentialLibrary.displayName}}
</LinkTo>
{{else}}
{{credentialLibrary.displayName}}
</LinkTo>
{{/if}}
<p>{{credentialLibrary.description}}</p>
</row.headerCell>
<row.cell>
@@ -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);
@@ -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.'
);
});
});
@@ -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.');
});
});
@@ -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'));
});
});
@@ -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.');
});
});