|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import 'should-http'; |
| 3 | +import 'should-sinon'; |
| 4 | +import '../../lib/asserts'; |
| 5 | +import nock from 'nock'; |
| 6 | +import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; |
| 7 | +import { BitGo } from 'bitgo'; |
| 8 | +import { BaseCoin, Wallets, Wallet, decodeOrElse, common } from '@bitgo/sdk-core'; |
| 9 | +import { ExpressApiRouteRequest } from '../../../src/typedRoutes/api'; |
| 10 | +import { handleV2ShareWallet } from '../../../src/clientRoutes'; |
| 11 | +import { ShareWalletResponse } from '../../../src/typedRoutes/api/v2/shareWallet'; |
| 12 | + |
| 13 | +describe('Share Wallet (typed handler)', () => { |
| 14 | + let bitgo: TestBitGoAPI; |
| 15 | + |
| 16 | + before(async function () { |
| 17 | + if (!nock.isActive()) { |
| 18 | + nock.activate(); |
| 19 | + } |
| 20 | + bitgo = TestBitGo.decorate(BitGo, { env: 'test' }); |
| 21 | + bitgo.initializeTestVars(); |
| 22 | + nock.disableNetConnect(); |
| 23 | + nock.enableNetConnect('127.0.0.1'); |
| 24 | + }); |
| 25 | + |
| 26 | + after(() => { |
| 27 | + if (nock.isActive()) { |
| 28 | + nock.restore(); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + it('should call shareWallet (no stub), mock BitGo HTTP, and return typed response', async () => { |
| 33 | + const coin = 'tbtc'; |
| 34 | + const walletId = '59cd72485007a239fb00282ed480da1f'; |
| 35 | + const email = '[email protected]'; |
| 36 | + const permissions = 'view'; |
| 37 | + const message = 'hello'; |
| 38 | + |
| 39 | + const baseCoin = bitgo.coin(coin); |
| 40 | + const walletData = { |
| 41 | + id: walletId, |
| 42 | + coin: coin, |
| 43 | + keys: ['k1', 'k2', 'k3'], |
| 44 | + coinSpecific: {}, |
| 45 | + multisigType: 'onchain', |
| 46 | + type: 'hot', |
| 47 | + }; |
| 48 | + const realWallet = new Wallet(bitgo, baseCoin, walletData); |
| 49 | + const coinStub = sinon.createStubInstance(BaseCoin, { |
| 50 | + wallets: sinon.stub<[], Wallets>().returns({ |
| 51 | + get: sinon.stub<[any], Promise<Wallet>>().resolves(realWallet), |
| 52 | + } as any), |
| 53 | + }); |
| 54 | + |
| 55 | + const stubBitgo = sinon.createStubInstance(BitGo, { coin: sinon.stub<[string]>().returns(coinStub) }); |
| 56 | + |
| 57 | + const bgUrl = common.Environments[bitgo.getEnv()].uri; |
| 58 | + const getSharingKeyNock = nock(bgUrl).post('/api/v1/user/sharingkey', { email }).reply(200, { userId: 'u1' }); |
| 59 | + const shareResponse = { |
| 60 | + id: walletId, |
| 61 | + coin, |
| 62 | + wallet: walletId, |
| 63 | + fromUser: 'u0', |
| 64 | + toUser: 'u1', |
| 65 | + permissions, |
| 66 | + message, |
| 67 | + state: 'active', |
| 68 | + }; |
| 69 | + const createShareNock = nock(bgUrl) |
| 70 | + .post(`/api/v2/${coin}/wallet/${walletId}/share`, (body) => { |
| 71 | + body.user.should.equal('u1'); |
| 72 | + body.permissions.should.equal(permissions); |
| 73 | + body.skipKeychain.should.equal(true); |
| 74 | + if (message) body.message.should.equal(message); |
| 75 | + return true; |
| 76 | + }) |
| 77 | + .reply(200, shareResponse); |
| 78 | + |
| 79 | + const req = { |
| 80 | + bitgo: stubBitgo, |
| 81 | + decoded: { |
| 82 | + coin, |
| 83 | + id: walletId, |
| 84 | + email, |
| 85 | + permissions, |
| 86 | + message, |
| 87 | + }, |
| 88 | + } as unknown as ExpressApiRouteRequest<'express.v2.wallet.share', 'post'>; |
| 89 | + |
| 90 | + const res = await handleV2ShareWallet(req); |
| 91 | + decodeOrElse('ShareWalletResponse200', ShareWalletResponse[200], res, (errors) => { |
| 92 | + throw new Error(`Response did not match expected codec: ${errors}`); |
| 93 | + }); |
| 94 | + |
| 95 | + getSharingKeyNock.isDone().should.be.true(); |
| 96 | + createShareNock.isDone().should.be.true(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments