From 5abd4f1f4fc528a137779a1235dc55abfe0ca15f Mon Sep 17 00:00:00 2001 From: Cameron Perera Date: Fri, 10 Jan 2025 09:57:32 -0600 Subject: [PATCH] Icu 15470 credential store create test (#2650) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 💡 clean up urls in create test file ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-15470 * test: 💍 update cred store create to use selectors ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-15470 --- .../form/credential-store/vault/index.hbs | 4 +- .../credential-store/create-test.js | 93 +++++++++++++------ .../acceptance/credential-store/selectors.js | 5 + 3 files changed, 74 insertions(+), 28 deletions(-) diff --git a/ui/admin/app/components/form/credential-store/vault/index.hbs b/ui/admin/app/components/form/credential-store/vault/index.hbs index d689ae2030..e036d1d52c 100644 --- a/ui/admin/app/components/form/credential-store/vault/index.hbs +++ b/ui/admin/app/components/form/credential-store/vault/index.hbs @@ -109,7 +109,9 @@ {{#if @model.errors.address}} {{#each @model.errors.address as |error|}} - {{error.message}} + {{error.message}} {{/each}} {{/if}} diff --git a/ui/admin/tests/acceptance/credential-store/create-test.js b/ui/admin/tests/acceptance/credential-store/create-test.js index 3eb9219975..24f09d4a47 100644 --- a/ui/admin/tests/acceptance/credential-store/create-test.js +++ b/ui/admin/tests/acceptance/credential-store/create-test.js @@ -4,12 +4,14 @@ */ import { module, test } from 'qunit'; -import { visit, currentURL, find, click, fillIn } from '@ember/test-helpers'; +import { visit, currentURL, click, fillIn } from '@ember/test-helpers'; import { setupApplicationTest } from 'admin/tests/helpers'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { setupIndexedDb } from 'api/test-support/helpers/indexed-db'; import { authenticateSession } from 'ember-simple-auth/test-support'; import { Response } from 'miragejs'; +import * as selectors from './selectors'; +import * as commonSelectors from 'admin/tests/helpers/selectors'; module('Acceptance | credential-stores | create', function (hooks) { setupApplicationTest(hooks); @@ -30,8 +32,6 @@ module('Acceptance | credential-stores | create', function (hooks) { }; const urls = { - globalScope: null, - projectScope: null, credentialStores: null, newCredentialStore: null, }; @@ -48,9 +48,7 @@ module('Acceptance | credential-stores | create', function (hooks) { scope: { id: instances.scopes.org.id, type: 'org' }, }); // Generate route URLs for resources - urls.globalScope = `/scopes/global/scopes`; - urls.projectScope = `/scopes/${instances.scopes.project.id}`; - urls.credentialStores = `${urls.projectScope}/credential-stores`; + urls.credentialStores = `/scopes/${instances.scopes.project.id}/credential-stores`; urls.newCredentialStore = `${urls.credentialStores}/new`; // Generate resource counter getCredentialStoresCount = () => { @@ -72,8 +70,10 @@ module('Acceptance | credential-stores | create', function (hooks) { featuresService.enable('static-credentials'); const count = getStaticCredentialStoresCount(); await visit(urls.newCredentialStore); - await fillIn('[name="name"]', 'random string'); - await click('[type="submit"]'); + + await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE); + await click(commonSelectors.SAVE_BTN); + assert.strictEqual(getStaticCredentialStoresCount(), count + 1); }); @@ -81,17 +81,21 @@ module('Acceptance | credential-stores | create', function (hooks) { featuresService.enable('static-credentials'); const count = getVaultCredentialStoresCount(); await visit(urls.newCredentialStore); - await fillIn('[name="name"]', 'random string'); - await click('[value="vault"]'); - await click('[type="submit"]'); + + await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE); + await click(selectors.TYPE_VAULT); + await click(commonSelectors.SAVE_BTN); + assert.strictEqual(getVaultCredentialStoresCount(), count + 1); }); test('Users can cancel create new credential stores', async function (assert) { const count = getCredentialStoresCount(); await visit(urls.newCredentialStore); - await fillIn('[name="name"]', 'random string'); - await click('.rose-form-actions [type="button"]'); + + await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE); + await click(commonSelectors.CANCEL_BTN); + assert.strictEqual(currentURL(), urls.credentialStores); assert.strictEqual(getCredentialStoresCount(), count); }); @@ -101,15 +105,18 @@ module('Acceptance | credential-stores | create', function (hooks) { 'credential-stores' ] = []; await visit(urls.credentialStores); + assert.notOk( instances.scopes.project.authorized_collection_actions[ 'credential-stores' ].includes('create'), ); - assert.notOk(find(`[href="${urls.newCredentialStore}"]`)); + assert.dom(commonSelectors.HREF(urls.newCredentialStore)).doesNotExist(); }); - test('saving a new credential store with invalid fields displays error messages', async function (assert) { + test('saving a new static credential store with invalid fields displays error messages', async function (assert) { + const errorMsg = + 'Invalid request. Request attempted to make second resource with the same field value that must be unique.'; this.server.post('/credential-stores', () => { return new Response( 400, @@ -117,7 +124,7 @@ module('Acceptance | credential-stores | create', function (hooks) { { status: 400, code: 'invalid_argument', - message: 'The request was invalid.', + message: errorMsg, details: { request_fields: [ { @@ -130,23 +137,55 @@ module('Acceptance | credential-stores | create', function (hooks) { ); }); await visit(urls.newCredentialStore); - await click('[type="submit"]'); + + await click(commonSelectors.SAVE_BTN); + + assert.dom(commonSelectors.ALERT_TOAST_BODY).hasText(errorMsg); + }); + + test('saving a new vault credential store with invalid fields displays error messages', async function (assert) { + this.server.post('/credential-stores', () => { + return new Response( + 400, + {}, + { + status: 400, + code: 'invalid_argument', + message: 'The request was invalid.', + details: { + request_fields: [ + { + name: 'attributes.address', + description: + 'Field required for creating a vault credential store.', + }, + ], + }, + }, + ); + }); + await visit(urls.newCredentialStore); + + await click(commonSelectors.SAVE_BTN); + assert - .dom('[data-test-toast-notification] .hds-alert__description') + .dom(commonSelectors.ALERT_TOAST_BODY) .hasText('The request was invalid.'); - assert.ok( - find('[data-test-error-message-name]').textContent.trim(), - 'Name is required.', - ); + assert + .dom(selectors.FIELD_VAULT_ADDRESS_ERROR) + .hasText('Field required for creating a vault credential store.'); }); test('Users can link to docs page for new credential store', async function (assert) { await visit(urls.newCredentialStore); - assert.ok( - find( - `[href="https://developer.hashicorp.com/boundary/docs/concepts/domain-model/credential-stores"]`, - ), - ); + + assert + .dom( + commonSelectors.HREF( + 'https://developer.hashicorp.com/boundary/docs/concepts/domain-model/credential-stores', + ), + ) + .isVisible(); }); test('users cannot directly navigate to new credential store route without proper authorization', async function (assert) { diff --git a/ui/admin/tests/acceptance/credential-store/selectors.js b/ui/admin/tests/acceptance/credential-store/selectors.js index cfd3a2fe7e..681714bf75 100644 --- a/ui/admin/tests/acceptance/credential-store/selectors.js +++ b/ui/admin/tests/acceptance/credential-store/selectors.js @@ -11,3 +11,8 @@ export const EDIT_ACTION = export const CODE_EDITOR_BODY = '[data-test-code-editor-field-editor] textarea'; export const CODE_BLOCK_BODY = '.hds-code-block__code'; + +export const TYPE_VAULT = '[value="vault"]'; + +export const FIELD_VAULT_ADDRESS_ERROR = + '[data-test-error-message-vault-address]';