|
| 1 | +import { OrchestratorFolders } from './orchestrator-folders'; |
| 2 | + |
| 3 | +jest.mock('../orchestrator', () => ({ |
| 4 | + __esModule: true, |
| 5 | + default: { |
| 6 | + buildParameters: { |
| 7 | + orchestratorRepoName: 'game-ci/unity-builder', |
| 8 | + githubRepo: 'myorg/myrepo', |
| 9 | + gitPrivateToken: 'ghp_test123', |
| 10 | + gitAuthMode: 'header', |
| 11 | + buildGuid: 'test-guid', |
| 12 | + projectPath: '', |
| 13 | + buildPath: 'Builds', |
| 14 | + cacheKey: 'test-cache', |
| 15 | + }, |
| 16 | + lockedWorkspace: '', |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('./orchestrator-options', () => ({ |
| 21 | + __esModule: true, |
| 22 | + default: { |
| 23 | + useSharedBuilder: false, |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('../services/core/orchestrator-system', () => ({ |
| 28 | + OrchestratorSystem: { |
| 29 | + Run: jest.fn().mockResolvedValue(''), |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +const mockOrchestrator = require('../orchestrator').default; |
| 34 | + |
| 35 | +describe('OrchestratorFolders git auth', () => { |
| 36 | + beforeEach(() => { |
| 37 | + jest.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('useHeaderAuth', () => { |
| 41 | + it('should return true when gitAuthMode is header', () => { |
| 42 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 43 | + expect(OrchestratorFolders.useHeaderAuth).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return true when gitAuthMode is undefined (default)', () => { |
| 47 | + mockOrchestrator.buildParameters.gitAuthMode = undefined; |
| 48 | + expect(OrchestratorFolders.useHeaderAuth).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return false when gitAuthMode is url', () => { |
| 52 | + mockOrchestrator.buildParameters.gitAuthMode = 'url'; |
| 53 | + expect(OrchestratorFolders.useHeaderAuth).toBe(false); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('unityBuilderRepoUrl', () => { |
| 58 | + it('should not include token in URL when using header auth', () => { |
| 59 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 60 | + const url = OrchestratorFolders.unityBuilderRepoUrl; |
| 61 | + expect(url).toBe('https://github.com/game-ci/unity-builder.git'); |
| 62 | + expect(url).not.toContain('ghp_test123'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should include token in URL when using url auth (legacy)', () => { |
| 66 | + mockOrchestrator.buildParameters.gitAuthMode = 'url'; |
| 67 | + const url = OrchestratorFolders.unityBuilderRepoUrl; |
| 68 | + expect(url).toBe('https://ghp_test123@github.com/game-ci/unity-builder.git'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('targetBuildRepoUrl', () => { |
| 73 | + it('should not include token in URL when using header auth', () => { |
| 74 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 75 | + const url = OrchestratorFolders.targetBuildRepoUrl; |
| 76 | + expect(url).toBe('https://github.com/myorg/myrepo.git'); |
| 77 | + expect(url).not.toContain('ghp_test123'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should include token in URL when using url auth (legacy)', () => { |
| 81 | + mockOrchestrator.buildParameters.gitAuthMode = 'url'; |
| 82 | + const url = OrchestratorFolders.targetBuildRepoUrl; |
| 83 | + expect(url).toBe('https://ghp_test123@github.com/myorg/myrepo.git'); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('gitAuthConfigScript', () => { |
| 88 | + it('should emit http.extraHeader commands in header mode', () => { |
| 89 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 90 | + const script = OrchestratorFolders.gitAuthConfigScript; |
| 91 | + expect(script).toContain('http.extraHeader'); |
| 92 | + expect(script).toContain('GIT_PRIVATE_TOKEN'); |
| 93 | + expect(script).toContain('Authorization: Basic'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should emit no-op comment in url mode', () => { |
| 97 | + mockOrchestrator.buildParameters.gitAuthMode = 'url'; |
| 98 | + const script = OrchestratorFolders.gitAuthConfigScript; |
| 99 | + expect(script).toContain('legacy'); |
| 100 | + expect(script).not.toContain('http.extraHeader'); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('configureGitAuth', () => { |
| 105 | + it('should run git config with http.extraHeader in header mode', async () => { |
| 106 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 107 | + mockOrchestrator.buildParameters.gitPrivateToken = 'ghp_test123'; |
| 108 | + const { OrchestratorSystem } = require('../services/core/orchestrator-system'); |
| 109 | + |
| 110 | + await OrchestratorFolders.configureGitAuth(); |
| 111 | + |
| 112 | + // Verify the base64 encoding and extraHeader config are correct |
| 113 | + const expectedEncoded = Buffer.from('x-access-token:ghp_test123').toString('base64'); |
| 114 | + expect(OrchestratorSystem.Run).toHaveBeenCalledWith( |
| 115 | + expect.stringContaining(expectedEncoded), |
| 116 | + ); |
| 117 | + expect(OrchestratorSystem.Run).toHaveBeenCalledWith( |
| 118 | + expect.stringContaining('.extraHeader'), |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not run git config in url mode', async () => { |
| 123 | + mockOrchestrator.buildParameters.gitAuthMode = 'url'; |
| 124 | + const { OrchestratorSystem } = require('../services/core/orchestrator-system'); |
| 125 | + |
| 126 | + await OrchestratorFolders.configureGitAuth(); |
| 127 | + |
| 128 | + expect(OrchestratorSystem.Run).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not run git config when no token is available', async () => { |
| 132 | + mockOrchestrator.buildParameters.gitAuthMode = 'header'; |
| 133 | + mockOrchestrator.buildParameters.gitPrivateToken = ''; |
| 134 | + const originalEnv = process.env.GIT_PRIVATE_TOKEN; |
| 135 | + delete process.env.GIT_PRIVATE_TOKEN; |
| 136 | + const { OrchestratorSystem } = require('../services/core/orchestrator-system'); |
| 137 | + |
| 138 | + await OrchestratorFolders.configureGitAuth(); |
| 139 | + |
| 140 | + expect(OrchestratorSystem.Run).not.toHaveBeenCalled(); |
| 141 | + if (originalEnv !== undefined) process.env.GIT_PRIVATE_TOKEN = originalEnv; |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments