Add mirage support principal managed groups (#1542)

* chore: 🤖 mocks for principals/managed-groups & roles
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2022-12-22 10:23:49 -08:00
1 parent 1006680060
commit 392da009d0
7 files changed
+90 -13

No files matched your search

+28 -5
View File
@@ -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');
+5 -1
View File
@@ -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 });
},
}),
+1
View File
@@ -5,4 +5,5 @@ export default Model.extend({
scope: belongsTo(),
users: hasMany(),
groups: hasMany(),
managedGroups: hasMany(),
});
+38 -3
View File
@@ -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) => {
+13 -1
View File
@@ -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
@@ -271,6 +271,7 @@ role:
types:
user: User
group: Group
managed-group: Managed Group
actions:
add-principals: Add Principals
grant:
@@ -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"]');