Skip to content

Commit 3a27260

Browse files
authored
Merge pull request finos#1357 from fabiovincenzi/failing-tests
fix: enable skipped proxy tests
2 parents 4a21364 + 1d4b1ff commit 3a27260

5 files changed

Lines changed: 79 additions & 38 deletions

File tree

src/proxy/index.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,42 @@ export class Proxy {
9191
}
9292

9393
public stop(): Promise<void> {
94-
return new Promise((resolve, reject) => {
95-
try {
96-
// Close HTTP server if it exists
97-
if (this.httpServer) {
98-
this.httpServer.close(() => {
99-
console.log('HTTP server closed');
100-
this.httpServer = null;
94+
const closePromises: Promise<void>[] = [];
95+
96+
// Close HTTP server if it exists
97+
if (this.httpServer) {
98+
closePromises.push(
99+
new Promise((resolve, reject) => {
100+
this.httpServer!.close((err) => {
101+
if (err) {
102+
reject(err);
103+
} else {
104+
console.log('HTTP server closed');
105+
this.httpServer = null;
106+
resolve();
107+
}
101108
});
102-
}
109+
}),
110+
);
111+
}
103112

104-
// Close HTTPS server if it exists
105-
if (this.httpsServer) {
106-
this.httpsServer.close(() => {
107-
console.log('HTTPS server closed');
108-
this.httpsServer = null;
113+
// Close HTTPS server if it exists
114+
if (this.httpsServer) {
115+
closePromises.push(
116+
new Promise((resolve, reject) => {
117+
this.httpsServer!.close((err) => {
118+
if (err) {
119+
reject(err);
120+
} else {
121+
console.log('HTTPS server closed');
122+
this.httpsServer = null;
123+
resolve();
124+
}
109125
});
110-
}
126+
}),
127+
);
128+
}
111129

112-
resolve();
113-
} catch (error) {
114-
reject(error);
115-
}
116-
});
130+
return Promise.all(closePromises).then(() => {});
117131
}
118132
}

src/service/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const { GIT_PROXY_UI_PORT: uiPort } = serverConfig;
2020
const DEFAULT_SESSION_MAX_AGE_HOURS = 12;
2121

2222
const app: Express = express();
23-
const _httpServer = http.createServer(app);
23+
let _httpServer: http.Server | null = null;
2424

2525
/**
2626
* CORS Configuration
@@ -170,6 +170,7 @@ async function start(proxy: Proxy) {
170170

171171
const app = await createApp(proxy);
172172

173+
_httpServer = http.createServer(app);
173174
_httpServer.listen(uiPort);
174175

175176
console.log(`Service Listening on ${uiPort}`);
@@ -181,13 +182,28 @@ async function start(proxy: Proxy) {
181182
/**
182183
* Stops the proxy service.
183184
*/
184-
async function stop() {
185-
console.log(`Stopping Service Listening on ${uiPort}`);
186-
_httpServer.close();
185+
async function stop(): Promise<void> {
186+
if (!_httpServer) {
187+
return Promise.resolve();
188+
}
189+
190+
return new Promise((resolve, reject) => {
191+
console.log(`Stopping Service Listening on ${uiPort}`);
192+
_httpServer!.close((err) => {
193+
if (err) {
194+
reject(err);
195+
} else {
196+
console.log('Service stopped');
197+
resolve();
198+
}
199+
});
200+
});
187201
}
188202

189203
export const Service = {
190204
start,
191205
stop,
192-
httpServer: _httpServer,
206+
get httpServer() {
207+
return _httpServer;
208+
},
193209
};

src/service/passport/local.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import * as db from '../../db';
55

66
export const type = 'local';
77

8+
// Dynamic import to always get the current db module instance
9+
// This is necessary for test environments where modules may be reset
10+
const getDb = () => import('../../db');
11+
812
export const configure = async (passport: PassportStatic): Promise<PassportStatic> => {
913
passport.use(
1014
new LocalStrategy(
@@ -14,7 +18,8 @@ export const configure = async (passport: PassportStatic): Promise<PassportStati
1418
done: (err: any, user?: any, info?: any) => void,
1519
) => {
1620
try {
17-
const user = await db.findUser(username);
21+
const dbModule = await getDb();
22+
const user = await dbModule.findUser(username);
1823
if (!user) {
1924
return done(null, false, { message: 'Incorrect username.' });
2025
}
@@ -38,7 +43,8 @@ export const configure = async (passport: PassportStatic): Promise<PassportStati
3843

3944
passport.deserializeUser(async (username: string, done) => {
4045
try {
41-
const user = await db.findUser(username);
46+
const dbModule = await getDb();
47+
const user = await dbModule.findUser(username);
4248
done(null, user);
4349
} catch (err) {
4450
done(err, null);

test/proxy.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import http from 'http';
12
import https from 'https';
23
import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest';
34
import fs from 'fs';
@@ -15,7 +16,7 @@ import fs from 'fs';
1516
TODO: Find root cause of this error and fix it
1617
https://github.com/finos/git-proxy/issues/1294
1718
*/
18-
describe.skip('Proxy Module TLS Certificate Loading', () => {
19+
describe('Proxy Module TLS Certificate Loading', () => {
1920
let proxyModule: any;
2021
let mockConfig: any;
2122
let mockHttpServer: any;
@@ -82,12 +83,17 @@ describe.skip('Proxy Module TLS Certificate Loading', () => {
8283
};
8384
});
8485

85-
vi.doMock('../src/db', () => ({
86-
getRepos: mockDb.getRepos,
87-
createRepo: mockDb.createRepo,
88-
addUserCanPush: mockDb.addUserCanPush,
89-
addUserCanAuthorise: mockDb.addUserCanAuthorise,
90-
}));
86+
vi.doMock('../src/db', async (importOriginal) => {
87+
const actual: any = await importOriginal();
88+
return {
89+
...actual,
90+
getRepos: mockDb.getRepos,
91+
createRepo: mockDb.createRepo,
92+
addUserCanPush: mockDb.addUserCanPush,
93+
addUserCanAuthorise: mockDb.addUserCanAuthorise,
94+
getAllProxiedHosts: vi.fn().mockResolvedValue([]),
95+
};
96+
});
9197

9298
vi.doMock('../src/proxy/chain', async (importOriginal) => {
9399
const actual: any = await importOriginal();
@@ -97,10 +103,9 @@ describe.skip('Proxy Module TLS Certificate Loading', () => {
97103
};
98104
});
99105

100-
vi.spyOn(https, 'createServer').mockReturnValue({
101-
listen: vi.fn().mockReturnThis(),
102-
close: vi.fn(),
103-
} as any);
106+
vi.spyOn(http, 'createServer').mockReturnValue(mockHttpServer);
107+
108+
vi.spyOn(https, 'createServer').mockReturnValue(mockHttpsServer);
104109

105110
const ProxyClass = (await import('../src/proxy/index')).Proxy;
106111
proxyModule = new ProxyClass();

test/testProxyRoute.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ afterAll(() => {
4646
vi.resetModules();
4747
});
4848

49-
describe.skip('proxy express application', () => {
49+
describe('proxy express application', () => {
5050
let apiApp: Express;
5151
let proxy: Proxy;
5252
let cookie: string;

0 commit comments

Comments
 (0)