|
| 1 | +import 'should'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import nock from 'nock'; |
| 4 | +import { app as expressApp } from '../../../masterExpressApp'; |
| 5 | +import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types'; |
| 6 | +import sinon from 'sinon'; |
| 7 | +import * as middleware from '../../../shared/middleware'; |
| 8 | +import * as masterMiddleware from '../../../api/master/middleware/middleware'; |
| 9 | +import { BitGoRequest } from '../../../types/request'; |
| 10 | +import { BitGoAPI } from '@bitgo-beta/sdk-api'; |
| 11 | +import { EnclavedExpressClient } from '../../../api/master/clients/enclavedExpressClient'; |
| 12 | + |
| 13 | +describe('Non Recovery Tests', () => { |
| 14 | + let agent: request.SuperAgentTest; |
| 15 | + let mockBitgo: BitGoAPI; |
| 16 | + const enclavedExpressUrl = 'http://enclaved.invalid'; |
| 17 | + const accessToken = 'test-token'; |
| 18 | + const config: MasterExpressConfig = { |
| 19 | + appMode: AppMode.MASTER_EXPRESS, |
| 20 | + port: 0, |
| 21 | + bind: 'localhost', |
| 22 | + timeout: 60000, |
| 23 | + env: 'test', |
| 24 | + disableEnvCheck: true, |
| 25 | + authVersion: 2, |
| 26 | + enclavedExpressUrl: enclavedExpressUrl, |
| 27 | + enclavedExpressCert: 'dummy-cert', |
| 28 | + tlsMode: TlsMode.DISABLED, |
| 29 | + httpLoggerFile: '', |
| 30 | + allowSelfSigned: true, |
| 31 | + recoveryMode: false, |
| 32 | + }; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + nock.disableNetConnect(); |
| 36 | + nock.enableNetConnect('127.0.0.1'); |
| 37 | + |
| 38 | + // Create mock BitGo instance with base functionality |
| 39 | + mockBitgo = new BitGoAPI({ env: 'test' }); |
| 40 | + |
| 41 | + // Setup middleware stubs before creating app |
| 42 | + sinon.stub(middleware, 'prepareBitGo').callsFake(() => (req, res, next) => { |
| 43 | + (req as BitGoRequest<MasterExpressConfig>).bitgo = mockBitgo; |
| 44 | + (req as BitGoRequest<MasterExpressConfig>).config = config; |
| 45 | + next(); |
| 46 | + }); |
| 47 | + |
| 48 | + // Create app after middleware is stubbed |
| 49 | + const app = expressApp(config); |
| 50 | + agent = request.agent(app); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + nock.cleanAll(); |
| 55 | + sinon.restore(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('Recovery', () => { |
| 59 | + const coin = 'tbtc'; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + sinon.stub(masterMiddleware, 'validateMasterExpressConfig').callsFake((req, res, next) => { |
| 63 | + (req as BitGoRequest<MasterExpressConfig>).params = { coin }; |
| 64 | + (req as BitGoRequest<MasterExpressConfig>).enclavedExpressClient = |
| 65 | + new EnclavedExpressClient(config, coin); |
| 66 | + next(); |
| 67 | + return undefined; |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fail to run mbe recovery if not in recovery mode', async () => { |
| 72 | + const coin = 'tbtc'; |
| 73 | + const userPub = 'xpub_user'; |
| 74 | + const backupPub = 'xpub_backup'; |
| 75 | + const bitgoPub = 'xpub_bitgo'; |
| 76 | + const recoveryDestination = 'tb1qprdy6jwxrrr2qrwgd2tzl8z99hqp29jn6f3sguxulqm448myj6jsy2nwsu'; |
| 77 | + const response = await agent |
| 78 | + .post(`/api/${coin}/wallet/recovery`) |
| 79 | + .set('Authorization', `Bearer ${accessToken}`) |
| 80 | + .send({ |
| 81 | + multiSigRecoveryParams: { |
| 82 | + userPub, |
| 83 | + backupPub, |
| 84 | + bitgoPub, |
| 85 | + walletContractAddress: '', |
| 86 | + }, |
| 87 | + recoveryDestinationAddress: recoveryDestination, |
| 88 | + coin, |
| 89 | + apiKey: 'key', |
| 90 | + coinSpecificParams: { |
| 91 | + evmRecoveryOptions: { |
| 92 | + gasPrice: 20000000000, |
| 93 | + gasLimit: 500000, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }); |
| 97 | + response.status.should.equal(500); |
| 98 | + response.body.should.have.property('error'); |
| 99 | + response.body.should.have.property('details'); |
| 100 | + response.body.details.should.containEql( |
| 101 | + 'Recovery operations are not enabled. The server must be in recovery mode to perform this action.', |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('Recovery Consolidation', () => { |
| 107 | + it('should fail to run mbe recovery consolidation if not in recovery mode', async () => { |
| 108 | + const response = await agent |
| 109 | + .post(`/api/trx/wallet/recoveryconsolidations`) |
| 110 | + .set('Authorization', `Bearer ${accessToken}`) |
| 111 | + .send({ |
| 112 | + multisigType: 'onchain', |
| 113 | + userPub: 'user-xpub', |
| 114 | + backupPub: 'backup-xpub', |
| 115 | + bitgoPub: 'bitgo-xpub', |
| 116 | + tokenContractAddress: 'tron-token', |
| 117 | + startingScanIndex: 1, |
| 118 | + endingScanIndex: 3, |
| 119 | + }); |
| 120 | + |
| 121 | + response.status.should.equal(500); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments