Skip to content

Commit

Permalink
Ensure global roles are listed in a consistent order (#11306)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwmac authored Jul 1, 2024
1 parent 6fa6079 commit ba76ad7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cypress/e2e/po/components/global-role-binding.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ export default class GlobalRoleBindings extends ComponentPo {
roleCheckbox(roleId: string) {
return new CheckboxInputPo(`[data-testid="grb-checkbox-${ roleId }"]`);
}

globalOptions() {
return this.self().find('.checkbox-section--global .checkbox-label-slot .checkbox-label').then((els) => {
return Cypress.$.makeArray(els).map((el) => el.innerText);
});
}
}
30 changes: 30 additions & 0 deletions cypress/e2e/tests/pages/users-and-auth/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ describe('Users', { tags: ['@usersAndAuths', '@adminUser'] }, () => {
});
});

it('shows global roles in specific order', () => {
// Intercept roles request and change the order
cy.intercept('GET', `/v1/management.cattle.io.globalroles?*`, (req) => {
req.continue((res) => {
// Move the Administrator role to the end of the list
const adminIndex = res.body.data.findIndex((item) => item.id === 'admin');
const adminRole = res.body.data[adminIndex];

res.body.data.splice(adminIndex, 1);
res.body.data.push(adminRole);

res.send(res.body);
});
});

usersPo.goTo();
usersPo.list().create();
userCreate.waitForPage();

const mgmtUserEditPo = new MgmtUserEditPo();

mgmtUserEditPo.globalRoleBindings().globalOptions().then((list) => {
expect(list.length).to.eq(4);
expect(list[0]).to.eq('Administrator');
expect(list[1]).to.eq('Restricted Administrator');
expect(list[2]).to.eq('Standard User');
expect(list[3]).to.eq('User-Base');
});
});

it('can Refresh Group Memberships', () => {
// Refresh Group Membership and verify request is made
usersPo.goTo();
Expand Down
22 changes: 22 additions & 0 deletions shell/components/GlobalRoleBindings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,41 @@ export default {
const sort = (a, b) => a.nameDisplay.localeCompare(b.nameDisplay);
// global roles are not sorted
this.sortedRoles.builtin = this.sortedRoles.builtin.sort(sort);
this.sortedRoles.custom = this.sortedRoles.custom.sort(sort);
if (!this.isCreate) {
this.globalRoleBindings = await this.$store.dispatch('management/findAll', { type: MANAGEMENT.GLOBAL_ROLE_BINDING, opt: { force: true } });
}
// Sort the global roles - use the order defined in 'globalPermissions' and then add the remaining roles after
const globalRoles = [];
const globalRolesAdded = {};
this.globalPermissions.forEach((id) => {
const role = this.sortedRoles.global.find((r) => r.id === id);
if (role) {
globalRoles.push(role);
globalRolesAdded[id] = true;
}
});
// Remaining global roles
const remainingGlobalRoles = this.sortedRoles.global.filter((r) => !globalRolesAdded[r.id]);
this.sortedRoles.global = globalRoles;
this.sortedRoles.global.push(...remainingGlobalRoles);
// End sort of global roles
this.update();
}
} catch (e) { }
},
data() {
return {
// This not only identifies global roles but the order here is the order we want to display them in the UI
globalPermissions: [
'admin',
'restricted-admin',
Expand Down

0 comments on commit ba76ad7

Please sign in to comment.