Skip to content

Commit

Permalink
fix: adding more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Jan 25, 2024
1 parent ebf6428 commit ba24539
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/v0/destinations/marketo_bulk_upload/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
RetryableError,
NetworkError,
TransformationError,
isDefinedAndNotNull,
} = require('@rudderstack/integrations-lib');
const { handleHttpRequest } = require('../../../adapters/network');
const tags = require('../../util/tags');
Expand Down Expand Up @@ -130,9 +131,12 @@ const getAccessToken = async (config) => {
});

// sample response : {response: '[ENOTFOUND] :: DNS lookup failed', status: 400}
if (!isHttpStatusSuccess(accessTokenResponse.status)) {
if (
isDefinedAndNotNull(accessTokenResponse.status) &&
!isHttpStatusSuccess(accessTokenResponse.status)
) {
throw new NetworkError(
`Could not retrieve authorisation token due to error ${accessTokenResponse}`,
`Could not retrieve authorisation token due to error ${JSON.stringify(accessTokenResponse)}`,
hydrateStatusForServer(accessTokenResponse.status, FETCH_ACCESS_TOKEN),
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(accessTokenResponse.status),
Expand Down
57 changes: 56 additions & 1 deletion src/v0/destinations/marketo_bulk_upload/util.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const util = require('./util.js');
const axios = require('axios');
const networkAdapter = require('../../../adapters/network');
const { handleHttpRequest } = networkAdapter;
const { AbortedError, RetryableError, NetworkError } = require('@rudderstack/integrations-lib');

// Mock the handleHttpRequest function
jest.mock('../../../adapters/network');
Expand Down Expand Up @@ -61,4 +61,59 @@ describe('util.getAccessToken', () => {
feature: 'transformation',
});
});

it('should throw a NetworkError on unsuccessful HTTP status', async () => {
handleHttpRequest.mockResolvedValueOnce({
processedResponse: unsuccessfulResponse,
});

const config = {
clientId: 'dummyClientId',
clientSecret: 'dummyClientSecret',
munchkinId: 'dummyMunchkinId',
};

await expect(util.getAccessToken(config)).rejects.toThrow(NetworkError);
});

it('should throw a RetryableError when expires_in is 0', async () => {
handleHttpRequest.mockResolvedValueOnce({
processedResponse: {
...successfulResponse,
response: { ...successfulResponse.response, expires_in: 0 },
},
});

const config = {
clientId: 'dummyClientId',
clientSecret: 'dummyClientSecret',
munchkinId: 'dummyMunchkinId',
};

await expect(util.getAccessToken(config)).rejects.toThrow(RetryableError);
});

it('should throw an AbortedError on unsuccessful response', async () => {
handleHttpRequest.mockResolvedValueOnce({ processedResponse: invalidClientErrorResponse });

const config = {
clientId: 'invalidClientID',
clientSecret: 'dummyClientSecret',
munchkinId: 'dummyMunchkinId',
};

await expect(util.getAccessToken(config)).rejects.toThrow(NetworkError);
});

it('should throw abortable error response', async () => {
handleHttpRequest.mockResolvedValueOnce({ processedResponse: emptyResponse });

const config = {
clientId: 'dummyClientId',
clientSecret: 'dummyClientSecret',
munchkinId: 'dummyMunchkinId',
};

await expect(util.getAccessToken(config)).rejects.toThrow(AbortedError);
});
});

0 comments on commit ba24539

Please sign in to comment.