From fbc023da65e6e046085ad5f9b9ef2f15e2aec48d Mon Sep 17 00:00:00 2001 From: lisbet-alvarez <107949262+lisbet-alvarez@users.noreply.github.com> Date: Wed, 30 Nov 2022 10:06:04 -0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20acceptance=20tests?= =?UTF-8?q?=20in=20create-test.js=20for=20users=20(#1476)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 💡 acceptance tests in create-test.js for users ✅ Closes: ICU-6965 * refactor: 💡 use this.server.schema instead of this.server.db --- .../tests/acceptance/users/create-test.js | 132 +++++++++++------- ui/admin/tests/acceptance/users/list-test.js | 4 +- ui/admin/tests/acceptance/users/read-test.js | 6 +- 3 files changed, 88 insertions(+), 54 deletions(-) diff --git a/ui/admin/tests/acceptance/users/create-test.js b/ui/admin/tests/acceptance/users/create-test.js index eb2331f5b..4c1cf0b96 100644 --- a/ui/admin/tests/acceptance/users/create-test.js +++ b/ui/admin/tests/acceptance/users/create-test.js @@ -1,5 +1,5 @@ import { module, test } from 'qunit'; -import { visit, currentURL, click, fillIn, find } from '@ember/test-helpers'; +import { visit, currentURL, click, fillIn } from '@ember/test-helpers'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { Response } from 'miragejs'; @@ -14,65 +14,104 @@ module('Acceptance | users | create', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); - let orgScope; - let usersURL; - let newUserURL; + let getUsersCount; + + const instances = { + scopes: { + global: null, + org: null, + }, + user: null, + }; + + const urls = { + orgScope: null, + users: null, + user: null, + newUser: null, + }; hooks.beforeEach(function () { - this.server.create('scope', { id: 'global' }); - orgScope = this.server.create( - 'scope', - { - type: 'org', - scope: { id: 'global', type: 'global' }, - }, - 'withChildren' - ); - - usersURL = `/scopes/${orgScope.id}/users`; - newUserURL = `${usersURL}/new`; - + 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; + }; authenticateSession({}); }); test('can create new users', async function (assert) { assert.expect(1); - const usersCount = this.server.db.users.length; - await visit(newUserURL); + const usersCount = getUsersCount(); + await visit(urls.users); + + await click(`[href="${urls.newUser}"]`); await fillIn('[name="name"]', 'User name'); await click('[type="submit"]'); - assert.strictEqual(this.server.db.users.length, usersCount + 1); + + assert.strictEqual(getUsersCount(), usersCount + 1); }); - test('Users can navigate to new users route with proper authorization', async function (assert) { + test('users can navigate to new users route with proper authorization', async function (assert) { assert.expect(2); - await visit(usersURL); - assert.ok(orgScope.authorized_collection_actions.users.includes('create')); - assert.ok(find(`[href="${newUserURL}"]`)); - }); + await visit(urls.orgScope); - test('Users cannot navigate to new users route without proper authorization', async function (assert) { - assert.expect(2); - orgScope.authorized_collection_actions.users = []; - await visit(usersURL); - assert.notOk( - orgScope.authorized_collection_actions.users.includes('create') + await click(`[href="${urls.users}"]`); + + assert.true( + instances.scopes.org.authorized_collection_actions.users.includes( + 'create' + ) ); - assert.notOk(find(`[href="${newUserURL}"]`)); + assert.dom(`[href="${urls.newUser}"]`).exists(); + }); + + test('users cannot navigate to new users route without proper authorization', async function (assert) { + assert.expect(2); + 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' + ) + ); + + assert.dom('.rose-button-primary').doesNotExist(); }); test('can cancel creation of a new user', async function (assert) { assert.expect(2); - const usersCount = this.server.db.users.length; - await visit(newUserURL); + const usersCount = getUsersCount(); + await visit(urls.users); + + await click(`[href="${urls.newUser}"]`); await fillIn('[name="name"]', 'User name'); await click('.rose-form-actions [type="button"]'); - assert.strictEqual(currentURL(), usersURL); - assert.strictEqual(this.server.db.users.length, usersCount); + + assert.strictEqual(currentURL(), urls.users); + assert.strictEqual(getUsersCount(), usersCount); }); test('saving a new user with invalid fields displays error messages', async function (assert) { - assert.expect(2); + assert.expect(3); + const usersCount = getUsersCount(); + await visit(urls.users); this.server.post('/users', () => { return new Response( 400, @@ -92,18 +131,13 @@ module('Acceptance | users | create', function (hooks) { } ); }); - await visit(newUserURL); - await fillIn('[name="name"]', 'User name'); + + await click(`[href="${urls.newUser}"]`); + await fillIn('[name="description"]', 'test'); await click('[type="submit"]'); - assert.strictEqual( - find('.rose-notification-body').textContent.trim(), - 'The request was invalid.', - 'Displays primary error message.' - ); - assert.strictEqual( - find('.rose-form-error-message').textContent.trim(), - 'Name is required.', - 'Displays field-level errors.' - ); + + assert.strictEqual(getUsersCount(), usersCount); + assert.dom('.rose-notification-body').hasText('The request was invalid.'); + assert.dom('.rose-form-error-message').hasText('Name is required.'); }); }); diff --git a/ui/admin/tests/acceptance/users/list-test.js b/ui/admin/tests/acceptance/users/list-test.js index d357fb947..4cfde3603 100644 --- a/ui/admin/tests/acceptance/users/list-test.js +++ b/ui/admin/tests/acceptance/users/list-test.js @@ -39,8 +39,8 @@ module('Acceptance | users | list', function (hooks) { }); urls.globalScope = `/scopes/global/scopes`; urls.orgScope = `/scopes/${instances.scopes.org.id}`; - urls.users = `/scopes/${instances.scopes.org.id}/users`; - urls.user = `/scopes/${instances.scopes.org.id}/users/${instances.user.id}`; + urls.users = `${urls.orgScope}/users`; + urls.user = `${urls.users}/${instances.user.id}`; authenticateSession({}); }); diff --git a/ui/admin/tests/acceptance/users/read-test.js b/ui/admin/tests/acceptance/users/read-test.js index 9e8f54b0f..3c9185c25 100644 --- a/ui/admin/tests/acceptance/users/read-test.js +++ b/ui/admin/tests/acceptance/users/read-test.js @@ -37,9 +37,9 @@ module('Acceptance | users | read', function (hooks) { instances.user = this.server.create('user', { scope: instances.scopes.org, }); - urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; - urls.users = `/scopes/${instances.scopes.org.id}/users`; - urls.user = `/scopes/${instances.scopes.org.id}/users/${instances.user.id}`; + urls.orgScope = `/scopes/${instances.scopes.org.id}`; + urls.users = `${urls.orgScope}/users`; + urls.user = `${urls.users}/${instances.user.id}`; authenticateSession({}); });