From e4c6bb27591672ba369750c6a1c03bfa600fe4d7 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 9 Nov 2023 20:08:48 +0530 Subject: [PATCH] fix: cloneDeep for all, add test-cases to reflect same referencing & null message cases Signed-off-by: Sai Sankeerth --- src/helpers/geoLocation.ts | 5 +- test/helper/helper.test.ts | 17 ++ .../destinations/webhook/processor/data.ts | 249 ++++++++++++++++++ 3 files changed, 269 insertions(+), 2 deletions(-) diff --git a/src/helpers/geoLocation.ts b/src/helpers/geoLocation.ts index b1f1c8b455..fc80d74e6c 100644 --- a/src/helpers/geoLocation.ts +++ b/src/helpers/geoLocation.ts @@ -23,8 +23,8 @@ export default class GeoLocationHelper { public static getMessageWithGeoLocationData( message: Record, ): Record { - const msg = cloneDeep(message || {}); - if (isEmpty(msg?.context?.geo || {})) { + let msg = message; + if (isEmpty(msg?.context?.geo)) { // geo-location data was not sent return {}; } @@ -46,6 +46,7 @@ export default class GeoLocationHelper { {}, ); if (!isEmpty(address) || !isEmpty(mappedAddress)) { + msg = cloneDeep(message || {}); set(msg, addressKey, { ...address, ...mappedAddress }); } return msg; diff --git a/test/helper/helper.test.ts b/test/helper/helper.test.ts index 52ba215acb..5eac756736 100644 --- a/test/helper/helper.test.ts +++ b/test/helper/helper.test.ts @@ -283,6 +283,8 @@ describe('GeoLocationHelper tests', () => { city: 'Dharamshala', postalCode: '123546', }); + // same reference check + expect(enhancedMsg.traits.address).toStrictEqual(msg.traits.address); }); test("when context.geo doesn't have some properties, do not make use of non-available values in context.geo", () => { @@ -407,6 +409,21 @@ describe('GeoLocationHelper tests', () => { const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg); expect(enhancedMsg).toEqual({}); + // @ts-ignore + expect(enhancedMsg?.traits?.address).toBe(undefined); + // @ts-ignore + expect(enhancedMsg?.context?.traits?.address).toBe(undefined); + }); + + test('when message is null(no geo-enrichment happened), enrichment would not happen', () => { + const msg = null; + + // @ts-ignore + const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg); + + expect(enhancedMsg).toEqual({}); + // @ts-ignore + expect(enhancedMsg?.traits).toBe(undefined); }); }); diff --git a/test/integrations/destinations/webhook/processor/data.ts b/test/integrations/destinations/webhook/processor/data.ts index d02fa58227..92a67170e0 100644 --- a/test/integrations/destinations/webhook/processor/data.ts +++ b/test/integrations/destinations/webhook/processor/data.ts @@ -1,3 +1,251 @@ +const geoLocationData = [ + { + name: 'webhook', + description: '[GeoLocation] Test 0', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + anonymousId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + context: { + device: { + id: 'df16bffa-5c3d-4fbb-9bce-3bab098129a7R', + manufacturer: 'Xiaomi', + model: 'Redmi 6', + name: 'xiaomi', + }, + network: { carrier: 'Banglalink' }, + os: { name: 'android', version: '8.1.0' }, + traits: { + address: { city: 'Dhaka', country: 'Bangladesh' }, + anonymousId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + }, + geo: { + city: 'Gurugram', + country: 'IN', + ip: '223.190.82.63', + location: '28.459700,77.028200', + postal: '122001', + region: 'Haryana', + timezone: 'Asia/Kolkata', + }, + }, + event: 'spin_result', + integrations: { All: true }, + message_id: 'a80f82be-9bdc-4a9f-b2a5-15621ee41df8', + properties: { + additional_bet_index: 0, + battle_id: 'N/A', + bet_amount: 9, + bet_level: 1, + bet_multiplier: 1, + coin_balance: 9466052, + current_module_name: 'CasinoGameModule', + days_in_game: 0, + extra_param: 'N/A', + fb_profile: '0', + featureGameType: 'N/A', + game_fps: 30, + game_id: 'fireEagleBase', + game_name: 'FireEagleSlots', + gem_balance: 0, + graphicsQuality: 'HD', + idfa: '2bf99787-33d2-4ae2-a76a-c49672f97252', + internetReachability: 'ReachableViaLocalAreaNetwork', + isLowEndDevice: 'False', + is_auto_spin: 'False', + is_turbo: 'False', + isf: 'False', + ishighroller: 'False', + jackpot_win_amount: 90, + jackpot_win_type: 'Silver', + level: 6, + lifetime_gem_balance: 0, + no_of_spin: 1, + player_total_battles: 0, + player_total_shields: 0, + start_date: '2019-08-01', + total_payments: 0, + tournament_id: 'T1561970819', + userId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + versionSessionCount: 2, + win_amount: 0, + }, + timestamp: '2019-09-01T15:46:51.693229+05:30', + type: 'track', + user_properties: { + coin_balance: 9466052, + current_module_name: 'CasinoGameModule', + fb_profile: '0', + game_fps: 30, + game_name: 'FireEagleSlots', + gem_balance: 0, + graphicsQuality: 'HD', + idfa: '2bf99787-33d2-4ae2-a76a-c49672f97252', + internetReachability: 'ReachableViaLocalAreaNetwork', + isLowEndDevice: false, + level: 6, + lifetime_gem_balance: 0, + player_total_battles: 0, + player_total_shields: 0, + start_date: '2019-08-01', + total_payments: 0, + userId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + versionSessionCount: 2, + }, + }, + destination: { + Config: { + webhookUrl: 'http://6b0e6a60.ngrok.io', + headers: [ + { from: '', to: '' }, + { from: 'test2', to: 'value2' }, + ], + }, + DestinationDefinition: { + Config: { + cdkV2Enabled: true, + }, + }, + }, + metadata: { + destinationId: 'd1', + workspaceId: 'w1', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + metadata: { + destinationId: 'd1', + workspaceId: 'w1', + }, + output: { + body: { + XML: {}, + JSON_ARRAY: {}, + JSON: { + timestamp: '2019-09-01T15:46:51.693229+05:30', + user_properties: { + total_payments: 0, + internetReachability: 'ReachableViaLocalAreaNetwork', + level: 6, + userId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + coin_balance: 9466052, + player_total_shields: 0, + isLowEndDevice: false, + game_fps: 30, + idfa: '2bf99787-33d2-4ae2-a76a-c49672f97252', + graphicsQuality: 'HD', + current_module_name: 'CasinoGameModule', + player_total_battles: 0, + lifetime_gem_balance: 0, + gem_balance: 0, + fb_profile: '0', + start_date: '2019-08-01', + versionSessionCount: 2, + game_name: 'FireEagleSlots', + }, + integrations: { All: true }, + event: 'spin_result', + message_id: 'a80f82be-9bdc-4a9f-b2a5-15621ee41df8', + anonymousId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + context: { + geo: { + city: 'Gurugram', + country: 'IN', + ip: '223.190.82.63', + location: '28.459700,77.028200', + postal: '122001', + region: 'Haryana', + timezone: 'Asia/Kolkata', + }, + device: { + model: 'Redmi 6', + manufacturer: 'Xiaomi', + id: 'df16bffa-5c3d-4fbb-9bce-3bab098129a7R', + name: 'xiaomi', + }, + traits: { + anonymousId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + address: { + city: 'Dhaka', + country: 'Bangladesh', + postalCode: '122001', + state: 'Haryana', + }, + }, + os: { version: '8.1.0', name: 'android' }, + network: { carrier: 'Banglalink' }, + }, + type: 'track', + properties: { + userId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + jackpot_win_type: 'Silver', + coin_balance: 9466052, + bet_level: 1, + ishighroller: 'False', + tournament_id: 'T1561970819', + battle_id: 'N/A', + bet_amount: 9, + fb_profile: '0', + player_total_shields: 0, + is_turbo: 'False', + player_total_battles: 0, + bet_multiplier: 1, + start_date: '2019-08-01', + versionSessionCount: 2, + graphicsQuality: 'HD', + is_auto_spin: 'False', + days_in_game: 0, + additional_bet_index: 0, + isLowEndDevice: 'False', + game_fps: 30, + extra_param: 'N/A', + idfa: '2bf99787-33d2-4ae2-a76a-c49672f97252', + current_module_name: 'CasinoGameModule', + game_id: 'fireEagleBase', + featureGameType: 'N/A', + gem_balance: 0, + internetReachability: 'ReachableViaLocalAreaNetwork', + total_payments: 0, + level: 6, + win_amount: 0, + no_of_spin: 1, + game_name: 'FireEagleSlots', + jackpot_win_amount: 90, + lifetime_gem_balance: 0, + isf: 'False', + }, + }, + FORM: {}, + }, + files: {}, + endpoint: 'http://6b0e6a60.ngrok.io', + userId: 'c82cbdff-e5be-4009-ac78-cdeea09ab4b1', + headers: { 'content-type': 'application/json', test2: 'value2' }, + version: '1', + params: {}, + type: 'REST', + method: 'POST', + }, + statusCode: 200, + }, + ], + }, + }, + }, +]; export const data = [ { name: 'webhook', @@ -2167,4 +2415,5 @@ export const data = [ }, }, }, + ...geoLocationData, ];