Skip to content

Commit

Permalink
Icu 15470 credential store create test (#2650)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cameronperera authored Jan 10, 2025
1 parent 862ce64 commit 5abd4f1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
{{#if @model.errors.address}}
<F.Error as |E|>
{{#each @model.errors.address as |error|}}
<E.Message>{{error.message}}</E.Message>
<E.Message
data-test-error-message-vault-address
>{{error.message}}</E.Message>
{{/each}}
</F.Error>
{{/if}}
Expand Down
93 changes: 66 additions & 27 deletions ui/admin/tests/acceptance/credential-store/create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -30,8 +32,6 @@ module('Acceptance | credential-stores | create', function (hooks) {
};

const urls = {
globalScope: null,
projectScope: null,
credentialStores: null,
newCredentialStore: null,
};
Expand All @@ -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 = () => {
Expand All @@ -72,26 +70,32 @@ 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);
});

test('Users can create a new credential store of type vault', async function (assert) {
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);
});
Expand All @@ -101,23 +105,26 @@ 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,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
message: errorMsg,
details: {
request_fields: [
{
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions ui/admin/tests/acceptance/credential-store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]';

0 comments on commit 5abd4f1

Please sign in to comment.