Skip to content

Commit a3b3a73

Browse files
committed
chore: fix tests for enclaved express
Ticket: WP-4352
1 parent c2fd622 commit a3b3a73

File tree

1 file changed

+64
-34
lines changed

1 file changed

+64
-34
lines changed

src/__tests__/config.test.ts

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { config, TlsMode } from '../config';
1+
import { config, isEnclavedConfig, TlsMode } from '../config';
22

33
describe('Configuration', () => {
44
const originalEnv = process.env;
@@ -15,46 +15,76 @@ describe('Configuration', () => {
1515
process.env = originalEnv;
1616
});
1717

18-
it('should use default configuration when no environment variables are set', () => {
19-
const cfg = config();
20-
expect(cfg.port).toBe(3080);
21-
expect(cfg.bind).toBe('localhost');
22-
expect(cfg.tlsMode).toBe(TlsMode.ENABLED);
23-
expect(cfg.timeout).toBe(305 * 1000);
18+
it('should throw error when APP_MODE is not set', () => {
19+
expect(() => config()).toThrow('APP_MODE environment variable is required');
2420
});
2521

26-
it('should read port from environment variable', () => {
27-
process.env.MASTER_BITGO_EXPRESS_PORT = '4000';
28-
const cfg = config();
29-
expect(cfg.port).toBe(4000);
22+
it('should throw error when APP_MODE is invalid', () => {
23+
process.env.APP_MODE = 'invalid';
24+
expect(() => config()).toThrow('Invalid APP_MODE: invalid');
3025
});
3126

32-
it('should read TLS mode from environment variables', () => {
33-
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'true';
34-
let cfg = config();
35-
expect(cfg.tlsMode).toBe(TlsMode.DISABLED);
27+
describe('Enclaved Mode', () => {
28+
beforeEach(() => {
29+
process.env.APP_MODE = 'enclaved';
30+
});
3631

37-
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'false';
38-
process.env.MTLS_ENABLED = 'true';
39-
cfg = config();
40-
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
41-
});
32+
it('should use default configuration when no environment variables are set', () => {
33+
const cfg = config();
34+
expect(isEnclavedConfig(cfg)).toBe(true);
35+
if (isEnclavedConfig(cfg)) {
36+
expect(cfg.port).toBe(3080);
37+
expect(cfg.bind).toBe('localhost');
38+
expect(cfg.tlsMode).toBe(TlsMode.ENABLED);
39+
expect(cfg.timeout).toBe(305 * 1000);
40+
}
41+
});
4242

43-
it('should throw error when both TLS disabled and mTLS enabled', () => {
44-
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'true';
45-
process.env.MTLS_ENABLED = 'true';
46-
expect(() => config()).toThrow('Cannot have both TLS disabled and mTLS enabled');
47-
});
43+
it('should read port from environment variable', () => {
44+
process.env.MASTER_BITGO_EXPRESS_PORT = '4000';
45+
const cfg = config();
46+
expect(isEnclavedConfig(cfg)).toBe(true);
47+
if (isEnclavedConfig(cfg)) {
48+
expect(cfg.port).toBe(4000);
49+
}
50+
});
51+
52+
it('should read TLS mode from environment variables', () => {
53+
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'true';
54+
let cfg = config();
55+
expect(isEnclavedConfig(cfg)).toBe(true);
56+
if (isEnclavedConfig(cfg)) {
57+
expect(cfg.tlsMode).toBe(TlsMode.DISABLED);
58+
}
59+
60+
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'false';
61+
process.env.MTLS_ENABLED = 'true';
62+
cfg = config();
63+
expect(isEnclavedConfig(cfg)).toBe(true);
64+
if (isEnclavedConfig(cfg)) {
65+
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
66+
}
67+
});
68+
69+
it('should throw error when both TLS disabled and mTLS enabled', () => {
70+
process.env.MASTER_BITGO_EXPRESS_DISABLE_TLS = 'true';
71+
process.env.MTLS_ENABLED = 'true';
72+
expect(() => config()).toThrow('Cannot have both TLS disabled and mTLS enabled');
73+
});
4874

49-
it('should read mTLS settings from environment variables', () => {
50-
process.env.MTLS_ENABLED = 'true';
51-
process.env.MTLS_REQUEST_CERT = 'true';
52-
process.env.MTLS_REJECT_UNAUTHORIZED = 'true';
53-
process.env.MTLS_ALLOWED_CLIENT_FINGERPRINTS = 'ABC123,DEF456';
75+
it('should read mTLS settings from environment variables', () => {
76+
process.env.MTLS_ENABLED = 'true';
77+
process.env.MTLS_REQUEST_CERT = 'true';
78+
process.env.MTLS_REJECT_UNAUTHORIZED = 'true';
79+
process.env.MTLS_ALLOWED_CLIENT_FINGERPRINTS = 'ABC123,DEF456';
5480

55-
const cfg = config();
56-
expect(cfg.mtlsRequestCert).toBe(true);
57-
expect(cfg.mtlsRejectUnauthorized).toBe(true);
58-
expect(cfg.mtlsAllowedClientFingerprints).toEqual(['ABC123', 'DEF456']);
81+
const cfg = config();
82+
expect(isEnclavedConfig(cfg)).toBe(true);
83+
if (isEnclavedConfig(cfg)) {
84+
expect(cfg.mtlsRequestCert).toBe(true);
85+
expect(cfg.mtlsRejectUnauthorized).toBe(true);
86+
expect(cfg.mtlsAllowedClientFingerprints).toEqual(['ABC123', 'DEF456']);
87+
}
88+
});
5989
});
6090
});

0 commit comments

Comments
 (0)