Skip to content

Commit

Permalink
chore: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik committed Apr 4, 2024
1 parent 95b5803 commit 0c1b9ae
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 42 deletions.
69 changes: 27 additions & 42 deletions src/v0/destinations/sfmc/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
isEmpty,
} = require('@rudderstack/integrations-lib');
const { EventType } = require('../../../constants');
const { handleHttpRequest } = require('../../../adapters/network');
const { CONFIG_CATEGORIES, MAPPING_CONFIG, ENDPOINTS } = require('./config');
const {
removeUndefinedAndNullValues,
Expand All @@ -21,64 +22,48 @@ const {
getHashFromArray,
simpleProcessRouterDest,
} = require('../../util');
const {
getDynamicErrorType,
nodeSysErrorToStatus,
} = require('../../../adapters/utils/networkUtils');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { isHttpStatusSuccess } = require('../../util');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { handleHttpRequest } = require('../../../adapters/network');

const CONTACT_KEY_KEY = 'Contact Key';

// DOC: https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-s2s.htm

const getToken = async (clientId, clientSecret, subdomain) => {
try {
const { processedResponse: processedResponseSfmc } = await handleHttpRequest(
'post',
`https://${subdomain}.${ENDPOINTS.GET_TOKEN}`,
{
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
},
{
'Content-Type': JSON_MIME_TYPE,
},
{
destType: 'sfmc',
feature: 'transformation',
endpointPath: '/token',
requestMethod: 'POST',
module: 'router',
},
);
if (processedResponseSfmc.response) {
return processedResponseSfmc.response.access_token;
}
const { processedResponse: processedResponseSfmc } = await handleHttpRequest(
'post',
`https://${subdomain}.${ENDPOINTS.GET_TOKEN}`,
{
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
},
{
'Content-Type': JSON_MIME_TYPE,
},
{
destType: 'sfmc',
feature: 'transformation',
endpointPath: '/token',
requestMethod: 'POST',
module: 'router',
},
);

if (!isHttpStatusSuccess(processedResponseSfmc.status)) {
throw new NetworkError(
'Could not retrieve access token',
processedResponseSfmc.status || 400,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseSfmc.status || 400),
},
JSON.stringify(processedResponseSfmc),
processedResponseSfmc.response?.message || JSON.stringify(processedResponseSfmc),
);
} catch (error) {
if (!isEmpty(error.response)) {
const status = error.status || 400;
throw new NetworkError(`Authorization Failed ${error.response.statusText}`, status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
});
} else {
const httpError = nodeSysErrorToStatus(error.code);
const status = httpError.status || 400;
throw new NetworkError(`Authorization Failed ${httpError.message}`, status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
});
}
}

return processedResponseSfmc.response.access_token;
};

// DOC : https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/createContacts.htm
Expand Down
50 changes: 50 additions & 0 deletions test/integrations/destinations/sfmc/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,54 @@ export const networkCallsData = [
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest401.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
status: 401,
data: {
error: 'invalid_client',
error_description:
'Invalid client ID. Use the client ID in Marketing Cloud Installed Packages.',
error_uri: 'https://developer.salesforce.com/docs',
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest429.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
status: 429,
data: {
message: 'Your requests are temporarily blocked.',
errorcode: 50200,
documentation:
'https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/error-handling.htm',
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest-dns.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
data: {},
status: 400,
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest-null.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
data: null,
status: 500,
},
},
];
Loading

0 comments on commit 0c1b9ae

Please sign in to comment.