Skip to content

Commit 07d8951

Browse files
feat(awm): backup awm client configurations
Ticket: WCN-362
1 parent ade43be commit 07d8951

20 files changed

Lines changed: 1681 additions & 165 deletions
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import 'should';
2+
import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types';
3+
import {
4+
createAwmClient,
5+
createAwmBackupClient,
6+
} from '../../../masterBitgoExpress/clients/advancedWalletManagerClient';
7+
8+
describe('AWM Backup Client', () => {
9+
const baseConfig: MasterExpressConfig = {
10+
appMode: AppMode.MASTER_EXPRESS,
11+
port: 3081,
12+
bind: 'localhost',
13+
timeout: 60000,
14+
httpLoggerFile: '',
15+
env: 'test',
16+
disableEnvCheck: true,
17+
authVersion: 2,
18+
advancedWalletManagerUrl: 'http://primary-awm.invalid',
19+
awmServerCaCert: 'dummy-cert',
20+
tlsMode: TlsMode.DISABLED,
21+
clientCertAllowSelfSigned: true,
22+
};
23+
24+
describe('createAwmBackupClient', () => {
25+
it('should return undefined when no backup URL is configured', () => {
26+
const result = createAwmBackupClient(baseConfig, 'tbtc');
27+
(result === undefined).should.be.true();
28+
});
29+
30+
it('should create a client when backup URL is configured', () => {
31+
const config: MasterExpressConfig = {
32+
...baseConfig,
33+
advancedWalletManagerBackupUrl: 'http://backup-awm.invalid',
34+
};
35+
const result = createAwmBackupClient(config, 'tbtc');
36+
(result !== undefined).should.be.true();
37+
});
38+
39+
it('should create a client pointing to the backup URL, not the primary', () => {
40+
const config: MasterExpressConfig = {
41+
...baseConfig,
42+
advancedWalletManagerBackupUrl: 'http://backup-awm.invalid',
43+
};
44+
const backupClient = createAwmBackupClient(config, 'tbtc');
45+
const primaryClient = createAwmClient(config, 'tbtc');
46+
47+
// Both clients should exist
48+
(backupClient !== undefined).should.be.true();
49+
(primaryClient !== undefined).should.be.true();
50+
51+
// They should be different instances
52+
(backupClient !== primaryClient).should.be.true();
53+
});
54+
55+
it('should fall back to primary certs when backup-specific certs are not set', () => {
56+
const config: MasterExpressConfig = {
57+
...baseConfig,
58+
tlsMode: TlsMode.MTLS,
59+
advancedWalletManagerBackupUrl: 'https://backup-awm.invalid',
60+
awmServerCaCert: 'primary-ca-cert',
61+
awmClientTlsKey: 'primary-client-key',
62+
awmClientTlsCert: 'primary-client-cert',
63+
// No backup-specific certs set — should fall back to primary
64+
};
65+
const result = createAwmBackupClient(config, 'tbtc');
66+
// Should succeed using the primary certs as fallback
67+
(result !== undefined).should.be.true();
68+
});
69+
70+
it('should use backup-specific certs when provided', () => {
71+
const config: MasterExpressConfig = {
72+
...baseConfig,
73+
tlsMode: TlsMode.MTLS,
74+
advancedWalletManagerBackupUrl: 'https://backup-awm.invalid',
75+
awmServerCaCert: 'primary-ca-cert',
76+
awmClientTlsKey: 'primary-client-key',
77+
awmClientTlsCert: 'primary-client-cert',
78+
awmBackupServerCaCert: 'backup-ca-cert',
79+
awmBackupClientTlsKey: 'backup-client-key',
80+
awmBackupClientTlsCert: 'backup-client-cert',
81+
};
82+
const result = createAwmBackupClient(config, 'tbtc');
83+
(result !== undefined).should.be.true();
84+
});
85+
86+
it('should throw when backup URL is set but mTLS certs are missing', () => {
87+
const config: MasterExpressConfig = {
88+
...baseConfig,
89+
tlsMode: TlsMode.MTLS,
90+
advancedWalletManagerBackupUrl: 'https://backup-awm.invalid',
91+
// No certs at all — constructor should throw
92+
awmServerCaCert: undefined as any,
93+
awmClientTlsKey: undefined,
94+
awmClientTlsCert: undefined,
95+
};
96+
(() => createAwmBackupClient(config, 'tbtc')).should.throw(
97+
/Failed to create backup advanced wallet manager client/,
98+
);
99+
});
100+
});
101+
102+
describe('fallback behavior in middleware', () => {
103+
it('should use primary client for both user and backup when no backup URL is set', () => {
104+
const primaryClient = createAwmClient(baseConfig, 'tbtc');
105+
const backupClient = createAwmBackupClient(baseConfig, 'tbtc');
106+
107+
(primaryClient !== undefined).should.be.true();
108+
// No backup URL → backup client is undefined → middleware falls back to primary
109+
(backupClient === undefined).should.be.true();
110+
111+
// Middleware would do: awmBackupClient = backupClient ?? primaryClient
112+
const effectiveBackupClient = backupClient ?? primaryClient;
113+
(effectiveBackupClient === primaryClient).should.be.true();
114+
});
115+
116+
it('should use separate client for backup when backup URL is set', () => {
117+
const config: MasterExpressConfig = {
118+
...baseConfig,
119+
advancedWalletManagerBackupUrl: 'http://backup-awm.invalid',
120+
};
121+
const primaryClient = createAwmClient(config, 'tbtc');
122+
const backupClient = createAwmBackupClient(config, 'tbtc');
123+
124+
(primaryClient !== undefined).should.be.true();
125+
(backupClient !== undefined).should.be.true();
126+
127+
// Middleware would do: awmBackupClient = backupClient ?? primaryClient
128+
const effectiveBackupClient = backupClient ?? primaryClient;
129+
(effectiveBackupClient === backupClient).should.be.true();
130+
(effectiveBackupClient !== primaryClient).should.be.true();
131+
});
132+
});
133+
});

0 commit comments

Comments
 (0)