Skip to content

Commit

Permalink
fix: Prevent deletion of built in roles
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Jan 14, 2022
1 parent c1826ca commit bfcad65
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/lib/services/access-service.ts
Expand Up @@ -427,6 +427,8 @@ export class AccessService {
}

async deleteRole(id: number): Promise<void> {
await this.validateRoleIsNotBuiltIn(id);

const roleUsers = await this.getUsersForRole(id);

if (roleUsers.length > 0) {
Expand Down Expand Up @@ -455,7 +457,7 @@ export class AccessService {
const role = await this.store.get(roleId);
if (role.type !== CUSTOM_ROLE_TYPE) {
throw new InvalidOperationError(
'You can not change built in roles.',
'You cannot change built in roles.',
);
}
}
Expand Down
53 changes: 50 additions & 3 deletions src/test/e2e/services/access-service.e2e.test.ts
Expand Up @@ -757,21 +757,68 @@ test('Should be allowed move feature toggle to project when the user has access'
);
});

test('Should not be allowed to edit a built in role', async () => {
test('Should not be allowed to edit a root role', async () => {
expect.assertions(1);

const editRole = await accessService.getRoleByName(RoleName.EDITOR);
const roleUpdate = {
id: editRole.id,
name: 'NoLongerTheEditor',
description: 'Ha!',
description: '',
};

try {
await accessService.updateRole(roleUpdate);
} catch (e) {
expect(e.toString()).toBe(
'InvalidOperationError: You can not change built in roles.',
'InvalidOperationError: You cannot change built in roles.',
);
}
});

test('Should not be allowed to delete a root role', async () => {
expect.assertions(1);

const editRole = await accessService.getRoleByName(RoleName.EDITOR);

try {
await accessService.deleteRole(editRole.id);
} catch (e) {
expect(e.toString()).toBe(
'InvalidOperationError: You cannot change built in roles.',
);
}
});

test('Should not be allowed to edit a project role', async () => {
expect.assertions(1);

const ownerRole = await accessService.getRoleByName(RoleName.OWNER);
const roleUpdate = {
id: ownerRole.id,
name: 'NoLongerTheEditor',
description: '',
};

try {
await accessService.updateRole(roleUpdate);
} catch (e) {
expect(e.toString()).toBe(
'InvalidOperationError: You cannot change built in roles.',
);
}
});

test('Should not be allowed to delete a project role', async () => {
expect.assertions(1);

const ownerRole = await accessService.getRoleByName(RoleName.OWNER);

try {
await accessService.deleteRole(ownerRole.id);
} catch (e) {
expect(e.toString()).toBe(
'InvalidOperationError: You cannot change built in roles.',
);
}
});

0 comments on commit bfcad65

Please sign in to comment.