2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-24 17:51:01 -04:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
|
import { visit, currentURL, click, fillIn, find } from '@ember/test-helpers';
|
|
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
|
|
|
|
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
|
|
|
|
import { Response } from 'miragejs';
|
2020-07-29 15:43:09 -04:00
|
|
|
import {
|
|
|
|
|
authenticateSession,
|
|
|
|
|
// These are left here intentionally for future reference.
|
|
|
|
|
//currentSession,
|
|
|
|
|
//invalidateSession,
|
|
|
|
|
} from 'ember-simple-auth/test-support';
|
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);
|
2021-08-31 07:46:52 -07:00
|
|
|
|
2020-09-23 13:27:50 -04:00
|
|
|
const instances = {
|
|
|
|
|
scopes: {
|
|
|
|
|
global: null,
|
|
|
|
|
org: null,
|
|
|
|
|
},
|
|
|
|
|
group: null,
|
2021-08-31 07:46:52 -07:00
|
|
|
orgScope: null,
|
2020-09-23 13:27:50 -04:00
|
|
|
};
|
|
|
|
|
const urls = {
|
|
|
|
|
groups: null,
|
|
|
|
|
group: null,
|
|
|
|
|
newGroup: null,
|
|
|
|
|
};
|
2020-07-28 12:10:22 -04:00
|
|
|
|
2020-06-24 17:51:01 -04:00
|
|
|
hooks.beforeEach(function () {
|
2020-07-29 15:43:09 -04:00
|
|
|
authenticateSession({});
|
2021-08-31 07:46:52 -07:00
|
|
|
instances.orgScope = this.server.create(
|
|
|
|
|
'scope',
|
|
|
|
|
{
|
|
|
|
|
type: 'org',
|
|
|
|
|
scope: { id: 'global', type: 'global' },
|
|
|
|
|
},
|
|
|
|
|
'withChildren'
|
|
|
|
|
);
|
2020-09-23 13:27:50 -04:00
|
|
|
instances.group = this.server.create('group', {
|
2021-08-31 07:46:52 -07:00
|
|
|
scope: instances.orgScope,
|
2020-09-23 13:27:50 -04:00
|
|
|
});
|
2021-08-31 07:46:52 -07:00
|
|
|
urls.groups = `/scopes/${instances.orgScope.id}/groups`;
|
2020-09-23 13:27:50 -04:00
|
|
|
urls.group = `${urls.groups}/${instances.group.id}`;
|
|
|
|
|
urls.newGroup = `${urls.groups}/new`;
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('can create new group', async function (assert) {
|
2020-07-29 15:43:09 -04:00
|
|
|
assert.expect(1);
|
|
|
|
|
const groupsCount = this.server.db.groups.length;
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2020-06-24 17:51:01 -04:00
|
|
|
await fillIn('[name="name"]', 'group name');
|
|
|
|
|
await click('[type="submit"]');
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(this.server.db.groups.length, groupsCount + 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) {
|
|
|
|
|
assert.expect(2);
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.groups);
|
2021-08-31 07:46:52 -07:00
|
|
|
assert.ok(
|
|
|
|
|
instances.orgScope.authorized_collection_actions.groups.includes('create')
|
|
|
|
|
);
|
2021-11-05 14:23:20 -04:00
|
|
|
assert.ok(find(`[href="${urls.newGroup}"]`));
|
2021-08-31 07:46:52 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('cannot navigate to new groups route without proper authorization', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
instances.orgScope.authorized_collection_actions.groups = [];
|
2021-11-05 14:23:20 -04:00
|
|
|
await visit(urls.groups);
|
2021-08-31 07:46:52 -07:00
|
|
|
assert.notOk(
|
|
|
|
|
instances.orgScope.authorized_collection_actions.groups.includes('create')
|
|
|
|
|
);
|
2021-11-05 14:23:20 -04:00
|
|
|
assert.notOk(find(`[href="${urls.newGroup}"]`));
|
2021-08-31 07:46:52 -07:00
|
|
|
});
|
2020-06-24 17:51:01 -04:00
|
|
|
test('can cancel new group creation', async function (assert) {
|
2020-07-29 15:43:09 -04:00
|
|
|
assert.expect(2);
|
|
|
|
|
const groupsCount = this.server.db.groups.length;
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2020-06-24 17:51:01 -04:00
|
|
|
await fillIn('[name="name"]', 'group name');
|
|
|
|
|
await click('.rose-form-actions [type="button"]');
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(currentURL(), urls.groups);
|
|
|
|
|
assert.strictEqual(this.server.db.groups.length, groupsCount);
|
2020-06-24 17:51:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('saving a new group with invalid fields displays error messages', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
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
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
2020-09-23 13:27:50 -04:00
|
|
|
await visit(urls.newGroup);
|
2020-06-24 17:51:01 -04:00
|
|
|
await fillIn('[name="name"]', 'group name');
|
|
|
|
|
await click('[type="submit"]');
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(
|
2021-10-23 11:48:10 -04:00
|
|
|
find('.rose-notification-body').textContent.trim(),
|
2020-06-24 17:51:01 -04:00
|
|
|
'The request was invalid.',
|
|
|
|
|
'Displays primary error message.'
|
|
|
|
|
);
|
2022-07-07 11:18:51 -07:00
|
|
|
assert.strictEqual(
|
2020-06-24 17:51:01 -04:00
|
|
|
find('.rose-form-error-message').textContent.trim(),
|
|
|
|
|
'Name is required.',
|
|
|
|
|
'Displays field-level errors.'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|