From 451c1e861b5e9cdec4a2a783ca9351b9f6a2f0f5 Mon Sep 17 00:00:00 2001 From: dharini Date: Thu, 2 Sep 2021 15:28:40 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20hosts=20capabilities=20c?= =?UTF-8?q?reate=20lit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/mirage/factories/host-catalog.js | 5 + .../scope/host-catalogs/host-catalog/hosts.js | 13 ++- .../host-catalogs/host-catalog/hosts/index.js | 10 +- .../host-catalogs/host-catalog/-actions.hbs | 15 +-- .../host-catalog/-navigation.hbs | 10 +- .../host-catalog/hosts/index.hbs | 35 ++++++- .../{hosts-test.js => hosts/create-test.js} | 36 +++++++- .../host-catalogs/hosts/list-test.js | 92 +++++++++++++++++++ 8 files changed, 197 insertions(+), 19 deletions(-) rename ui/admin/tests/acceptance/host-catalogs/{hosts-test.js => hosts/create-test.js} (74%) create mode 100644 ui/admin/tests/acceptance/host-catalogs/hosts/list-test.js diff --git a/addons/api/mirage/factories/host-catalog.js b/addons/api/mirage/factories/host-catalog.js index cd033f7f3..b01ffc065 100644 --- a/addons/api/mirage/factories/host-catalog.js +++ b/addons/api/mirage/factories/host-catalog.js @@ -10,6 +10,11 @@ export default factory.extend({ 'update', 'delete', ], + authorized_collection_actions: () => { + return { + hosts: ['create', 'list'], + }; + }, withChildren: trait({ afterCreate(hostCatalog, server) { const { scope } = hostCatalog; diff --git a/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts.js b/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts.js index a41073688..5969a8b67 100644 --- a/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts.js +++ b/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts.js @@ -10,7 +10,7 @@ export default class ScopesScopeHostCatalogsHostCatalogHostsRoute extends Route @service intl; @service notify; - + @service can; // =methods /** @@ -18,10 +18,17 @@ export default class ScopesScopeHostCatalogsHostCatalogHostsRoute extends Route * @return {Promise{[HostModel]}} */ model() { - const { id: host_catalog_id } = this.modelFor( + const hostCatalog = this.modelFor( 'scopes.scope.host-catalogs.host-catalog' ); - return this.store.query('host', { host_catalog_id }); + const { id: host_catalog_id } = hostCatalog; + if ( + this.can.can('list collection', hostCatalog, { + collection: 'hosts', + }) + ) { + return this.store.query('host', { host_catalog_id }); + } } // =actions diff --git a/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts/index.js b/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts/index.js index c219c570f..617551d99 100644 --- a/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts/index.js +++ b/ui/admin/app/routes/scopes/scope/host-catalogs/host-catalog/hosts/index.js @@ -1,3 +1,11 @@ import Route from '@ember/routing/route'; -export default class ScopesScopeHostCatalogsHostCatalogHostsIndexRoute extends Route {} +export default class ScopesScopeHostCatalogsHostCatalogHostsIndexRoute extends Route { + setupController(controller) { + const hostCatalog = this.modelFor( + 'scopes.scope.host-catalogs.host-catalog' + ); + super.setupController(...arguments); + controller.setProperties({ hostCatalog }); + } +} diff --git a/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/-actions.hbs b/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/-actions.hbs index bf5fe452e..4f70d7987 100644 --- a/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/-actions.hbs +++ b/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/-actions.hbs @@ -3,12 +3,15 @@ @dropdownRight={{true}} as |dropdown| > - - {{t 'resources.host.actions.create'}} - + {{#if (can 'create collection' @model collection='hosts')}} + + + {{t 'resources.host.actions.create'}} + + {{/if}} {{t 'titles.details'}} - - {{t 'resources.host.title_plural'}} - + + {{#if (can 'navigate collection' @model collection='hosts')}} + + + {{t 'resources.host.title_plural'}} + + {{/if}} {{t 'resources.host-set.title_plural'}} diff --git a/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/hosts/index.hbs b/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/hosts/index.hbs index 6687cbf76..117689f14 100644 --- a/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/hosts/index.hbs +++ b/ui/admin/app/templates/scopes/scope/host-catalogs/host-catalog/hosts/index.hbs @@ -55,12 +55,37 @@ as |message| > - {{t 'resources.host.description'}} + + {{#if (can 'list collection' this.hostCatalog collection='hosts')}} + {{! can list (at least): show default welcome message}} + {{t 'resources.host.description'}} + + {{else if (can + 'create collection' this.hostCatalog collection='hosts' + )}} + {{! can create (only): show create-but-not-list welcome message}} + {{t + 'descriptions.create-but-not-list' + resource=(t 'resources.host.title_plural') + }} + {{else}} + {{! can neither list nor create }} + {{t + 'descriptions.neither-list-nor-create' + resource=(t 'resources.host.title_plural') + }} + {{/if}} - - - {{t 'titles.new'}} - + + {{#if (can 'create collection' this.hostCatalog collection='hosts')}} + + + + {{t 'titles.new'}} + + {{/if}} diff --git a/ui/admin/tests/acceptance/host-catalogs/hosts-test.js b/ui/admin/tests/acceptance/host-catalogs/hosts/create-test.js similarity index 74% rename from ui/admin/tests/acceptance/host-catalogs/hosts-test.js rename to ui/admin/tests/acceptance/host-catalogs/hosts/create-test.js index a4ab70a60..1b50b301b 100644 --- a/ui/admin/tests/acceptance/host-catalogs/hosts-test.js +++ b/ui/admin/tests/acceptance/host-catalogs/hosts/create-test.js @@ -10,7 +10,7 @@ import { //invalidateSession, } from 'ember-simple-auth/test-support'; -module('Acceptance | host-catalogs | hosts', function (hooks) { +module('Acceptance | host-catalogs | hosts | create', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); @@ -79,6 +79,40 @@ module('Acceptance | host-catalogs | hosts', function (hooks) { assert.equal(getHostCount(), count + 1); }); + test('Users cannot create a new host without proper authorization', async function (assert) { + assert.expect(2); + instances.hostCatalog.authorized_collection_actions.hosts = []; + await visit(urls.hostCatalog); + assert.notOk( + instances.hostCatalog.authorized_collection_actions.hosts.includes( + 'create' + ) + ); + assert.notOk(find(`.rose-layout-page-actions [href="${urls.newHost}"]`)); + }); + test('Users can navigate to new host catalogs route with proper authorization', async function (assert) { + assert.expect(2); + await visit(urls.hostCatalog); + assert.ok( + instances.hostCatalog.authorized_collection_actions.hosts.includes( + 'create' + ) + ); + assert.ok(find(`[href="${urls.hosts}"]`)); + }); + + test('Users cannot navigate to new host catalogs route without proper authorization', async function (assert) { + assert.expect(2); + instances.hostCatalog.authorized_collection_actions.hosts = []; + await visit(urls.hostCatalog); + assert.notOk( + instances.hostCatalog.authorized_collection_actions.hosts.includes( + 'create' + ) + ); + assert.notOk(find(`[href="${urls.hosts}"]`)); + }); + test('can cancel create new host', async function (assert) { assert.expect(2); const count = getHostCount(); diff --git a/ui/admin/tests/acceptance/host-catalogs/hosts/list-test.js b/ui/admin/tests/acceptance/host-catalogs/hosts/list-test.js new file mode 100644 index 000000000..356693474 --- /dev/null +++ b/ui/admin/tests/acceptance/host-catalogs/hosts/list-test.js @@ -0,0 +1,92 @@ +import { module, test } from 'qunit'; +import { visit, find } from '@ember/test-helpers'; +import { setupApplicationTest } from 'ember-qunit'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { + authenticateSession, + // These are left here intentionally for future reference. + //currentSession, + //invalidateSession, +} from 'ember-simple-auth/test-support'; + +module('Acceptance | host-catalogs | hosts | list', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + project: null, + hostCatalog: null, + host: null, + }, + }; + const urls = { + globalScope: null, + orgScope: null, + projectScope: null, + hostCatalogs: null, + hostCatalog: null, + hosts: null, + host: null, + unknownHost: null, + newHost: null, + }; + + hooks.beforeEach(function () { + // Generate resources + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.scopes.org = this.server.create('scope', { + type: 'org', + scope: { id: 'global', type: 'global' }, + }); + instances.scopes.project = this.server.create('scope', { + type: 'project', + scope: { id: instances.scopes.org.id, type: 'org' }, + }); + instances.hostCatalog = this.server.create('host-catalog', { + scope: instances.scopes.project, + }); + instances.host = this.server.create('host', { + scope: instances.scopes.project, + hostCatalog: instances.hostCatalog, + }); + // Generate route URLs for resources + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.hostCatalogs = `${urls.projectScope}/host-catalogs`; + urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`; + urls.hosts = `${urls.hostCatalog}/hosts`; + urls.host = `${urls.hosts}/${instances.host.id}`; + urls.unknownHost = `${urls.hosts}/foo`; + urls.newHost = `${urls.hosts}/new`; + authenticateSession({}); + }); + test('Users can navigate to hosts with proper authorization', async function (assert) { + assert.expect(2); + await visit(urls.hostCatalog); + assert.ok( + instances.hostCatalog.authorized_collection_actions.hosts.includes('list') + ); + assert.ok(find(`[href="${urls.hosts}"]`)); + }); + + test('User cannot navigate to index without either list or create actions', async function (assert) { + assert.expect(2); + instances.hostCatalog.authorized_collection_actions.hosts = []; + await visit(urls.hostCatalog); + assert.notOk( + instances.hostCatalog.authorized_collection_actions.hosts.includes('list') + ); + assert.notOk(find(`[href="${urls.hosts}"]`)); + }); + + test('User can navigate to index with only create action', async function (assert) { + assert.expect(1); + instances.hostCatalog.authorized_collection_actions.hosts = ['create']; + await visit(urls.hostCatalog); + assert.ok(find(`[href="${urls.hosts}"]`)); + }); +});