-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathservice.tls.test.ts
More file actions
152 lines (123 loc) · 5.02 KB
/
service.tls.test.ts
File metadata and controls
152 lines (123 loc) · 5.02 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
import http from 'http';
import https from 'https';
import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest';
import fs from 'fs';
describe('Service Module TLS', () => {
let serviceModule: any;
let mockConfig: any;
let mockHttpServer: any;
let mockHttpsServer: any;
let mockProxy: any;
beforeEach(async () => {
vi.resetModules();
mockConfig = {
getTLSEnabled: vi.fn(),
getTLSKeyPemPath: vi.fn(),
getTLSCertPemPath: vi.fn(),
getRateLimit: vi.fn().mockReturnValue({ windowMs: 15 * 60 * 1000, max: 100 }),
getCookieSecret: vi.fn().mockReturnValue('test-secret'),
getSessionMaxAgeHours: vi.fn().mockReturnValue(12),
getCSRFProtection: vi.fn().mockReturnValue(false),
};
mockHttpServer = {
listen: vi.fn().mockReturnThis(),
close: vi.fn().mockImplementation((cb) => {
if (cb) cb();
}),
on: vi.fn().mockReturnThis(),
};
mockHttpsServer = {
listen: vi.fn().mockImplementation((_port: any, cb: any) => {
if (cb) cb();
return mockHttpsServer;
}),
close: vi.fn().mockImplementation((cb: any) => {
if (cb) cb();
}),
on: vi.fn().mockReturnThis(),
};
mockProxy = {};
vi.doMock('../src/config', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
getTLSEnabled: mockConfig.getTLSEnabled,
getTLSKeyPemPath: mockConfig.getTLSKeyPemPath,
getTLSCertPemPath: mockConfig.getTLSCertPemPath,
getRateLimit: mockConfig.getRateLimit,
getCookieSecret: mockConfig.getCookieSecret,
getSessionMaxAgeHours: mockConfig.getSessionMaxAgeHours,
getCSRFProtection: mockConfig.getCSRFProtection,
};
});
vi.doMock('../src/db', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
getSessionStore: vi.fn().mockReturnValue(undefined),
};
});
vi.doMock('../src/service/passport', () => ({
configure: vi.fn().mockResolvedValue({
initialize: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()),
session: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()),
}),
}));
vi.doMock('../src/service/routes', () => ({
default: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()),
}));
vi.spyOn(http, 'createServer').mockReturnValue(mockHttpServer as any);
vi.spyOn(https, 'createServer').mockReturnValue(mockHttpsServer as any);
serviceModule = await import('../src/service/index');
});
afterEach(async () => {
try {
await serviceModule.Service.stop();
} catch (err) {
console.error('Error occurred when stopping the service: ', err);
}
vi.restoreAllMocks();
});
describe('TLS certificate file reading', () => {
it('should start HTTPS server and read TLS 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.mockImplementation((path: any) => {
if (path === '/path/to/key.pem') return mockKeyContent;
if (path === '/path/to/cert.pem') return mockCertContent;
return Buffer.from('default');
});
await serviceModule.Service.start(mockProxy);
expect(https.createServer).toHaveBeenCalled();
expect(fsStub).toHaveBeenCalledWith('/path/to/key.pem');
expect(fsStub).toHaveBeenCalledWith('/path/to/cert.pem');
});
it('should not start HTTPS server when TLS is disabled', async () => {
mockConfig.getTLSEnabled.mockReturnValue(false);
await serviceModule.Service.start(mockProxy);
expect(https.createServer).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 serviceModule.Service.start(mockProxy);
expect(fsStub).not.toHaveBeenCalled();
});
it('should close both HTTP and HTTPS servers on stop() when TLS is enabled', async () => {
mockConfig.getTLSEnabled.mockReturnValue(true);
mockConfig.getTLSKeyPemPath.mockReturnValue('/path/to/key.pem');
mockConfig.getTLSCertPemPath.mockReturnValue('/path/to/cert.pem');
vi.spyOn(fs, 'readFileSync').mockReturnValue(Buffer.from('mock-content'));
await serviceModule.Service.start(mockProxy);
await serviceModule.Service.stop();
expect(mockHttpServer.close).toHaveBeenCalled();
expect(mockHttpsServer.close).toHaveBeenCalled();
});
});
});