|
| 1 | +import 'should'; |
| 2 | + |
| 3 | +import * as request from 'supertest'; |
| 4 | +import nock from 'nock'; |
| 5 | +import { app as expressApp } from '../../masterExpressApp'; |
| 6 | +import { AppMode, MasterExpressConfig, TlsMode } from '../../types'; |
| 7 | +import { Environments } from '@bitgo/sdk-core'; |
| 8 | +import { before, after } from 'mocha'; |
| 9 | + |
| 10 | +describe('POST /api/:coin/wallet/generate', () => { |
| 11 | + let agent: request.SuperAgentTest; |
| 12 | + const enclavedExpressUrl = 'http://enclaved.invalid'; |
| 13 | + const bitgoApiUrl = Environments.test.uri; |
| 14 | + const coin = 'tbtc'; |
| 15 | + const accessToken = 'test-token'; |
| 16 | + |
| 17 | + before(() => { |
| 18 | + nock.disableNetConnect(); |
| 19 | + nock.enableNetConnect('127.0.0.1'); |
| 20 | + |
| 21 | + const config: MasterExpressConfig = { |
| 22 | + appMode: AppMode.MASTER_EXPRESS, |
| 23 | + port: 0, // Let OS assign a free port |
| 24 | + bind: 'localhost', |
| 25 | + timeout: 60000, |
| 26 | + logFile: '', |
| 27 | + env: 'test', |
| 28 | + disableEnvCheck: true, |
| 29 | + authVersion: 2, |
| 30 | + enclavedExpressUrl: enclavedExpressUrl, |
| 31 | + enclavedExpressCert: 'dummy-cert', |
| 32 | + tlsMode: TlsMode.DISABLED, |
| 33 | + mtlsRequestCert: false, |
| 34 | + allowSelfSigned: true, |
| 35 | + }; |
| 36 | + |
| 37 | + const app = expressApp(config); |
| 38 | + agent = request.agent(app); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + nock.cleanAll(); |
| 43 | + }); |
| 44 | + |
| 45 | + after(() => { |
| 46 | + nock.restore(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should generate a wallet by calling the enclaved express service', async () => { |
| 50 | + const userKeychainNock = nock(enclavedExpressUrl) |
| 51 | + .post(`/api/${coin}/key/independent`) |
| 52 | + .reply(200, { |
| 53 | + pub: 'xpub_user', |
| 54 | + source: 'user', |
| 55 | + type: 'independent', |
| 56 | + }); |
| 57 | + |
| 58 | + const backupKeychainNock = nock(enclavedExpressUrl) |
| 59 | + .post(`/api/${coin}/key/independent`) |
| 60 | + .reply(200, { |
| 61 | + pub: 'xpub_backup', |
| 62 | + source: 'backup', |
| 63 | + type: 'independent', |
| 64 | + }); |
| 65 | + |
| 66 | + const bitgoAddUserKeyNock = nock(bitgoApiUrl) |
| 67 | + .post(`/api/v2/${coin}/key`, { |
| 68 | + pub: 'xpub_user', |
| 69 | + keyType: 'independent', |
| 70 | + source: 'user', |
| 71 | + }) |
| 72 | + .matchHeader('any', () => true) |
| 73 | + .reply(200, { id: 'user-key-id', pub: 'xpub_user' }); |
| 74 | + |
| 75 | + const bitgoAddBackupKeyNock = nock(bitgoApiUrl) |
| 76 | + .post(`/api/v2/${coin}/key`, { |
| 77 | + pub: 'xpub_backup', |
| 78 | + keyType: 'independent', |
| 79 | + source: 'backup', |
| 80 | + }) |
| 81 | + .matchHeader('any', () => true) |
| 82 | + .reply(200, { id: 'backup-key-id', pub: 'xpub_backup' }); |
| 83 | + |
| 84 | + const bitgoAddBitGoKeyNock = nock(bitgoApiUrl) |
| 85 | + .post(`/api/v2/${coin}/key`, { |
| 86 | + source: 'bitgo', |
| 87 | + keyType: 'independent', |
| 88 | + enterprise: 'test_enterprise', |
| 89 | + }) |
| 90 | + .reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' }); |
| 91 | + |
| 92 | + const bitgoAddWalletNock = nock(bitgoApiUrl) |
| 93 | + .post(`/api/v2/${coin}/wallet/add`, { |
| 94 | + label: 'test_wallet', |
| 95 | + m: 2, |
| 96 | + n: 3, |
| 97 | + keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], |
| 98 | + type: 'cold', |
| 99 | + subType: 'onPrem', |
| 100 | + multisigType: 'onchain', |
| 101 | + enterprise: 'test_enterprise', |
| 102 | + }) |
| 103 | + .matchHeader('any', () => true) |
| 104 | + .reply(200, { |
| 105 | + id: 'new-wallet-id', |
| 106 | + multisigType: 'onchain', |
| 107 | + type: 'cold', |
| 108 | + subType: 'onPrem', |
| 109 | + }); |
| 110 | + |
| 111 | + const response = await agent |
| 112 | + .post(`/api/${coin}/wallet/generate`) |
| 113 | + .set('Authorization', `Bearer ${accessToken}`) |
| 114 | + .send({ |
| 115 | + label: 'test_wallet', |
| 116 | + enterprise: 'test_enterprise', |
| 117 | + }); |
| 118 | + |
| 119 | + response.status.should.equal(200); |
| 120 | + response.body.should.have.property('wallet'); |
| 121 | + response.body.wallet.should.have.properties({ |
| 122 | + id: 'new-wallet-id', |
| 123 | + multisigType: 'onchain', |
| 124 | + type: 'cold', |
| 125 | + subType: 'onPrem', |
| 126 | + }); |
| 127 | + response.body.should.have.propertyByPath('userKeychain', 'pub').eql('xpub_user'); |
| 128 | + response.body.should.have.propertyByPath('backupKeychain', 'pub').eql('xpub_backup'); |
| 129 | + response.body.should.have.propertyByPath('bitgoKeychain', 'pub').eql('xpub_bitgo'); |
| 130 | + |
| 131 | + userKeychainNock.done(); |
| 132 | + backupKeychainNock.done(); |
| 133 | + bitgoAddUserKeyNock.done(); |
| 134 | + bitgoAddBackupKeyNock.done(); |
| 135 | + bitgoAddBitGoKeyNock.done(); |
| 136 | + bitgoAddWalletNock.done(); |
| 137 | + }); |
| 138 | +}); |
0 commit comments