forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.test.ts
More file actions
178 lines (149 loc) · 5.61 KB
/
Copy pathproxy.test.ts
File metadata and controls
178 lines (149 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Copyright 2026 GitProxy Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import http from 'http';
import https from 'https';
import { describe, it, beforeEach, afterEach, expect, vi, Mock } from 'vitest';
import fs from 'fs';
import { GitProxyConfig } from '../src/config/generated/config';
import { Proxy } from '../src/proxy';
import { handleErrorAndLog } from '../src/utils/errors';
describe('Proxy Module TLS Certificate Loading', () => {
let proxyModule: Proxy;
let mockConfig: any;
let mockHttpServer: any;
let mockHttpsServer: any;
beforeEach(async () => {
vi.resetModules();
mockConfig = {
getCommitConfig: vi.fn(),
getTLSEnabled: vi.fn(),
getTLSKeyPemPath: vi.fn(),
getTLSCertPemPath: vi.fn(),
getPlugins: vi.fn().mockReturnValue([]),
getAuthorisedList: vi.fn().mockReturnValue([]),
};
const mockDb = {
getRepos: vi.fn().mockResolvedValue([]),
createRepo: vi.fn().mockResolvedValue(undefined),
addUserCanPush: vi.fn().mockResolvedValue(undefined),
addUserCanAuthorise: vi.fn().mockResolvedValue(undefined),
};
const mockPluginLoader = {
load: vi.fn().mockResolvedValue(undefined),
};
mockHttpServer = {
listen: vi.fn().mockImplementation((_port, cb) => {
if (cb) cb();
return mockHttpServer;
}),
close: vi.fn().mockImplementation((cb) => {
if (cb) cb();
}),
on: vi.fn().mockReturnThis(),
};
mockHttpsServer = {
listen: vi.fn().mockImplementation((_port, cb) => {
if (cb) cb();
return mockHttpsServer;
}),
close: vi.fn().mockImplementation((cb) => {
if (cb) cb();
}),
on: vi.fn().mockReturnThis(),
};
vi.doMock('../src/plugin', () => {
return {
PluginLoader: vi.fn(() => mockPluginLoader),
};
});
vi.doMock('../src/config', async (importOriginal) => {
const actual = await importOriginal<GitProxyConfig>();
return {
...actual,
getTLSEnabled: mockConfig.getTLSEnabled,
getTLSKeyPemPath: mockConfig.getTLSKeyPemPath,
getTLSCertPemPath: mockConfig.getTLSCertPemPath,
getPlugins: mockConfig.getPlugins,
getAuthorisedList: mockConfig.getAuthorisedList,
};
});
vi.doMock('../src/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/db')>();
return {
...actual,
getRepos: mockDb.getRepos,
createRepo: mockDb.createRepo,
addUserCanPush: mockDb.addUserCanPush,
addUserCanAuthorise: mockDb.addUserCanAuthorise,
getAllProxiedHosts: vi.fn().mockResolvedValue([]),
};
});
vi.doMock('../src/proxy/chain', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/proxy/chain')>();
return {
...actual,
chainPluginLoader: null,
};
});
vi.spyOn(http, 'createServer').mockReturnValue(mockHttpServer);
vi.spyOn(https, 'createServer').mockReturnValue(mockHttpsServer);
const ProxyClass = (await import('../src/proxy/index')).Proxy;
proxyModule = new ProxyClass();
});
afterEach(async () => {
try {
await proxyModule.stop();
} catch (error: unknown) {
handleErrorAndLog(error, 'Error occurred when stopping the proxy');
}
vi.restoreAllMocks();
});
describe('TLS certificate file reading', () => {
it('should read TLS key and cert files when TLS is enabled and paths are provided', async () => {
const mockKeyContent = Buffer.from('mock-key-content');
const mockCertContent = Buffer.from('mock-cert-content');
mockConfig.getTLSEnabled.mockReturnValue(true);
mockConfig.getTLSKeyPemPath.mockReturnValue('/path/to/key.pem');
mockConfig.getTLSCertPemPath.mockReturnValue('/path/to/cert.pem');
const fsStub = vi.spyOn(fs, 'readFileSync');
fsStub.mockReturnValue(Buffer.from('default-cert'));
fsStub.mockImplementation((path: any) => {
if (path === '/path/to/key.pem') return mockKeyContent;
if (path === '/path/to/cert.pem') return mockCertContent;
return Buffer.from('default-cert');
});
await proxyModule.start();
expect(fsStub).toHaveBeenCalledWith('/path/to/key.pem');
expect(fsStub).toHaveBeenCalledWith('/path/to/cert.pem');
});
it('should not read TLS files when TLS is disabled', async () => {
mockConfig.getTLSEnabled.mockReturnValue(false);
mockConfig.getTLSKeyPemPath.mockReturnValue('/path/to/key.pem');
mockConfig.getTLSCertPemPath.mockReturnValue('/path/to/cert.pem');
const fsStub = vi.spyOn(fs, 'readFileSync');
await proxyModule.start();
expect(fsStub).not.toHaveBeenCalled();
});
it('should not read TLS files when paths are not provided', async () => {
mockConfig.getTLSEnabled.mockReturnValue(true);
mockConfig.getTLSKeyPemPath.mockReturnValue(null);
mockConfig.getTLSCertPemPath.mockReturnValue(null);
const fsStub = vi.spyOn(fs, 'readFileSync');
await proxyModule.start();
expect(fsStub).not.toHaveBeenCalled();
});
});
});