Skip to content

Commit

Permalink
feat: only poll PP in the open payments middleware (interledger#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurapov authored and Muasa-harman committed Jul 13, 2023
1 parent 1b33da8 commit d5fab1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function createPaymentPointerMiddleware() {
const paymentPointerService = await ctx.container.use(
'paymentPointerService'
)
const paymentPointer = await paymentPointerService.getByUrl(
const paymentPointer = await paymentPointerService.getOrPollByUrl(
ctx.paymentPointerUrl
)

Expand Down
50 changes: 36 additions & 14 deletions packages/backend/src/open_payments/payment_pointer/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,42 @@ describe('Open Payments Payment Pointer Service', (): void => {
})

describe('Get Payment Pointer By Url', (): void => {
test('can retrieve payment pointer by url', async (): Promise<void> => {
const paymentPointer = await createPaymentPointer(deps)
await expect(
paymentPointerService.getByUrl(paymentPointer.url)
).resolves.toEqual(paymentPointer)

await expect(
paymentPointerService.getByUrl(paymentPointer.url + '/path')
).resolves.toBeUndefined()

await expect(
paymentPointerService.getByUrl('prefix+' + paymentPointer.url)
).resolves.toBeUndefined()
})

test(
'returns undefined if no payment pointer exists with url',
withConfigOverride(
() => config,
{ paymentPointerLookupTimeoutMs: 1 },
async (): Promise<void> => {
await expect(
paymentPointerService.getByUrl('test.nope')
).resolves.toBeUndefined()
}
)
)
})

describe('Get Or Poll Payment Pointer By Url', (): void => {
describe('existing payment pointer', (): void => {
test('can retrieve payment pointer by url', async (): Promise<void> => {
const paymentPointer = await createPaymentPointer(deps)
await expect(
paymentPointerService.getByUrl(paymentPointer.url)
paymentPointerService.getOrPollByUrl(paymentPointer.url)
).resolves.toEqual(paymentPointer)

await expect(
paymentPointerService.getByUrl(paymentPointer.url + '/path')
).resolves.toBeUndefined()

await expect(
paymentPointerService.getByUrl('prefix+' + paymentPointer.url)
).resolves.toBeUndefined()
})
})

Expand All @@ -308,7 +330,7 @@ describe('Open Payments Payment Pointer Service', (): void => {
async (): Promise<void> => {
const paymentPointerUrl = `https://${faker.internet.domainName()}/.well-known/pay`
await expect(
paymentPointerService.getByUrl(paymentPointerUrl)
paymentPointerService.getOrPollByUrl(paymentPointerUrl)
).resolves.toBeUndefined()

const paymentPointerNotFoundEvents =
Expand All @@ -331,9 +353,9 @@ describe('Open Payments Payment Pointer Service', (): void => {
async (): Promise<void> => {
const paymentPointerUrl = `https://${faker.internet.domainName()}/.well-known/pay`

const [getByUrlPaymentPointer, createdPaymentPointer] =
const [getOrPollByUrlPaymentPointer, createdPaymentPointer] =
await Promise.all([
paymentPointerService.getByUrl(paymentPointerUrl),
paymentPointerService.getOrPollByUrl(paymentPointerUrl),
(async () => {
await sleep(5)
return createPaymentPointer(deps, {
Expand All @@ -342,8 +364,8 @@ describe('Open Payments Payment Pointer Service', (): void => {
})()
])

assert.ok(getByUrlPaymentPointer)
expect(getByUrlPaymentPointer).toEqual(createdPaymentPointer)
assert.ok(getOrPollByUrlPaymentPointer)
expect(getOrPollByUrlPaymentPointer).toEqual(createdPaymentPointer)
}
)
)
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/open_payments/payment_pointer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface PaymentPointerService {
update(options: UpdateOptions): Promise<PaymentPointer | PaymentPointerError>
get(id: string): Promise<PaymentPointer | undefined>
getByUrl(url: string): Promise<PaymentPointer | undefined>
getOrPollByUrl(url: string): Promise<PaymentPointer | undefined>
getPage(pagination?: Pagination): Promise<PaymentPointer[]>
processNext(): Promise<string | undefined>
triggerEvents(limit: number): Promise<number>
Expand Down Expand Up @@ -79,7 +80,8 @@ export async function createPaymentPointerService({
create: (options) => createPaymentPointer(deps, options),
update: (options) => updatePaymentPointer(deps, options),
get: (id) => getPaymentPointer(deps, id),
getByUrl: (url) => getOrRequestPaymentPointer(deps, url),
getByUrl: (url) => getPaymentPointerByUrl(deps, url),
getOrPollByUrl: (url) => getOrPollByUrl(deps, url),
getPage: (pagination?) => getPaymentPointerPage(deps, pagination),
processNext: () => processNextPaymentPointer(deps),
triggerEvents: (limit) => triggerPaymentPointerEvents(deps, limit)
Expand Down Expand Up @@ -173,7 +175,7 @@ async function getPaymentPointer(
.withGraphFetched('asset')
}

async function getOrRequestPaymentPointer(
async function getOrPollByUrl(
deps: ServiceDependencies,
url: string
): Promise<PaymentPointer | undefined> {
Expand Down

0 comments on commit d5fab1e

Please sign in to comment.