Skip to content

Commit b2248b3

Browse files
authored
Merge pull request #9084 from BitGo/GN-2899-sdk-getconnections-query-params
fix: use query params for GET address-book methods
2 parents d6d0061 + 3fe461e commit b2248b3

2 files changed

Lines changed: 171 additions & 4 deletions

File tree

modules/sdk-core/src/bitgo/address-book/address-book.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export class AddressBook implements IAddressBook {
5050
*/
5151
getConnections(params?: GetAddressBookConnectionsParams): Promise<GetAddressBookConnectionsResponse> {
5252
const url = this.bitgo.microservicesUrl('/api/address-book/v1/connections');
53-
return this.bitgo.get(url).set('enterprise-id', this.enterpriseId).send(params).result();
53+
return this.bitgo
54+
.get(url)
55+
.set('enterprise-id', this.enterpriseId)
56+
.query(params ?? {})
57+
.result();
5458
}
5559

5660
/**
@@ -84,7 +88,6 @@ export class AddressBook implements IAddressBook {
8488
const response: GetAddressBookListingResponse = await this.bitgo
8589
.get(url)
8690
.set('enterprise-id', this.enterpriseId)
87-
.send()
8891
.result();
8992
this._listing = response;
9093
return this.listing() as AddressBookListing;
@@ -125,7 +128,11 @@ export class AddressBook implements IAddressBook {
125128
params?: GetAddressBookListingEntryContactsParams
126129
): Promise<GetAddressBookListingEntryContactsResponse> {
127130
const url = this.bitgo.microservicesUrl('/api/address-book/v1/listing/entry/contacts');
128-
return this.bitgo.get(url).set('enterprise-id', this.enterpriseId).send(params).result();
131+
return this.bitgo
132+
.get(url)
133+
.set('enterprise-id', this.enterpriseId)
134+
.query(params ?? {})
135+
.result();
129136
}
130137

131138
/**
@@ -135,7 +142,11 @@ export class AddressBook implements IAddressBook {
135142
params?: GetAddressBookListingEntryDirectoryParams
136143
): Promise<GetAddressBookListingEntryDirectoryResponse> {
137144
const url = this.bitgo.microservicesUrl('/api/address-book/v1/listing/entry/directory');
138-
return this.bitgo.get(url).set('enterprise-id', this.enterpriseId).send(params).result();
145+
return this.bitgo
146+
.get(url)
147+
.set('enterprise-id', this.enterpriseId)
148+
.query(params ?? {})
149+
.result();
139150
}
140151

141152
/**
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)