Skip to content
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

feat: only poll for payment pointers in the open payments middleware #1528

Merged
merged 1 commit into from
Jun 29, 2023
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
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