|
| 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 | + logFile: '', |
| 24 | + env: 'test', |
| 25 | + disableEnvCheck: true, |
| 26 | + authVersion: 2, |
| 27 | + enclavedExpressUrl: enclavedExpressUrl, |
| 28 | + enclavedExpressCert: 'dummy-cert', |
| 29 | + tlsMode: TlsMode.DISABLED, |
| 30 | + mtlsRequestCert: false, |
| 31 | + allowSelfSigned: true, |
| 32 | + recoveryMode: false, |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + nock.disableNetConnect(); |
| 37 | + nock.enableNetConnect('127.0.0.1'); |
| 38 | + |
| 39 | + // Create mock BitGo instance with base functionality |
| 40 | + mockBitgo = { |
| 41 | + coin: sinon.stub(), |
| 42 | + _coinFactory: {}, |
| 43 | + _useAms: false, |
| 44 | + initCoinFactory: sinon.stub(), |
| 45 | + registerToken: sinon.stub(), |
| 46 | + getValidate: sinon.stub(), |
| 47 | + validateAddress: sinon.stub(), |
| 48 | + verifyAddress: sinon.stub(), |
| 49 | + verifyPassword: sinon.stub(), |
| 50 | + encrypt: sinon.stub(), |
| 51 | + decrypt: sinon.stub(), |
| 52 | + lock: sinon.stub(), |
| 53 | + unlock: sinon.stub(), |
| 54 | + getSharingKey: sinon.stub(), |
| 55 | + ping: sinon.stub(), |
| 56 | + authenticate: sinon.stub(), |
| 57 | + authenticateWithAccessToken: sinon.stub(), |
| 58 | + logout: sinon.stub(), |
| 59 | + me: sinon.stub(), |
| 60 | + session: sinon.stub(), |
| 61 | + getUser: sinon.stub(), |
| 62 | + users: sinon.stub(), |
| 63 | + getWallet: sinon.stub(), |
| 64 | + getWallets: sinon.stub(), |
| 65 | + addWallet: sinon.stub(), |
| 66 | + removeWallet: sinon.stub(), |
| 67 | + getAsUser: sinon.stub(), |
| 68 | + register: sinon.stub(), |
| 69 | + } as unknown as BitGoAPI; |
| 70 | + |
| 71 | + // Setup middleware stubs before creating app |
| 72 | + sinon.stub(middleware, 'prepareBitGo').callsFake(() => (req, res, next) => { |
| 73 | + (req as BitGoRequest<MasterExpressConfig>).bitgo = mockBitgo; |
| 74 | + (req as BitGoRequest<MasterExpressConfig>).config = config; |
| 75 | + next(); |
| 76 | + }); |
| 77 | + |
| 78 | + // Create app after middleware is stubbed |
| 79 | + const app = expressApp(config); |
| 80 | + agent = request.agent(app); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + nock.cleanAll(); |
| 85 | + sinon.restore(); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('Recovery', () => { |
| 89 | + const coin = 'tbtc'; |
| 90 | + |
| 91 | + beforeEach(() => { |
| 92 | + sinon.stub(masterMiddleware, 'validateMasterExpressConfig').callsFake((req, res, next) => { |
| 93 | + (req as BitGoRequest<MasterExpressConfig>).params = { coin }; |
| 94 | + (req as BitGoRequest<MasterExpressConfig>).enclavedExpressClient = |
| 95 | + new EnclavedExpressClient(config, coin); |
| 96 | + next(); |
| 97 | + return undefined; |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should fail to run recovery if not in recovery mode', async () => { |
| 102 | + const coin = 'tbtc'; |
| 103 | + const userPub = 'xpub_user'; |
| 104 | + const backupPub = 'xpub_backup'; |
| 105 | + const bitgoPub = 'xpub_bitgo'; |
| 106 | + const recoveryDestination = 'tb1qprdy6jwxrrr2qrwgd2tzl8z99hqp29jn6f3sguxulqm448myj6jsy2nwsu'; |
| 107 | + const response = await agent |
| 108 | + .post(`/api/${coin}/wallet/recovery`) |
| 109 | + .set('Authorization', `Bearer ${accessToken}`) |
| 110 | + .send({ |
| 111 | + multiSigRecoveryParams: { |
| 112 | + userPub, |
| 113 | + backupPub, |
| 114 | + bitgoPub, |
| 115 | + walletContractAddress: '', |
| 116 | + }, |
| 117 | + recoveryDestinationAddress: recoveryDestination, |
| 118 | + coin, |
| 119 | + apiKey: 'key', |
| 120 | + coinSpecificParams: { |
| 121 | + evmRecoveryOptions: { |
| 122 | + gasPrice: 20000000000, |
| 123 | + gasLimit: 500000, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + response.status.should.equal(500); |
| 128 | + response.body.should.have.property('error'); |
| 129 | + response.body.should.have.property('details'); |
| 130 | + response.body.details.should.containEql( |
| 131 | + 'Recovery operations are not enabled. The server must be in recovery mode to perform this action.', |
| 132 | + ); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('Recovery Consolidation', () => { |
| 137 | + it('should fail to run recovery consolidation if not in recovery mode', async () => { |
| 138 | + const response = await agent |
| 139 | + .post(`/api/trx/wallet/recoveryconsolidations`) |
| 140 | + .set('Authorization', `Bearer ${accessToken}`) |
| 141 | + .send({ |
| 142 | + multisigType: 'onchain', |
| 143 | + userPub: 'user-xpub', |
| 144 | + backupPub: 'backup-xpub', |
| 145 | + bitgoPub: 'bitgo-xpub', |
| 146 | + tokenContractAddress: 'tron-token', |
| 147 | + startingScanIndex: 1, |
| 148 | + endingScanIndex: 3, |
| 149 | + }); |
| 150 | + |
| 151 | + response.status.should.equal(500); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments