Skip to content

Commit

Permalink
fix: test cases fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Oct 3, 2024
1 parent 3aea42f commit 99c8efa
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 31 deletions.
88 changes: 65 additions & 23 deletions src/v0/destinations/salesforce/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const { RetryableError, ThrottledError, AbortedError } = require('@rudderstack/integrations-lib');
const {
RetryableError,
ThrottledError,
AbortedError,
// isDefinedAndNotNull,
} = require('@rudderstack/integrations-lib');
const { handleHttpRequest } = require('../../../adapters/network');
const { getAuthErrCategoryFromStCode } = require('../../util');
const Cache = require('../../util/cache');
Expand All @@ -14,6 +19,55 @@ const {

const ACCESS_TOKEN_CACHE = new Cache(ACCESS_TOKEN_CACHE_TTL);

const getErrorMessage = (response) => {
if (response && Array.isArray(response) && response[0]?.message?.length > 0) {
return response[0].message;
}
return JSON.stringify(response);
};

const handleAuthError = (
errorCode,
authKey,
authorizationFlow,
sourceMessage,
destResponse,
status,
) => {
// eslint-disable-next-line sonarjs/no-small-switch
switch (errorCode) {
case 'INVALID_SESSION_ID':
if (authorizationFlow === OAUTH) {
throw new RetryableError(
`${DESTINATION} Request Failed - due to "INVALID_SESSION_ID", (Retryable) ${sourceMessage}`,
500,
destResponse,
getAuthErrCategoryFromStCode(status),
);
}
ACCESS_TOKEN_CACHE.del(authKey);
throw new RetryableError(
`${DESTINATION} Request Failed - due to "INVALID_SESSION_ID", (Retryable) ${sourceMessage}`,
500,
destResponse,
);
default:
throw new AbortedError(
`${DESTINATION} Request Failed: "${status}" due to "${getErrorMessage(destResponse.response)}", (Aborted) ${sourceMessage}`,
400,
destResponse,
);
}
};

const handleCommonAbortableError = (destResponse, sourceMessage, status) => {
throw new AbortedError(
`${DESTINATION} Request Failed: "${status}" due to "${getErrorMessage(destResponse.response)}", (Aborted) ${sourceMessage}`,
400,
destResponse,
);
};

/**
* ref: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
* handles Salesforce application level failures
Expand All @@ -28,33 +82,20 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori
const matchErrorCode = (errorCode) =>
response && Array.isArray(response) && response.some((resp) => resp?.errorCode === errorCode);

const getErrorMessage = () => {
if (response && Array.isArray(response) && response[0]?.message?.length > 0) {
return response[0].message;
}
return JSON.stringify(response);
};

switch (status) {
case 401:
if (authKey && matchErrorCode('INVALID_SESSION_ID')) {
if (authorizationFlow === OAUTH) {
throw new RetryableError(
`${DESTINATION} Request Failed - due to "INVALID_SESSION_ID", (Retryable) ${sourceMessage}`,
500,
destResponse,
getAuthErrCategoryFromStCode(status),
);
}
ACCESS_TOKEN_CACHE.del(authKey);
throw new RetryableError(
`${DESTINATION} Request Failed - due to "INVALID_SESSION_ID", (Retryable) ${sourceMessage}`,
500,
handleAuthError(
'INVALID_SESSION_ID',
authKey,
authorizationFlow,
sourceMessage,
destResponse,
status,
);
}
handleAuthError('DEFAULT', authKey, authorizationFlow, sourceMessage, destResponse, status);
break;

Check warning on line 98 in src/v0/destinations/salesforce/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/utils.js#L98

Added line #L98 was not covered by tests

case 403:
if (matchErrorCode('REQUEST_LIMIT_EXCEEDED')) {
throw new ThrottledError(
Expand All @@ -76,20 +117,21 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori
destResponse,
);
}
handleCommonAbortableError(destResponse, sourceMessage, status);
break;

Check warning on line 121 in src/v0/destinations/salesforce/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/utils.js#L121

Added line #L121 was not covered by tests

case 503:
case 500:
throw new RetryableError(
`${DESTINATION} Request Failed - due to "${getErrorMessage()}", (Retryable) ${sourceMessage}`,
`${DESTINATION} Request Failed - due to "${getErrorMessage(response)}", (Retryable) ${sourceMessage}`,
500,
destResponse,
);

default:

Check warning on line 131 in src/v0/destinations/salesforce/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/utils.js#L131

Added line #L131 was not covered by tests
// Default case: aborting for all other error codes
throw new AbortedError(

Check warning on line 133 in src/v0/destinations/salesforce/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/utils.js#L133

Added line #L133 was not covered by tests
`${DESTINATION} Request Failed: "${status}" due to "${getErrorMessage()}", (Aborted) ${sourceMessage}`,
`${DESTINATION} Request Failed: "${status}" due to "${getErrorMessage(response)}", (Aborted) ${sourceMessage}`,
400,
destResponse,
);
Expand Down
4 changes: 2 additions & 2 deletions test/integrations/destinations/salesforce/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1476,10 +1476,10 @@ export const data = [
type: 'REST',
method: 'POST',
endpoint:
'http://dummyurl.com/services/data/v50.0/sobjects/custom_object__c/a005g0000383kmUAAQ?_HttpMethod=PATCH',
'https://ap15.salesforce.com/services/data/v50.0/sobjects/custom_object__c/a005g0000383kmUAAQ?_HttpMethod=PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer dummyAccessToken',
Authorization: 'Bearer dummy.access.token',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "Bearer dummy.access.token" is used as
authorization header
.
},
params: {},
body: {
Expand Down
57 changes: 51 additions & 6 deletions test/integrations/destinations/salesforce/router/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,14 @@ export const data = [
type: 'identify',
userId: '1e7673da-9473-49c6-97f7-da848ecafa76',
},
metadata: { jobId: 1, userId: 'u1' },
metadata: {
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
destination: {
Config: {
initialAccessToken: 'dummyInitialAccessToken',
Expand Down Expand Up @@ -697,7 +704,15 @@ export const data = [
},
],
metadata: [
{ destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' }, jobId: 1, userId: 'u1' },
{
destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' },
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
],
batched: false,
statusCode: 200,
Expand Down Expand Up @@ -787,7 +802,14 @@ export const data = [
type: 'identify',
userId: '1e7673da-9473-49c6-97f7-da848ecafa76',
},
metadata: { jobId: 1, userId: 'u1' },
metadata: {
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
destination: {
Config: {
initialAccessToken: 'dummyInitialAccessToken',
Expand Down Expand Up @@ -855,7 +877,15 @@ export const data = [
},
],
metadata: [
{ destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' }, jobId: 1, userId: 'u1' },
{
destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' },
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
],
batched: false,
statusCode: 200,
Expand Down Expand Up @@ -945,7 +975,14 @@ export const data = [
type: 'identify',
userId: '1e7673da-9473-49c6-97f7-da848ecafa76',
},
metadata: { jobId: 1, userId: 'u1' },
metadata: {
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
destination: {
Config: {
initialAccessToken: 'dummyInitialAccessToken',
Expand Down Expand Up @@ -1013,7 +1050,15 @@ export const data = [
},
],
metadata: [
{ destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' }, jobId: 1, userId: 'u1' },
{
destInfo: { authKey: '1WqFFH5esuVPnUgHkvEoYxDcX3y' },
jobId: 1,
userId: 'u1',
secret: {
access_token: 'dummy.access.token',
instance_url: 'https://ap15.salesforce.com',
},
},
],
batched: false,
statusCode: 200,
Expand Down

0 comments on commit 99c8efa

Please sign in to comment.