From 9ca1beaa0ee848e862082ba6326621a3e5d9ed2d Mon Sep 17 00:00:00 2001 From: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com> Date: Tue, 7 May 2024 14:02:03 -0700 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20remove=20route-actio?= =?UTF-8?q?n=20usage=20for=20storage-policies=20(#2286)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 💡 remove route-action usage for storage-policies ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12607 --- .../form/scope/add-storage-policy/create.hbs | 4 +- .../scopes/scope/add-storage-policy/create.js | 37 ++++++++ .../scopes/scope/add-storage-policy/index.js | 40 +++++++++ .../scopes/scope/policies/index.js | 57 +++++++++++++ .../controllers/scopes/scope/policies/new.js | 5 ++ .../scopes/scope/policies/policy/index.js | 5 ++ .../scopes/scope/add-storage-policy/create.js | 31 +------ .../scopes/scope/add-storage-policy/index.js | 30 ------- ui/admin/app/routes/scopes/scope/policies.js | 44 ---------- .../scope/add-storage-policy/create.hbs | 4 +- .../scopes/scope/add-storage-policy/index.hbs | 4 +- .../templates/scopes/scope/policies/index.hbs | 2 +- .../templates/scopes/scope/policies/new.hbs | 4 +- .../scopes/scope/policies/policy/index.hbs | 4 +- .../scope/add-storage-policy/create-test.js | 79 +++++++++++++++++ .../scope/add-storage-policy/index-test.js | 85 +++++++++++++++++++ .../scopes/scope/policies/index-test.js | 83 ++++++++++++++++++ .../scopes/scope/policies/new-test.js | 11 +++ .../scope/policies/policy/index-test.js | 16 ++++ 19 files changed, 430 insertions(+), 115 deletions(-) create mode 100644 ui/admin/app/controllers/scopes/scope/add-storage-policy/create.js create mode 100644 ui/admin/app/controllers/scopes/scope/add-storage-policy/index.js create mode 100644 ui/admin/app/controllers/scopes/scope/policies/index.js create mode 100644 ui/admin/app/controllers/scopes/scope/policies/new.js create mode 100644 ui/admin/app/controllers/scopes/scope/policies/policy/index.js create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/create-test.js create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/index-test.js create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/policies/index-test.js create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/policies/new-test.js create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/policies/policy/index-test.js diff --git a/ui/admin/app/components/form/scope/add-storage-policy/create.hbs b/ui/admin/app/components/form/scope/add-storage-policy/create.hbs index 71e5c3ae3..1f1c241a1 100644 --- a/ui/admin/app/components/form/scope/add-storage-policy/create.hbs +++ b/ui/admin/app/components/form/scope/add-storage-policy/create.hbs @@ -21,8 +21,8 @@ diff --git a/ui/admin/app/controllers/scopes/scope/add-storage-policy/create.js b/ui/admin/app/controllers/scopes/scope/add-storage-policy/create.js new file mode 100644 index 000000000..c43ffdd6b --- /dev/null +++ b/ui/admin/app/controllers/scopes/scope/add-storage-policy/create.js @@ -0,0 +1,37 @@ +import Controller from '@ember/controller'; +import { inject as service } from '@ember/service'; +import { action } from '@ember/object'; +import { notifySuccess, notifyError } from 'core/decorators/notify'; +import { loading } from 'ember-loading'; + +export default class ScopesScopeAddStoragePolicyCreateController extends Controller { + // =services + + @service router; + + // =actions + + /** + * Handle save + * @param {PolicyModel} policy + */ + @action + @loading + @notifyError(({ message }) => message) + @notifySuccess('notifications.save-success') + async save(policy) { + await policy.save(); + await this.router.transitionTo('scopes.scope.add-storage-policy'); + await this.router.refresh(); + } + + /** + * Rollback changes on policies. + * @param {PolicyModel} policy + */ + @action + cancel(policy) { + policy.rollbackAttributes(); + this.router.transitionTo('scopes.scope.add-storage-policy'); + } +} diff --git a/ui/admin/app/controllers/scopes/scope/add-storage-policy/index.js b/ui/admin/app/controllers/scopes/scope/add-storage-policy/index.js new file mode 100644 index 000000000..40f3078b4 --- /dev/null +++ b/ui/admin/app/controllers/scopes/scope/add-storage-policy/index.js @@ -0,0 +1,40 @@ +import Controller from '@ember/controller'; +import { inject as service } from '@ember/service'; +import { action } from '@ember/object'; +import { loading } from 'ember-loading'; +import { notifySuccess, notifyError } from 'core/decorators/notify'; + +export default class ScopesScopeAddStoragePolicyIndexController extends Controller { + // =services + + @service router; + + // =actions + + /** + * Deletes the scope and redirects to index. + * @param {Model} scope + */ + @action + @loading + @notifyError(({ message }) => message, { catch: true }) + @notifySuccess('notifications.save-success') + async attachStoragePolicy(scope) { + const { storage_policy_id } = scope; + if (storage_policy_id) { + await scope.attachStoragePolicy(storage_policy_id); + } + await scope.save(); + await this.router.transitionTo('scopes.scope.edit', scope); + } + + /** + * Rollback changes on add storage policy. + * @param {ScopeModel} scope + */ + @action + cancel(scope) { + scope.rollbackAttributes(); + this.router.transitionTo('scopes.scope.edit', scope); + } +} diff --git a/ui/admin/app/controllers/scopes/scope/policies/index.js b/ui/admin/app/controllers/scopes/scope/policies/index.js new file mode 100644 index 000000000..7ad3f0ed7 --- /dev/null +++ b/ui/admin/app/controllers/scopes/scope/policies/index.js @@ -0,0 +1,57 @@ +import Controller from '@ember/controller'; +import { inject as service } from '@ember/service'; +import { action } from '@ember/object'; +import { loading } from 'ember-loading'; +import { confirm } from 'core/decorators/confirm'; +import { notifySuccess, notifyError } from 'core/decorators/notify'; + +export default class ScopesScopePoliciesIndexController extends Controller { + // =services + + @service can; + @service router; + + // =actions + + /** + * Rollback changes on a policy. + * @param {PolicyModel} policy + */ + @action + cancel(policy) { + const { isNew } = policy; + policy.rollbackAttributes(); + if (isNew) this.router.transitionTo('scopes.scope.policies'); + } + + /** + * Save a policy in current scope. + * @param {PolicyModel} policy + */ + @action + @loading + @notifyError(({ message }) => message) + @notifySuccess('notifications.save-success') + async save(policy) { + await policy.save(); + if (this.can.can('read model', policy)) { + await this.router.transitionTo('scopes.scope.policies.policy', policy); + } else { + await this.router.transitionTo('scopes.scope.policies'); + } + await this.router.refresh(); + } + + /** + * Deletes the policy. + * @param {PolicyModel} policy + */ + @action + @loading + @confirm('questions.delete-confirm') + @notifyError(({ message }) => message, { catch: true }) + @notifySuccess('notifications.delete-success') + async delete(policy) { + await policy.destroyRecord(); + } +} diff --git a/ui/admin/app/controllers/scopes/scope/policies/new.js b/ui/admin/app/controllers/scopes/scope/policies/new.js new file mode 100644 index 000000000..ab58f845f --- /dev/null +++ b/ui/admin/app/controllers/scopes/scope/policies/new.js @@ -0,0 +1,5 @@ +import Controller, { inject as controller } from '@ember/controller'; + +export default class ScopesScopePoliciesNewController extends Controller { + @controller('scopes/scope/policies/index') policies; +} diff --git a/ui/admin/app/controllers/scopes/scope/policies/policy/index.js b/ui/admin/app/controllers/scopes/scope/policies/policy/index.js new file mode 100644 index 000000000..7a2121f59 --- /dev/null +++ b/ui/admin/app/controllers/scopes/scope/policies/policy/index.js @@ -0,0 +1,5 @@ +import Controller, { inject as controller } from '@ember/controller'; + +export default class ScopesScopePoliciesPolicyIndexController extends Controller { + @controller('scopes/scope/policies/index') policies; +} diff --git a/ui/admin/app/routes/scopes/scope/add-storage-policy/create.js b/ui/admin/app/routes/scopes/scope/add-storage-policy/create.js index c460cdc8c..775e3645e 100644 --- a/ui/admin/app/routes/scopes/scope/add-storage-policy/create.js +++ b/ui/admin/app/routes/scopes/scope/add-storage-policy/create.js @@ -6,15 +6,12 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; import { TYPE_POLICY } from 'api/models/policy'; -import { action } from '@ember/object'; -import { notifySuccess, notifyError } from 'core/decorators/notify'; -import { loading } from 'ember-loading'; export default class ScopesScopeAddStoragePolicyCreateRoute extends Route { // =services @service store; - @service router; + // =methods /** @@ -30,30 +27,4 @@ export default class ScopesScopeAddStoragePolicyCreateRoute extends Route { record.scopeModel = scopeModel; return record; } - - // =actions - - /** - * Handle save - * @param {PolicyModel} policy - */ - @action - @loading - @notifyError(({ message }) => message) - @notifySuccess('notifications.save-success') - async save(policy) { - await policy.save(); - await this.router.transitionTo('scopes.scope.add-storage-policy'); - this.refresh(); - } - - /** - * Rollback changes on policies. - * @param {PolicyModel} policy - */ - @action - cancel(policy) { - policy.rollbackAttributes(); - this.router.transitionTo('scopes.scope.add-storage-policy'); - } } diff --git a/ui/admin/app/routes/scopes/scope/add-storage-policy/index.js b/ui/admin/app/routes/scopes/scope/add-storage-policy/index.js index ae07dd5d8..8b023fdb1 100644 --- a/ui/admin/app/routes/scopes/scope/add-storage-policy/index.js +++ b/ui/admin/app/routes/scopes/scope/add-storage-policy/index.js @@ -5,9 +5,6 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; -import { action } from '@ember/object'; -import { loading } from 'ember-loading'; -import { notifySuccess, notifyError } from 'core/decorators/notify'; export default class ScopesScopeAddStoragePolicyIndexRoute extends Route { // =services @@ -47,31 +44,4 @@ export default class ScopesScopeAddStoragePolicyIndexRoute extends Route { super.setupController(...arguments); controller.set('policyList', this.policyList); } - - /** - * Deletes the scope and redirects to index. - * @param {Model} scope - */ - @action - @loading - @notifyError(({ message }) => message, { catch: true }) - @notifySuccess('notifications.save-success') - async attachStoragePolicy(scope) { - const { storage_policy_id } = scope; - if (storage_policy_id) { - await scope.attachStoragePolicy(storage_policy_id); - } - await scope.save(); - await this.router.transitionTo('scopes.scope.edit', scope); - } - - /** - * Rollback changes on add storage policy. - * @param {PolicyModel} policy - */ - @action - cancel(scope) { - scope.rollbackAttributes(); - this.router.transitionTo('scopes.scope.edit', scope); - } } diff --git a/ui/admin/app/routes/scopes/scope/policies.js b/ui/admin/app/routes/scopes/scope/policies.js index acdffcfe6..e719a5113 100644 --- a/ui/admin/app/routes/scopes/scope/policies.js +++ b/ui/admin/app/routes/scopes/scope/policies.js @@ -5,10 +5,6 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; -import { action } from '@ember/object'; -import { loading } from 'ember-loading'; -import { confirm } from 'core/decorators/confirm'; -import { notifySuccess, notifyError } from 'core/decorators/notify'; export default class ScopesScopePoliciesRoute extends Route { // =services @@ -43,44 +39,4 @@ export default class ScopesScopePoliciesRoute extends Route { return this.store.query('policy', { scope_id }); } } - - //actions - - /** - * Rollback changes on a policy. - * @param {PolicyModel} policy - */ - @action - cancel(policy) { - const { isNew } = policy; - policy.rollbackAttributes(); - if (isNew) this.router.transitionTo('scopes.scope.policies'); - } - - /** - * Save a policy in current scope. - * @param {PolicyModel} policy - */ - @action - @loading - @notifyError(({ message }) => message) - @notifySuccess('notifications.save-success') - async save(policy) { - await policy.save(); - await this.router.transitionTo('scopes.scope.policies.policy', policy); - - this.refresh(); - } - /** - * Deletes the policy. - * @param {PolicyModel} policy - */ - @action - @loading - @confirm('questions.delete-confirm') - @notifyError(({ message }) => message, { catch: true }) - @notifySuccess('notifications.delete-success') - async delete(policy) { - await policy.destroyRecord(); - } } diff --git a/ui/admin/app/templates/scopes/scope/add-storage-policy/create.hbs b/ui/admin/app/templates/scopes/scope/add-storage-policy/create.hbs index 7a76b2f3e..b5ccc5876 100644 --- a/ui/admin/app/templates/scopes/scope/add-storage-policy/create.hbs +++ b/ui/admin/app/templates/scopes/scope/add-storage-policy/create.hbs @@ -25,8 +25,8 @@ diff --git a/ui/admin/app/templates/scopes/scope/add-storage-policy/index.hbs b/ui/admin/app/templates/scopes/scope/add-storage-policy/index.hbs index 1313ed044..b211019a8 100644 --- a/ui/admin/app/templates/scopes/scope/add-storage-policy/index.hbs +++ b/ui/admin/app/templates/scopes/scope/add-storage-policy/index.hbs @@ -24,8 +24,8 @@ diff --git a/ui/admin/app/templates/scopes/scope/policies/index.hbs b/ui/admin/app/templates/scopes/scope/policies/index.hbs index 92790580c..9e52322d2 100644 --- a/ui/admin/app/templates/scopes/scope/policies/index.hbs +++ b/ui/admin/app/templates/scopes/scope/policies/index.hbs @@ -107,7 +107,7 @@ {{/if}} diff --git a/ui/admin/app/templates/scopes/scope/policies/new.hbs b/ui/admin/app/templates/scopes/scope/policies/new.hbs index 0aa8a03eb..4b6a8ffa4 100644 --- a/ui/admin/app/templates/scopes/scope/policies/new.hbs +++ b/ui/admin/app/templates/scopes/scope/policies/new.hbs @@ -25,8 +25,8 @@ diff --git a/ui/admin/app/templates/scopes/scope/policies/policy/index.hbs b/ui/admin/app/templates/scopes/scope/policies/policy/index.hbs index 0670d07d6..85680dbfd 100644 --- a/ui/admin/app/templates/scopes/scope/policies/policy/index.hbs +++ b/ui/admin/app/templates/scopes/scope/policies/policy/index.hbs @@ -28,8 +28,8 @@ \ No newline at end of file diff --git a/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/create-test.js b/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/create-test.js new file mode 100644 index 000000000..ed149a42e --- /dev/null +++ b/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/create-test.js @@ -0,0 +1,79 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { visit } from '@ember/test-helpers'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { authenticateSession } from 'ember-simple-auth/test-support'; + +module( + 'Unit | Controller | scopes/scope/add-storage-policy/create', + function (hooks) { + setupTest(hooks); + setupMirage(hooks); + + let store; + let controller; + + const instances = { + scopes: { + global: null, + }, + policy: null, + }; + + const urls = { + globalScope: null, + addStoragePolicy: null, + }; + + hooks.beforeEach(async function () { + authenticateSession({}); + store = this.owner.lookup('service:store'); + controller = this.owner.lookup( + 'controller:scopes/scope/add-storage-policy/create', + ); + + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.policy = this.server.create('policy', { + scopeId: 'global', + }); + + urls.globalScope = `/scopes/global`; + urls.addStoragePolicy = `${urls.globalScope}/add-storage-policy`; + }); + + test('it exists', function (assert) { + assert.ok(controller); + }); + + test('cancel action rolls-back changes on the specified model', async function (assert) { + await visit(urls.addStoragePolicy); + const policyBefore = await store.findRecord( + 'policy', + instances.policy.id, + ); + policyBefore.name = 'test'; + + assert.strictEqual(policyBefore.name, 'test'); + + await controller.cancel(policyBefore); + const policyAfter = await store.findRecord('policy', instances.policy.id); + + assert.notEqual(policyAfter.name, 'test'); + assert.deepEqual(policyBefore, policyAfter); + }); + + test('save action saves changes on the specified model', async function (assert) { + await visit(urls.addStoragePolicy); + const policyBefore = await store.findRecord( + 'policy', + instances.policy.id, + ); + policyBefore.name = 'test'; + + await controller.save(policyBefore); + const policyAfter = await store.findRecord('policy', instances.policy.id); + + assert.strictEqual(policyAfter.name, 'test'); + }); + }, +); diff --git a/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/index-test.js b/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/index-test.js new file mode 100644 index 000000000..63d220c67 --- /dev/null +++ b/ui/admin/tests/unit/controllers/scopes/scope/add-storage-policy/index-test.js @@ -0,0 +1,85 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { visit } from '@ember/test-helpers'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { authenticateSession } from 'ember-simple-auth/test-support'; + +module( + 'Unit | Controller | scopes/scope/add-storage-policy/index', + function (hooks) { + setupTest(hooks); + setupMirage(hooks); + + let store; + let controller; + + const instances = { + scopes: { + global: null, + }, + policy: null, + }; + + const urls = { + globalScope: null, + addStoragePolicy: null, + }; + + hooks.beforeEach(async function () { + authenticateSession({}); + store = this.owner.lookup('service:store'); + controller = this.owner.lookup( + 'controller:scopes/scope/add-storage-policy/index', + ); + + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.policy = this.server.create('policy', { + scopeId: 'global', + }); + + urls.globalScope = `/scopes/global`; + urls.addStoragePolicy = `${urls.globalScope}/add-storage-policy`; + }); + + test('it exists', function (assert) { + assert.ok(controller); + }); + + test('cancel action rolls-back changes on the specified model', async function (assert) { + await visit(urls.addStoragePolicy); + const scopeBefore = await store.findRecord( + 'scope', + instances.scopes.global.id, + ); + scopeBefore.storage_policy_id = instances.policy.id; + + assert.strictEqual(scopeBefore.storage_policy_id, instances.policy.id); + + await controller.cancel(scopeBefore); + const scopeAfter = await store.findRecord( + 'scope', + instances.scopes.global.id, + ); + + assert.notOk(scopeAfter.storage_policy_id); + assert.deepEqual(scopeBefore, scopeAfter); + }); + + test('attachStoragePolicy action saves changes on the specified model', async function (assert) { + await visit(urls.addStoragePolicy); + const scopeBefore = await store.findRecord( + 'scope', + instances.scopes.global.id, + ); + scopeBefore.storage_policy_id = instances.policy.id; + + await controller.attachStoragePolicy(scopeBefore); + const scopeAfter = await store.findRecord( + 'scope', + instances.scopes.global.id, + ); + + assert.strictEqual(scopeAfter.storage_policy_id, instances.policy.id); + }); + }, +); diff --git a/ui/admin/tests/unit/controllers/scopes/scope/policies/index-test.js b/ui/admin/tests/unit/controllers/scopes/scope/policies/index-test.js new file mode 100644 index 000000000..5ede381aa --- /dev/null +++ b/ui/admin/tests/unit/controllers/scopes/scope/policies/index-test.js @@ -0,0 +1,83 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { visit } from '@ember/test-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'; + +module('Unit | Controller | scopes/scope/policies/index', function (hooks) { + setupTest(hooks); + setupMirage(hooks); + setupIndexedDb(hooks); + + let store; + let controller; + let getPolicyCount; + + const instances = { + scopes: { + global: null, + }, + policy: null, + }; + + const urls = { + globalScope: null, + policies: null, + }; + + hooks.beforeEach(async function () { + authenticateSession({}); + store = this.owner.lookup('service:store'); + controller = this.owner.lookup('controller:scopes/scope/policies/index'); + + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.policy = this.server.create('policy', { + scopeId: 'global', + }); + + urls.globalScope = `/scopes/global`; + urls.policies = `${urls.globalScope}/policies`; + + getPolicyCount = () => this.server.schema.policies.all().models.length; + }); + + test('it exists', function (assert) { + assert.ok(controller); + }); + + test('cancel action rolls-back changes on the specified model', async function (assert) { + await visit(urls.policies); + const policyBefore = await store.findRecord('policy', instances.policy.id); + policyBefore.name = 'test'; + + assert.strictEqual(policyBefore.name, 'test'); + + await controller.cancel(policyBefore); + const policyAfter = await store.findRecord('policy', instances.policy.id); + + assert.notEqual(policyAfter.name, 'test'); + assert.deepEqual(policyBefore, policyAfter); + }); + + test('save action saves changes on the specified model', async function (assert) { + await visit(urls.policies); + const policyBefore = await store.findRecord('policy', instances.policy.id); + policyBefore.name = 'test'; + + await controller.save(policyBefore); + const policyAfter = await store.findRecord('policy', instances.policy.id); + + assert.strictEqual(policyAfter.name, 'test'); + }); + + test('delete action destroys specified model', async function (assert) { + await visit(urls.globalScope); + const policy = await store.findRecord('policy', instances.policy.id); + const policyCount = getPolicyCount(); + + await controller.delete(policy); + + assert.strictEqual(getPolicyCount(), policyCount - 1); + }); +}); diff --git a/ui/admin/tests/unit/controllers/scopes/scope/policies/new-test.js b/ui/admin/tests/unit/controllers/scopes/scope/policies/new-test.js new file mode 100644 index 000000000..10fc6dbbb --- /dev/null +++ b/ui/admin/tests/unit/controllers/scopes/scope/policies/new-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Controller | scopes/scope/policies/new', function (hooks) { + setupTest(hooks); + + test('it exists', function (assert) { + let controller = this.owner.lookup('controller:scopes/scope/policies/new'); + assert.ok(controller); + }); +}); diff --git a/ui/admin/tests/unit/controllers/scopes/scope/policies/policy/index-test.js b/ui/admin/tests/unit/controllers/scopes/scope/policies/policy/index-test.js new file mode 100644 index 000000000..656526c6f --- /dev/null +++ b/ui/admin/tests/unit/controllers/scopes/scope/policies/policy/index-test.js @@ -0,0 +1,16 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module( + 'Unit | Controller | scopes/scope/policies/policy/index', + function (hooks) { + setupTest(hooks); + + test('it exists', function (assert) { + let controller = this.owner.lookup( + 'controller:scopes/scope/policies/policy/index', + ); + assert.ok(controller); + }); + }, +);