diff --git a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Continuation Request.bru b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Continuation Request.bru index c75d5df7ea..01d39ebf3f 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Continuation Request.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Continuation Request.bru @@ -21,9 +21,9 @@ script:pre-request { } script:post-response { - const body = res.getBody(); - bru.setEnvVar("accessToken", body.access_token.value); - bru.setEnvVar("tokenId", body.access_token.manage.split('/').pop()) + const scripts = require('./scripts'); + + scripts.storeTokenDetails(); } tests { diff --git a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Incoming Payment.bru b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Incoming Payment.bru index 1851537335..6fffd0742c 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Incoming Payment.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Incoming Payment.bru @@ -36,20 +36,24 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); - bru.setEnvVar("incomingPaymentUrl", body.id); - bru.setEnvVar("debitAmount", JSON.stringify({ - "value": "500", - "assetCode": "USD", - "assetScale": 2 - })) + if (body?.id) { + bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + bru.setEnvVar("incomingPaymentUrl", body.id); + + bru.setEnvVar("debitAmount", JSON.stringify({ + "value": "500", + "assetCode": "USD", + "assetScale": 2 + })) + + bru.setEnvVar("receiveAmount", JSON.stringify({ + "value": "500", + "assetCode": "USD", + "assetScale": 2 + })) + } - bru.setEnvVar("receiveAmount", JSON.stringify({ - "value": "500", - "assetCode": "USD", - "assetScale": 2 - })) } tests { diff --git a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Outgoing Payment.bru b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Outgoing Payment.bru index 5095764d0f..4d9e3a2a63 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Outgoing Payment.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Create Outgoing Payment.bru @@ -35,11 +35,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - console.log(res.status) - console.log(res.statusText) - console.log({body}) - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); - + + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get receiver wallet address.bru b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get receiver wallet address.bru index 8f2e84a2ee..5b9b0277a9 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get receiver wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get receiver wallet address.bru @@ -23,7 +23,11 @@ script:pre-request { script:post-response { const url = require('url') - const body = res.body + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() bru.setEnvVar("receiverAssetCode", body?.assetCode) bru.setEnvVar("receiverAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get sender wallet address.bru b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get sender wallet address.bru index fe4b6f2fd3..384cfa6282 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get sender wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments Without Quote/Get sender wallet address.bru @@ -23,7 +23,12 @@ script:pre-request { script:post-response { const url = require('url') - const body = res.body + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() + bru.setEnvVar("senderAssetCode", body?.assetCode) bru.setEnvVar("senderAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Continuation Request.bru b/bruno/collections/Rafiki/Examples/Open Payments/Continuation Request.bru index c75d5df7ea..01d39ebf3f 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Continuation Request.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Continuation Request.bru @@ -21,9 +21,9 @@ script:pre-request { } script:post-response { - const body = res.getBody(); - bru.setEnvVar("accessToken", body.access_token.value); - bru.setEnvVar("tokenId", body.access_token.manage.split('/').pop()) + const scripts = require('./scripts'); + + scripts.storeTokenDetails(); } tests { diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Create Incoming Payment.bru b/bruno/collections/Rafiki/Examples/Open Payments/Create Incoming Payment.bru index 553c9ef622..afc6464ee6 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Create Incoming Payment.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Create Incoming Payment.bru @@ -41,7 +41,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + + if (body?.id) { + bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + } } diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Create Outgoing Payment.bru b/bruno/collections/Rafiki/Examples/Open Payments/Create Outgoing Payment.bru index 58601494f7..e14eaa6536 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Create Outgoing Payment.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Create Outgoing Payment.bru @@ -34,7 +34,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Create Quote.bru b/bruno/collections/Rafiki/Examples/Open Payments/Create Quote.bru index f7c3ea5995..a7708217f4 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Create Quote.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Create Quote.bru @@ -32,9 +32,11 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("quoteId", body.id.split("/").pop()); - bru.setEnvVar("quoteDebitAmount", JSON.stringify(body.debitAmount)) - bru.setEnvVar("quoteReceiveAmount", JSON.stringify(body.receiveAmount)) + if (body?.id) { + bru.setEnvVar("quoteId", body.id.split("/").pop()); + bru.setEnvVar("quoteDebitAmount", JSON.stringify(body.debitAmount)) + bru.setEnvVar("quoteReceiveAmount", JSON.stringify(body.receiveAmount)) + } } diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Get receiver wallet address.bru b/bruno/collections/Rafiki/Examples/Open Payments/Get receiver wallet address.bru index 8f2e84a2ee..9e5acc50c4 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Get receiver wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Get receiver wallet address.bru @@ -22,8 +22,12 @@ script:pre-request { script:post-response { const url = require('url') - - const body = res.body + + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() bru.setEnvVar("receiverAssetCode", body?.assetCode) bru.setEnvVar("receiverAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Examples/Open Payments/Get sender wallet address.bru b/bruno/collections/Rafiki/Examples/Open Payments/Get sender wallet address.bru index fe4b6f2fd3..9665a40e32 100644 --- a/bruno/collections/Rafiki/Examples/Open Payments/Get sender wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Open Payments/Get sender wallet address.bru @@ -23,7 +23,11 @@ script:pre-request { script:post-response { const url = require('url') - const body = res.body + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() bru.setEnvVar("senderAssetCode", body?.assetCode) bru.setEnvVar("senderAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Continuation Request.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Continuation Request.bru index c75d5df7ea..01d39ebf3f 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Continuation Request.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Continuation Request.bru @@ -21,9 +21,9 @@ script:pre-request { } script:post-response { - const body = res.getBody(); - bru.setEnvVar("accessToken", body.access_token.value); - bru.setEnvVar("tokenId", body.access_token.manage.split('/').pop()) + const scripts = require('./scripts'); + + scripts.storeTokenDetails(); } tests { diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Create Incoming Payment.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Create Incoming Payment.bru index 360ad36aea..071e7df0be 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Create Incoming Payment.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Create Incoming Payment.bru @@ -36,17 +36,20 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); - bru.setEnvVar("quoteDebitAmount", JSON.stringify({ - value: "99999999999999", - assetCode: "USD", - assetScale: 2 - })) - bru.setEnvVar("quoteReceiveAmount", JSON.stringify({ - value: "99999999999999", - assetCode: "USD", - assetScale: 2 - })) + + if (body?.id) { + bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + bru.setEnvVar("quoteDebitAmount", JSON.stringify({ + value: "99999999999999", + assetCode: "USD", + assetScale: 2 + })) + bru.setEnvVar("quoteReceiveAmount", JSON.stringify({ + value: "99999999999999", + assetCode: "USD", + assetScale: 2 + })) + } } tests { diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 1.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 1.bru index d05b060e22..6bdb591816 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 1.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 1.bru @@ -40,8 +40,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); - + + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 2.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 2.bru index 32cce4c122..6dce96ac06 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 2.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 2.bru @@ -40,8 +40,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 3.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 3.bru index a37a54d190..814d2171de 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 3.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Create Outgoing Payment 3.bru @@ -40,8 +40,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Get receiver wallet address.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Get receiver wallet address.bru index 8f2e84a2ee..d59f39b24f 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Get receiver wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Get receiver wallet address.bru @@ -23,7 +23,12 @@ script:pre-request { script:post-response { const url = require('url') - const body = res.body + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() + bru.setEnvVar("receiverAssetCode", body?.assetCode) bru.setEnvVar("receiverAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Examples/Web Monetization/Get sender wallet address.bru b/bruno/collections/Rafiki/Examples/Web Monetization/Get sender wallet address.bru index fe4b6f2fd3..9665a40e32 100644 --- a/bruno/collections/Rafiki/Examples/Web Monetization/Get sender wallet address.bru +++ b/bruno/collections/Rafiki/Examples/Web Monetization/Get sender wallet address.bru @@ -23,7 +23,11 @@ script:pre-request { script:post-response { const url = require('url') - const body = res.body + if (res.getStatus() !== 200) { + return + } + + const body = res.getBody() bru.setEnvVar("senderAssetCode", body?.assetCode) bru.setEnvVar("senderAssetScale", body?.assetScale) diff --git a/bruno/collections/Rafiki/Open Payments APIs/Incoming Payments/Create Incoming Payment.bru b/bruno/collections/Rafiki/Open Payments APIs/Incoming Payments/Create Incoming Payment.bru index c72e3cd91f..c718828e1c 100644 --- a/bruno/collections/Rafiki/Open Payments APIs/Incoming Payments/Create Incoming Payment.bru +++ b/bruno/collections/Rafiki/Open Payments APIs/Incoming Payments/Create Incoming Payment.bru @@ -42,7 +42,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + + if (body?.id) { + bru.setEnvVar("incomingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Open Payments APIs/Outgoing Payments/Create Outgoing Payment.bru b/bruno/collections/Rafiki/Open Payments APIs/Outgoing Payments/Create Outgoing Payment.bru index c0e404ad13..62ec4793f7 100644 --- a/bruno/collections/Rafiki/Open Payments APIs/Outgoing Payments/Create Outgoing Payment.bru +++ b/bruno/collections/Rafiki/Open Payments APIs/Outgoing Payments/Create Outgoing Payment.bru @@ -35,7 +35,10 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + + if (body?.id) { + bru.setEnvVar("outgoingPaymentId", body.id.split("/").pop()); + } } tests { diff --git a/bruno/collections/Rafiki/Open Payments APIs/Quotes/Create Quote.bru b/bruno/collections/Rafiki/Open Payments APIs/Quotes/Create Quote.bru index 9059ff40f9..1e53624a2c 100644 --- a/bruno/collections/Rafiki/Open Payments APIs/Quotes/Create Quote.bru +++ b/bruno/collections/Rafiki/Open Payments APIs/Quotes/Create Quote.bru @@ -37,10 +37,12 @@ script:pre-request { script:post-response { const body = res.getBody(); - bru.setEnvVar("quoteId", body.id.split("/").pop()); - bru.setEnvVar("quoteDebitAmount", JSON.stringify(body.debitAmount)) - bru.setEnvVar("quoteReceiveAmount", JSON.stringify(body.receiveAmount)) - + + if (body?.id) { + bru.setEnvVar("quoteId", body.id.split("/").pop()); + bru.setEnvVar("quoteDebitAmount", JSON.stringify(body.debitAmount)) + bru.setEnvVar("quoteReceiveAmount", JSON.stringify(body.receiveAmount)) + } } tests { diff --git a/bruno/collections/Rafiki/scripts.js b/bruno/collections/Rafiki/scripts.js index d53ba17b91..8ea6b7a614 100644 --- a/bruno/collections/Rafiki/scripts.js +++ b/bruno/collections/Rafiki/scripts.js @@ -147,10 +147,16 @@ const scripts = { storeTokenDetails: function () { const body = res.getBody() - bru.setEnvVar('accessToken', body?.access_token?.value) - bru.setEnvVar('continueToken', body.continue.access_token.value) - bru.setEnvVar('continueId', body.continue.uri.split('/').pop()) - bru.setEnvVar('tokenId', body?.access_token?.manage.split('/').pop()) + + if (body?.access_token) { + bru.setEnvVar('accessToken', body.access_token.value) + bru.setEnvVar('tokenId', body.access_token.manage.split('/').pop()) + } + + if (body?.continue) { + bru.setEnvVar('continueToken', body.continue.access_token.value) + bru.setEnvVar('continueId', body.continue.uri.split('/').pop()) + } }, loadWalletAddressIdsIntoVariables: async function () { diff --git a/packages/auth/src/shared/gnapErrors.test.ts b/packages/auth/src/shared/gnapErrors.test.ts index 17eb654d44..8afe168ed1 100644 --- a/packages/auth/src/shared/gnapErrors.test.ts +++ b/packages/auth/src/shared/gnapErrors.test.ts @@ -38,14 +38,13 @@ describe('gnapServerErrorMiddleware', (): void => { throw error }) - const ctxThrowSpy = jest.spyOn(ctx, 'throw') - - await expect(gnapServerErrorMiddleware(ctx, next)).rejects.toMatchObject({ - status: error.status, - message: error.message + await expect(gnapServerErrorMiddleware(ctx, next)).resolves.toBeUndefined() + expect(ctx.body).toEqual({ + error: { + description: error.message + } }) - - expect(ctxThrowSpy).toHaveBeenCalledWith(error.status, error.message) + expect(ctx.status).toBe(error.status) expect(next).toHaveBeenCalledTimes(1) }) @@ -59,19 +58,27 @@ describe('gnapServerErrorMiddleware', (): void => { throw error }) - const ctxThrowSpy = jest.spyOn(ctx, 'throw') - - await expect(gnapServerErrorMiddleware(ctx, next)).rejects.toMatchObject({ - status: error.status, + await expect(gnapServerErrorMiddleware(ctx, next)).resolves.toBeUndefined() + expect(ctx.body).toEqual({ error: { code: error.code, description: error.message } }) + expect(ctx.status).toBe(error.status) + expect(next).toHaveBeenCalledTimes(1) + }) - expect(ctxThrowSpy).toHaveBeenCalledWith(error.status, error.code, { - error: { code: error.code, description: error.message } + test('handles unknown error', async (): Promise => { + const error = new Error('unexpected') + const next = jest.fn().mockImplementationOnce(() => { + throw error }) + + const ctxThrowSpy = jest.spyOn(ctx, 'throw') + + await expect(gnapServerErrorMiddleware(ctx, next)).rejects.toThrow() + expect(ctxThrowSpy).toHaveBeenCalledWith(500) expect(next).toHaveBeenCalledTimes(1) }) }) diff --git a/packages/auth/src/shared/gnapErrors.ts b/packages/auth/src/shared/gnapErrors.ts index 947f7eaf94..1f6444c421 100644 --- a/packages/auth/src/shared/gnapErrors.ts +++ b/packages/auth/src/shared/gnapErrors.ts @@ -57,10 +57,19 @@ export async function gnapServerErrorMiddleware( 'Received error when handling Open Payments GNAP request' ) - ctx.throw(err.status, err.code, { - error: { code: err.code, description: err.message } - }) - } else if (err instanceof OpenAPIValidatorMiddlewareError) { + const gnapErrorResponse: GNAPErrorResponse = { + error: { + code: err.code, + description: err.message + } + } + + ctx.status = err.status + ctx.body = gnapErrorResponse + return + } + + if (err instanceof OpenAPIValidatorMiddlewareError) { const finalStatus = err.status || 400 logger.info( @@ -72,7 +81,11 @@ export async function gnapServerErrorMiddleware( 'Received OpenAPI validation error when handling Open Payments GNAP request' ) - ctx.throw(finalStatus, err.message) + ctx.status = finalStatus + ctx.body = { + error: { description: err.message } + } + return } logger.error( diff --git a/packages/auth/src/tests/errors.ts b/packages/auth/src/tests/errors.ts deleted file mode 100644 index 9e9cfec806..0000000000 --- a/packages/auth/src/tests/errors.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { GNAPErrorCode, GNAPErrorResponse } from '../shared/gnapErrors' - -export function generateGNAPErrorResponse( - code: GNAPErrorCode, - description?: string -): GNAPErrorResponse { - return { - error: { - code, - description - } - } -}