From 392da009d0fe76b82f24a2ec50f8909d24da0d63 Mon Sep 17 00:00:00 2001 From: Dharini Jeeva Date: Thu, 22 Dec 2022 10:23:49 -0800 Subject: [PATCH] Add mirage support principal managed groups (#1542) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 mocks for principals/managed-groups & roles --- addons/api/mirage/config.js | 33 ++++++++++++--- addons/api/mirage/factories/role.js | 6 ++- addons/api/mirage/models/role.js | 1 + addons/api/mirage/scenarios/default.js | 41 +++++++++++++++++-- addons/api/mirage/serializers/role.js | 14 ++++++- addons/core/translations/resources/en-us.yaml | 1 + .../tests/acceptance/roles/principals-test.js | 7 ++-- 7 files changed, 90 insertions(+), 13 deletions(-) diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index 95bbe9ac7..d18bc0676 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -267,14 +267,17 @@ export default function () { this.del('/roles/:id'); this.post( '/roles/:idMethod', - function ({ roles, users, groups }, { params: { idMethod } }) { + function ( + { roles, users, groups, managedGroups }, + { params: { idMethod } } + ) { const attrs = this.normalizedRequestAttrs(); const id = idMethod.split(':')[0]; const method = idMethod.split(':')[1]; const role = roles.find(id); let updatedAttrs = {}; - // Principals is a combined list of users and groups, but in Mirage we've + // Principals is a combined list of users, groups and managed groups, but in Mirage we've // internally modelled them as separate lists. Therefore we must check // the type by looking up the user or group first, then determine which // list to add the principal to. @@ -283,25 +286,31 @@ export default function () { version: attrs.version, userIds: role.userIds, groupIds: role.groupIds, + managedGroupIds: role.managedGroupIds, }; attrs.principalIds.forEach((id) => { const isUser = users.find(id); const isGroup = groups.find(id); + const isManagedGroup = managedGroups.find(id); if (isUser && !updatedAttrs.userIds.includes(id)) { updatedAttrs.userIds.push(id); } if (isGroup && !updatedAttrs.groupIds.includes(id)) { updatedAttrs.groupIds.push(id); } + if (isManagedGroup && !updatedAttrs.managedGroupIds.includes(id)) { + updatedAttrs.managedGroupIds.push(id); + } }); } - // If deleting principals, filter them out of both users and groups, + // If deleting principals, filter the ids out of users, groups and managed group lists, // and in this case don't care about the type if (method === 'remove-principals') { updatedAttrs = { version: attrs.version, userIds: role.userIds, groupIds: role.groupIds, + managedGroupIds: role.managedGroupIds, }; updatedAttrs.userIds = updatedAttrs.userIds.filter((id) => { return !attrs.principalIds.includes(id); @@ -309,6 +318,11 @@ export default function () { updatedAttrs.groupIds = updatedAttrs.groupIds.filter((id) => { return !attrs.principalIds.includes(id); }); + updatedAttrs.managedGroupIds = updatedAttrs.managedGroupIds.filter( + (id) => { + return !attrs.principalIds.includes(id); + } + ); } if (method === 'set-grants') { @@ -580,8 +594,17 @@ export default function () { // managed-groups this.get( '/managed-groups', - ({ managedGroups }, { queryParams: { auth_method_id: authMethodId } }) => { - return managedGroups.where({ authMethodId }); + ( + { managedGroups }, + { queryParams: { auth_method_id: authMethodId, filter } } + ) => { + let resultSet; + if (authMethodId) { + resultSet = managedGroups.where({ authMethodId }); + } else { + resultSet = managedGroups.all(); + } + return resultSet.filter(makeBooleanFilter(filter)); } ); this.post('/managed-groups'); diff --git a/addons/api/mirage/factories/role.js b/addons/api/mirage/factories/role.js index 841558fde..72138f61c 100644 --- a/addons/api/mirage/factories/role.js +++ b/addons/api/mirage/factories/role.js @@ -28,9 +28,13 @@ export default factory.extend({ withPrincipals: trait({ afterCreate(role, server) { const { scope } = role; + const { id: scopeId } = scope; const users = server.createList('user', 2, { scope }); const groups = server.createList('group', 2, { scope }); - role.update({ users, groups }); + const managedGroups = server.schema.managedGroups.where({ + scopeId, + }).models; + role.update({ users, groups, managedGroups }); }, }), diff --git a/addons/api/mirage/models/role.js b/addons/api/mirage/models/role.js index 97be034f9..54acf6d50 100644 --- a/addons/api/mirage/models/role.js +++ b/addons/api/mirage/models/role.js @@ -5,4 +5,5 @@ export default Model.extend({ scope: belongsTo(), users: hasMany(), groups: hasMany(), + managedGroups: hasMany(), }); diff --git a/addons/api/mirage/scenarios/default.js b/addons/api/mirage/scenarios/default.js index 113dd3194..7ce918221 100644 --- a/addons/api/mirage/scenarios/default.js +++ b/addons/api/mirage/scenarios/default.js @@ -20,7 +20,7 @@ export default function (server) { // Auth const globalAuthMethods = server.createList( 'auth-method', - 1, + 5, { scope: globalScope }, 'withAccountsAndUsersAndManagedGroups' ); @@ -51,8 +51,43 @@ export default function (server) { server.createList('group', 1, { scope: globalScope }, 'withMembers'); server.createList('group', 5, { scope: orgScope }, 'withMembers'); // Role - server.createList('role', 1, { scope: globalScope }, 'withPrincipals'); - server.createList('role', 5, { scope: orgScope }, 'withPrincipals'); + const globalScopeRoles = server.createList( + 'role', + 1, + { scope: globalScope }, + 'withPrincipals' + ); + //create managed groups for role/principals in globalScope + globalScopeRoles.forEach((role) => { + const { scope } = role; + const oidcAuthMethod = globalAuthMethods.filter( + (auth) => auth.type === 'oidc' + )[0]; + const managedGroups = server.createList('managed-group', 2, { + scope, + authMethodId: oidcAuthMethod.id, + }); + role.update({ managedGroups }); + }); + + const orgScopeRoles = server.createList( + 'role', + 5, + { scope: orgScope }, + 'withPrincipals' + ); + //create managed groups for role/principals in orgScope + orgScopeRoles.forEach((role) => { + const { scope } = role; + const oidcAuthMethod = orgAuthMethods.filter( + (auth) => auth.type === 'oidc' + )[0]; + const managedGroups = server.createList('managed-group', 2, { + scope, + authMethodId: oidcAuthMethod.id, + }); + role.update({ managedGroups }); + }); // Other resources server.schema.scopes.where({ type: 'project' }).models.forEach((scope) => { diff --git a/addons/api/mirage/serializers/role.js b/addons/api/mirage/serializers/role.js index db04fe328..14b8d82ac 100644 --- a/addons/api/mirage/serializers/role.js +++ b/addons/api/mirage/serializers/role.js @@ -18,7 +18,19 @@ export default ApplicationSerializer.extend({ id: group.id, type: 'group', })); - json.principals = [...serializedUsers, ...serializedGroups]; + const serializedManagedGroups = model.managedGroups.models.map( + (managedGroup) => ({ + scope_id: model.scope.id, + id: managedGroup.id, + type: 'managed group', + }) + ); + json.principals = [ + ...serializedUsers, + ...serializedGroups, + ...serializedManagedGroups, + ]; + if (!json.principals.length) delete json.principals; // default grant scope diff --git a/addons/core/translations/resources/en-us.yaml b/addons/core/translations/resources/en-us.yaml index 035a8e4ae..eacbb9877 100644 --- a/addons/core/translations/resources/en-us.yaml +++ b/addons/core/translations/resources/en-us.yaml @@ -271,6 +271,7 @@ role: types: user: User group: Group + managed-group: Managed Group actions: add-principals: Add Principals grant: diff --git a/ui/admin/tests/acceptance/roles/principals-test.js b/ui/admin/tests/acceptance/roles/principals-test.js index 5a6ad8b00..9d6c88b86 100644 --- a/ui/admin/tests/acceptance/roles/principals-test.js +++ b/ui/admin/tests/acceptance/roles/principals-test.js @@ -46,7 +46,8 @@ module('Acceptance | roles | principals', function (hooks) { ); principalsCount = this.server.db.roles[0].userIds.length + - this.server.db.roles[0].groupIds.length; + this.server.db.roles[0].groupIds.length + + this.server.db.roles[0].managedGroupIds.length; urls.roles = `/scopes/${instances.scopes.org.id}/roles`; urls.role = `${urls.roles}/${instances.role.id}`; urls.rolePrincipals = `${urls.role}/principals`; @@ -111,7 +112,7 @@ module('Acceptance | roles | principals', function (hooks) { test('select and save principals to add', async function (assert) { assert.expect(3); - instances.role.update({ userIds: [], groupIds: [] }); + instances.role.update({ userIds: [], groupIds: [], managedGroupIds: [] }); await visit(urls.rolePrincipals); assert.strictEqual(findAll('tbody tr').length, 0); await click('.rose-layout-page-actions a'); @@ -154,7 +155,7 @@ module('Acceptance | roles | principals', function (hooks) { } ); }); - instances.role.update({ userIds: [], groupIds: [] }); + instances.role.update({ userIds: [], groupIds: [], managedGroupIds: [] }); await visit(urls.addPrincipals); await click('tbody label'); await click('form [type="submit"]');