-
Notifications
You must be signed in to change notification settings - Fork 303
fix: use query params for GET address-book methods #9084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+171
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
modules/sdk-core/test/unit/bitgo/address-book/address-book.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import * as sinon from 'sinon'; | ||
| import * as superagent from 'superagent'; | ||
| import 'should'; | ||
| import { AddressBook } from '../../../../src/bitgo/address-book/address-book'; | ||
|
|
||
| describe('AddressBook', function () { | ||
| let addressBook: AddressBook; | ||
| let mockBitGo: any; | ||
| const enterpriseId = 'test-enterprise-id'; | ||
|
|
||
| function makeGetStub() { | ||
| const queryStub = sinon.stub().returns({ result: sinon.stub().resolves({}) }); | ||
| const setStub = sinon.stub().returns({ query: queryStub }); | ||
| mockBitGo.get.returns({ set: setStub }); | ||
|
seanchen0818 marked this conversation as resolved.
|
||
| return { setStub, queryStub }; | ||
| } | ||
|
|
||
| function makeParameterlessGetStub(response: Record<string, unknown> = {}) { | ||
| const resultStub = sinon.stub().resolves(response); | ||
| const setStub = sinon.stub().returns({ result: resultStub }); | ||
| mockBitGo.get.returns({ set: setStub }); | ||
| return { setStub, resultStub }; | ||
| } | ||
|
|
||
| beforeEach(function () { | ||
| mockBitGo = { | ||
| get: sinon.stub(), | ||
| microservicesUrl: sinon.stub().callsFake((path: string) => `https://app.bitgo.com${path}`), | ||
| }; | ||
| addressBook = new AddressBook(enterpriseId, mockBitGo); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| describe('getConnections', function () { | ||
| it('should pass params as query string, not request body', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
| const params = { connectionType: 'DVP' as const, status: 'INACTIVE' as const, offset: 0, limit: 10 }; | ||
|
|
||
| await addressBook.getConnections(params); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, params); | ||
| }); | ||
|
|
||
| it('should work with no params', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
|
|
||
| await addressBook.getConnections(); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, {}); | ||
| }); | ||
|
|
||
| it('should pass array filters to query unchanged', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
| const params = { | ||
| ownerWalletId: ['wallet-a', 'wallet-b'], | ||
| targetWalletId: ['wallet-c'], | ||
| }; | ||
|
|
||
| await addressBook.getConnections(params); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/connections'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, params); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getConnections array query param serialization', function () { | ||
| it('superagent serializes string[] as repeated keys, which address-book accepts via nonEmptyArrayFromQueryParam', function () { | ||
| const req = superagent.get('https://example.com').query({ | ||
| ownerWalletId: ['wallet-a', 'wallet-b'], | ||
| targetWalletId: ['wallet-c'], | ||
| }); | ||
| // Trigger superagent's query-string assembly without sending the request. | ||
| req.end(() => undefined); | ||
|
|
||
| req.url!.should.equal( | ||
| 'https://example.com?ownerWalletId=wallet-a&ownerWalletId=wallet-b&targetWalletId=wallet-c' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getListing', function () { | ||
| it('should use GET with enterprise-id header and no query or body', async function () { | ||
| const listing = { | ||
| id: 'listing-id', | ||
| enterpriseId, | ||
| name: 'Test Listing', | ||
| owner: 'owner', | ||
| createdAt: '2024-01-01', | ||
| updatedAt: '2024-01-01', | ||
| }; | ||
| const { setStub, resultStub } = makeParameterlessGetStub(listing); | ||
|
|
||
| const result = await addressBook.getListing(); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/global'); | ||
| sinon.assert.calledOnce(setStub); | ||
| sinon.assert.calledWith(setStub, 'enterprise-id', enterpriseId); | ||
| sinon.assert.calledOnce(resultStub); | ||
| result.should.deepEqual(listing); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getListingEntryContacts', function () { | ||
| it('should pass params as query string, not request body', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
| const params = { status: 'ACTIVE' as const, limit: 5 }; | ||
|
|
||
| await addressBook.getListingEntryContacts(params); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/contacts'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, params); | ||
| }); | ||
|
|
||
| it('should work with no params', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
|
|
||
| await addressBook.getListingEntryContacts(); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/contacts'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, {}); | ||
| }); | ||
| }); | ||
|
seanchen0818 marked this conversation as resolved.
|
||
|
|
||
| describe('getListingEntryDirectory', function () { | ||
| it('should pass params as query string, not request body', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
| const params = { status: 'ACTIVE' as const, limit: 5 }; | ||
|
|
||
| await addressBook.getListingEntryDirectory(params); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/directory'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, params); | ||
| }); | ||
|
|
||
| it('should work with no params', async function () { | ||
| const { queryStub } = makeGetStub(); | ||
|
|
||
| await addressBook.getListingEntryDirectory(); | ||
|
|
||
| sinon.assert.calledWith(mockBitGo.get, 'https://app.bitgo.com/api/address-book/v1/listing/entry/directory'); | ||
| sinon.assert.calledOnce(queryStub); | ||
| sinon.assert.calledWith(queryStub, {}); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.