|
| 1 | +import { DeploymentConfig, SecurityConfig } from '@n8n/config'; |
| 2 | +import { Container } from '@n8n/di'; |
| 3 | +import { mock } from 'jest-mock-extended'; |
| 4 | +import type { IExecuteFunctions } from 'n8n-workflow'; |
| 5 | +import type { SimpleGit } from 'simple-git'; |
| 6 | +import simpleGit from 'simple-git'; |
| 7 | + |
| 8 | +import { Git } from '../Git.node'; |
| 9 | + |
| 10 | +const mockGit = { |
| 11 | + log: jest.fn(), |
| 12 | + env: jest.fn().mockReturnThis(), |
| 13 | +}; |
| 14 | + |
| 15 | +jest.mock('simple-git'); |
| 16 | +const mockSimpleGit = simpleGit as jest.MockedFunction<typeof simpleGit>; |
| 17 | +mockSimpleGit.mockReturnValue(mockGit as unknown as SimpleGit); |
| 18 | + |
| 19 | +describe('Git Node', () => { |
| 20 | + let gitNode: Git; |
| 21 | + let executeFunctions: jest.Mocked<IExecuteFunctions>; |
| 22 | + let deploymentConfig: jest.Mocked<DeploymentConfig>; |
| 23 | + let securityConfig: jest.Mocked<SecurityConfig>; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + |
| 28 | + deploymentConfig = mock<DeploymentConfig>({ |
| 29 | + type: 'default', |
| 30 | + }); |
| 31 | + securityConfig = mock<SecurityConfig>({ |
| 32 | + disableBareRepos: false, |
| 33 | + }); |
| 34 | + Container.set(DeploymentConfig, deploymentConfig); |
| 35 | + Container.set(SecurityConfig, securityConfig); |
| 36 | + |
| 37 | + executeFunctions = mock<IExecuteFunctions>({ |
| 38 | + getInputData: jest.fn().mockReturnValue([{ json: {} }]), |
| 39 | + getNodeParameter: jest.fn(), |
| 40 | + helpers: { |
| 41 | + returnJsonArray: jest |
| 42 | + .fn() |
| 43 | + .mockImplementation((data: unknown[]) => data.map((item: unknown) => ({ json: item }))), |
| 44 | + }, |
| 45 | + }); |
| 46 | + executeFunctions.getNodeParameter.mockImplementation((name: string) => { |
| 47 | + switch (name) { |
| 48 | + case 'operation': |
| 49 | + return 'log'; |
| 50 | + case 'repositoryPath': |
| 51 | + return '/tmp/test-repo'; |
| 52 | + case 'options': |
| 53 | + return {}; |
| 54 | + default: |
| 55 | + return ''; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + mockGit.log.mockResolvedValue({ all: [] }); |
| 60 | + |
| 61 | + gitNode = new Git(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('Bare Repository Configuration', () => { |
| 65 | + it('should add safe.bareRepository=explicit when deployment type is cloud', async () => { |
| 66 | + deploymentConfig.type = 'cloud'; |
| 67 | + securityConfig.disableBareRepos = false; |
| 68 | + |
| 69 | + await gitNode.execute.call(executeFunctions); |
| 70 | + |
| 71 | + expect(mockSimpleGit).toHaveBeenCalledWith( |
| 72 | + expect.objectContaining({ |
| 73 | + config: ['safe.bareRepository=explicit'], |
| 74 | + }), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should add safe.bareRepository=explicit when disableBareRepos is true', async () => { |
| 79 | + deploymentConfig.type = 'default'; |
| 80 | + securityConfig.disableBareRepos = true; |
| 81 | + |
| 82 | + await gitNode.execute.call(executeFunctions); |
| 83 | + |
| 84 | + expect(mockSimpleGit).toHaveBeenCalledWith( |
| 85 | + expect.objectContaining({ |
| 86 | + config: ['safe.bareRepository=explicit'], |
| 87 | + }), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should add safe.bareRepository=explicit when both cloud and disableBareRepos are true', async () => { |
| 92 | + deploymentConfig.type = 'cloud'; |
| 93 | + securityConfig.disableBareRepos = true; |
| 94 | + |
| 95 | + await gitNode.execute.call(executeFunctions); |
| 96 | + |
| 97 | + expect(mockSimpleGit).toHaveBeenCalledWith( |
| 98 | + expect.objectContaining({ |
| 99 | + config: ['safe.bareRepository=explicit'], |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not add safe.bareRepository=explicit when neither cloud nor disableBareRepos is true', async () => { |
| 105 | + deploymentConfig.type = 'default'; |
| 106 | + securityConfig.disableBareRepos = false; |
| 107 | + |
| 108 | + await gitNode.execute.call(executeFunctions); |
| 109 | + |
| 110 | + expect(mockSimpleGit).toHaveBeenCalledWith( |
| 111 | + expect.objectContaining({ |
| 112 | + config: [], |
| 113 | + }), |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments