Skip to content

Commit

Permalink
fix: address and context.geo not present case
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed Nov 7, 2023
1 parent d789f0e commit d3d6b72
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/helpers/geoLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
170 changes: 168 additions & 2 deletions test/middleware/enricher.test.ts
Original file line number Diff line number Diff line change
@@ -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[] = [
{
Expand Down Expand Up @@ -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: '[email protected]',
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[] = [
{
Expand Down

0 comments on commit d3d6b72

Please sign in to comment.