|
| 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, decodeOrElse, common } from '@bitgo/sdk-core'; |
| 9 | +import { ExpressApiRouteRequest } from '../../../src/typedRoutes/api'; |
| 10 | +import { handleV2CancelWalletShare } from '../../../src/clientRoutes'; |
| 11 | +import { CancelWalletShareResponse } from '../../../src/typedRoutes/api/v2/cancelWalletShare'; |
| 12 | + |
| 13 | +describe('Cancel Wallet Share (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 cancelShare and return a typed response', async () => { |
| 33 | + const coin = 'tbtc'; |
| 34 | + const shareId = 'abc123shareId'; |
| 35 | + |
| 36 | + const cancelResponse = { |
| 37 | + changed: true, |
| 38 | + state: 'canceled', |
| 39 | + }; |
| 40 | + |
| 41 | + const cancelShareStub = sinon.stub().resolves(cancelResponse); |
| 42 | + const coinStub = sinon.createStubInstance(BaseCoin, { |
| 43 | + wallets: sinon.stub<[], Wallets>().returns({ |
| 44 | + cancelShare: cancelShareStub, |
| 45 | + } as any), |
| 46 | + }); |
| 47 | + |
| 48 | + const stubBitgo = sinon.createStubInstance(BitGo, { coin: sinon.stub<[string]>().returns(coinStub) }); |
| 49 | + |
| 50 | + const req = { |
| 51 | + bitgo: stubBitgo, |
| 52 | + decoded: { |
| 53 | + coin, |
| 54 | + id: shareId, |
| 55 | + }, |
| 56 | + } as unknown as ExpressApiRouteRequest<'express.wallet.cancelShare', 'delete'>; |
| 57 | + |
| 58 | + const res = await handleV2CancelWalletShare(req); |
| 59 | + |
| 60 | + cancelShareStub.calledOnceWith({ walletShareId: shareId }).should.be.true(); |
| 61 | + decodeOrElse('CancelWalletShareResponse200', CancelWalletShareResponse[200], res, (errors) => { |
| 62 | + throw new Error(`Response did not match expected codec: ${errors}`); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should pass the walletShareId from the route param to cancelShare', async () => { |
| 67 | + const coin = 'tbtc'; |
| 68 | + const shareId = 'someOtherShareId'; |
| 69 | + |
| 70 | + const cancelShareStub = sinon.stub().resolves({ changed: false, state: 'canceled' }); |
| 71 | + const coinStub = sinon.createStubInstance(BaseCoin, { |
| 72 | + wallets: sinon.stub<[], Wallets>().returns({ |
| 73 | + cancelShare: cancelShareStub, |
| 74 | + } as any), |
| 75 | + }); |
| 76 | + |
| 77 | + const stubBitgo = sinon.createStubInstance(BitGo, { coin: sinon.stub<[string]>().returns(coinStub) }); |
| 78 | + |
| 79 | + const req = { |
| 80 | + bitgo: stubBitgo, |
| 81 | + decoded: { |
| 82 | + coin, |
| 83 | + id: shareId, |
| 84 | + }, |
| 85 | + } as unknown as ExpressApiRouteRequest<'express.wallet.cancelShare', 'delete'>; |
| 86 | + |
| 87 | + await handleV2CancelWalletShare(req); |
| 88 | + |
| 89 | + cancelShareStub.calledOnceWith({ walletShareId: shareId }).should.be.true(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments