mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
fix(core): Fix role management controller no-changelog (#19107)
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import type { CreateRoleDto, UpdateRoleDto } from '@n8n/api-types';
|
||||
import { testDb } from '@n8n/backend-test-utils';
|
||||
|
||||
import { cleanupRolesAndScopes } from '../shared/db/roles';
|
||||
import { createMember, createOwner } from '../shared/db/users';
|
||||
import type { SuperAgentTest } from '../shared/types';
|
||||
import { setupTestServer } from '../shared/utils';
|
||||
import {
|
||||
PROJECT_ADMIN_ROLE,
|
||||
PROJECT_EDITOR_ROLE,
|
||||
PROJECT_OWNER_ROLE,
|
||||
PROJECT_VIEWER_ROLE,
|
||||
} from '@n8n/db';
|
||||
|
||||
describe('RoleController - Integration Tests', () => {
|
||||
const testServer = setupTestServer({ endpointGroups: ['role'] });
|
||||
let ownerAgent: SuperAgentTest;
|
||||
let memberAgent: SuperAgentTest;
|
||||
|
||||
beforeAll(async () => {
|
||||
await testDb.init();
|
||||
const owner = await createOwner();
|
||||
const member = await createMember();
|
||||
ownerAgent = testServer.authAgentFor(owner);
|
||||
memberAgent = testServer.authAgentFor(member);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// Enable CUSTOM_ROLES license for all tests by default
|
||||
testServer.license.enable('feat:customRoles');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await cleanupRolesAndScopes();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await testDb.terminate();
|
||||
});
|
||||
|
||||
it.each([PROJECT_ADMIN_ROLE, PROJECT_EDITOR_ROLE, PROJECT_VIEWER_ROLE, PROJECT_OWNER_ROLE])(
|
||||
'should return 200 and the role data for role $slug',
|
||||
async (role) => {
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
const response = await memberAgent.get(`/roles/${role.slug}`).expect(200);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
response.body.data.scopes.sort();
|
||||
expect(response.body).toEqual({
|
||||
data: {
|
||||
slug: role.slug,
|
||||
displayName: role.displayName,
|
||||
description: role.description,
|
||||
systemRole: role.systemRole,
|
||||
roleType: role.roleType,
|
||||
scopes: role.scopes.map((scope) => scope.slug).sort(),
|
||||
licensed: expect.any(Boolean),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it('should create a custom role', async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
const createRoleDto: CreateRoleDto = {
|
||||
displayName: 'Custom Project Role',
|
||||
description: 'A custom role for project management',
|
||||
roleType: 'project',
|
||||
scopes: ['workflow:read', 'workflow:create'].sort(),
|
||||
};
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
const response = await ownerAgent.post('/roles').send(createRoleDto).expect(200);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
response.body.data.scopes.sort();
|
||||
expect(response.body).toEqual({
|
||||
data: {
|
||||
...createRoleDto,
|
||||
slug: expect.any(String),
|
||||
licensed: expect.any(Boolean),
|
||||
systemRole: false,
|
||||
},
|
||||
});
|
||||
|
||||
const availableRole = await memberAgent.get(`/roles/${response.body.data.slug}`).expect(200);
|
||||
|
||||
availableRole.body.data.scopes.sort();
|
||||
expect(availableRole.body).toEqual({
|
||||
data: {
|
||||
...createRoleDto,
|
||||
slug: response.body.data.slug,
|
||||
licensed: expect.any(Boolean),
|
||||
systemRole: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should update a custom role', async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
const createRoleDto: CreateRoleDto = {
|
||||
displayName: 'Custom Project Role',
|
||||
description: 'A custom role for project management',
|
||||
roleType: 'project',
|
||||
scopes: ['workflow:read', 'workflow:create'].sort(),
|
||||
};
|
||||
|
||||
const createResponse = await ownerAgent.post('/roles').send(createRoleDto).expect(200);
|
||||
|
||||
expect(createResponse.body?.data?.slug).toBeDefined();
|
||||
const generatedRoleSlug = createResponse.body.data.slug;
|
||||
|
||||
const updateRoleDto: UpdateRoleDto = {
|
||||
displayName: 'Custom Project Role Updated',
|
||||
description: 'A custom role for project management - updated',
|
||||
};
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
const response = await ownerAgent
|
||||
.patch(`/roles/${generatedRoleSlug}`)
|
||||
.send(updateRoleDto)
|
||||
.expect(200);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
response.body.data.scopes.sort();
|
||||
expect(response.body).toEqual({
|
||||
data: {
|
||||
...updateRoleDto,
|
||||
scopes: ['workflow:read', 'workflow:create'].sort(),
|
||||
slug: generatedRoleSlug,
|
||||
roleType: 'project',
|
||||
licensed: expect.any(Boolean),
|
||||
systemRole: false,
|
||||
},
|
||||
});
|
||||
|
||||
const availableRole = await memberAgent.get(`/roles/${response.body.data.slug}`).expect(200);
|
||||
|
||||
availableRole.body.data.scopes.sort();
|
||||
expect(availableRole.body).toEqual({
|
||||
data: {
|
||||
...updateRoleDto,
|
||||
scopes: ['workflow:read', 'workflow:create'].sort(),
|
||||
slug: generatedRoleSlug,
|
||||
roleType: 'project',
|
||||
licensed: expect.any(Boolean),
|
||||
systemRole: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CreateRoleDto, UpdateRoleDto } from '@n8n/api-types';
|
||||
import { mockInstance } from '@n8n/backend-test-utils';
|
||||
import type { Role } from '@n8n/permissions';
|
||||
import { type Role } from '@n8n/permissions';
|
||||
|
||||
import { RoleService } from '@/services/role.service';
|
||||
|
||||
@@ -314,7 +314,7 @@ describe('RoleController', () => {
|
||||
//
|
||||
expect(response.body).toEqual({ data: mockCreatedRole });
|
||||
// Parameter verification skipped - test framework issue
|
||||
expect(roleService.createCustomRole).toHaveBeenCalledTimes(1);
|
||||
expect(roleService.createCustomRole).toHaveBeenCalledWith(createRoleDto);
|
||||
});
|
||||
|
||||
it('should create role without description', async () => {
|
||||
@@ -348,6 +348,7 @@ describe('RoleController', () => {
|
||||
// ASSERT
|
||||
//
|
||||
expect(response.body).toEqual({ data: mockCreatedRole });
|
||||
expect(roleService.createCustomRole).toHaveBeenCalledWith(createRoleDto);
|
||||
});
|
||||
|
||||
it('should handle service errors gracefully', async () => {
|
||||
@@ -398,7 +399,7 @@ describe('RoleController', () => {
|
||||
const updateRoleDto: UpdateRoleDto = {
|
||||
displayName: 'Updated Role Name',
|
||||
description: 'Updated description',
|
||||
scopes: ['workflow:read', 'workflow:edit'],
|
||||
scopes: ['workflow:read', 'workflow:update'],
|
||||
};
|
||||
|
||||
const mockUpdatedRole: Role = {
|
||||
@@ -423,7 +424,7 @@ describe('RoleController', () => {
|
||||
//
|
||||
expect(response.body).toEqual({ data: mockUpdatedRole });
|
||||
// Parameter verification skipped - test framework issue
|
||||
expect(roleService.updateCustomRole).toHaveBeenCalledTimes(1);
|
||||
expect(roleService.updateCustomRole).toHaveBeenCalledWith(roleSlug, updateRoleDto);
|
||||
});
|
||||
|
||||
it('should update only provided fields', async () => {
|
||||
@@ -456,6 +457,7 @@ describe('RoleController', () => {
|
||||
// ASSERT
|
||||
//
|
||||
expect(response.body).toEqual({ data: mockUpdatedRole });
|
||||
expect(roleService.updateCustomRole).toHaveBeenCalledWith(roleSlug, updateRoleDto);
|
||||
});
|
||||
|
||||
it('should handle service errors gracefully', async () => {
|
||||
|
||||
Reference in New Issue
Block a user