Skip to content

Commit

Permalink
fix: adding new googleUtil folder to migrate the util and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Dec 22, 2023
1 parent 940c365 commit 9f4ef9a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const {
defaultBatchRequestConfig,
getSuccessRespEvents,
checkInvalidRtTfEvents,
populateConsentForGoogleDestinations,
} = require('../../util');
const { populateConsentForGoogleDestinations } = require('../../util/googleUtils');
const {
CALL_CONVERSION,
trackCallConversionsMapping,
Expand Down
30 changes: 30 additions & 0 deletions src/v0/util/googleUtils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const GOOGLE_ALLOWED_CONSENT_STATUS = ['UNSPECIFIED', 'UNKNOWN', 'GRANTED', 'DENIED'];

/**
* Populates the consent object based on the provided properties.
*
* @param {object} properties - message.properties containing properties related to consent.
* @returns {object} - An object containing consent information.
* ref : https://developers.google.com/google-ads/api/rest/reference/rest/v15/Consent
*/
const populateConsentForGoogleDestinations = (properties) => {
const consent = {};

if (
properties?.userDataConsent &&
GOOGLE_ALLOWED_CONSENT_STATUS.includes(properties.userDataConsent)
) {
consent.adUserData = properties.userDataConsent;
}

if (
properties?.personalizationConsent &&
GOOGLE_ALLOWED_CONSENT_STATUS.includes(properties.personalizationConsent)
) {
consent.adPersonalization = properties.personalizationConsent;
}

return consent;
};

module.exports = { populateConsentForGoogleDestinations };
50 changes: 50 additions & 0 deletions src/v0/util/googleUtils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { populateConsentForGoogleDestinations } = require('./index');

describe('unit test for populateConsentForGoogleDestinations', () => {
// Returns an empty object when no properties are provided.
it('should return an empty object when no properties are provided', () => {
const result = populateConsentForGoogleDestinations({});
expect(result).toEqual({});
});

// Sets adUserData property of consent object when userDataConsent property is provided and its value is one of the allowed consent statuses.
it('should set adUserData property of consent object when userDataConsent property is provided and its value is one of the allowed consent statuses', () => {
const properties = { userDataConsent: 'GRANTED' };
const result = populateConsentForGoogleDestinations(properties);
expect(result).toEqual({ adUserData: 'GRANTED' });
});

// Sets adPersonalization property of consent object when personalizationConsent property is provided and its value is one of the allowed consent statuses.
it('should set adPersonalization property of consent object when personalizationConsent property is provided and its value is one of the allowed consent statuses', () => {
const properties = { personalizationConsent: 'DENIED' };
const result = populateConsentForGoogleDestinations(properties);
expect(result).toEqual({ adPersonalization: 'DENIED' });
});

// Returns an empty object when properties parameter is not provided.
it('should return an empty object when properties parameter is not provided', () => {
const result = populateConsentForGoogleDestinations();
expect(result).toEqual({});
});

// Returns an empty object when properties parameter is null.
it('should return an empty object when properties parameter is null', () => {
const result = populateConsentForGoogleDestinations(null);
expect(result).toEqual({});
});

// Returns an empty object when properties parameter is an empty object.
it('should return an empty object when properties parameter is an empty object', () => {
const result = populateConsentForGoogleDestinations({});
expect(result).toEqual({});
});

// Returns an empty object when properties parameter is an empty object.
it('should return an empty object when properties parameter contains adUserData and adPersonalization with non-allowed values', () => {
const result = populateConsentForGoogleDestinations({
adUserData: 'RANDOM',
personalizationConsent: 'RANDOM',
});
expect(result).toEqual({});
});
});
23 changes: 0 additions & 23 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2108,28 +2108,6 @@ const parseConfigArray = (arr, key) => {
return arr.map((item) => item[key]);
};

const GOOGLE_ALLOWED_CONSENT_STATUS = ['UNSPECIFIED', 'UNKNOWN', 'GRANTED', 'DENIED'];

const populateConsentForGoogleDestinations = (properties) => {
const consent = {};

if (
properties?.userDataConsent &&
GOOGLE_ALLOWED_CONSENT_STATUS.includes(properties.userDataConsent)
) {
consent.adUserData = properties.userDataConsent;
}

if (
properties?.personalizationConsent &&
GOOGLE_ALLOWED_CONSENT_STATUS.includes(properties.personalizationConsent)
) {
consent.adPersonalization = properties.personalizationConsent;
}

return consent;
};

// ========================================================================
// EXPORTS
// ========================================================================
Expand Down Expand Up @@ -2240,5 +2218,4 @@ module.exports = {
isNewStatusCodesAccepted,
IsGzipSupported,
parseConfigArray,
populateConsentForGoogleDestinations,
};

0 comments on commit 9f4ef9a

Please sign in to comment.