2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2020-06-24 17:51:01 -04:00
|
|
|
import { module, test } from 'qunit';
|
2024-11-06 10:59:16 -08:00
|
|
|
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
|
2024-12-02 12:51:24 -06:00
|
|
|
import { setupApplicationTest } from 'admin/tests/helpers';
|
2020-06-24 17:51:01 -04:00
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
2024-04-02 13:27:02 -05:00
|
|
|
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
|
2020-06-24 17:51:01 -04:00
|
|
|
import { Response } from 'miragejs';
|
2024-11-06 10:59:16 -08:00
|
|
|
import { authenticateSession } from 'ember-simple-auth/test-support';
|
|
|
|
|
|
|
|
|
|
import * as commonSelectors from 'admin/tests/helpers/selectors';
|
2020-06-24 17:51:01 -04:00
|
|
|
|
2021-08-25 23:43:07 -07:00
|
|
|
module('Acceptance | groups | create', function (hooks) {
|
2020-06-24 17:51:01 -04:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
|
setupMirage(hooks);
|
2024-04-02 13:27:02 -05:00
|
|
|
setupIndexedDb(hooks);
|
2024-11-06 10:59:16 -08:00
|
|
|
let groupsCount;
|
2021-08-31 07:46:52 -07:00
|
|
|
|
2020-09-23 13:27:50 -04:00
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
org: null,
|
|
|
|
|
},
|
|
|
|
|
group: null,
|
|
|
|
|
};
|
|
|
|
|
const urls = {
|
|
|
|
|
groups: null,
|
|
|
|
|
group: null,
|
|
|
|
|
newGroup: null,
|
|
|
|
|
};
|
2020-07-28 12:10:22 -04:00
|
|
|
|
2024-12-03 10:27:27 -08:00
|
|
|
hooks.beforeEach(async function () {
|
|
|
|
|
await authenticateSession({});
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org = this.server.create(
|
2021-08-31 07:46:52 -07:00
|
|
|
'scope',
|
|
|
|
|
{
|
|
|
|
|
type: 'org',
|
|
|
|
|
scope: { id: 'global', type: 'global' },
|
|
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
'withChildren',
|
2021-08-31 07:46:52 -07:00
|
|
|
);
|
2020-09-23 13:27:50 -04:00
|
|
|
instances.group = this.server.create('group', {
|
2023-11-02 16:08:25 -07:00
|
|
|
scope: instances.scopes.org,
|
2020-09-23 13:27:50 -04:00
|
|
|
});
|
2023-11-02 16:08:25 -07:00
|
|
|
urls.groups = `/scopes/${instances.scopes.org.id}/groups`;
|
2020-09-23 13:27:50 -04:00
|
|
|
urls.group = `${urls.groups}/${instances.group.id}`;
|
|
|
|
|
urls.newGroup = `${urls.groups}/new`;
|
2024-11-06 10:59:16 -08:00
|
|
|
|
|
|
|
|
groupsCount = () => this.server.schema.groups.all().models.length;
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('can create new group', async function (assert) {
|
2024-11-06 10:59:16 -08:00
|
|
|
const count = groupsCount();
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2024-11-06 10:59:16 -08:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
await fillIn(
|
|
|
|
|
commonSelectors.FIELD_DESCRIPTION,
|
|
|
|
|
commonSelectors.FIELD_DESCRIPTION_VALUE,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await click(commonSelectors.SAVE_BTN);
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(groupsCount(), count + 1);
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
|
|
|
|
|
2021-08-31 07:46:52 -07:00
|
|
|
test('can navigate to new groups route with proper authorization', async function (assert) {
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.groups);
|
2024-11-06 10:59:16 -08:00
|
|
|
|
2021-08-31 07:46:52 -07:00
|
|
|
assert.ok(
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.groups.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-08-31 07:46:52 -07:00
|
|
|
);
|
2024-11-06 10:59:16 -08:00
|
|
|
assert.dom(commonSelectors.HREF(urls.newGroup)).isVisible();
|
2021-08-31 07:46:52 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('cannot navigate to new groups route without proper authorization', async function (assert) {
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.groups = [];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.groups);
|
2024-11-06 10:59:16 -08:00
|
|
|
|
|
|
|
|
assert.notOk(
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.groups.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2021-08-31 07:46:52 -07:00
|
|
|
);
|
2024-11-06 10:59:16 -08:00
|
|
|
assert.dom(commonSelectors.HREF(urls.newGroup)).doesNotExist();
|
2021-08-31 07:46:52 -07:00
|
|
|
});
|
2024-11-06 10:59:16 -08:00
|
|
|
|
2020-06-24 17:51:01 -04:00
|
|
|
test('can cancel new group creation', async function (assert) {
|
2024-11-06 10:59:16 -08:00
|
|
|
const count = groupsCount();
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2024-11-06 10:59:16 -08:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
|
|
|
|
|
await click(commonSelectors.CANCEL_BTN);
|
|
|
|
|
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.groups);
|
2024-11-06 10:59:16 -08:00
|
|
|
assert.strictEqual(groupsCount(), count);
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('saving a new group with invalid fields displays error messages', async function (assert) {
|
2020-09-09 16:24:48 -04:00
|
|
|
this.server.post('/groups', () => {
|
2020-06-24 17:51:01 -04:00
|
|
|
return new Response(
|
|
|
|
|
400,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
status: 400,
|
|
|
|
|
code: 'invalid_argument',
|
|
|
|
|
message: 'The request was invalid.',
|
|
|
|
|
details: {
|
2020-09-11 12:01:56 -04:00
|
|
|
request_fields: [
|
2020-06-24 17:51:01 -04:00
|
|
|
{
|
|
|
|
|
name: 'name',
|
2020-09-11 12:01:56 -04:00
|
|
|
description: 'Name is required.',
|
2020-06-24 17:51:01 -04:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-11-22 14:07:41 -05:00
|
|
|
},
|
2020-06-24 17:51:01 -04:00
|
|
|
);
|
|
|
|
|
});
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2024-11-06 10:59:16 -08:00
|
|
|
|
|
|
|
|
await fillIn(commonSelectors.FIELD_NAME, commonSelectors.FIELD_NAME_VALUE);
|
|
|
|
|
|
|
|
|
|
await click(commonSelectors.SAVE_BTN);
|
|
|
|
|
|
|
|
|
|
assert
|
|
|
|
|
.dom(commonSelectors.ALERT_TOAST_BODY)
|
|
|
|
|
.hasText('The request was invalid.');
|
|
|
|
|
assert.dom(commonSelectors.FIELD_NAME_ERROR).hasText('Name is required.');
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
2023-11-02 16:08:25 -07:00
|
|
|
|
|
|
|
|
test('users cannot directly navigate to new group route without proper authorization', async function (assert) {
|
|
|
|
|
instances.scopes.org.authorized_collection_actions.groups =
|
|
|
|
|
instances.scopes.org.authorized_collection_actions.groups.filter(
|
2023-11-22 14:07:41 -05:00
|
|
|
(item) => item !== 'create',
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await visit(urls.newGroup);
|
|
|
|
|
|
2024-11-06 10:59:16 -08:00
|
|
|
assert.notOk(
|
2023-11-02 16:08:25 -07:00
|
|
|
instances.scopes.org.authorized_collection_actions.groups.includes(
|
2023-11-22 14:07:41 -05:00
|
|
|
'create',
|
|
|
|
|
),
|
2023-11-02 16:08:25 -07:00
|
|
|
);
|
|
|
|
|
assert.strictEqual(currentURL(), urls.groups);
|
|
|
|
|
});
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|