Skip to content

Commit

Permalink
fix: existing test-cases for error handling
Browse files Browse the repository at this point in the history
- added new test-cases
  • Loading branch information
Sai Sankeerth committed Aug 29, 2024
1 parent 38886d6 commit 38b1075
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/v0/destinations/reddit/networkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ 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')) {
errorMessage = `Request failed due to ${response}`;
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,
);
}
Expand Down
102 changes: 95 additions & 7 deletions test/integrations/destinations/reddit/dataDelivery/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const v0oauthScenarios = [
},
output: {
response: {
status: 500,
status: 401,
body: {
output: {
authErrorCategory: 'REFRESH_TOKEN',
Expand All @@ -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,
},
},
},
Expand Down Expand Up @@ -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,
},
},
},
Expand Down
57 changes: 57 additions & 0 deletions test/integrations/destinations/reddit/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "Bearer dummyAccessToken" is used as
authorization header
.
'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',
},
},
];

0 comments on commit 38b1075

Please sign in to comment.