From a368d65d10214bd99a47ea2cbf072705ae2ff120 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 9 Sep 2020 12:24:01 -0400 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20implement=20new?= =?UTF-8?q?=20pathing=20for=20auth=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/auth-method.js | 13 ++++++++++ addons/api/addon/serializers/auth-method.js | 13 ++++++++++ addons/api/app/adapters/auth-method.js | 1 + addons/api/app/serializers/auth-method.js | 1 + .../tests/unit/adapters/auth-method-test.js | 12 ++++++++++ .../unit/serializers/auth-method-test.js | 24 +++++++++++++++++++ .../app/routes/scopes/scope/auth-methods.js | 9 +++---- .../scopes/scope/auth-methods/auth-method.js | 5 ++-- .../routes/scopes/scope/auth-methods/new.js | 7 +++++- .../app/routes/scopes/scope/authenticate.js | 4 ++-- ui/core/mirage/config.js | 12 ++++++---- ui/core/mirage/models/auth-method.js | 7 ++++++ ui/core/mirage/scenarios/default.js | 3 ++- ui/core/mirage/serializers/auth-method.js | 19 ++++++++++++++- ui/core/mirage/serializers/org.js | 5 ---- ui/core/mirage/serializers/project.js | 5 ---- .../tests/acceptance/authentication-test.js | 22 +++++++---------- 17 files changed, 121 insertions(+), 41 deletions(-) create mode 100644 addons/api/addon/adapters/auth-method.js create mode 100644 addons/api/addon/serializers/auth-method.js create mode 100644 addons/api/app/adapters/auth-method.js create mode 100644 addons/api/app/serializers/auth-method.js create mode 100644 addons/api/tests/unit/adapters/auth-method-test.js create mode 100644 addons/api/tests/unit/serializers/auth-method-test.js create mode 100644 ui/core/mirage/models/auth-method.js delete mode 100644 ui/core/mirage/serializers/org.js delete mode 100644 ui/core/mirage/serializers/project.js diff --git a/addons/api/addon/adapters/auth-method.js b/addons/api/addon/adapters/auth-method.js new file mode 100644 index 000000000..8aa06ee68 --- /dev/null +++ b/addons/api/addon/adapters/auth-method.js @@ -0,0 +1,13 @@ +import ApplicationAdapter from './application'; + +export default class AuthMethodAdapter extends ApplicationAdapter { + + // =attributes + + /** + * Auth methods use the new, unprefixed pathing style. + * @override + */ + hasScopePrefix = false; + +} diff --git a/addons/api/addon/serializers/auth-method.js b/addons/api/addon/serializers/auth-method.js new file mode 100644 index 000000000..d0c3c9312 --- /dev/null +++ b/addons/api/addon/serializers/auth-method.js @@ -0,0 +1,13 @@ +import ApplicationSerializer from './application'; + +export default class AuthMethodSerializer extends ApplicationSerializer { + + // =attributes + + /** + * Serialized the scope ID into the `scope_id` field. + * @type {boolean} + */ + serializeScopeID = true; + +} diff --git a/addons/api/app/adapters/auth-method.js b/addons/api/app/adapters/auth-method.js new file mode 100644 index 000000000..02c325d6e --- /dev/null +++ b/addons/api/app/adapters/auth-method.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/auth-method'; diff --git a/addons/api/app/serializers/auth-method.js b/addons/api/app/serializers/auth-method.js new file mode 100644 index 000000000..39dadcf9d --- /dev/null +++ b/addons/api/app/serializers/auth-method.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/auth-method'; diff --git a/addons/api/tests/unit/adapters/auth-method-test.js b/addons/api/tests/unit/adapters/auth-method-test.js new file mode 100644 index 000000000..06921d04c --- /dev/null +++ b/addons/api/tests/unit/adapters/auth-method-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | auth method', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let adapter = this.owner.lookup('adapter:auth-method'); + assert.ok(adapter); + }); +}); diff --git a/addons/api/tests/unit/serializers/auth-method-test.js b/addons/api/tests/unit/serializers/auth-method-test.js new file mode 100644 index 000000000..3ea3b3a32 --- /dev/null +++ b/addons/api/tests/unit/serializers/auth-method-test.js @@ -0,0 +1,24 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | auth method', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let store = this.owner.lookup('service:store'); + let serializer = store.serializerFor('auth-method'); + + assert.ok(serializer); + }); + + test('it serializes records', function(assert) { + let store = this.owner.lookup('service:store'); + let record = store.createRecord('auth-method', {}); + + let serializedRecord = record.serialize(); + + assert.ok(serializedRecord); + }); + +}); diff --git a/ui/core/app/routes/scopes/scope/auth-methods.js b/ui/core/app/routes/scopes/scope/auth-methods.js index 7527ac784..ca382d686 100644 --- a/ui/core/app/routes/scopes/scope/auth-methods.js +++ b/ui/core/app/routes/scopes/scope/auth-methods.js @@ -23,8 +23,8 @@ export default class ScopesScopeAuthMethodsRoute extends Route { * @return {Promise[AuthMethodModel]} */ async model() { - const { id: scopeID} = this.modelFor('scopes.scope'); - return this.store.findAll('auth-method', { adapterOptions: { scopeID } }); + const { id: scope_id } = this.modelFor('scopes.scope'); + return this.store.query('auth-method', { scope_id }); } // =actions @@ -48,11 +48,12 @@ export default class ScopesScopeAuthMethodsRoute extends Route { const { isNew } = authMethod; try { await authMethod.save(); + await this + .transitionTo('scopes.scope.auth-methods.auth-method', authMethod); this.refresh(); this.notify.success( this.intl.t(isNew ? 'notify.create-success' : 'notify.save-success') ); - this.transitionTo('scopes.scope.auth-methods.auth-method', authMethod); } catch (error) { //TODO: replace with translated strings this.notify.error(error.message, { closeAfter: null }); @@ -67,9 +68,9 @@ export default class ScopesScopeAuthMethodsRoute extends Route { async delete(authMethod) { try { await authMethod.destroyRecord(); + await this.transitionTo('scopes.scope.auth-methods'); this.refresh(); this.notify.success(this.intl.t('notify.delete-success')); - this.transitionTo('scopes.scope.auth-methods'); } catch (error) { //TODO: replace with translated strings this.notify.error(error.message, { closeAfter: null }); diff --git a/ui/core/app/routes/scopes/scope/auth-methods/auth-method.js b/ui/core/app/routes/scopes/scope/auth-methods/auth-method.js index 5cc752eac..e75ba7cb3 100644 --- a/ui/core/app/routes/scopes/scope/auth-methods/auth-method.js +++ b/ui/core/app/routes/scopes/scope/auth-methods/auth-method.js @@ -9,8 +9,7 @@ export default class ScopesScopeAuthMethodsAuthMethodRoute extends Route { * @param {string} params.auth_method_id * @return {authMethodModel} */ - async model({ auth_method_id}) { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findRecord('auth-method', auth_method_id, { adapterOptions: { scopeID }}); + async model({ auth_method_id }) { + return this.store.findRecord('auth-method', auth_method_id); } } diff --git a/ui/core/app/routes/scopes/scope/auth-methods/new.js b/ui/core/app/routes/scopes/scope/auth-methods/new.js index ceeb6a5d7..09fa7d445 100644 --- a/ui/core/app/routes/scopes/scope/auth-methods/new.js +++ b/ui/core/app/routes/scopes/scope/auth-methods/new.js @@ -1,6 +1,7 @@ import Route from '@ember/routing/route'; export default class ScopesScopeAuthMethodsNewRoute extends Route { + // =methods /** @@ -9,6 +10,10 @@ export default class ScopesScopeAuthMethodsNewRoute extends Route { */ model() { const scopeModel = this.modelFor('scopes.scope'); - return this.store.createRecord('auth-method', { type: 'password', scopeModel }); + return this.store.createRecord('auth-method', { + scopeModel, + type: 'password' + }); } + } diff --git a/ui/core/app/routes/scopes/scope/authenticate.js b/ui/core/app/routes/scopes/scope/authenticate.js index ef08a1566..d011a6b45 100644 --- a/ui/core/app/routes/scopes/scope/authenticate.js +++ b/ui/core/app/routes/scopes/scope/authenticate.js @@ -15,11 +15,11 @@ export default class ScopesScopeAuthenticateRoute extends Route { * @return {Promise} `{scope, scopes, authMethods}` */ model() { - const adapterOptions = { scopeID: this.modelFor('scopes.scope').id }; + const { id: scope_id } = this.modelFor('scopes.scope'); return hash({ scope: this.modelFor('scopes.scope'), scopes: this.modelFor('scopes'), - authMethods: this.store.findAll('auth-method', { adapterOptions }), + authMethods: this.store.query('auth-method', { scope_id }), // for integration testing: // authMethods: A([{ diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index cd9b48928..7d33d0b89 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -41,11 +41,13 @@ export default function() { // Auth & IAM resources - this.get('/scopes/:scope_id/auth-methods'); - this.post('/scopes/:scope_id/auth-methods'); - this.get('/scopes/:scope_id/auth-methods/:id'); - this.patch('/scopes/:scope_id/auth-methods/:id'); - this.del('/scopes/:scope_id/auth-methods/:id'); + this.get('/auth-methods', ({ authMethods }, { queryParams: { scope_id: scopeId } }) => { + return authMethods.where({ scopeId }); + }); + this.post('/auth-methods'); + this.get('/auth-methods/:id'); + this.patch('/auth-methods/:id'); + this.del('/auth-methods/:id'); // Authenticate/deauthenticate routes this.post('/scopes/:scope_id/auth-methods/:id_method', authHandler); diff --git a/ui/core/mirage/models/auth-method.js b/ui/core/mirage/models/auth-method.js new file mode 100644 index 000000000..08b1f5ad3 --- /dev/null +++ b/ui/core/mirage/models/auth-method.js @@ -0,0 +1,7 @@ +import { Model, belongsTo } from 'ember-cli-mirage'; + +export default Model.extend({ + + scope: belongsTo() + +}); diff --git a/ui/core/mirage/scenarios/default.js b/ui/core/mirage/scenarios/default.js index d08c2fb75..84d6708d8 100644 --- a/ui/core/mirage/scenarios/default.js +++ b/ui/core/mirage/scenarios/default.js @@ -15,7 +15,8 @@ export default function(server) { const scopePojo = { id: orgScope.id, type: orgScope.type }; // Auth - server.createList('auth-method', 3, { scope: scopePojo }); + server.createList('auth-method', 3, { scope: globalScope }); + server.createList('auth-method', 3, { scope: orgScope }); // Groups and Users server.createList('group', 5, { scope: scopePojo }, 'withMembers'); diff --git a/ui/core/mirage/serializers/auth-method.js b/ui/core/mirage/serializers/auth-method.js index cabe9ee7b..3d0c37374 100644 --- a/ui/core/mirage/serializers/auth-method.js +++ b/ui/core/mirage/serializers/auth-method.js @@ -1,5 +1,22 @@ import ApplicationSerializer from './application'; +import { underscore } from '@ember/string'; export default ApplicationSerializer.extend({ - modelName: 'auth-method' + modelName: 'auth-method', + + keyForRelationshipId(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_id`; + }, + + _hashForModel(model) { + const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); + json.scope = { + id: model.scope.id, + type: model.scope.type + }; + return json; + } + }); diff --git a/ui/core/mirage/serializers/org.js b/ui/core/mirage/serializers/org.js deleted file mode 100644 index 43404176b..000000000 --- a/ui/core/mirage/serializers/org.js +++ /dev/null @@ -1,5 +0,0 @@ -import ApplicationSerializer from './application'; - -export default ApplicationSerializer.extend({ - modelName: 'org' -}); diff --git a/ui/core/mirage/serializers/project.js b/ui/core/mirage/serializers/project.js deleted file mode 100644 index 370fb5cf9..000000000 --- a/ui/core/mirage/serializers/project.js +++ /dev/null @@ -1,5 +0,0 @@ -import ApplicationSerializer from './application'; - -export default ApplicationSerializer.extend({ - modelName: 'project' -}); diff --git a/ui/core/tests/acceptance/authentication-test.js b/ui/core/tests/acceptance/authentication-test.js index f7abb618b..86425d6fa 100644 --- a/ui/core/tests/acceptance/authentication-test.js +++ b/ui/core/tests/acceptance/authentication-test.js @@ -21,7 +21,9 @@ module('Acceptance | authentication', function (hooks) { let scopesURL; let orgScopeURL; let scope; + let globalAuthMethod; let authMethod; + let globalAuthMethodID; let authMethodID; let globalAuthenticateURL; let authenticateURL; @@ -43,14 +45,16 @@ module('Acceptance | authentication', function (hooks) { 'withChildren' ); scope = { id: orgScope.id, type: orgScope.type }; - authMethod = this.server.create('auth-method', { scope }); + globalAuthMethod = this.server.create('auth-method', { scope: globalScope }); + authMethod = this.server.create('auth-method', { scope: orgScope }); orgScopeID = orgScope.id; + globalAuthMethodID = globalAuthMethod.id; authMethodID = authMethod.id; scopesURL = `/scopes`; orgScopeURL = `/scopes/${orgScopeID}`; globalAuthenticateURL = `/scopes/global/authenticate`; authenticateURL = `/scopes/${orgScopeID}/authenticate`; - authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${authMethodID}`; + authMethodGlobalAuthenticateURL = `/scopes/global/authenticate/${globalAuthMethodID}`; authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`; orgsURL = `/scopes/global/orgs`; projectsURL = `/scopes/${orgScopeID}/projects`; @@ -74,18 +78,8 @@ module('Acceptance | authentication', function (hooks) { test('visiting auth method when the scope cannot be loaded is still allowed', async function (assert) { assert.expect(1); - orgScope.destroy(); - this.server.get('/scopes', ({ scopes }, { queryParams: { scope_id } }) => { - // Default parent scope is global - if (!scope_id) scope_id = 'global'; - const results = scopes.where(scope => { - return scope.scope ? scope.scope.id === scope_id : false - }); - if (!results.length) { - return new Response(404); - } else { - return results; - } + this.server.get('/scopes', () => { + return new Response(404); }); await visit(authMethodAuthenticateURL); await a11yAudit(); From e80708eeb48bc630d30bc1274f15dcc7f41b7d93 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 9 Sep 2020 16:24:48 -0400 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20implement=20new?= =?UTF-8?q?=20pathing=20for=20groups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/group.js | 13 +++++++++++++ addons/api/addon/serializers/group.js | 8 ++++++++ addons/api/app/adapters/group.js | 1 + addons/api/tests/unit/adapters/application-test.js | 8 +++----- addons/api/tests/unit/adapters/group-test.js | 12 ++++++++++++ addons/api/tests/unit/models/group-test.js | 6 +++--- ui/core/app/routes/scopes/scope/groups.js | 4 ++-- ui/core/app/routes/scopes/scope/groups/group.js | 6 +----- .../scopes/scope/roles/role/add-principals.js | 4 ++-- .../routes/scopes/scope/roles/role/principals.js | 2 +- ui/core/mirage/config.js | 14 ++++++++------ ui/core/mirage/factories/group.js | 3 ++- ui/core/mirage/models/group.js | 3 ++- ui/core/mirage/scenarios/default.js | 2 +- ui/core/mirage/serializers/group.js | 10 ++++++++++ ui/core/tests/acceptance/bread-crumbs-test.js | 2 +- ui/core/tests/acceptance/groups-test.js | 13 +++++-------- 17 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 addons/api/addon/adapters/group.js create mode 100644 addons/api/app/adapters/group.js create mode 100644 addons/api/tests/unit/adapters/group-test.js diff --git a/addons/api/addon/adapters/group.js b/addons/api/addon/adapters/group.js new file mode 100644 index 000000000..2a621c443 --- /dev/null +++ b/addons/api/addon/adapters/group.js @@ -0,0 +1,13 @@ +import ApplicationAdapter from './application'; + +export default class GroupAdapter extends ApplicationAdapter { + + // =attributes + + /** + * Groups use the new, unprefixed pathing style. + * @override + */ + hasScopePrefix = false; + +} diff --git a/addons/api/addon/serializers/group.js b/addons/api/addon/serializers/group.js index bbccc068f..2c7327a84 100644 --- a/addons/api/addon/serializers/group.js +++ b/addons/api/addon/serializers/group.js @@ -2,6 +2,14 @@ import ApplicationSerializer from './application'; export default class GroupSerializer extends ApplicationSerializer { + // =attributes + + /** + * Serialized the scope ID into the `scope_id` field. + * @type {boolean} + */ + serializeScopeID = true; + // =methods /** diff --git a/addons/api/app/adapters/group.js b/addons/api/app/adapters/group.js new file mode 100644 index 000000000..bb77ff26c --- /dev/null +++ b/addons/api/app/adapters/group.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/group'; diff --git a/addons/api/tests/unit/adapters/application-test.js b/addons/api/tests/unit/adapters/application-test.js index 64ae9fd27..a230c5848 100644 --- a/addons/api/tests/unit/adapters/application-test.js +++ b/addons/api/tests/unit/adapters/application-test.js @@ -93,13 +93,11 @@ module('Unit | Adapter | application', function (hooks) { test('it can request records through the store from a specified scope', async function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); - this.server.get('/v1/scopes/p_456/groups', () => { - assert.ok(true, 'Scoped resource URL was requested.'); + this.server.get('/v1/groups', (_, { queryParams: { scope_id }}) => { + assert.equal(scope_id, 'p_456', 'Scoped resource URL was requested.'); return {items: []}; }); - await store.findAll('group', { - adapterOptions: { scopeID: 'p_456' } - }); + await store.query('group', { scope_id: 'p_456' }); }); test('it rewrites PUT to PATCH, but leaves others unchanged', function (assert) { diff --git a/addons/api/tests/unit/adapters/group-test.js b/addons/api/tests/unit/adapters/group-test.js new file mode 100644 index 000000000..d39297843 --- /dev/null +++ b/addons/api/tests/unit/adapters/group-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | group', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let adapter = this.owner.lookup('adapter:group'); + assert.ok(adapter); + }); +}); diff --git a/addons/api/tests/unit/models/group-test.js b/addons/api/tests/unit/models/group-test.js index 0c52ba369..e5b2423c8 100644 --- a/addons/api/tests/unit/models/group-test.js +++ b/addons/api/tests/unit/models/group-test.js @@ -43,7 +43,7 @@ module('Unit | Model | group', function(hooks) { test('it has an `addMembers` method that groups a specific POST API endpoint and serialization', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/groups/123abc:add-members', (schema, request) => { + this.server.post('/v1/groups/123abc:add-members', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { member_ids: ['123_abc', 'foobar'], @@ -73,7 +73,7 @@ module('Unit | Model | group', function(hooks) { test('it has a `removeMembers` method that groups a specific POST API endpoint and serialization', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/groups/123abc:remove-members', (schema, request) => { + this.server.post('/v1/groups/123abc:remove-members', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { member_ids: ['1', '3'], @@ -103,7 +103,7 @@ module('Unit | Model | group', function(hooks) { test('it has a `removeMember` method that deletes a single member set using `removeMembers` method', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/groups/123abc:remove-members', (schema, request) => { + this.server.post('/v1/groups/123abc:remove-members', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { member_ids: ['3'], diff --git a/ui/core/app/routes/scopes/scope/groups.js b/ui/core/app/routes/scopes/scope/groups.js index c2c75c8fb..d51718c20 100644 --- a/ui/core/app/routes/scopes/scope/groups.js +++ b/ui/core/app/routes/scopes/scope/groups.js @@ -23,8 +23,8 @@ export default class ScopesScopeGroupsRoute extends Route { * @return {Promise[GroupModel]} */ async model() { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findAll('group', { adapterOptions: { scopeID } }); + const { id: scope_id } = this.modelFor('scopes.scope'); + return this.store.query('group', { scope_id }); } // =actions diff --git a/ui/core/app/routes/scopes/scope/groups/group.js b/ui/core/app/routes/scopes/scope/groups/group.js index b39abc4b8..e7ed63424 100644 --- a/ui/core/app/routes/scopes/scope/groups/group.js +++ b/ui/core/app/routes/scopes/scope/groups/group.js @@ -11,11 +11,7 @@ export default class ScopesScopeGroupsGroupRoute extends Route { * @return {groupModel} */ model({ group_id }) { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findRecord('group', group_id, { - reload: true, - adapterOptions: { scopeID }, - }); + return this.store.findRecord('group', group_id, { reload: true }); } /** diff --git a/ui/core/app/routes/scopes/scope/roles/role/add-principals.js b/ui/core/app/routes/scopes/scope/roles/role/add-principals.js index 860f3ba00..30f40366c 100644 --- a/ui/core/app/routes/scopes/scope/roles/role/add-principals.js +++ b/ui/core/app/routes/scopes/scope/roles/role/add-principals.js @@ -18,7 +18,7 @@ export default class ScopesScopeRolesRoleAddPrincipalsRoute extends Route { */ beforeModel() { this.store.unloadAll('user'); - this.store.unloadAll('group'); + //this.store.unloadAll('group'); } /** @@ -32,7 +32,7 @@ export default class ScopesScopeRolesRoleAddPrincipalsRoute extends Route { return hash({ role, users: this.store.findAll('user', options), - groups: this.store.findAll('group', options) + groups: this.store.query('group', { scope_id: scopeID }) }); } diff --git a/ui/core/app/routes/scopes/scope/roles/role/principals.js b/ui/core/app/routes/scopes/scope/roles/role/principals.js index 91fa7eb2a..9f7545830 100644 --- a/ui/core/app/routes/scopes/scope/roles/role/principals.js +++ b/ui/core/app/routes/scopes/scope/roles/role/principals.js @@ -32,7 +32,7 @@ export default class ScopesScopeRolesRolePrincipalsRoute extends Route { const groups = role.principals .filterBy('type', 'group') .map(({ principal_id }) => - this.store.findRecord('group', principal_id, { adapterOptions }) + this.store.findRecord('group', principal_id) .then(model => ({ type: 'group', model diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 7d33d0b89..0446758fb 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -107,12 +107,14 @@ export default function() { }); // IAM: Groups - this.get('/scopes/:scope_id/groups'); - this.post('/scopes/:scope_id/groups'); - this.get('/scopes/:scope_id/groups/:id'); - this.patch('/scopes/:scope_id/groups/:id'); - this.del('/scopes/:scope_id/groups/:id'); - this.post('/scopes/:scope_id/groups/:idMethod', function ({ groups }, { params: { idMethod } }) { + this.get('/groups', ({ groups }, { queryParams: { scope_id: scopeId } }) => { + return groups.where({ scopeId }); + }); + this.post('/groups'); + this.get('/groups/:id'); + this.patch('/groups/:id'); + this.del('/groups/:id'); + this.post('/groups/:idMethod', function ({ groups }, { params: { idMethod } }) { const attrs = this.normalizedRequestAttrs(); const id = idMethod.split(':')[0]; const method = idMethod.split(':')[1]; diff --git a/ui/core/mirage/factories/group.js b/ui/core/mirage/factories/group.js index 9531de4f8..e52d8bb82 100644 --- a/ui/core/mirage/factories/group.js +++ b/ui/core/mirage/factories/group.js @@ -11,7 +11,8 @@ export default factory.extend({ withMembers: trait({ afterCreate(group, server) { const scope = group.scope; - const members = server.createList('user', 2, { scope }); + const scopePojo = { id: scope.id, type: scope.type }; + const members = server.createList('user', 2, { scope: scopePojo }); group.update({ members }); } }) diff --git a/ui/core/mirage/models/group.js b/ui/core/mirage/models/group.js index 7f556c505..3d73f3d73 100644 --- a/ui/core/mirage/models/group.js +++ b/ui/core/mirage/models/group.js @@ -1,7 +1,8 @@ -import { Model, hasMany } from 'ember-cli-mirage'; +import { Model, belongsTo, hasMany } from 'ember-cli-mirage'; export default Model.extend({ + scope: belongsTo(), members: hasMany('user') }); diff --git a/ui/core/mirage/scenarios/default.js b/ui/core/mirage/scenarios/default.js index 84d6708d8..b42e7ca5f 100644 --- a/ui/core/mirage/scenarios/default.js +++ b/ui/core/mirage/scenarios/default.js @@ -19,7 +19,7 @@ export default function(server) { server.createList('auth-method', 3, { scope: orgScope }); // Groups and Users - server.createList('group', 5, { scope: scopePojo }, 'withMembers'); + server.createList('group', 5, { scope: orgScope }, 'withMembers'); // Role server.createList('role', 5, { scope: scopePojo }, 'withPrincipals'); diff --git a/ui/core/mirage/serializers/group.js b/ui/core/mirage/serializers/group.js index 2854224f8..ca0f9b138 100644 --- a/ui/core/mirage/serializers/group.js +++ b/ui/core/mirage/serializers/group.js @@ -10,9 +10,19 @@ export default ApplicationSerializer.extend({ )}_ids`; }, + keyForRelationshipId(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_id`; + }, + _hashForModel(model) { const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); json.member_ids = model.memberIds; + json.scope = { + id: model.scope.id, + type: model.scope.type + }; return json; } diff --git a/ui/core/tests/acceptance/bread-crumbs-test.js b/ui/core/tests/acceptance/bread-crumbs-test.js index 879eb311b..aafeecb5a 100644 --- a/ui/core/tests/acceptance/bread-crumbs-test.js +++ b/ui/core/tests/acceptance/bread-crumbs-test.js @@ -44,7 +44,7 @@ module('Acceptance | breadcrumbs', function (hooks) { project = getProjectScope(); user = this.server.create('user'); role = this.server.create('role'); - group = this.server.create('group'); + group = this.server.create('group', { scope: orgScope }); hostCatalog = this.server.create('host-catalog', { scope: { id: project.id, diff --git a/ui/core/tests/acceptance/groups-test.js b/ui/core/tests/acceptance/groups-test.js index a6ad6ccd9..017d2e669 100644 --- a/ui/core/tests/acceptance/groups-test.js +++ b/ui/core/tests/acceptance/groups-test.js @@ -30,10 +30,7 @@ module('Acceptance | groups', function (hooks) { ); const group = this.server.create('group', { - scope: { - id: orgScope.id, - type: orgScope.type, - }, + scope: orgScope }); groupsURL = `/scopes/${orgScope.id}/groups`; @@ -78,7 +75,7 @@ module('Acceptance | groups', function (hooks) { test('saving a new group with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.post('/scopes/:scope_id/groups', () => { + this.server.post('/groups', () => { return new Response( 400, {}, @@ -139,7 +136,7 @@ module('Acceptance | groups', function (hooks) { test('saving an existing group with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.patch('/scopes/:scope_id/groups/:id', () => { + this.server.patch('/groups/:id', () => { return new Response( 400, {}, @@ -175,7 +172,7 @@ module('Acceptance | groups', function (hooks) { test('errors are displayed when save project fails', async function (assert) { assert.expect(1); - this.server.patch('/scopes/:scope_id/groups/:id', () => { + this.server.patch('/groups/:id', () => { return new Response( 490, {}, @@ -198,7 +195,7 @@ module('Acceptance | groups', function (hooks) { test('errors are displayed when delete project fails', async function (assert) { assert.expect(1); - this.server.del('/scopes/:scope_id/groups/:id', () => { + this.server.del('/groups/:id', () => { return new Response( 490, {}, From 4a63c6484ff182bc20d7e03099a8b0e52c24abbb Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 9 Sep 2020 16:53:41 -0400 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20implement=20new?= =?UTF-8?q?=20pathing=20for=20users?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/user.js | 13 +++ addons/api/addon/serializers/user.js | 13 +++ addons/api/app/adapters/user.js | 1 + addons/api/app/serializers/user.js | 1 + addons/api/tests/unit/adapters/user-test.js | 12 +++ addons/api/tests/unit/models/base-test.js | 14 ++-- .../unit/serializers/application-test.js | 4 +- .../api/tests/unit/serializers/user-test.js | 23 ++++++ .../scopes/scope/groups/group/add-members.js | 5 +- .../scopes/scope/roles/role/add-principals.js | 16 +--- ui/core/app/routes/scopes/scope/users.js | 4 +- ui/core/app/routes/scopes/scope/users/user.js | 5 +- ui/core/mirage/config.js | 80 ++++++++++--------- ui/core/mirage/models/user.js | 7 ++ ui/core/mirage/serializers/user.js | 19 ++++- ui/core/tests/acceptance/bread-crumbs-test.js | 2 +- ui/core/tests/acceptance/users-test.js | 13 ++- 17 files changed, 154 insertions(+), 78 deletions(-) create mode 100644 addons/api/addon/adapters/user.js create mode 100644 addons/api/addon/serializers/user.js create mode 100644 addons/api/app/adapters/user.js create mode 100644 addons/api/app/serializers/user.js create mode 100644 addons/api/tests/unit/adapters/user-test.js create mode 100644 addons/api/tests/unit/serializers/user-test.js create mode 100644 ui/core/mirage/models/user.js diff --git a/addons/api/addon/adapters/user.js b/addons/api/addon/adapters/user.js new file mode 100644 index 000000000..0d8242019 --- /dev/null +++ b/addons/api/addon/adapters/user.js @@ -0,0 +1,13 @@ +import ApplicationAdapter from './application'; + +export default class UserAdapter extends ApplicationAdapter { + + // =attributes + + /** + * Users use the new, unprefixed pathing style. + * @override + */ + hasScopePrefix = false; + +} diff --git a/addons/api/addon/serializers/user.js b/addons/api/addon/serializers/user.js new file mode 100644 index 000000000..7e9e54c21 --- /dev/null +++ b/addons/api/addon/serializers/user.js @@ -0,0 +1,13 @@ +import ApplicationSerializer from './application'; + +export default class UserSerializer extends ApplicationSerializer { + + // =attributes + + /** + * Serialized the scope ID into the `scope_id` field. + * @type {boolean} + */ + serializeScopeID = true; + +} diff --git a/addons/api/app/adapters/user.js b/addons/api/app/adapters/user.js new file mode 100644 index 000000000..78f340b99 --- /dev/null +++ b/addons/api/app/adapters/user.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/user'; diff --git a/addons/api/app/serializers/user.js b/addons/api/app/serializers/user.js new file mode 100644 index 000000000..5c5a92643 --- /dev/null +++ b/addons/api/app/serializers/user.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/user'; diff --git a/addons/api/tests/unit/adapters/user-test.js b/addons/api/tests/unit/adapters/user-test.js new file mode 100644 index 000000000..0332ed354 --- /dev/null +++ b/addons/api/tests/unit/adapters/user-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | user', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let adapter = this.owner.lookup('adapter:user'); + assert.ok(adapter); + }); +}); diff --git a/addons/api/tests/unit/models/base-test.js b/addons/api/tests/unit/models/base-test.js index 40be071fe..cc89967a0 100644 --- a/addons/api/tests/unit/models/base-test.js +++ b/addons/api/tests/unit/models/base-test.js @@ -97,17 +97,17 @@ module('Unit | Model | base', function (hooks) { assert.expect(3); const store = this.owner.lookup('service:store'); const model = store.createRecord('user', { scopeID: 'o_123' }); - this.server.post('/v1/scopes/o_123/users', () => { + this.server.post('/v1/users', () => { assert.ok(true, 'Correctly scoped create record URL was requested.') return {}; }); await model.save(); - this.server.patch('/v1/scopes/o_123/users', () => { + this.server.patch('/v1/users', () => { assert.ok(true, 'Correctly scoped update record URL was requested.') return {}; }); await model.save(); - this.server.delete('/v1/scopes/o_123/users', () => { + this.server.delete('/v1/users', () => { assert.ok(true, 'Correctly scoped delete record URL was requested.') return {}; }); @@ -120,17 +120,17 @@ module('Unit | Model | base', function (hooks) { const customScopeID = 'global'; const scope = {scope_id: 'o_123'}; const model = store.createRecord('user', { scope }); - this.server.post('/v1/scopes/global/users', () => { + this.server.post('/v1/users', () => { assert.ok(true, 'Correctly scoped create record URL was requested.') return {}; }); await model.save({adapterOptions: {scopeID: customScopeID}}); - this.server.patch('/v1/scopes/global/users', () => { + this.server.patch('/v1/users', () => { assert.ok(true, 'Correctly scoped update record URL was requested.') return {}; }); await model.save({adapterOptions: {scopeID: customScopeID}}); - this.server.delete('/v1/scopes/global/users', () => { + this.server.delete('/v1/users', () => { assert.ok(true, 'Correctly scoped delete record URL was requested.') return {}; }); @@ -143,7 +143,7 @@ module('Unit | Model | base', function (hooks) { const method = 'my-custom-method'; const scope = {scope_id: 'o_123'}; const model = store.createRecord('user', { scope }); - this.server.post(`/v1/scopes/o_123/users:${method}`, () => { + this.server.post(`/v1/users:${method}`, () => { assert.ok(true, 'Correctly scoped create record URL was requested.') return {}; }); diff --git a/addons/api/tests/unit/serializers/application-test.js b/addons/api/tests/unit/serializers/application-test.js index e838a8da0..07385b071 100644 --- a/addons/api/tests/unit/serializers/application-test.js +++ b/addons/api/tests/unit/serializers/application-test.js @@ -25,7 +25,8 @@ module('Unit | Serializer | application', function (hooks) { const serializedRecord = record.serialize(); assert.deepEqual(serializedRecord, { name: 'User', - description: 'Description' + description: 'Description', + scope_id: 'global' }); }); @@ -33,6 +34,7 @@ module('Unit | Serializer | application', function (hooks) { assert.expect(2); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('user'); + serializer.serializeScopeID = false; const record = store.createRecord('user', { name: 'User', description: 'Description', diff --git a/addons/api/tests/unit/serializers/user-test.js b/addons/api/tests/unit/serializers/user-test.js new file mode 100644 index 000000000..b96a53b13 --- /dev/null +++ b/addons/api/tests/unit/serializers/user-test.js @@ -0,0 +1,23 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | user', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let store = this.owner.lookup('service:store'); + let serializer = store.serializerFor('user'); + + assert.ok(serializer); + }); + + test('it serializes records', function(assert) { + let store = this.owner.lookup('service:store'); + let record = store.createRecord('user', {}); + + let serializedRecord = record.serialize(); + + assert.ok(serializedRecord); + }); +}); diff --git a/ui/core/app/routes/scopes/scope/groups/group/add-members.js b/ui/core/app/routes/scopes/scope/groups/group/add-members.js index f3b8717a9..303a73662 100644 --- a/ui/core/app/routes/scopes/scope/groups/group/add-members.js +++ b/ui/core/app/routes/scopes/scope/groups/group/add-members.js @@ -24,10 +24,11 @@ export default class ScopesScopeGroupsGroupAddMembersRoute extends Route { * @return {Promise{GroupModel, [UserModel]}} */ model() { - const { id: scopeID } = this.modelFor('scopes.scope'); + const group = this.modelFor('scopes.scope.groups.group'); + const { scopeID: scope_id } = group; return hash({ group: this.modelFor('scopes.scope.groups.group'), - users: this.store.findAll('user', { adapterOptions: { scopeID } }) + users: this.store.query('user', { scope_id }) }); } diff --git a/ui/core/app/routes/scopes/scope/roles/role/add-principals.js b/ui/core/app/routes/scopes/scope/roles/role/add-principals.js index 30f40366c..afa955a90 100644 --- a/ui/core/app/routes/scopes/scope/roles/role/add-principals.js +++ b/ui/core/app/routes/scopes/scope/roles/role/add-principals.js @@ -12,27 +12,17 @@ export default class ScopesScopeRolesRoleAddPrincipalsRoute extends Route { // =methods - /** - * Empty out any previously loaded users and groups. - * (collectively, principals). - */ - beforeModel() { - this.store.unloadAll('user'); - //this.store.unloadAll('group'); - } - /** * Returns the current role, all users, and all groups * @return {{role: RoleModel, users: [UserModel], groups: [GroupModel]}} */ model() { const role = this.modelFor('scopes.scope.roles.role'); - const { scopeID } = role; - const options = { adapterOptions: { scopeID } }; + const { scopeID: scope_id } = role; return hash({ role, - users: this.store.findAll('user', options), - groups: this.store.query('group', { scope_id: scopeID }) + users: this.store.query('user', { scope_id }), + groups: this.store.query('group', { scope_id }) }); } diff --git a/ui/core/app/routes/scopes/scope/users.js b/ui/core/app/routes/scopes/scope/users.js index 20731c90f..3efffbde1 100644 --- a/ui/core/app/routes/scopes/scope/users.js +++ b/ui/core/app/routes/scopes/scope/users.js @@ -23,8 +23,8 @@ export default class ScopesScopeUsersRoute extends Route { * @return {Promise{[UserModel]}} */ async model() { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findAll('user', { adapterOptions: { scopeID } }); + const { id: scope_id } = this.modelFor('scopes.scope'); + return this.store.query('user', { scope_id }); } // =actions diff --git a/ui/core/app/routes/scopes/scope/users/user.js b/ui/core/app/routes/scopes/scope/users/user.js index ffbf19e7e..d26912707 100644 --- a/ui/core/app/routes/scopes/scope/users/user.js +++ b/ui/core/app/routes/scopes/scope/users/user.js @@ -10,9 +10,6 @@ export default class ScopesScopeUsersUserRoute extends Route { * @return {UserModel} */ async model({ user_id }) { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findRecord('user', user_id, { - adapterOptions: { scopeID }, - }); + return this.store.findRecord('user', user_id); } } diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 0446758fb..f3d185893 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -54,11 +54,47 @@ export default function() { this.post('/scopes/:id_method', deauthHandler); // IAM : Users - this.get('/scopes/:scope_id/users'); - this.post('/scopes/:scope_id/users'); - this.get('/scopes/:scope_id/users/:id'); - this.patch('/scopes/:scope_id/users/:id'); - this.del('/scopes/:scope_id/users/:id'); + this.get('/users', ({ users }, { queryParams: { scope_id: scopeId } }) => { + return users.where({ scopeId }); + }); + this.post('/users'); + this.get('/users/:id'); + this.patch('/users/:id'); + this.del('/users/:id'); + + // IAM: Groups + this.get('/groups', ({ groups }, { queryParams: { scope_id: scopeId } }) => { + return groups.where({ scopeId }); + }); + this.post('/groups'); + this.get('/groups/:id'); + this.patch('/groups/:id'); + this.del('/groups/:id'); + this.post('/groups/:idMethod', function ({ groups }, { params: { idMethod } }) { + const attrs = this.normalizedRequestAttrs(); + const id = idMethod.split(':')[0]; + const method = idMethod.split(':')[1]; + const group = groups.find(id); + const updatedAttrs = { + version: attrs.version, + memberIds: group.memberIds + }; + // If adding members, push them into the array + if (method === 'add-members') { + attrs.memberIds.forEach(id => { + if (!updatedAttrs.memberIds.includes(id)) { + updatedAttrs.memberIds.push(id); + } + }); + } + // If deleting members, filter them out of the array + if (method === 'remove-members') { + updatedAttrs.memberIds = updatedAttrs.memberIds.filter(id => { + return !attrs.memberIds.includes(id); + }); + } + return group.update(updatedAttrs); + }); // IAM: Roles this.get('/scopes/:scope_id/roles'); @@ -106,40 +142,6 @@ export default function() { return role.update(updatedAttrs); }); - // IAM: Groups - this.get('/groups', ({ groups }, { queryParams: { scope_id: scopeId } }) => { - return groups.where({ scopeId }); - }); - this.post('/groups'); - this.get('/groups/:id'); - this.patch('/groups/:id'); - this.del('/groups/:id'); - this.post('/groups/:idMethod', function ({ groups }, { params: { idMethod } }) { - const attrs = this.normalizedRequestAttrs(); - const id = idMethod.split(':')[0]; - const method = idMethod.split(':')[1]; - const group = groups.find(id); - const updatedAttrs = { - version: attrs.version, - memberIds: group.memberIds - }; - // If adding members, push them into the array - if (method === 'add-members') { - attrs.memberIds.forEach(id => { - if (!updatedAttrs.memberIds.includes(id)) { - updatedAttrs.memberIds.push(id); - } - }); - } - // If deleting members, filter them out of the array - if (method === 'remove-members') { - updatedAttrs.memberIds = updatedAttrs.memberIds.filter(id => { - return !attrs.memberIds.includes(id); - }); - } - return group.update(updatedAttrs); - }); - // Other resources // host-catalog diff --git a/ui/core/mirage/models/user.js b/ui/core/mirage/models/user.js new file mode 100644 index 000000000..08b1f5ad3 --- /dev/null +++ b/ui/core/mirage/models/user.js @@ -0,0 +1,7 @@ +import { Model, belongsTo } from 'ember-cli-mirage'; + +export default Model.extend({ + + scope: belongsTo() + +}); diff --git a/ui/core/mirage/serializers/user.js b/ui/core/mirage/serializers/user.js index 142cbf508..499b249eb 100644 --- a/ui/core/mirage/serializers/user.js +++ b/ui/core/mirage/serializers/user.js @@ -1,5 +1,22 @@ import ApplicationSerializer from './application'; +import { underscore } from '@ember/string'; export default ApplicationSerializer.extend({ - modelName: 'user' + modelName: 'user', + + keyForRelationshipId(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_id`; + }, + + _hashForModel(model) { + const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); + json.scope = { + id: model.scope.id, + type: model.scope.type + }; + return json; + } + }); diff --git a/ui/core/tests/acceptance/bread-crumbs-test.js b/ui/core/tests/acceptance/bread-crumbs-test.js index aafeecb5a..32382e582 100644 --- a/ui/core/tests/acceptance/bread-crumbs-test.js +++ b/ui/core/tests/acceptance/bread-crumbs-test.js @@ -42,7 +42,7 @@ module('Acceptance | breadcrumbs', function (hooks) { this.server.schema.scopes.where({ type: 'project' }).models[0]; project = getProjectScope(); - user = this.server.create('user'); + user = this.server.create('user', { scope: orgScope }); role = this.server.create('role'); group = this.server.create('group', { scope: orgScope }); hostCatalog = this.server.create('host-catalog', { diff --git a/ui/core/tests/acceptance/users-test.js b/ui/core/tests/acceptance/users-test.js index 55e4ac108..26e6b3c55 100644 --- a/ui/core/tests/acceptance/users-test.js +++ b/ui/core/tests/acceptance/users-test.js @@ -30,10 +30,7 @@ module('Acceptance | users', function (hooks) { ); const user = this.server.create('user', { - scope: { - id: orgScope.id, - type: orgScope.type, - }, + scope: orgScope }); usersURL = `/scopes/${orgScope.id}/users`; @@ -78,7 +75,7 @@ module('Acceptance | users', function (hooks) { test('saving a new user with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.post('scopes/:scope_id/users', () => { + this.server.post('/users', () => { return new Response( 400, {}, @@ -139,7 +136,7 @@ module('Acceptance | users', function (hooks) { test('saving an existing user with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.patch('/scopes/:scope_id/users/:id', () => { + this.server.patch('/users/:id', () => { return new Response( 400, {}, @@ -175,7 +172,7 @@ module('Acceptance | users', function (hooks) { test('errors are displayed when saving user fails', async function (assert) { assert.expect(1); - this.server.patch('/scopes/:scope_id/users/:id', () => { + this.server.patch('/users/:id', () => { return new Response( 490, {}, @@ -198,7 +195,7 @@ module('Acceptance | users', function (hooks) { test('errors are displayed when user deletion fails', async function (assert) { assert.expect(1); - this.server.del('/scopes/:scope_id/users/:id', () => { + this.server.del('/users/:id', () => { return new Response( 490, {}, From 76fe50c9e360c6e11b29c5053d45b07d1fd542ca Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Thu, 10 Sep 2020 11:03:44 -0400 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20implement=20new?= =?UTF-8?q?=20pathing=20for=20roles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/role.js | 13 +++++++++++++ addons/api/addon/serializers/role.js | 8 ++++++++ addons/api/app/adapters/role.js | 1 + addons/api/tests/unit/adapters/role-test.js | 12 ++++++++++++ addons/api/tests/unit/models/role-test.js | 8 ++++---- ui/core/app/routes/scopes/scope/roles.js | 4 ++-- ui/core/mirage/config.js | 14 ++++++++------ ui/core/mirage/factories/group.js | 3 +-- ui/core/mirage/factories/role.js | 5 +++-- ui/core/mirage/models/role.js | 3 ++- ui/core/mirage/scenarios/default.js | 3 +-- ui/core/mirage/serializers/role.js | 17 ++++++++++++++--- ui/core/tests/acceptance/bread-crumbs-test.js | 2 +- ui/core/tests/acceptance/roles-test.js | 13 +++++-------- 14 files changed, 75 insertions(+), 31 deletions(-) create mode 100644 addons/api/addon/adapters/role.js create mode 100644 addons/api/app/adapters/role.js create mode 100644 addons/api/tests/unit/adapters/role-test.js diff --git a/addons/api/addon/adapters/role.js b/addons/api/addon/adapters/role.js new file mode 100644 index 000000000..036466645 --- /dev/null +++ b/addons/api/addon/adapters/role.js @@ -0,0 +1,13 @@ +import ApplicationAdapter from './application'; + +export default class RoleAdapter extends ApplicationAdapter { + + // =attributes + + /** + * Roles use the new, unprefixed pathing style. + * @override + */ + hasScopePrefix = false; + +} diff --git a/addons/api/addon/serializers/role.js b/addons/api/addon/serializers/role.js index 89d10353d..5c858bc1f 100644 --- a/addons/api/addon/serializers/role.js +++ b/addons/api/addon/serializers/role.js @@ -3,6 +3,14 @@ import { get } from '@ember/object'; export default class RoleSerializer extends ApplicationSerializer { + // =attributes + + /** + * Serialized the scope ID into the `scope_id` field. + * @type {boolean} + */ + serializeScopeID = true; + // =methods /** diff --git a/addons/api/app/adapters/role.js b/addons/api/app/adapters/role.js new file mode 100644 index 000000000..f12325923 --- /dev/null +++ b/addons/api/app/adapters/role.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/role'; diff --git a/addons/api/tests/unit/adapters/role-test.js b/addons/api/tests/unit/adapters/role-test.js new file mode 100644 index 000000000..3d847ae20 --- /dev/null +++ b/addons/api/tests/unit/adapters/role-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | role', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let adapter = this.owner.lookup('adapter:role'); + assert.ok(adapter); + }); +}); diff --git a/addons/api/tests/unit/models/role-test.js b/addons/api/tests/unit/models/role-test.js index 50a0fdb2d..42aefae25 100644 --- a/addons/api/tests/unit/models/role-test.js +++ b/addons/api/tests/unit/models/role-test.js @@ -14,7 +14,7 @@ module('Unit | Model | role', function (hooks) { test('it has a `saveGrants` method that targets a specific POST API endpoint and serialization', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/roles/123abc:set-grants', (schema, request) => { + this.server.post('/v1/roles/123abc:set-grants', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { grant_strings: ['grant1', 'grant2'], @@ -44,7 +44,7 @@ module('Unit | Model | role', function (hooks) { test('it has an `addPrincipals` method that targets a specific POST API endpoint and serialization', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/roles/123abc:add-principals', (schema, request) => { + this.server.post('/v1/roles/123abc:add-principals', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { principal_ids: ['123_abc', 'foobar'], @@ -77,7 +77,7 @@ module('Unit | Model | role', function (hooks) { test('it has a `removePrincipals` method that targets a specific POST API endpoint and serialization', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/roles/123abc:remove-principals', (schema, request) => { + this.server.post('/v1/roles/123abc:remove-principals', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { principal_ids: ['1', '3'], @@ -110,7 +110,7 @@ module('Unit | Model | role', function (hooks) { test('it has a `removePrincipal` method that deletes a single principal using `removePrincipals` method', async function (assert) { assert.expect(1); - this.server.post('/v1/scopes/o_1/roles/123abc:remove-principals', (schema, request) => { + this.server.post('/v1/roles/123abc:remove-principals', (schema, request) => { const body = JSON.parse(request.requestBody); assert.deepEqual(body, { principal_ids: ['3'], diff --git a/ui/core/app/routes/scopes/scope/roles.js b/ui/core/app/routes/scopes/scope/roles.js index b0418e399..00c4fc383 100644 --- a/ui/core/app/routes/scopes/scope/roles.js +++ b/ui/core/app/routes/scopes/scope/roles.js @@ -23,8 +23,8 @@ export default class ScopesScopeRolesRoute extends Route { * @return {Promise{[RoleModel]}} */ async model() { - const { id: scopeID } = this.modelFor('scopes.scope'); - return this.store.findAll('role', { adapterOptions: { scopeID } }); + const { id: scope_id } = this.modelFor('scopes.scope'); + return this.store.query('role', { scope_id }); } // =actions diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index f3d185893..11c78a555 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -97,12 +97,14 @@ export default function() { }); // IAM: Roles - this.get('/scopes/:scope_id/roles'); - this.post('/scopes/:scope_id/roles'); - this.get('/scopes/:scope_id/roles/:id'); - this.patch('/scopes/:scope_id/roles/:id'); - this.del('/scopes/:scope_id/roles/:id'); - this.post('/scopes/:scope_id/roles/:idMethod', function ({ roles, users, groups }, { params: { idMethod } }) { + this.get('/roles', ({ roles }, { queryParams: { scope_id: scopeId } }) => { + return roles.where({ scopeId }); + }); + this.post('/roles'); + this.get('/roles/:id'); + this.patch('/roles/:id'); + this.del('/roles/:id'); + this.post('/roles/:idMethod', function ({ roles, users, groups }, { params: { idMethod } }) { const attrs = this.normalizedRequestAttrs(); const id = idMethod.split(':')[0]; const method = idMethod.split(':')[1]; diff --git a/ui/core/mirage/factories/group.js b/ui/core/mirage/factories/group.js index e52d8bb82..9531de4f8 100644 --- a/ui/core/mirage/factories/group.js +++ b/ui/core/mirage/factories/group.js @@ -11,8 +11,7 @@ export default factory.extend({ withMembers: trait({ afterCreate(group, server) { const scope = group.scope; - const scopePojo = { id: scope.id, type: scope.type }; - const members = server.createList('user', 2, { scope: scopePojo }); + const members = server.createList('user', 2, { scope }); group.update({ members }); } }) diff --git a/ui/core/mirage/factories/role.js b/ui/core/mirage/factories/role.js index adebbbc12..ea5734835 100644 --- a/ui/core/mirage/factories/role.js +++ b/ui/core/mirage/factories/role.js @@ -14,8 +14,9 @@ export default factory.extend({ */ withPrincipals: trait({ afterCreate(role, server) { - const users = server.createList('user', 2); - const groups = server.createList('group', 2); + const { scope } = role; + const users = server.createList('user', 2, { scope }); + const groups = server.createList('group', 2, { scope }); role.update({ users, groups }); } }) diff --git a/ui/core/mirage/models/role.js b/ui/core/mirage/models/role.js index 6c2517f3b..744d1025a 100644 --- a/ui/core/mirage/models/role.js +++ b/ui/core/mirage/models/role.js @@ -1,7 +1,8 @@ -import { Model, hasMany } from 'ember-cli-mirage'; +import { Model, belongsTo, hasMany } from 'ember-cli-mirage'; export default Model.extend({ + scope: belongsTo(), users: hasMany(), groups: hasMany() diff --git a/ui/core/mirage/scenarios/default.js b/ui/core/mirage/scenarios/default.js index b42e7ca5f..c878699ef 100644 --- a/ui/core/mirage/scenarios/default.js +++ b/ui/core/mirage/scenarios/default.js @@ -12,7 +12,6 @@ export default function(server) { type: 'org', scope: { id: globalScope.id, type: globalScope.type } }, 'withChildren')[0]; - const scopePojo = { id: orgScope.id, type: orgScope.type }; // Auth server.createList('auth-method', 3, { scope: globalScope }); @@ -21,7 +20,7 @@ export default function(server) { // Groups and Users server.createList('group', 5, { scope: orgScope }, 'withMembers'); // Role - server.createList('role', 5, { scope: scopePojo }, 'withPrincipals'); + server.createList('role', 5, { scope: orgScope }, 'withPrincipals'); // Other resources server.schema.scopes.where({type: 'project'}).models.forEach(scope => { diff --git a/ui/core/mirage/serializers/role.js b/ui/core/mirage/serializers/role.js index 56f3d0195..47623471d 100644 --- a/ui/core/mirage/serializers/role.js +++ b/ui/core/mirage/serializers/role.js @@ -1,24 +1,35 @@ import ApplicationSerializer from './application'; +import { underscore } from '@ember/string'; export default ApplicationSerializer.extend({ modelName: 'role', + keyForRelationshipId(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_id`; + }, + _hashForModel(model) { const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); - + const serializedUsers = model.users.models.map(user => ({ scope_id: model.scope.id, id: user.id, type: 'user' })); - const serializedGroups = model.groups.models.map(group => ({ scope_id: model.scope.id, id: group.id, type: 'group' })); - json.principals = [ ...serializedUsers, ...serializedGroups ]; + + json.scope = { + id: model.scope.id, + type: model.scope.type + }; + return json; } }); diff --git a/ui/core/tests/acceptance/bread-crumbs-test.js b/ui/core/tests/acceptance/bread-crumbs-test.js index 32382e582..9400e9dbe 100644 --- a/ui/core/tests/acceptance/bread-crumbs-test.js +++ b/ui/core/tests/acceptance/bread-crumbs-test.js @@ -43,7 +43,7 @@ module('Acceptance | breadcrumbs', function (hooks) { project = getProjectScope(); user = this.server.create('user', { scope: orgScope }); - role = this.server.create('role'); + role = this.server.create('role', { scope: orgScope }); group = this.server.create('group', { scope: orgScope }); hostCatalog = this.server.create('host-catalog', { scope: { diff --git a/ui/core/tests/acceptance/roles-test.js b/ui/core/tests/acceptance/roles-test.js index 07f55d080..e1b1b3538 100644 --- a/ui/core/tests/acceptance/roles-test.js +++ b/ui/core/tests/acceptance/roles-test.js @@ -30,10 +30,7 @@ module('Acceptance | roles', function (hooks) { ); const role = this.server.create('role', { - scope: { - id: orgScope.id, - type: orgScope.type, - }, + scope: orgScope }); rolesURL = `/scopes/${orgScope.id}/roles`; @@ -78,7 +75,7 @@ module('Acceptance | roles', function (hooks) { test('saving a new role with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.post('/scopes/:scope_id/roles', () => { + this.server.post('/roles', () => { return new Response( 400, {}, @@ -139,7 +136,7 @@ module('Acceptance | roles', function (hooks) { test('saving an existing role with invalid fields displays error messages', async function (assert) { assert.expect(2); - this.server.patch('/scopes/:scope_id/roles/:id', () => { + this.server.patch('/roles/:id', () => { return new Response( 400, {}, @@ -175,7 +172,7 @@ module('Acceptance | roles', function (hooks) { test('errors are displayed when save project fails', async function (assert) { assert.expect(1); - this.server.patch('/scopes/:scope_id/roles/:id', () => { + this.server.patch('/roles/:id', () => { return new Response( 490, {}, @@ -198,7 +195,7 @@ module('Acceptance | roles', function (hooks) { test('errors are displayed when delete project fails', async function (assert) { assert.expect(1); - this.server.del('/scopes/:scope_id/roles/:id', () => { + this.server.del('/roles/:id', () => { return new Response( 490, {},