From d3d6b7281276f01c29daafff928623c73f11e2c2 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Tue, 7 Nov 2023 18:40:43 +0530 Subject: [PATCH] fix: address and context.geo not present case Signed-off-by: Sai Sankeerth --- src/helpers/geoLocation.ts | 4 +- test/middleware/enricher.test.ts | 170 ++++++++++++++++++++++++++++++- 2 files changed, 171 insertions(+), 3 deletions(-) diff --git a/src/helpers/geoLocation.ts b/src/helpers/geoLocation.ts index 177bc41837..1c9dfe6d62 100644 --- a/src/helpers/geoLocation.ts +++ b/src/helpers/geoLocation.ts @@ -34,7 +34,9 @@ export default class GeoLocationHelper { }, {}, ); - set(msg, addressKey, { ...address, ...mappedAddress }); + if (!isEmpty(address) || !isEmpty(mappedAddress)) { + set(msg, addressKey, { ...address, ...mappedAddress }); + } return msg; } } diff --git a/test/middleware/enricher.test.ts b/test/middleware/enricher.test.ts index 3b173800f3..5474ae3cfb 100644 --- a/test/middleware/enricher.test.ts +++ b/test/middleware/enricher.test.ts @@ -1,7 +1,7 @@ -import { ProcessorTransformationRequest } from '../../src/types'; +import { ProcessorTransformationRequest, RouterTransformationRequest } from '../../src/types'; import Enricher from '../../src/middlewares/enricher'; -describe('geoLocation Enrichment during processor transformation tests', () => { +describe('[GeoLocation Enrichment] Processor transformation tests', () => { test('should enrich when context.geo is populated correctly', () => { const inputData: ProcessorTransformationRequest[] = [ { @@ -435,6 +435,172 @@ describe('geoLocation Enrichment during processor transformation tests', () => { expect(ctx.request.body[0].message.traits?.address).toBe(undefined); }); + test('should not contain address object, when context.geo & address are not present', () => { + const inputData: ProcessorTransformationRequest[] = [ + { + destination: { + ID: '1afjtc6chkhdeKsXYrNFOzR5D9v', + Name: 'Autopilot', + DestinationDefinition: { + ID: '1afjX4MlAucK57Q0ctTVlD27Tvo', + Name: 'AUTOPILOT', + DisplayName: 'Autopilot', + Config: { + cdkEnabled: true, + excludeKeys: [], + includeKeys: [], + }, + }, + Config: { + apiKey: 'dummyApiKey', + customMappings: [ + { + from: '0001', + to: 'Signup', + }, + ], + triggerId: '00XX', + }, + Enabled: true, + Transformations: [], + // @ts-ignore + IsProcessorEnabled: true, + }, + message: { + anonymousId: 'ac7722c2-ccb6-4ae2-baf6-1effe861f4cd', + channel: 'web', + context: { + app: { + build: '1.0.0', + name: 'RudderLabs JavaScript SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.1.1-rc.2', + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.1.1-rc.2', + }, + locale: 'en-GB', + os: { + name: '', + version: '', + }, + page: { + path: '/tests/html/index4.html', + referrer: '', + search: '', + title: '', + url: 'http://localhost/tests/html/index4.html', + }, + screen: { + density: 2, + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36', + }, + integrations: { + All: true, + }, + messageId: 'fad9b3fb-5778-4db3-9fb6-7168b554191f', + originalTimestamp: '2020-04-17T14:42:44.722Z', + receivedAt: '2020-04-17T20:12:44.758+05:30', + request_ip: '[::1]:53513', + sentAt: '2020-04-17T14:42:44.722Z', + traits: { + age: 23, + email: 'testmp@rudderstack.com', + firstname: 'Test Kafka', + }, + timestamp: '2020-04-17T20:12:44.758+05:30', + type: 'identify', + userId: 'user12345', + }, + }, + { + destination: { + ID: '1afjtc6chkhdeKsXYrNFOzR5D9v', + Name: 'Autopilot', + DestinationDefinition: { + ID: '1afjX4MlAucK57Q0ctTVlD27Tvo', + Name: 'AUTOPILOT', + DisplayName: 'Autopilot', + Config: { + cdkEnabled: true, + excludeKeys: [], + includeKeys: [], + }, + }, + Config: { + apiKey: 'dummyApiKey', + customMappings: [ + { + from: '0001', + to: 'Signup', + }, + ], + triggerId: '00XX', + }, + Enabled: true, + Transformations: [], + // @ts-ignore + IsProcessorEnabled: true, + }, + message: { + anonymousId: 'ac7722c2-ccb6-4ae2-baf6-1effe861f4cd', + channel: 'web', + context: { + app: { + build: '1.0.0', + name: 'RudderLabs JavaScript SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.1.1-rc.2', + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.1.1-rc.2', + }, + locale: 'en-GB', + os: { + name: '', + version: '', + }, + page: { + path: '/tests/html/index4.html', + referrer: '', + search: '', + title: '', + url: 'http://localhost/tests/html/index4.html', + }, + screen: { + density: 2, + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36', + }, + integrations: { + All: true, + }, + messageId: 'fad9b3fb-5778-4db3-9fb6-7168b554191f', + originalTimestamp: '2020-04-17T14:42:44.722Z', + receivedAt: '2020-04-17T20:12:44.758+05:30', + request_ip: '[::1]:53513', + sentAt: '2020-04-17T14:42:44.722Z', + traits: { + key: 'val', + }, + timestamp: '2020-04-17T20:12:44.758+05:30', + type: 'identify', + userId: 'user12345', + }, + }, + ]; + const ctx = { request: { body: inputData } }; + // @ts-ignore + Enricher.enrichGeoLocation(ctx); + expect(ctx.request.body[0].message.traits).not.toHaveProperty('address'); + expect(ctx.request.body[1].message.traits).not.toHaveProperty('address'); + }); + test('should enrich when context.geo is populated correctly for multiple payloads with their own geolocation data', () => { const inputData: ProcessorTransformationRequest[] = [ {