|
| 1 | +import { Logger } from '@n8n/backend-common'; |
| 2 | +import { mockInstance } from '@n8n/backend-test-utils'; |
| 3 | +import type { IExecutionDb } from '@n8n/db'; |
| 4 | +import type { ExecutionStatus, WorkflowExecuteMode } from 'n8n-workflow'; |
| 5 | + |
| 6 | +import { |
| 7 | + ExecutionRedactionService, |
| 8 | + type ExecutionRedactionOptions, |
| 9 | +} from '../execution-redaction.service'; |
| 10 | + |
| 11 | +describe('ExecutionRedactionService', () => { |
| 12 | + const logger = mockInstance(Logger); |
| 13 | + let service: ExecutionRedactionService; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + service = new ExecutionRedactionService(logger); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('processExecution', () => { |
| 20 | + const createMockExecution = (): IExecutionDb => { |
| 21 | + // @ts-expect-error - Partial mock data for testing |
| 22 | + return { |
| 23 | + id: 'execution-123', |
| 24 | + mode: 'manual' as WorkflowExecuteMode, |
| 25 | + createdAt: new Date('2024-01-01'), |
| 26 | + startedAt: new Date('2024-01-01'), |
| 27 | + stoppedAt: new Date('2024-01-01'), |
| 28 | + workflowId: 'workflow-123', |
| 29 | + finished: true, |
| 30 | + retryOf: undefined, |
| 31 | + retrySuccessId: undefined, |
| 32 | + status: 'success' as ExecutionStatus, |
| 33 | + waitTill: null, |
| 34 | + storedAt: 'db', |
| 35 | + data: { |
| 36 | + version: 1, |
| 37 | + resultData: { |
| 38 | + runData: {}, |
| 39 | + }, |
| 40 | + executionData: { |
| 41 | + contextData: {}, |
| 42 | + nodeExecutionStack: [], |
| 43 | + metadata: {}, |
| 44 | + waitingExecution: {}, |
| 45 | + waitingExecutionSource: null, |
| 46 | + }, |
| 47 | + }, |
| 48 | + workflowData: { |
| 49 | + id: 'workflow-123', |
| 50 | + name: 'Test Workflow', |
| 51 | + active: false, |
| 52 | + isArchived: false, |
| 53 | + createdAt: new Date('2024-01-01'), |
| 54 | + updatedAt: new Date('2024-01-01'), |
| 55 | + nodes: [], |
| 56 | + connections: {}, |
| 57 | + settings: {}, |
| 58 | + staticData: {}, |
| 59 | + activeVersionId: null, |
| 60 | + }, |
| 61 | + } as IExecutionDb; |
| 62 | + }; |
| 63 | + |
| 64 | + it('should return unmodified execution when no options provided', async () => { |
| 65 | + const execution = createMockExecution(); |
| 66 | + |
| 67 | + const result = await service.processExecution(execution); |
| 68 | + |
| 69 | + expect(result).toBe(execution); |
| 70 | + expect(result).toEqual(execution); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return unmodified execution when applyRedaction is false', async () => { |
| 74 | + const execution = createMockExecution(); |
| 75 | + const options: ExecutionRedactionOptions = { |
| 76 | + applyRedaction: false, |
| 77 | + }; |
| 78 | + |
| 79 | + const result = await service.processExecution(execution, options); |
| 80 | + |
| 81 | + expect(result).toBe(execution); |
| 82 | + expect(result).toEqual(execution); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return unmodified execution when applyRedaction is true (stub behavior)', async () => { |
| 86 | + const execution = createMockExecution(); |
| 87 | + const options: ExecutionRedactionOptions = { |
| 88 | + applyRedaction: true, |
| 89 | + }; |
| 90 | + |
| 91 | + const result = await service.processExecution(execution, options); |
| 92 | + |
| 93 | + // Stub implementation should return unmodified execution |
| 94 | + expect(result).toBe(execution); |
| 95 | + expect(result).toEqual(execution); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return unmodified execution with context options', async () => { |
| 99 | + const execution = createMockExecution(); |
| 100 | + const options: ExecutionRedactionOptions = { |
| 101 | + applyRedaction: true, |
| 102 | + context: { |
| 103 | + userId: 'user-123', |
| 104 | + projectId: 'project-123', |
| 105 | + }, |
| 106 | + }; |
| 107 | + |
| 108 | + const result = await service.processExecution(execution, options); |
| 109 | + |
| 110 | + expect(result).toBe(execution); |
| 111 | + expect(result).toEqual(execution); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should log debug message when processing execution', async () => { |
| 115 | + const execution = createMockExecution(); |
| 116 | + const options: ExecutionRedactionOptions = { |
| 117 | + applyRedaction: true, |
| 118 | + }; |
| 119 | + |
| 120 | + await service.processExecution(execution, options); |
| 121 | + |
| 122 | + expect(logger.debug).toHaveBeenCalledWith('Processing execution for redaction', { |
| 123 | + executionId: execution.id, |
| 124 | + options, |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('canUserReveal', () => { |
| 130 | + it('should return false (stub behavior)', async () => { |
| 131 | + const userId = 'user-123'; |
| 132 | + const executionId = 'execution-123'; |
| 133 | + |
| 134 | + const result = await service.canUserReveal(userId, executionId); |
| 135 | + |
| 136 | + expect(result).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should log debug message when checking reveal permissions', async () => { |
| 140 | + const userId = 'user-123'; |
| 141 | + const executionId = 'execution-123'; |
| 142 | + |
| 143 | + await service.canUserReveal(userId, executionId); |
| 144 | + |
| 145 | + expect(logger.debug).toHaveBeenCalledWith('Checking reveal permissions', { |
| 146 | + userId, |
| 147 | + executionId, |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should return false for different user and execution combinations', async () => { |
| 152 | + const result1 = await service.canUserReveal('user-1', 'execution-1'); |
| 153 | + const result2 = await service.canUserReveal('user-2', 'execution-2'); |
| 154 | + |
| 155 | + expect(result1).toBe(false); |
| 156 | + expect(result2).toBe(false); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments