chore(core): Use roles from database in global roles (#17853)

This commit is contained in:
Andreas Fitzek
2025-08-22 16:02:01 +02:00
committed by GitHub
parent 350f84c49f
commit a8e4387f4d
117 changed files with 875 additions and 410 deletions

View File

@@ -1,12 +1,14 @@
import type { SourceControlledFile } from '@n8n/api-types';
import type {
Variables,
FolderWithWorkflowAndSubFolderCount,
TagEntity,
User,
FolderRepository,
TagRepository,
WorkflowEntity,
import {
type Variables,
type FolderWithWorkflowAndSubFolderCount,
type TagEntity,
type User,
type FolderRepository,
type TagRepository,
type WorkflowEntity,
GLOBAL_MEMBER_ROLE,
GLOBAL_ADMIN_ROLE,
} from '@n8n/db';
import { Container } from '@n8n/di';
import { mock } from 'jest-mock-extended';
@@ -149,8 +151,9 @@ describe('SourceControlService', () => {
describe('getStatus', () => {
it('ensure updatedAt field for last deleted tag', async () => {
// ARRANGE
const user = mock<User>();
user.role = 'global:admin';
const user = mock<User>({
role: GLOBAL_ADMIN_ROLE,
});
sourceControlImportService.getRemoteVersionIdsFromFiles.mockResolvedValue([]);
sourceControlImportService.getLocalVersionIdsFromDb.mockResolvedValue([]);
@@ -204,8 +207,9 @@ describe('SourceControlService', () => {
it('ensure updatedAt field for last deleted folder', async () => {
// ARRANGE
const user = mock<User>();
user.role = 'global:admin';
const user = mock<User>({
role: GLOBAL_ADMIN_ROLE,
});
sourceControlImportService.getRemoteVersionIdsFromFiles.mockResolvedValue([]);
sourceControlImportService.getLocalVersionIdsFromDb.mockResolvedValue([]);
@@ -262,8 +266,9 @@ describe('SourceControlService', () => {
it('conflict depends on the value of `direction`', async () => {
// ARRANGE
const user = mock<User>();
user.role = 'global:admin';
const user = mock<User>({
role: GLOBAL_ADMIN_ROLE,
});
// Define a credential that does only exist locally.
// Pulling this would delete it so it should be marked as a conflict.
@@ -368,8 +373,9 @@ describe('SourceControlService', () => {
it('should throw `ForbiddenError` if direction is pull and user is not allowed to globally pull', async () => {
// ARRANGE
const user = mock<User>();
user.role = 'global:member';
const user = mock<User>({
role: GLOBAL_MEMBER_ROLE,
});
// ACT
await expect(
@@ -387,7 +393,7 @@ describe('SourceControlService', () => {
'should return file content for $type',
async ({ type, id, content }) => {
jest.spyOn(gitService, 'getFileContent').mockResolvedValue(content);
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
const user = mock<User>({ id: 'user-id', role: GLOBAL_ADMIN_ROLE });
const result = await sourceControlService.getRemoteFileEntity({ user, type, id });
@@ -398,7 +404,7 @@ describe('SourceControlService', () => {
it.each<SourceControlledFile['type']>(['folders', 'credential', 'tags', 'variables'])(
'should throw an error if the file type is not handled',
async (type) => {
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
const user = mock<User>({ id: 'user-id', role: { slug: 'global:admin' } });
await expect(
sourceControlService.getRemoteFileEntity({ user, type, id: 'unknown' }),
).rejects.toThrow(`Unsupported file type: ${type}`);
@@ -407,7 +413,7 @@ describe('SourceControlService', () => {
it('should fail if the git service fails to get the file content', async () => {
jest.spyOn(gitService, 'getFileContent').mockRejectedValue(new Error('Git service error'));
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
const user = mock<User>({ id: 'user-id', role: { slug: 'global:admin' } });
await expect(
sourceControlService.getRemoteFileEntity({ user, type: 'workflow', id: '1234' }),
@@ -417,7 +423,7 @@ describe('SourceControlService', () => {
it('should throw an error if the user does not have access to the project', async () => {
const user = mock<User>({
id: 'user-id',
role: 'global:member',
role: { slug: 'global:member' },
});
jest
.spyOn(sourceControlScopedService, 'getWorkflowsInAdminProjectsFromContext')
@@ -429,7 +435,7 @@ describe('SourceControlService', () => {
});
it('should return content for an authorized workflow', async () => {
const user = mock<User>({ id: 'user-id', role: 'global:member' });
const user = mock<User>({ id: 'user-id', role: { slug: 'global:member' } });
jest
.spyOn(sourceControlScopedService, 'getWorkflowsInAdminProjectsFromContext')
.mockResolvedValue([{ id: '1234' } as WorkflowEntity]);