From 38b10754fce5a046a5d64f3d4efe459b1f68acd0 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 29 Aug 2024 18:41:07 +0530 Subject: [PATCH] fix: existing test-cases for error handling - added new test-cases --- src/v0/destinations/reddit/networkHandler.js | 6 +- .../destinations/reddit/dataDelivery/oauth.ts | 102 ++++++++++++++++-- .../destinations/reddit/network.ts | 57 ++++++++++ 3 files changed, 154 insertions(+), 11 deletions(-) diff --git a/src/v0/destinations/reddit/networkHandler.js b/src/v0/destinations/reddit/networkHandler.js index d51c8b1ea1..e691255a26 100644 --- a/src/v0/destinations/reddit/networkHandler.js +++ b/src/v0/destinations/reddit/networkHandler.js @@ -12,7 +12,6 @@ const redditRespHandler = (destResponse) => { // to handle the case when authorization-token is invalid if (status === 401) { let errorMessage = 'Authorization failed'; - let errorDetails = response; let authErrorCategory = ''; if (isString(response) && response.includes('Authorization Required')) { @@ -20,14 +19,13 @@ const redditRespHandler = (destResponse) => { authErrorCategory = REFRESH_TOKEN; } else if (response?.error?.reason === 'UNAUTHORIZED') { errorMessage = response.error.explanation || errorMessage; - errorDetails = response.error; authErrorCategory = REFRESH_TOKEN; } throw new RetryableError( - `${errorMessage} 'during reddit response transformation'`, + `${errorMessage} during reddit response transformation`, status, - errorDetails, + destResponse, authErrorCategory, ); } diff --git a/test/integrations/destinations/reddit/dataDelivery/oauth.ts b/test/integrations/destinations/reddit/dataDelivery/oauth.ts index 90368cd60b..8c0d486a7a 100644 --- a/test/integrations/destinations/reddit/dataDelivery/oauth.ts +++ b/test/integrations/destinations/reddit/dataDelivery/oauth.ts @@ -81,7 +81,7 @@ export const v0oauthScenarios = [ }, output: { response: { - status: 500, + status: 401, body: { output: { authErrorCategory: 'REFRESH_TOKEN', @@ -90,9 +90,53 @@ export const v0oauthScenarios = [ status: 401, }, message: - "Request failed due to Authorization Required 'during reddit response transformation'", + 'Request failed due to Authorization Required during reddit response transformation', statTags: expectedStatTags, - status: 500, + status: 401, + }, + }, + }, + }, + }, + { + id: 'reddit_v0_oauth_scenario_2', + name: 'reddit', + description: '[Proxy v0 API] :: Oauth where error response is an object from destination', + successCriteria: 'Should return 401 with authErrorCategory as REFRESH_TOKEN', + scenario: 'Oauth', + feature: 'dataDelivery', + module: 'destination', + version: 'v0', + input: { + request: { + body: generateProxyV0Payload({ + ...commonRequestParameters, + endpoint: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + }), + method: 'POST', + }, + }, + output: { + response: { + status: 401, + body: { + output: { + authErrorCategory: 'REFRESH_TOKEN', + destinationResponse: { + response: { + success: false, + error: { + reason: 'UNAUTHORIZED', + explanation: + 'This server could not verify that you are authorized to access the document you requested.', + }, + }, + status: 401, + }, + message: + 'This server could not verify that you are authorized to access the document you requested. during reddit response transformation', + statTags: expectedStatTags, + status: 401, }, }, }, @@ -124,21 +168,65 @@ export const v1oauthScenarios = [ }, output: { response: { - status: 500, + status: 401, body: { output: { authErrorCategory: 'REFRESH_TOKEN', message: - "Request failed due to Authorization Required 'during reddit response transformation'", + 'Request failed due to Authorization Required during reddit response transformation', response: [ { error: '"Authorization Required"', metadata: generateMetadata(1), - statusCode: 500, + statusCode: 401, + }, + ], + statTags: expectedStatTags, + status: 401, + }, + }, + }, + }, + }, + { + id: 'reddit_v1_oauth_scenario_2', + name: 'reddit', + description: '[Proxy v1 API] :: Oauth where error response is an object from destination', + successCriteria: 'Should return 401 with authErrorCategory as REFRESH_TOKEN', + scenario: 'Oauth', + feature: 'dataDelivery', + module: 'destination', + version: 'v1', + input: { + request: { + body: generateProxyV1Payload( + { + ...commonRequestParameters, + endpoint: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + }, + [generateMetadata(1)], + ), + method: 'POST', + }, + }, + output: { + response: { + status: 401, + body: { + output: { + authErrorCategory: 'REFRESH_TOKEN', + message: + 'This server could not verify that you are authorized to access the document you requested. during reddit response transformation', + response: [ + { + error: + '{"success":false,"error":{"reason":"UNAUTHORIZED","explanation":"This server could not verify that you are authorized to access the document you requested."}}', + metadata: generateMetadata(1), + statusCode: 401, }, ], statTags: expectedStatTags, - status: 500, + status: 401, }, }, }, diff --git a/test/integrations/destinations/reddit/network.ts b/test/integrations/destinations/reddit/network.ts index 7c436e8fb8..562c8e95ad 100644 --- a/test/integrations/destinations/reddit/network.ts +++ b/test/integrations/destinations/reddit/network.ts @@ -97,4 +97,61 @@ export const networkCallsData = [ }, httpRes: { data: 'Authorization Required', status: 401, statusText: 'Unauthorized' }, }, + { + httpReq: { + url: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + data: { + events: [ + { + event_at: '2019-10-14T09:03:17.562Z', + event_type: { + tracking_type: 'ViewContent', + }, + user: { + aaid: 'c12d34889302d3c656b5699fa9190b51c50d6f62fce57e13bd56b503d66c487a', + email: 'ac144532d9e4efeab19475d9253a879173ea12a3d2238d1cb8a332a7b3a105f2', + external_id: '7b023241a3132b792a5a33915a5afb3133cbb1e13d72879689bf6504de3b036d', + ip_address: 'e80bd55a3834b7c2a34ade23c7ecb54d2a49838227080f50716151e765a619db', + user_agent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + screen_dimensions: {}, + }, + event_metadata: { + item_count: 3, + products: [ + { + id: '123', + name: 'Monopoly', + category: 'Games', + }, + { + id: '345', + name: 'UNO', + category: 'Games', + }, + ], + }, + }, + ], + }, + params: { destination: 'reddit' }, + headers: { + Authorization: 'Bearer dummyAccessToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + httpRes: { + data: { + success: false, + error: { + reason: 'UNAUTHORIZED', + explanation: + 'This server could not verify that you are authorized to access the document you requested.', + }, + }, + status: 401, + statusText: 'Unauthorized', + }, + }, ];