Skip to content

Commit

Permalink
chore(auth): return objects during GNAP errors (#2779)
Browse files Browse the repository at this point in the history
* chore(auth): return object on gnap error

* chore(auth): remove unused file

* chore(bruno): add conditionals to post-request scripts to not hide errors
  • Loading branch information
mkurapov authored Jun 25, 2024
1 parent ca95fe2 commit 0a6c72a
Show file tree
Hide file tree
Showing 25 changed files with 161 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions bruno/collections/Rafiki/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
33 changes: 20 additions & 13 deletions packages/auth/src/shared/gnapErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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<void> => {
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)
})
})
Loading

0 comments on commit 0a6c72a

Please sign in to comment.