Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions localenv/happy-life-bank/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ services:
ENABLE_TELEMETRY: true
KEY_ID: 53f2d913-e98a-40b9-b270-372d0547f23d
OPERATOR_TENANT_ID: cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d
WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED: true
depends_on:
- cloud-nine-backend
healthcheck:
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ export const Config = {
'GRAPHQL_IDEMPOTENCY_KEY_TTL_MS',
86400000
),
walletAddressNotFoundPollingEnabled: envBool(
'WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED',
false
),
walletAddressLookupTimeoutMs: envInt(
'WALLET_ADDRESS_LOOKUP_TIMEOUT_MS',
1500
Expand Down
56 changes: 50 additions & 6 deletions packages/backend/src/open_payments/wallet_address/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import assert from 'assert'
import { OpenPaymentsServerRouteError } from '../route-errors'
import { WalletAddressService } from './service'
import { WalletAddressAdditionalProperty } from './additional_property/model'
import { withConfigOverride } from '../../tests/helpers'

describe('Wallet Address Routes', (): void => {
let deps: IocContract<AppServices>
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('Wallet Address Routes', (): void => {
headers: { Accept: 'application/json' }
})
jest
.spyOn(walletAddressService, 'getOrPollByUrl')
.spyOn(walletAddressService, 'getByUrl')
.mockResolvedValueOnce(undefined)

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

const getOrPollByUrlSpy = jest.spyOn(
walletAddressService,
'getOrPollByUrl'
)
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')

expect.assertions(3)
try {
Expand All @@ -85,7 +83,7 @@ describe('Wallet Address Routes', (): void => {
assert(err instanceof OpenPaymentsServerRouteError)
expect(err.status).toBe(404)
expect(err.message).toBe('Could not get wallet address')
await expect(getOrPollByUrlSpy.mock.results[0].value).resolves.toEqual(
await expect(getByUrlSpy.mock.results[0].value).resolves.toEqual(
walletAddress
)
}
Expand Down Expand Up @@ -146,19 +144,65 @@ describe('Wallet Address Routes', (): void => {
}
})

test(
'polls wallet address if config enabled',
withConfigOverride(
() => Config,
{ walletAddressNotFoundPollingEnabled: true },
async (): Promise<void> => {
const walletAddress = await createWalletAddress(deps, {
tenantId: config.operatorTenantId,
publicName: faker.person.firstName()
})

const getOrPollByUrlSpy = jest
.spyOn(walletAddressService, 'getOrPollByUrl')
.mockResolvedValueOnce(walletAddress)
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')

const ctx = createContext<WalletAddressUrlContext>({
headers: { Accept: 'application/json' },
url: '/'
})
ctx.walletAddressUrl = walletAddress.address
await expect(walletAddressRoutes.get(ctx)).resolves.toBeUndefined()
expect(ctx.response).toSatisfyApiSpec()
expect(getOrPollByUrlSpy).toHaveBeenCalledTimes(1)
expect(getByUrlSpy).not.toHaveBeenCalled()
expect(ctx.body).toEqual({
id: walletAddress.address,
publicName: walletAddress.publicName,
assetCode: walletAddress.asset.code,
assetScale: walletAddress.asset.scale,
// Ensure the tenant id is returned for auth and resource server:
authServer: `${config.authServerGrantUrl}/${walletAddress.tenantId}`,
resourceServer: `${config.openPaymentsUrl}/${walletAddress.tenantId}`
})
}
)
)

test('returns wallet address', async (): Promise<void> => {
const walletAddress = await createWalletAddress(deps, {
tenantId: config.operatorTenantId,
publicName: faker.person.firstName()
})

const getOrPollByUrlSpy = jest.spyOn(
walletAddressService,
'getOrPollByUrl'
)
const getByUrlSpy = jest.spyOn(walletAddressService, 'getByUrl')

const ctx = createContext<WalletAddressUrlContext>({
headers: { Accept: 'application/json' },
url: '/'
})
ctx.walletAddressUrl = walletAddress.address
await expect(walletAddressRoutes.get(ctx)).resolves.toBeUndefined()
expect(ctx.response).toSatisfyApiSpec()
expect(getOrPollByUrlSpy).not.toHaveBeenCalled()
expect(getByUrlSpy).toHaveBeenCalledTimes(1)
expect(ctx.body).toEqual({
id: walletAddress.address,
publicName: walletAddress.publicName,
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/open_payments/wallet_address/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export async function getWalletAddress(
throw new OpenPaymentsServerRouteError(404, 'Could not get wallet address')
}

const walletAddress = await deps.walletAddressService.getOrPollByUrl(
ctx.walletAddressUrl
)
const walletAddress = deps.config.walletAddressNotFoundPollingEnabled
? await deps.walletAddressService.getOrPollByUrl(ctx.walletAddressUrl)
: await deps.walletAddressService.getByUrl(ctx.walletAddressUrl)

if (!walletAddress?.isActive) {
throw new OpenPaymentsServerRouteError(
Expand Down
1 change: 1 addition & 0 deletions test/testenv/happy-life-bank/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
OPERATOR_TENANT_ID: cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d
ADMIN_API_SECRET: iyIgCprjb9uL8wFckR+pLEkJWMB7FJhgkvqhTQR/964=
ADMIN_API_SIGNATURE_VERSION: 1
WALLET_ADDRESS_NOT_FOUND_POLLING_ENABLED: true
volumes:
- ../private-key.pem:/workspace/private-key.pem
depends_on:
Expand Down
Loading