From 866dbf3e81754e71ff8ac08b258b359ec5cc6889 Mon Sep 17 00:00:00 2001 From: shrouti1507 <60211312+shrouti1507@users.noreply.github.com> Date: Tue, 20 Aug 2024 17:20:01 +0530 Subject: [PATCH] fix: attentive tag bugsnag issue (#3663) * fix: attentive tag bugsnag issue * fix: review comment addressed * fix: review comment addressed * fix: editing test caes * fix: editing test caes --- .../destinations/attentive_tag/transform.js | 6 +-- src/v0/destinations/attentive_tag/util.js | 13 +++-- .../destinations/attentive_tag/util.test.js | 51 +++++++++++++++++++ .../attentive_tag/processor/data.ts | 2 +- 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/v0/destinations/attentive_tag/util.test.js diff --git a/src/v0/destinations/attentive_tag/transform.js b/src/v0/destinations/attentive_tag/transform.js index fa05eb6c21..5522c078b0 100644 --- a/src/v0/destinations/attentive_tag/transform.js +++ b/src/v0/destinations/attentive_tag/transform.js @@ -16,7 +16,7 @@ const { const { getDestinationItemProperties, getExternalIdentifiersMapping, - getPropertiesKeyValidation, + arePropertiesValid, validateTimestamp, } = require('./util'); const { JSON_MIME_TYPE } = require('../../util/constant'); @@ -137,9 +137,9 @@ const trackResponseBuilder = (message, { Config }) => { payload = constructPayload(message, mappingConfig[ConfigCategory.TRACK.name]); endpoint = ConfigCategory.TRACK.endpoint; payload.type = get(message, 'event'); - if (!getPropertiesKeyValidation(payload)) { + if (!arePropertiesValid(payload.properties)) { throw new InstrumentationError( - '[Attentive Tag]:The event name contains characters which is not allowed', + '[Attentive Tag]:The properties contains characters which is not allowed', ); } } diff --git a/src/v0/destinations/attentive_tag/util.js b/src/v0/destinations/attentive_tag/util.js index c96d03028f..8e73f572fc 100644 --- a/src/v0/destinations/attentive_tag/util.js +++ b/src/v0/destinations/attentive_tag/util.js @@ -14,12 +14,19 @@ const { mappingConfig, ConfigCategory } = require('./config'); * The keys should not contain any of the values inside the validationArray. * STEP 1: Storing keys in the array. * Checking for the non-valid characters inside the keys of properties. + * ref: https://docs.attentivemobile.com/openapi/reference/tag/Custom-Events/ * @param {*} payload * @returns */ -const getPropertiesKeyValidation = (payload) => { +const arePropertiesValid = (properties) => { + if (!isDefinedAndNotNullAndNotEmpty(properties)) { + return true; + } + if (typeof properties !== 'object') { + return false; + } const validationArray = [`'`, `"`, `{`, `}`, `[`, `]`, ',', `,`]; - const keys = Object.keys(payload.properties); + const keys = Object.keys(properties); for (const key of keys) { for (const validationChar of validationArray) { if (key.includes(validationChar)) { @@ -134,6 +141,6 @@ const getDestinationItemProperties = (message, isItemsRequired) => { module.exports = { getDestinationItemProperties, getExternalIdentifiersMapping, - getPropertiesKeyValidation, + arePropertiesValid, validateTimestamp, }; diff --git a/src/v0/destinations/attentive_tag/util.test.js b/src/v0/destinations/attentive_tag/util.test.js new file mode 100644 index 0000000000..59fdc9f361 --- /dev/null +++ b/src/v0/destinations/attentive_tag/util.test.js @@ -0,0 +1,51 @@ +const { arePropertiesValid } = require('./util'); + +describe('arePropertiesValid', () => { + // returns true for valid properties object with no special characters in keys + it('should return true when properties object has no special characters in keys', () => { + const properties = { key1: 'value1', key2: 'value2' }; + const result = arePropertiesValid(properties); + expect(result).toBe(true); + }); + + // returns true for null properties input + it('should return true when properties input is null', () => { + const properties = null; + const result = arePropertiesValid(properties); + expect(result).toBe(true); + }); + + // returns false for properties object with keys containing special characters + it('should return false for properties object with keys containing special characters', () => { + const properties = { + key1: 'value1', + 'key,2': 'value2', + key3: 'value3', + }; + expect(arePropertiesValid(properties)).toBe(false); + }); + + // returns true for empty properties object + it('should return true for empty properties object', () => { + const properties = {}; + expect(arePropertiesValid(properties)).toBe(true); + }); + + // returns true for undefined properties input + it('should return true for undefined properties input', () => { + const result = arePropertiesValid(undefined); + expect(result).toBe(true); + }); + + // returns true for empty string properties input + it('should return true for empty string properties input', () => { + const result = arePropertiesValid(''); + expect(result).toBe(true); + }); + + // returns false for empty string properties input + it('should return false for non object properties input', () => { + const result = arePropertiesValid('1234'); + expect(result).toBe(false); + }); +}); diff --git a/test/integrations/destinations/attentive_tag/processor/data.ts b/test/integrations/destinations/attentive_tag/processor/data.ts index 12e0f4bea1..c026569439 100644 --- a/test/integrations/destinations/attentive_tag/processor/data.ts +++ b/test/integrations/destinations/attentive_tag/processor/data.ts @@ -1458,7 +1458,7 @@ export const data = [ body: [ { statusCode: 400, - error: '[Attentive Tag]:The event name contains characters which is not allowed', + error: '[Attentive Tag]:The properties contains characters which is not allowed', statTags: { destType: 'ATTENTIVE_TAG', errorCategory: 'dataValidation',