Files
threshold-ui/ui/admin/tests/acceptance/users/create-test.js
T

158 lines
4.5 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2020-06-23 15:42:37 -04:00
import { module, test } from 'qunit';
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'admin/tests/helpers';
2020-06-23 15:42:37 -04:00
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
2020-06-23 15:42:37 -04:00
import { Response } from 'miragejs';
2024-11-22 11:27:53 -08:00
import { authenticateSession } from 'ember-simple-auth/test-support';
2020-06-23 15:42:37 -04:00
module('Acceptance | users | create', function (hooks) {
2020-06-23 15:42:37 -04:00
setupApplicationTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
2020-06-23 15:42:37 -04:00
let getUsersCount;
const instances = {
scopes: {
global: null,
org: null,
},
user: null,
};
const urls = {
orgScope: null,
users: null,
user: null,
newUser: null,
};
hooks.beforeEach(async function () {
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.user = this.server.create('user', {
scope: instances.scopes.org,
});
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
urls.users = `${urls.orgScope}/users`;
urls.user = `${urls.users}/${instances.user.id}`;
urls.newUser = `${urls.users}/new`;
getUsersCount = () => {
return this.server.schema.users.all().models.length;
};
await authenticateSession({});
2020-06-23 15:42:37 -04:00
});
test('can create new users', async function (assert) {
const usersCount = getUsersCount();
await visit(urls.users);
await click(`[href="${urls.newUser}"]`);
2020-06-23 15:42:37 -04:00
await fillIn('[name="name"]', 'User name');
await click('[type="submit"]');
assert.strictEqual(getUsersCount(), usersCount + 1);
2020-06-23 15:42:37 -04:00
});
test('users can navigate to new users route with proper authorization', async function (assert) {
await visit(urls.orgScope);
await click(`[href="${urls.users}"]`);
assert.true(
instances.scopes.org.authorized_collection_actions.users.includes(
'create',
),
);
assert.dom(`[href="${urls.newUser}"]`).exists();
});
test('users cannot navigate to new users route without proper authorization', async function (assert) {
instances.scopes.org.authorized_collection_actions.users =
instances.scopes.org.authorized_collection_actions.users.filter(
(item) => item !== 'create',
);
await visit(urls.orgScope);
await click(`[href="${urls.users}"]`);
assert.false(
instances.scopes.org.authorized_collection_actions.users.includes(
'create',
),
2021-08-31 15:03:40 -04:00
);
assert.dom('.rose-button-primary').doesNotExist();
});
2020-06-23 15:42:37 -04:00
test('can cancel creation of a new user', async function (assert) {
const usersCount = getUsersCount();
await visit(urls.users);
await click(`[href="${urls.newUser}"]`);
2020-06-23 15:42:37 -04:00
await fillIn('[name="name"]', 'User name');
await click('.rose-form-actions [type="button"]');
assert.strictEqual(currentURL(), urls.users);
assert.strictEqual(getUsersCount(), usersCount);
2020-06-23 15:42:37 -04:00
});
test('saving a new user with invalid fields displays error messages', async function (assert) {
const usersCount = getUsersCount();
await visit(urls.users);
2020-09-09 16:53:41 -04:00
this.server.post('/users', () => {
2020-06-23 15:42:37 -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-23 15:42:37 -04:00
{
name: 'name',
2020-09-11 12:01:56 -04:00
description: 'Name is required.',
2020-06-23 15:42:37 -04:00
},
],
},
},
2020-06-23 15:42:37 -04:00
);
});
await click(`[href="${urls.newUser}"]`);
await fillIn('[name="description"]', 'test');
2020-06-23 15:42:37 -04:00
await click('[type="submit"]');
assert.strictEqual(getUsersCount(), usersCount);
assert
.dom('[data-test-toast-notification] .hds-alert__description')
.hasText('The request was invalid.');
assert.dom('[data-test-error-message-name]').hasText('Name is required.');
2020-06-23 15:42:37 -04:00
});
test('users cannot directly navigate to new user route without proper authorization', async function (assert) {
instances.scopes.org.authorized_collection_actions.users =
instances.scopes.org.authorized_collection_actions.users.filter(
(item) => item !== 'create',
);
await visit(urls.newUser);
assert.false(
instances.scopes.org.authorized_collection_actions.users.includes(
'create',
),
);
assert.strictEqual(currentURL(), urls.users);
});
2020-06-23 15:42:37 -04:00
});