mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-22 04:10:01 +00:00
refactor(core): Cache workflow ownership (#6738)
* refactor: Set up ownership service * refactor: Specify cache keys and values * refactor: Replace util with service calls * test: Mock service in tests * refactor: Use dependency injection * test: Write tests * refactor: Apply feedback from Omar and Micha * test: Fix tests * test: Fix missing spot * refactor: Return user entity from cache * refactor: More dependency injection!
This commit is contained in:
@@ -23,6 +23,7 @@ import * as testDb from '../integration/shared/testDb';
|
||||
import { mockNodeTypesData } from './Helpers';
|
||||
import type { SaveCredentialFunction } from '../integration/shared/types';
|
||||
import { mockInstance } from '../integration/shared/utils/';
|
||||
import { OwnershipService } from '@/services/ownership.service';
|
||||
|
||||
let mockNodeTypes: INodeTypes;
|
||||
let credentialOwnerRole: Role;
|
||||
@@ -221,6 +222,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
const userId = uuid();
|
||||
const fakeUser = new User();
|
||||
fakeUser.id = userId;
|
||||
const ownershipService = mockInstance(OwnershipService);
|
||||
|
||||
const ownerMockRole = new Role();
|
||||
ownerMockRole.name = 'owner';
|
||||
@@ -234,9 +236,11 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
|
||||
test('sets default policy from environment when subworkflow has none', async () => {
|
||||
config.set('workflows.callerPolicyDefaultOption', 'none');
|
||||
jest.spyOn(UserManagementHelper, 'getWorkflowOwner').mockImplementation(async (workflowId) => {
|
||||
return fakeUser;
|
||||
});
|
||||
jest
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => {
|
||||
return fakeUser;
|
||||
});
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(true);
|
||||
|
||||
const subworkflow = new Workflow({
|
||||
@@ -253,7 +257,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
|
||||
test('if sharing is disabled, ensures that workflows are owner by same user', async () => {
|
||||
jest
|
||||
.spyOn(UserManagementHelper, 'getWorkflowOwner')
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => fakeUser);
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(false);
|
||||
jest.spyOn(UserService, 'get').mockImplementation(async () => fakeUser);
|
||||
@@ -287,7 +291,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
test('list of ids must include the parent workflow id', async () => {
|
||||
const invalidParentWorkflowId = uuid();
|
||||
jest
|
||||
.spyOn(UserManagementHelper, 'getWorkflowOwner')
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => fakeUser);
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(true);
|
||||
jest.spyOn(UserService, 'get').mockImplementation(async () => fakeUser);
|
||||
@@ -313,7 +317,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
|
||||
test('sameOwner passes when both workflows are owned by the same user', async () => {
|
||||
jest
|
||||
.spyOn(UserManagementHelper, 'getWorkflowOwner')
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => fakeUser);
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(false);
|
||||
jest.spyOn(UserService, 'get').mockImplementation(async () => fakeUser);
|
||||
@@ -336,7 +340,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
test('workflowsFromAList works when the list contains the parent id', async () => {
|
||||
const workflowId = uuid();
|
||||
jest
|
||||
.spyOn(UserManagementHelper, 'getWorkflowOwner')
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => fakeUser);
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(true);
|
||||
jest.spyOn(UserService, 'get').mockImplementation(async () => fakeUser);
|
||||
@@ -362,7 +366,7 @@ describe('PermissionChecker.checkSubworkflowExecutePolicy', () => {
|
||||
|
||||
test('should not throw when workflow policy is set to any', async () => {
|
||||
jest
|
||||
.spyOn(UserManagementHelper, 'getWorkflowOwner')
|
||||
.spyOn(ownershipService, 'getWorkflowOwnerCached')
|
||||
.mockImplementation(async (workflowId) => fakeUser);
|
||||
jest.spyOn(UserManagementHelper, 'isSharingEnabled').mockReturnValue(true);
|
||||
jest.spyOn(UserService, 'get').mockImplementation(async () => fakeUser);
|
||||
|
||||
Reference in New Issue
Block a user