Skip to content

Commit

Permalink
fix: remove wallet address from op resource urls
Browse files Browse the repository at this point in the history
  • Loading branch information
sabineschaller committed Oct 24, 2023
1 parent 8bdbf05 commit dcad031
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
43 changes: 17 additions & 26 deletions packages/backend/src/open_payments/payment/incoming/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ import { IlpStreamCredentials } from '../../../payment-method/ilp/stream-credent
import { serializeAmount } from '../../amount'
import { IlpAddress } from 'ilp-packet'
import { IncomingPayment, IncomingPaymentState } from './model'
import { WalletAddress } from '../../wallet_address/model'

describe('Incoming Payment Model', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
let walletAddress: WalletAddress
let baseUrl: string
let incomingPayment: IncomingPayment

beforeAll(async (): Promise<void> => {
deps = initIocContainer(Config)
appContainer = await createTestApp(deps)
})

beforeEach(async (): Promise<void> => {
walletAddress = await createWalletAddress(deps)
baseUrl = new URL(walletAddress.url).origin
incomingPayment = await createIncomingPayment(deps, {
walletAddressId: walletAddress.id,
metadata: { description: 'my payment' }
})
})

afterEach(async (): Promise<void> => {
jest.useRealTimers()
await truncateTables(appContainer.knex)
Expand All @@ -31,14 +44,8 @@ describe('Incoming Payment Model', (): void => {

describe('toOpenPaymentsType', () => {
test('returns incoming payment', async () => {
const walletAddress = await createWalletAddress(deps)
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId: walletAddress.id,
metadata: { description: 'my payment' }
})

expect(incomingPayment.toOpenPaymentsType(walletAddress)).toEqual({
id: `${walletAddress.url}${IncomingPayment.urlPath}/${incomingPayment.id}`,
id: `${baseUrl}${IncomingPayment.urlPath}/${incomingPayment.id}`,
walletAddress: walletAddress.url,
completed: incomingPayment.completed,
receivedAmount: serializeAmount(incomingPayment.receivedAmount),
Expand All @@ -55,12 +62,6 @@ describe('Incoming Payment Model', (): void => {

describe('toOpenPaymentsTypeWithMethods', () => {
test('returns incoming payment with payment methods', async () => {
const walletAddress = await createWalletAddress(deps)
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId: walletAddress.id,
metadata: { description: 'my payment' }
})

const streamCredentials: IlpStreamCredentials = {
ilpAddress: 'test.ilp' as IlpAddress,
sharedSecret: Buffer.from('')
Expand All @@ -72,7 +73,7 @@ describe('Incoming Payment Model', (): void => {
streamCredentials
)
).toEqual({
id: `${walletAddress.url}${IncomingPayment.urlPath}/${incomingPayment.id}`,
id: `${baseUrl}${IncomingPayment.urlPath}/${incomingPayment.id}`,
walletAddress: walletAddress.url,
completed: incomingPayment.completed,
receivedAmount: serializeAmount(incomingPayment.receivedAmount),
Expand All @@ -94,15 +95,10 @@ describe('Incoming Payment Model', (): void => {
})

test('returns incoming payment with empty methods when stream credentials are undefined', async () => {
const walletAddress = await createWalletAddress(deps)
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId: walletAddress.id,
metadata: { description: 'my payment' }
})
expect(
incomingPayment.toOpenPaymentsTypeWithMethods(walletAddress)
).toEqual({
id: `${walletAddress.url}${IncomingPayment.urlPath}/${incomingPayment.id}`,
id: `${baseUrl}${IncomingPayment.urlPath}/${incomingPayment.id}`,
walletAddress: walletAddress.url,
completed: incomingPayment.completed,
receivedAmount: serializeAmount(incomingPayment.receivedAmount),
Expand All @@ -120,11 +116,6 @@ describe('Incoming Payment Model', (): void => {
test.each([IncomingPaymentState.Completed, IncomingPaymentState.Expired])(
'returns incoming payment with empty methods if payment state is %s',
async (paymentState): Promise<void> => {
const walletAddress = await createWalletAddress(deps)
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId: walletAddress.id,
metadata: { description: 'my payment' }
})
incomingPayment.state = paymentState

const streamCredentials: IlpStreamCredentials = {
Expand All @@ -138,7 +129,7 @@ describe('Incoming Payment Model', (): void => {
streamCredentials
)
).toEqual({
id: `${walletAddress.url}${IncomingPayment.urlPath}/${incomingPayment.id}`,
id: `${baseUrl}${IncomingPayment.urlPath}/${incomingPayment.id}`,
walletAddress: walletAddress.url,
completed: incomingPayment.completed,
receivedAmount: serializeAmount(incomingPayment.receivedAmount),
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/payment/incoming/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export class IncomingPayment
}

public getUrl(walletAddress: WalletAddress): string {
return `${walletAddress.url}${IncomingPayment.urlPath}/${this.id}`
const url = new URL(walletAddress.url)
return `${url.origin}${IncomingPayment.urlPath}/${this.id}`
}

public async onCredit({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Incoming Payment Routes', (): void => {

let asset: Asset
let walletAddress: WalletAddress
let baseUrl: string
let expiresAt: Date
let incomingAmount: Amount
let metadata: Record<string, unknown>
Expand All @@ -60,6 +61,7 @@ describe('Incoming Payment Routes', (): void => {
walletAddress = await createWalletAddress(deps, {
assetId: asset.id
})
baseUrl = new URL(walletAddress.url).origin
incomingAmount = {
value: BigInt('123'),
assetScale: asset.scale,
Expand Down Expand Up @@ -247,7 +249,7 @@ describe('Incoming Payment Routes', (): void => {
.pop()

expect(ctx.response.body).toEqual({
id: `${walletAddress.url}/incoming-payments/${incomingPaymentId}`,
id: `${baseUrl}/incoming-payments/${incomingPaymentId}`,
walletAddress: walletAddress.url,
incomingAmount: incomingAmount ? amount : undefined,
expiresAt: expiresAt || expect.any(String),
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/payment/outgoing/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class OutgoingPayment
}

public getUrl(walletAddress: WalletAddress): string {
return `${walletAddress.url}${OutgoingPayment.urlPath}/${this.id}`
const url = new URL(walletAddress.url)
return `${url.origin}${OutgoingPayment.urlPath}/${this.id}`
}

public get asset(): Asset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Outgoing Payment Routes', (): void => {
let outgoingPaymentRoutes: OutgoingPaymentRoutes
let outgoingPaymentService: OutgoingPaymentService
let walletAddress: WalletAddress
let baseUrl: string

const receivingWalletAddress = `https://wallet.example/${uuid()}`

Expand Down Expand Up @@ -69,6 +70,7 @@ describe('Outgoing Payment Routes', (): void => {
beforeEach(async (): Promise<void> => {
const asset = await createAsset(deps)
walletAddress = await createWalletAddress(deps, { assetId: asset.id })
baseUrl = new URL(walletAddress.url).origin
})

afterEach(async (): Promise<void> => {
Expand Down Expand Up @@ -104,7 +106,7 @@ describe('Outgoing Payment Routes', (): void => {
get: (ctx) => outgoingPaymentRoutes.get(ctx),
getBody: (outgoingPayment) => {
return {
id: `${walletAddress.url}/outgoing-payments/${outgoingPayment.id}`,
id: `${baseUrl}/outgoing-payments/${outgoingPayment.id}`,
walletAddress: walletAddress.url,
receiver: outgoingPayment.receiver,
quoteId: outgoingPayment.quote.getUrl(walletAddress),
Expand Down Expand Up @@ -171,7 +173,7 @@ describe('Outgoing Payment Routes', (): void => {
metadata
})
const options = {
quoteId: `${walletAddress.url}/quotes/${payment.quote.id}`,
quoteId: `${baseUrl}/quotes/${payment.quote.id}`,
client,
grant,
metadata
Expand All @@ -195,7 +197,7 @@ describe('Outgoing Payment Routes', (): void => {
.split('/')
.pop()
expect(ctx.response.body).toEqual({
id: `${walletAddress.url}/outgoing-payments/${outgoingPaymentId}`,
id: `${baseUrl}/outgoing-payments/${outgoingPaymentId}`,
walletAddress: walletAddress.url,
receiver: payment.receiver,
quoteId: options.quoteId,
Expand Down Expand Up @@ -225,7 +227,7 @@ describe('Outgoing Payment Routes', (): void => {
async (error): Promise<void> => {
const quoteId = uuid()
const ctx = setup({
quoteId: `${walletAddress.url}/quotes/${quoteId}`
quoteId: `${baseUrl}/quotes/${quoteId}`
})
const createSpy = jest
.spyOn(outgoingPaymentService, 'create')
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/quote/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export class Quote extends WalletAddressSubresource {
private debitAmountValue!: bigint

public getUrl(walletAddress: WalletAddress): string {
return `${walletAddress.url}${Quote.urlPath}/${this.id}`
const url = new URL(walletAddress.url)
return `${url.origin}${Quote.urlPath}/${this.id}`
}

public get debitAmount(): Amount {
Expand Down
10 changes: 6 additions & 4 deletions packages/backend/src/open_payments/quote/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ describe('Quote Routes', (): void => {
let config: IAppConfig
let quoteRoutes: QuoteRoutes
let walletAddress: WalletAddress
let baseUrl: string

const receiver = `https://wallet2.example/bob/incoming-payments/${uuid()}`
const receiver = `https://wallet2.example/incoming-payments/${uuid()}`
const asset = randomAsset()
const debitAmount: Amount = {
value: BigInt(123),
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('Quote Routes', (): void => {
walletAddress = await createWalletAddress(deps, {
assetId
})
baseUrl = new URL(walletAddress.url).origin
})

afterEach(async (): Promise<void> => {
Expand All @@ -99,7 +101,7 @@ describe('Quote Routes', (): void => {
get: (ctx) => quoteRoutes.get(ctx),
getBody: (quote) => {
return {
id: `${walletAddress.url}/quotes/${quote.id}`,
id: `${baseUrl}/quotes/${quote.id}`,
walletAddress: walletAddress.url,
receiver: quote.receiver,
debitAmount: serializeAmount(quote.debitAmount),
Expand Down Expand Up @@ -222,7 +224,7 @@ describe('Quote Routes', (): void => {
.pop()
assert.ok(quote)
expect(ctx.response.body).toEqual({
id: `${walletAddress.url}/quotes/${quoteId}`,
id: `${baseUrl}/quotes/${quoteId}`,
walletAddress: walletAddress.url,
receiver: quote.receiver,
debitAmount: {
Expand Down Expand Up @@ -272,7 +274,7 @@ describe('Quote Routes', (): void => {
.pop()
assert.ok(quote)
expect(ctx.response.body).toEqual({
id: `${walletAddress.url}/quotes/${quoteId}`,
id: `${baseUrl}/quotes/${quoteId}`,
walletAddress: walletAddress.url,
receiver: options.receiver,
debitAmount: {
Expand Down

0 comments on commit dcad031

Please sign in to comment.