From f6e1fb0e3b32713bd1bccb8fe8f14b194bbe99d4 Mon Sep 17 00:00:00 2001 From: lisbet-alvarez <107949262+lisbet-alvarez@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:34:03 -0700 Subject: [PATCH] Icu 5330 credentials crudl tests (#1219) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: 💍 Add credentials CRUDL tests * fix: 🐛 Deleted empty files * test: 💍 Add missing tests for credentials * test: 💍 Revise tests in read-test.js ✅ Closes: ICU-5330 --- .../credentials/create-test.js | 124 ++++++++++++ .../credentials/delete-test.js | 131 +++++++++++++ .../credential-store/credentials/list-test.js | 86 ++++++++ .../credential-store/credentials/read-test.js | 90 +++++++++ .../credentials/update-test.js | 184 ++++++++++++++++++ 5 files changed, 615 insertions(+) create mode 100644 ui/admin/tests/acceptance/credential-store/credentials/create-test.js create mode 100644 ui/admin/tests/acceptance/credential-store/credentials/delete-test.js create mode 100644 ui/admin/tests/acceptance/credential-store/credentials/list-test.js create mode 100644 ui/admin/tests/acceptance/credential-store/credentials/read-test.js create mode 100644 ui/admin/tests/acceptance/credential-store/credentials/update-test.js diff --git a/ui/admin/tests/acceptance/credential-store/credentials/create-test.js b/ui/admin/tests/acceptance/credential-store/credentials/create-test.js new file mode 100644 index 000000000..5b7fe2d7d --- /dev/null +++ b/ui/admin/tests/acceptance/credential-store/credentials/create-test.js @@ -0,0 +1,124 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, find, click, fillIn } 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'; + +module( + 'Acceptance | credential-stores | credentials | create', + function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + let getCredentialsCount; + + const instances = { + scopes: { + org: null, + project: null, + }, + }; + + const urls = { + projectScope: null, + credentialStores: null, + staticCredentialStore: null, + credentials: null, + newCredential: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.staticCredentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + type: 'static', + }); + // Generate route URLs for resources + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`; + urls.credentials = `${urls.staticCredentialStore}/credentials`; + urls.newCredential = `${urls.staticCredentialStore}/credentials/new`; + // Generate resource counter + getCredentialsCount = () => { + return this.server.schema.credentials.all().models.length; + }; + authenticateSession({}); + }); + + test('Users can create a new credential', async function (assert) { + assert.expect(1); + const credentialsCount = getCredentialsCount(); + await visit(urls.newCredential); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.strictEqual(getCredentialsCount(), credentialsCount + 1); + }); + + test('Users can cancel create new credential', async function (assert) { + assert.expect(2); + const credentialsCount = getCredentialsCount(); + await visit(urls.newCredential); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.strictEqual(currentURL(), urls.credentials); + assert.strictEqual(getCredentialsCount(), credentialsCount); + }); + + test('Users cannot navigate to new credential route without proper authorization', async function (assert) { + assert.expect(2); + instances.staticCredentialStore.authorized_collection_actions.credentials = + []; + await visit(urls.staticCredentialStore); + assert.notOk( + instances.staticCredentialStore.authorized_collection_actions.credentials.includes( + 'create' + ) + ); + assert.notOk(find(`[href="${urls.newCredential}"]`)); + }); + + test('saving a new credential with invalid fields displays error messages', async function (assert) { + assert.expect(2); + this.server.post('/credentials', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'Error in provided request.', + details: { + request_fields: [ + { + name: 'attributes.password', + description: + 'Field required for creating a username-password credential.', + }, + ], + }, + } + ); + }); + await visit(urls.newCredential); + await click('[type="submit"]'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Error in provided request.' + ); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Field required for creating a username-password credential.' + ); + }); + } +); diff --git a/ui/admin/tests/acceptance/credential-store/credentials/delete-test.js b/ui/admin/tests/acceptance/credential-store/credentials/delete-test.js new file mode 100644 index 000000000..b0679c364 --- /dev/null +++ b/ui/admin/tests/acceptance/credential-store/credentials/delete-test.js @@ -0,0 +1,131 @@ +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'ember-qunit'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { authenticateSession } from 'ember-simple-auth/test-support'; +import { click, currentURL, visit, find } from '@ember/test-helpers'; +import { resolve, reject } from 'rsvp'; +import sinon from 'sinon'; +import { Response } from 'miragejs'; + +module( + 'Acceptance | credential-stores | credentials | delete', + function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + let getCredentialsCount; + + const instances = { + scopes: { + org: null, + project: null, + }, + }; + + const urls = { + projectScope: null, + credentialStores: null, + staticCredentialStore: null, + credentials: null, + credential: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.staticCredentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + type: 'static', + }); + instances.credential = this.server.create('credential', { + scope: instances.scopes.project, + credentialStore: instances.staticCredentialStore, + type: 'username_password', + }); + // Generate route URLs for resources + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`; + urls.credentials = `${urls.staticCredentialStore}/credentials`; + urls.credential = `${urls.credentials}/${instances.credential.id}`; + // Generate resource counter + getCredentialsCount = () => { + return this.server.schema.credentials.all().models.length; + }; + authenticateSession({}); + }); + + test('can delete credential', async function (assert) { + assert.expect(2); + const credentialsCount = getCredentialsCount(); + await visit(urls.credential); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.strictEqual(currentURL(), urls.credentials); + assert.strictEqual(getCredentialsCount(), credentialsCount - 1); + }); + + test('cannot delete a credential store without proper authorization', async function (assert) { + assert.expect(3); + const credentialsCount = getCredentialsCount(); + instances.credential.authorized_actions = + instances.credential.authorized_actions.filter( + (item) => item !== 'delete' + ); + await visit(urls.credential); + assert.strictEqual(currentURL(), urls.credential); + assert.notOk( + find('.rose-layout-page-actions .rose-dropdown-button-danger') + ); + assert.strictEqual(getCredentialsCount(), credentialsCount); + }); + + test('can accept delete credential 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 credentialsCount = getCredentialsCount(); + await visit(urls.credential); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.strictEqual(getCredentialsCount(), credentialsCount - 1); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('cannot cancel delete credential 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 credentialsCount = getCredentialsCount(); + await visit(urls.credential); + await click('.rose-layout-page-actions .rose-dropdown-button-danger'); + assert.strictEqual(getCredentialsCount(), credentialsCount); + assert.ok(confirmService.confirm.calledOnce); + }); + + test('deleting a credential which errors displays error message', async function (assert) { + assert.expect(1); + this.server.del('/credentials/:id', () => { + return new Response( + 490, + {}, + { + status: 490, + code: 'error', + message: 'Oops.', + } + ); + }); + await visit(urls.credential); + 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-store/credentials/list-test.js b/ui/admin/tests/acceptance/credential-store/credentials/list-test.js new file mode 100644 index 000000000..e56104b8a --- /dev/null +++ b/ui/admin/tests/acceptance/credential-store/credentials/list-test.js @@ -0,0 +1,86 @@ +import { module, test } from 'qunit'; +import { visit, 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'; + +module('Acceptance | credential-stores | credentials | list', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + org: null, + project: null, + }, + }; + + const urls = { + projectScope: null, + credentialStores: null, + staticCredentialStore: null, + credentials: null, + newCredential: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.staticCredentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + type: 'static', + }); + // Generate route URLs for resources + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`; + urls.credentials = `${urls.staticCredentialStore}/credentials`; + urls.newCredential = `${urls.staticCredentialStore}/credentials/new`; + authenticateSession({}); + }); + + test('Users can navigate to credentials with proper authorization', async function (assert) { + assert.expect(2); + await visit(urls.staticCredentialStore); + assert.ok( + instances.staticCredentialStore.authorized_collection_actions.credentials.includes( + 'list' + ) + ); + assert.ok(find(`[href="${urls.credentials}"]`)); + }); + + test('User cannot navigate to index without either list or create action', async function (assert) { + assert.expect(3); + instances.staticCredentialStore.authorized_collection_actions.credentials = + []; + await visit(urls.staticCredentialStore); + assert.notOk( + instances.staticCredentialStore.authorized_collection_actions.credentials.includes( + 'list' + ) + ); + assert.notOk( + instances.staticCredentialStore.authorized_collection_actions.credentials.includes( + 'create' + ) + ); + assert.notOk(find(`[href="${urls.credentials}"]`)); + }); + + test('User can navigate to index with only create action', async function (assert) { + assert.expect(2); + instances.staticCredentialStore.authorized_collection_actions.credentials = + ['create']; + await visit(urls.staticCredentialStore); + assert.ok(find(`[href="${urls.credentials}"]`)); + assert.ok(find(`.rose-layout-page-actions [href="${urls.newCredential}"]`)); + }); +}); diff --git a/ui/admin/tests/acceptance/credential-store/credentials/read-test.js b/ui/admin/tests/acceptance/credential-store/credentials/read-test.js new file mode 100644 index 000000000..8f6df1584 --- /dev/null +++ b/ui/admin/tests/acceptance/credential-store/credentials/read-test.js @@ -0,0 +1,90 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, find, click } 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-stores | credentials | read', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + org: null, + project: null, + }, + }; + + const urls = { + projectScope: null, + credentialStores: null, + staticCredentialStore: null, + credentials: null, + credential: null, + unknownCredential: null, + }; + + hooks.beforeEach(function () { + // Generate resources + 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.staticCredentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + type: 'static', + }); + instances.credential = this.server.create('credential', { + scope: instances.scopes.project, + credentialStore: instances.staticCredentialStore, + type: 'username_password', + }); + // Generate route URLs for resources + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`; + urls.credentials = `${urls.staticCredentialStore}/credentials`; + urls.credential = `${urls.credentials}/${instances.credential.id}`; + urls.unknownCredential = `${urls.credentials}/foo`; + authenticateSession({}); + }); + + test('visiting /credentials', async function (assert) { + assert.expect(2); + await visit(urls.staticCredentialStore); + await click(`[href="${urls.credentials}"]`); + await a11yAudit(); + assert.strictEqual(currentURL(), urls.credentials); + await click(`[href="${urls.credential}"]`); + await a11yAudit(); + assert.strictEqual(currentURL(), urls.credential); + }); + + test('cannot navigate to a credential form without proper authorization', async function (assert) { + assert.expect(1); + instances.credential.authorized_actions = + instances.credential.authorized_actions.filter((item) => item != 'read'); + await visit(urls.credentials); + assert.notOk(find(`[href="${urls.credential}"]`)); + }); + + test('visiting an unknown credential displays 404 message', async function (assert) { + assert.expect(1); + await visit(urls.unknownCredential); + await a11yAudit(); + assert.ok(find('.rose-message-subtitle').textContent.trim(), 'Error 404'); + }); + + test('Users can link to docs page for credential store', async function (assert) { + assert.expect(1); + await visit(urls.credential); + assert.ok( + find(`[href="https://boundaryproject.io/help/admin-ui/credentials"]`) + ); + }); +}); diff --git a/ui/admin/tests/acceptance/credential-store/credentials/update-test.js b/ui/admin/tests/acceptance/credential-store/credentials/update-test.js new file mode 100644 index 000000000..672bc8d7c --- /dev/null +++ b/ui/admin/tests/acceptance/credential-store/credentials/update-test.js @@ -0,0 +1,184 @@ +import { module, test } from 'qunit'; +import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers'; +import { setupApplicationTest } from 'ember-qunit'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { Response } from 'miragejs'; +import { authenticateSession } from 'ember-simple-auth/test-support'; + +module( + 'Acceptance | credential-stores | credentials | update', + function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + }; + + const urls = { + projectScope: null, + credentialStores: null, + staticCredentialStore: null, + credentials: null, + credential: 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.staticCredentialStore = this.server.create('credential-store', { + scope: instances.scopes.project, + type: 'static', + }); + instances.credential = this.server.create('credential', { + scope: instances.scopes.project, + credentialStore: instances.staticCredentialStore, + type: 'username_password', + }); + // Generate route URLs for resources + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.staticCredentialStore = `${urls.credentialStores}/${instances.staticCredentialStore.id}`; + urls.credentials = `${urls.staticCredentialStore}/credentials`; + urls.credential = `${urls.credentials}/${instances.credential.id}`; + authenticateSession({}); + }); + + test('can save changes to existing credential', async function (assert) { + assert.expect(3); + assert.notEqual(instances.credential.name, 'random string'); + await visit(urls.credential); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="submit"]'); + assert.strictEqual(currentURL(), urls.credential); + assert.strictEqual( + this.server.schema.credentials.all().models[0].name, + 'random string' + ); + }); + + test('cannot make changes to an existing credential without proper authorization', async function (assert) { + assert.expect(1); + instances.credential.authorized_actions = + instances.credential.authorized_actions.filter( + (item) => item !== 'update' + ); + await visit(urls.credential); + assert.notOk(find('.rose-layout-page-actions .rose-button-secondary')); + }); + + test('can cancel changes to existing credential', async function (assert) { + assert.expect(2); + await visit(urls.credential); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('.rose-form-actions [type="button"]'); + assert.notEqual(instances.credential.name, 'random string'); + assert.strictEqual( + find('[name="name"]').value, + instances.credential.name + ); + }); + + test('saving an existing credential with invalid fields displays error message', async function (assert) { + assert.expect(2); + this.server.patch('/credentials/:id', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'Error in provided request.', + details: { + request_fields: [ + { + name: 'name', + description: 'Name is required.', + }, + ], + }, + } + ); + }); + await visit(urls.credential); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + await click('[type="submit"]'); + assert.ok( + find('[role="alert"]').textContent.trim(), + 'Error in provided request.' + ); + assert.ok( + find('.rose-form-error-message').textContent.trim(), + 'Name is required.' + ); + }); + + test('can discard unsaved credential changes via dialog', async function (assert) { + assert.expect(5); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + assert.notEqual(instances.credential.name, 'random string'); + await visit(urls.credential); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + assert.strictEqual(currentURL(), urls.credential); + try { + await visit(urls.credentials); + } catch (e) { + assert.ok(find('.rose-dialog')); + await click('.rose-dialog-footer button:first-child', 'Click Discard'); + assert.strictEqual(currentURL(), urls.credentials); + assert.notEqual( + this.server.schema.credentials.all().models[0].name, + 'random string' + ); + } + }); + + test('can cancel discard unsaved credential changes via dialog', async function (assert) { + assert.expect(5); + const confirmService = this.owner.lookup('service:confirm'); + confirmService.enabled = true; + assert.notEqual(instances.credential.name, 'random string'); + await visit(urls.credential); + await click('form [type="button"]', 'Activate edit mode'); + await fillIn('[name="name"]', 'random string'); + assert.strictEqual(currentURL(), urls.credential); + try { + await visit(urls.credentials); + } catch (e) { + assert.ok(find('.rose-dialog')); + await click('.rose-dialog-footer button:last-child', 'Click Cancel'); + assert.strictEqual(currentURL(), urls.credential); + assert.notEqual( + this.server.schema.credentials.all().models[0].name, + 'random string' + ); + } + }); + + test('password field renders in edit mode only for a credential', async function (assert) { + assert.expect(3); + await visit(urls.credential); + assert.notOk(find('[name="password"]')); + await click('form [type="button"]', 'Activate edit mode'); + assert.strictEqual(currentURL(), urls.credential); + assert.ok(find('[name="password"]')); + }); + } +);