-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): support SPSP queries (#1032)
* fix(backend): allow SPSP query to payment pointer * feat(backend): allow SPSP query to Open Payments connection Generalize SPSP routes. * chore(backend): add connection middleware Add incoming payment to ctx for both SPSP and connection query. * chore(backend): add SPSP middleware
- Loading branch information
1 parent
d7d5a31
commit 4df802b
Showing
10 changed files
with
362 additions
and
171 deletions.
There are no files selected for viewing
This file contains 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
70 changes: 70 additions & 0 deletions
70
packages/backend/src/open_payments/connection/middleware.test.ts
This file contains 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,70 @@ | ||
import assert from 'assert' | ||
import { v4 as uuid } from 'uuid' | ||
import { connectionMiddleware, ConnectionContext } from './middleware' | ||
import { Config } from '../../config/app' | ||
import { IocContract } from '@adonisjs/fold' | ||
import { initIocContainer } from '../../' | ||
import { AppServices } from '../../app' | ||
import { createTestApp, TestContainer } from '../../tests/app' | ||
import { createAsset } from '../../tests/asset' | ||
import { createContext } from '../../tests/context' | ||
import { createIncomingPayment } from '../../tests/incomingPayment' | ||
import { createPaymentPointer } from '../../tests/paymentPointer' | ||
import { truncateTables } from '../../tests/tableManager' | ||
|
||
describe('Connection Middleware', (): void => { | ||
let deps: IocContract<AppServices> | ||
let appContainer: TestContainer | ||
let ctx: ConnectionContext | ||
let next: jest.MockedFunction<() => Promise<void>> | ||
|
||
beforeAll(async (): Promise<void> => { | ||
deps = await initIocContainer(Config) | ||
appContainer = await createTestApp(deps) | ||
}) | ||
|
||
beforeEach((): void => { | ||
ctx = createContext<ConnectionContext>( | ||
{ | ||
headers: { | ||
Accept: 'application/json' | ||
} | ||
}, | ||
{} | ||
) | ||
ctx.container = deps | ||
next = jest.fn() | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await truncateTables(appContainer.knex) | ||
}) | ||
|
||
afterAll(async (): Promise<void> => { | ||
await appContainer.shutdown() | ||
}) | ||
|
||
test('returns 404 for unknown connection id', async (): Promise<void> => { | ||
ctx.params.id = uuid() | ||
await expect(connectionMiddleware(ctx, next)).rejects.toMatchObject({ | ||
status: 404, | ||
message: 'Not Found' | ||
}) | ||
expect(next).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('sets the context incomingPayment and calls next', async (): Promise<void> => { | ||
const asset = await createAsset(deps) | ||
const { id: paymentPointerId } = await createPaymentPointer(deps, { | ||
assetId: asset.id | ||
}) | ||
const incomingPayment = await createIncomingPayment(deps, { | ||
paymentPointerId | ||
}) | ||
assert.ok(incomingPayment.connectionId) | ||
ctx.params.id = incomingPayment.connectionId | ||
await expect(connectionMiddleware(ctx, next)).resolves.toBeUndefined() | ||
expect(next).toHaveBeenCalled() | ||
expect(ctx.incomingPayment).toEqual(incomingPayment) | ||
}) | ||
}) |
23 changes: 23 additions & 0 deletions
23
packages/backend/src/open_payments/connection/middleware.ts
This file contains 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,23 @@ | ||
import { AppContext } from '../../app' | ||
import { IncomingPayment } from '../payment/incoming/model' | ||
|
||
export interface ConnectionContext extends AppContext { | ||
incomingPayment: IncomingPayment | ||
} | ||
|
||
export const connectionMiddleware = async ( | ||
ctx: Omit<ConnectionContext, 'incomingPayment'> & { | ||
incomingPayment: Partial<ConnectionContext['incomingPayment']> | ||
}, | ||
next: () => Promise<unknown> | ||
): Promise<void> => { | ||
const incomingPaymentService = await ctx.container.use( | ||
'incomingPaymentService' | ||
) | ||
const incomingPayment = await incomingPaymentService.getByConnection( | ||
ctx.params.id | ||
) | ||
if (!incomingPayment) return ctx.throw(404) | ||
ctx.incomingPayment = incomingPayment | ||
await next() | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.