|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import * as superagent from 'superagent'; |
| 3 | +import 'should'; |
| 4 | +import { AddressBook } from '../../../../src/bitgo/address-book/address-book'; |
| 5 | + |
| 6 | +describe('AddressBook', function () { |
| 7 | + let addressBook: AddressBook; |
| 8 | + let mockBitGo: any; |
| 9 | + const enterpriseId = 'test-enterprise-id'; |
| 10 | + |
| 11 | + function makeGetStub() { |
| 12 | + const queryStub = sinon.stub().returns({ result: sinon.stub().resolves({}) }); |
| 13 | + const setStub = sinon.stub().returns({ query: queryStub }); |
| 14 | + mockBitGo.get.returns({ set: setStub }); |
| 15 | + return { setStub, queryStub }; |
| 16 | + } |
| 17 | + |
| 18 | + function makeParameterlessGetStub(response: Record<string, unknown> = {}) { |
| 19 | + const resultStub = sinon.stub().resolves(response); |
| 20 | + const setStub = sinon.stub().returns({ result: resultStub }); |
| 21 | + mockBitGo.get.returns({ set: setStub }); |
| 22 | + return { setStub, resultStub }; |
| 23 | + } |
| 24 | + |
| 25 | + beforeEach(function () { |
| 26 | + mockBitGo = { |
| 27 | + get: sinon.stub(), |
| 28 | + microservicesUrl: sinon.stub().callsFake((path: string) => `https://app.bitgo.com${path}`), |
| 29 | + }; |
| 30 | + addressBook = new AddressBook(enterpriseId, mockBitGo); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(function () { |
| 34 | + sinon.restore(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('getConnections', function () { |
| 38 | + it('should pass params as query string, not request body', async function () { |
| 39 | + const { queryStub } = makeGetStub(); |
| 40 | + const params = { connectionType: 'DVP' as const, status: 'INACTIVE' as const, offset: 0, limit: 10 }; |
| 41 | + |
| 42 | + await addressBook.getConnections(params); |
| 43 | + |
| 44 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); |
| 45 | + sinon.assert.calledOnce(queryStub); |
| 46 | + sinon.assert.calledWith(queryStub, params); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should work with no params', async function () { |
| 50 | + const { queryStub } = makeGetStub(); |
| 51 | + |
| 52 | + await addressBook.getConnections(); |
| 53 | + |
| 54 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); |
| 55 | + sinon.assert.calledOnce(queryStub); |
| 56 | + sinon.assert.calledWith(queryStub, {}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should pass array filters to query unchanged', async function () { |
| 60 | + const { queryStub } = makeGetStub(); |
| 61 | + const params = { |
| 62 | + ownerWalletId: ['wallet-a', 'wallet-b'], |
| 63 | + targetWalletId: ['wallet-c'], |
| 64 | + }; |
| 65 | + |
| 66 | + await addressBook.getConnections(params); |
| 67 | + |
| 68 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); |
| 69 | + sinon.assert.calledOnce(queryStub); |
| 70 | + sinon.assert.calledWith(queryStub, params); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('getConnections array query param serialization', function () { |
| 75 | + it('superagent serializes string[] as repeated keys, which address-book accepts via nonEmptyArrayFromQueryParam', function () { |
| 76 | + const req = superagent.get('https://example.com').query({ |
| 77 | + ownerWalletId: ['wallet-a', 'wallet-b'], |
| 78 | + targetWalletId: ['wallet-c'], |
| 79 | + }); |
| 80 | + // Trigger superagent's query-string assembly without sending the request. |
| 81 | + req.end(() => undefined); |
| 82 | + |
| 83 | + req.url!.should.equal( |
| 84 | + 'https://example.com?ownerWalletId=wallet-a&ownerWalletId=wallet-b&targetWalletId=wallet-c' |
| 85 | + ); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('getListing', function () { |
| 90 | + it('should use GET with enterprise-id header and no query or body', async function () { |
| 91 | + const listing = { |
| 92 | + id: 'listing-id', |
| 93 | + enterpriseId, |
| 94 | + name: 'Test Listing', |
| 95 | + owner: 'owner', |
| 96 | + createdAt: '2024-01-01', |
| 97 | + updatedAt: '2024-01-01', |
| 98 | + }; |
| 99 | + const { setStub, resultStub } = makeParameterlessGetStub(listing); |
| 100 | + |
| 101 | + const result = await addressBook.getListing(); |
| 102 | + |
| 103 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/global'); |
| 104 | + sinon.assert.calledOnce(setStub); |
| 105 | + sinon.assert.calledWith(setStub, 'enterprise-id', enterpriseId); |
| 106 | + sinon.assert.calledOnce(resultStub); |
| 107 | + result.should.deepEqual(listing); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('getListingEntryContacts', function () { |
| 112 | + it('should pass params as query string, not request body', async function () { |
| 113 | + const { queryStub } = makeGetStub(); |
| 114 | + const params = { status: 'ACTIVE' as const, limit: 5 }; |
| 115 | + |
| 116 | + await addressBook.getListingEntryContacts(params); |
| 117 | + |
| 118 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/contacts'); |
| 119 | + sinon.assert.calledOnce(queryStub); |
| 120 | + sinon.assert.calledWith(queryStub, params); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should work with no params', async function () { |
| 124 | + const { queryStub } = makeGetStub(); |
| 125 | + |
| 126 | + await addressBook.getListingEntryContacts(); |
| 127 | + |
| 128 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/contacts'); |
| 129 | + sinon.assert.calledOnce(queryStub); |
| 130 | + sinon.assert.calledWith(queryStub, {}); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('getListingEntryDirectory', function () { |
| 135 | + it('should pass params as query string, not request body', async function () { |
| 136 | + const { queryStub } = makeGetStub(); |
| 137 | + const params = { status: 'ACTIVE' as const, limit: 5 }; |
| 138 | + |
| 139 | + await addressBook.getListingEntryDirectory(params); |
| 140 | + |
| 141 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/directory'); |
| 142 | + sinon.assert.calledOnce(queryStub); |
| 143 | + sinon.assert.calledWith(queryStub, params); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should work with no params', async function () { |
| 147 | + const { queryStub } = makeGetStub(); |
| 148 | + |
| 149 | + await addressBook.getListingEntryDirectory(); |
| 150 | + |
| 151 | + sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/directory'); |
| 152 | + sinon.assert.calledOnce(queryStub); |
| 153 | + sinon.assert.calledWith(queryStub, {}); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments