Skip to content

Commit 3614303

Browse files
authored
feat: WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED flag in backend (#3719)
* feat(backend): add optional WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED env var * feat(backend): only poll for wallet addresses if WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED true * feat(localenv): set WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED to true for hlb * test(backend): update wallet address route tests * chore(integration): enable WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED in hlb
1 parent 52dd0d6 commit 3614303

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

localenv/happy-life-bank/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ services:
7676
ENABLE_TELEMETRY: true
7777
KEY_ID: 53f2d913-e98a-40b9-b270-372d0547f23d
7878
OPERATOR_TENANT_ID: cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d
79+
WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED: true
7980
depends_on:
8081
- cloud-nine-backend
8182
healthcheck:

packages/backend/src/config/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ export const Config = {
171171
'GRAPHQL_IDEMPOTENCY_KEY_TTL_MS',
172172
86400000
173173
),
174+
walletAddressNotFoundPollingEnabled: envBool(
175+
'WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED',
176+
false
177+
),
174178
walletAddressLookupTimeoutMs: envInt(
175179
'WALLET_ADDRESS_LOOKUP_TIMEOUT_MS',
176180
1500

packages/backend/src/open_payments/wallet_address/routes.test.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import assert from 'assert'
1313
import { OpenPaymentsServerRouteError } from '../route-errors'
1414
import { WalletAddressService } from './service'
1515
import { WalletAddressAdditionalProperty } from './additional_property/model'
16+
import { withConfigOverride } from '../../tests/helpers'
1617

1718
describe('Wallet Address Routes', (): void => {
1819
let deps: IocContract<AppServices>
@@ -47,7 +48,7 @@ describe('Wallet Address Routes', (): void => {
4748
headers: { Accept: 'application/json' }
4849
})
4950
jest
50-
.spyOn(walletAddressService, 'getOrPollByUrl')
51+
.spyOn(walletAddressService, 'getByUrl')
5152
.mockResolvedValueOnce(undefined)
5253

5354
expect.assertions(2)
@@ -73,10 +74,7 @@ describe('Wallet Address Routes', (): void => {
7374
})
7475
ctx.walletAddressUrl = walletAddress.address
7576

76-
const getOrPollByUrlSpy = jest.spyOn(
77-
walletAddressService,
78-
'getOrPollByUrl'
79-
)
77+
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')
8078

8179
expect.assertions(3)
8280
try {
@@ -85,7 +83,7 @@ describe('Wallet Address Routes', (): void => {
8583
assert(err instanceof OpenPaymentsServerRouteError)
8684
expect(err.status).toBe(404)
8785
expect(err.message).toBe('Could not get wallet address')
88-
await expect(getOrPollByUrlSpy.mock.results[0].value).resolves.toEqual(
86+
await expect(getByUrlSpy.mock.results[0].value).resolves.toEqual(
8987
walletAddress
9088
)
9189
}
@@ -146,19 +144,65 @@ describe('Wallet Address Routes', (): void => {
146144
}
147145
})
148146

147+
test(
148+
'polls wallet address if config enabled',
149+
withConfigOverride(
150+
() => Config,
151+
{ walletAddressNotFoundPollingEnabled: true },
152+
async (): Promise<void> => {
153+
const walletAddress = await createWalletAddress(deps, {
154+
tenantId: config.operatorTenantId,
155+
publicName: faker.person.firstName()
156+
})
157+
158+
const getOrPollByUrlSpy = jest
159+
.spyOn(walletAddressService, 'getOrPollByUrl')
160+
.mockResolvedValueOnce(walletAddress)
161+
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')
162+
163+
const ctx = createContext<WalletAddressUrlContext>({
164+
headers: { Accept: 'application/json' },
165+
url: '/'
166+
})
167+
ctx.walletAddressUrl = walletAddress.address
168+
await expect(walletAddressRoutes.get(ctx)).resolves.toBeUndefined()
169+
expect(ctx.response).toSatisfyApiSpec()
170+
expect(getOrPollByUrlSpy).toHaveBeenCalledTimes(1)
171+
expect(getByUrlSpy).not.toHaveBeenCalled()
172+
expect(ctx.body).toEqual({
173+
id: walletAddress.address,
174+
publicName: walletAddress.publicName,
175+
assetCode: walletAddress.asset.code,
176+
assetScale: walletAddress.asset.scale,
177+
// Ensure the tenant id is returned for auth and resource server:
178+
authServer: `${config.authServerGrantUrl}/${walletAddress.tenantId}`,
179+
resourceServer: `${config.openPaymentsUrl}/${walletAddress.tenantId}`
180+
})
181+
}
182+
)
183+
)
184+
149185
test('returns wallet address', async (): Promise<void> => {
150186
const walletAddress = await createWalletAddress(deps, {
151187
tenantId: config.operatorTenantId,
152188
publicName: faker.person.firstName()
153189
})
154190

191+
const getOrPollByUrlSpy = jest.spyOn(
192+
walletAddressService,
193+
'getOrPollByUrl'
194+
)
195+
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')
196+
155197
const ctx = createContext<WalletAddressUrlContext>({
156198
headers: { Accept: 'application/json' },
157199
url: '/'
158200
})
159201
ctx.walletAddressUrl = walletAddress.address
160202
await expect(walletAddressRoutes.get(ctx)).resolves.toBeUndefined()
161203
expect(ctx.response).toSatisfyApiSpec()
204+
expect(getOrPollByUrlSpy).not.toHaveBeenCalled()
205+
expect(getByUrlSpy).toHaveBeenCalledTimes(1)
162206
expect(ctx.body).toEqual({
163207
id: walletAddress.address,
164208
publicName: walletAddress.publicName,

packages/backend/src/open_payments/wallet_address/routes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export async function getWalletAddress(
3939
throw new OpenPaymentsServerRouteError(404, 'Could not get wallet address')
4040
}
4141

42-
const walletAddress = await deps.walletAddressService.getOrPollByUrl(
43-
ctx.walletAddressUrl
44-
)
42+
const walletAddress = deps.config.walletAddressNotFoundPollingEnabled
43+
? await deps.walletAddressService.getOrPollByUrl(ctx.walletAddressUrl)
44+
: await deps.walletAddressService.getByUrl(ctx.walletAddressUrl)
4545

4646
if (!walletAddress?.isActive) {
4747
throw new OpenPaymentsServerRouteError(

test/testenv/happy-life-bank/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ services:
4040
OPERATOR_TENANT_ID: cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d
4141
ADMIN_API_SECRET: iyIgCprjb9uL8wFckR+pLEkJWMB7FJhgkvqhTQR/964=
4242
ADMIN_API_SIGNATURE_VERSION: 1
43+
WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED: true
4344
volumes:
4445
- ../private-key.pem:/workspace/private-key.pem
4546
depends_on:

0 commit comments

Comments
 (0)