From fb8381a4e2fad67f1ab9103bab6fbe307495a01c Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Fri, 22 Dec 2023 11:51:27 +0530 Subject: [PATCH] fix: migrating util to google utils and adding test cases --- .../transform.js | 3 +- src/v0/util/googleUtils/index.js | 30 +++++++++++ src/v0/util/googleUtils/index.test.js | 50 +++++++++++++++++++ src/v0/util/index.js | 30 ----------- 4 files changed, 82 insertions(+), 31 deletions(-) create mode 100644 src/v0/util/googleUtils/index.js create mode 100644 src/v0/util/googleUtils/index.test.js diff --git a/src/v0/destinations/google_adwords_remarketing_lists/transform.js b/src/v0/destinations/google_adwords_remarketing_lists/transform.js index c93e97245c..9526973fb8 100644 --- a/src/v0/destinations/google_adwords_remarketing_lists/transform.js +++ b/src/v0/destinations/google_adwords_remarketing_lists/transform.js @@ -13,9 +13,10 @@ const { simpleProcessRouterDest, getDestinationExternalIDInfoForRetl, getAccessToken, - populateConsentForGoogleDestinations, } = require('../../util'); +const { populateConsentForGoogleDestinations } = require('../../util/googleUtils'); + const { offlineDataJobsMapping, addressInfoMapping, diff --git a/src/v0/util/googleUtils/index.js b/src/v0/util/googleUtils/index.js new file mode 100644 index 0000000000..c8d872e90e --- /dev/null +++ b/src/v0/util/googleUtils/index.js @@ -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 }; diff --git a/src/v0/util/googleUtils/index.test.js b/src/v0/util/googleUtils/index.test.js new file mode 100644 index 0000000000..27eff2a793 --- /dev/null +++ b/src/v0/util/googleUtils/index.test.js @@ -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({}); + }); +}); diff --git a/src/v0/util/index.js b/src/v0/util/index.js index b89491dc3c..34182a7685 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -2108,35 +2108,6 @@ const parseConfigArray = (arr, key) => { return arr.map((item) => item[key]); }; -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; -}; - // ======================================================================== // EXPORTS // ======================================================================== @@ -2247,5 +2218,4 @@ module.exports = { isNewStatusCodesAccepted, IsGzipSupported, parseConfigArray, - populateConsentForGoogleDestinations, };