From fc92084b357df2648a2578076d1828ef3b0965dc Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Fri, 16 Aug 2024 13:58:56 +0530 Subject: [PATCH] fix: attentive tag bugsnag issue --- .../destinations/attentive_tag/transform.js | 2 +- src/v0/destinations/attentive_tag/util.js | 11 ++++- .../destinations/attentive_tag/util.test.js | 45 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 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..4c81a01414 100644 --- a/src/v0/destinations/attentive_tag/transform.js +++ b/src/v0/destinations/attentive_tag/transform.js @@ -137,7 +137,7 @@ const trackResponseBuilder = (message, { Config }) => { payload = constructPayload(message, mappingConfig[ConfigCategory.TRACK.name]); endpoint = ConfigCategory.TRACK.endpoint; payload.type = get(message, 'event'); - if (!getPropertiesKeyValidation(payload)) { + if (!getPropertiesKeyValidation(payload.properties)) { throw new InstrumentationError( '[Attentive Tag]:The event name 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..56d5c475ff 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 getPropertiesKeyValidation = (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)) { 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..26ebee0f27 --- /dev/null +++ b/src/v0/destinations/attentive_tag/util.test.js @@ -0,0 +1,45 @@ +const { getPropertiesKeyValidation } = require('./util'); + +describe('getPropertiesKeyValidation', () => { + // 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 = getPropertiesKeyValidation(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 = getPropertiesKeyValidation(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(getPropertiesKeyValidation(properties)).toBe(false); + }); + + // returns true for empty properties object + it('should return true for empty properties object', () => { + const properties = {}; + expect(getPropertiesKeyValidation(properties)).toBe(true); + }); + + // returns true for undefined properties input + it('should return true for undefined properties input', () => { + const result = getPropertiesKeyValidation(undefined); + expect(result).toBe(true); + }); + + // returns true for empty string properties input + it('should return true for empty string properties input', () => { + const result = getPropertiesKeyValidation(''); + expect(result).toBe(true); + }); +});