Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Aug 20, 2024
1 parent 921fb38 commit 2cdc522
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/cdk/v2/destinations/smartly/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ steps:
- name: preparePayload
template: |
const payload = $.removeUndefinedAndNullValues($.constructPayload(.message, $.TRACK_CONFIG));
$.context.payloadList = $.getPayloads(.message.event, .destination.Config, payload)
$.verifyAdInteractionTime(payload.ad_interaction_time);
$.context.payloadList = $.getPayloads(.message.event_name, .destination.Config, payload)
- name: buildResponse
template: |
const response = $.buildResponseList($.context.payloadList)
Expand Down
35 changes: 34 additions & 1 deletion src/cdk/v2/destinations/smartly/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { BatchUtils } = require('@rudderstack/workflow-engine');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const moment = require('moment');
const config = require('./config');
const {
getHashFromArrayWithDuplicate,
Expand Down Expand Up @@ -31,6 +32,38 @@ const getPayloads = (event, Config, payload) => {
return payloadLists;

Check warning on line 32 in src/cdk/v2/destinations/smartly/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/smartly/utils.js#L31-L32

Added lines #L31 - L32 were not covered by tests
};

const verifyAdInteractionTime = (adInteractionTime) => {
if (isDefinedAndNotNull(adInteractionTime)) {
let adInteractionTimeMoment;

// Handle both UNIX timestamps and UTC date strings
if (typeof adInteractionTime === 'number') {
adInteractionTimeMoment = moment.unix(adInteractionTime); // Parse as UNIX timestamp
} else {
adInteractionTimeMoment = moment.utc(adInteractionTime); // Parse as UTC date string
}

const currentMoment = moment.utc(); // Current time in UTC

// Calculate the time difference in days
const diffDaysPast = currentMoment.diff(adInteractionTimeMoment, 'days');
const diffDaysFuture = adInteractionTimeMoment.diff(currentMoment, 'days');

// Define the day range: 3 years (1095 days) in the past and 1 year (365 days) in the future
const maxDaysPast = 3 * 365 + 1; // 1095 days + 1 day for a leap year
const maxDaysFuture = 1 * 365 + 1; // 365 days + 1 day for a leap year

// Check if adInteractionTime is within the allowed range
const isWithinAllowedRange = diffDaysPast <= maxDaysPast && diffDaysFuture <= maxDaysFuture;

if (!isWithinAllowedRange) {
throw new InstrumentationError(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
}
}
};

const buildResponseList = (payloadList) =>
payloadList.map((payload) => {
const response = defaultRequestConfig();
Expand Down Expand Up @@ -84,4 +117,4 @@ const batchResponseBuilder = (events) => {
return response;

Check warning on line 117 in src/cdk/v2/destinations/smartly/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/smartly/utils.js#L117

Added line #L117 was not covered by tests
};

module.exports = { batchResponseBuilder, getPayloads, buildResponseList };
module.exports = { batchResponseBuilder, getPayloads, buildResponseList, verifyAdInteractionTime };
66 changes: 66 additions & 0 deletions src/cdk/v2/destinations/smartly/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const moment = require('moment');
const { InstrumentationError } = require('@rudderstack/workflow-engine');
const { verifyAdInteractionTime } = require('./utils');

describe('verifyAdInteractionTime', () => {
it('should pass when adInteractionTime is 2 years in the past (UNIX timestamp)', () => {
// 2 years ago from now
const adInteractionTime = moment().subtract(2, 'years').unix();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
});

it('should pass when adInteractionTime is 10 months in the future (UNIX timestamp)', () => {
// 10 months in the future from now
const adInteractionTime = moment().add(10, 'months').unix();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
});

it('should fail when adInteractionTime is 4 years in the past (UNIX timestamp)', () => {
// 4 years ago from now
const adInteractionTime = moment().subtract(4, 'years').unix();
expect(() => verifyAdInteractionTime(adInteractionTime)).toThrow(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
});

it('should fail when adInteractionTime is 2 years in the future (UNIX timestamp)', () => {
// 2 years in the future from now
const adInteractionTime = moment().add(2, 'years').unix();
expect(() => verifyAdInteractionTime(adInteractionTime)).toThrow(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
});

it('should pass when adInteractionTime is exactly 3 years in the past (UTC date string)', () => {
// Exactly 3 years ago from now
const adInteractionTime = moment.utc().subtract(3, 'years').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
});

it('should pass when adInteractionTime is exactly 1 year in the future (UTC date string)', () => {
// Exactly 1 year in the future from now
const adInteractionTime = moment.utc().add(1, 'year').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
});

it('should fail when adInteractionTime is 4 years in the past (UTC date string)', () => {
// 4 years ago from now
const adInteractionTime = moment.utc().subtract(4, 'years').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).toThrow(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
});

it('should fail when adInteractionTime is 2 years in the future (UTC date string)', () => {
// 2 years in the future from now
const adInteractionTime = moment.utc().add(2, 'years').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).toThrow(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
});

it('should not throw an error if adInteractionTime is null or undefined', () => {
expect(() => verifyAdInteractionTime(null)).not.toThrow();
expect(() => verifyAdInteractionTime(undefined)).not.toThrow();
});
});
7 changes: 6 additions & 1 deletion test/integrations/destinations/smartly/processor/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { trackTestData } from './track';
import { validationFailures } from './validation';

export const data = [...trackTestData, ...validationFailures];
export const mockFns = (_) => {
// @ts-ignore
jest.useFakeTimers().setSystemTime(new Date('2024-02-01'));
};

export const data = [...trackTestData, ...validationFailures].map((d) => ({ ...d, mockFns }));
16 changes: 9 additions & 7 deletions test/integrations/destinations/smartly/processor/track.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const moment = require('moment');
import { verifyAdInteractionTime } from '../../../../../src/cdk/v2/destinations/smartly/utils';
import { destination } from '../commonConfig';

export const trackTestData = [
Expand All @@ -13,11 +15,11 @@ export const trackTestData = [
{
destination,
message: {
event: 'Add to cart',
event_name: 'Add to cart',
properties: {
platform: 'meta',
ad_unit_id: 228287,
ad_interaction_time: '1650626278',
ad_unit_id: '228287',
ad_interaction_time: '1612137600',
email: '[email protected]',
},
type: 'track',
Expand All @@ -44,16 +46,16 @@ export const trackTestData = [
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://s2s.smartly.io/events',
endpoint: 'https://s2s.smartly.io/events/batch',
headers: {},
params: {},
body: {
JSON: {
platform: 'meta',
ad_unit_id: 228287,
ad_interaction_time: '1650626278',
ad_unit_id: '228287',
ad_interaction_time: '1612137600',
conversions: '1',
event: 'Add to cart',
event_name: 'Add to cart',
},
JSON_ARRAY: {},
XML: {},
Expand Down
10 changes: 5 additions & 5 deletions test/integrations/destinations/smartly/processor/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const validationFailures = [
destination,
message: {
type: 'track',
event: 'product purchased',
event_name: 'product purchased',
sentAt: '2021-01-25T16:12:02.048Z',
userId: 'john123',
properties: {
products: [{}],
ad_unit_id: '22123387',
ad_interaction_time: '1650626278',
ad_interaction_time: '1690867200',
},
integrations: {
All: true,
Expand Down Expand Up @@ -72,7 +72,7 @@ export const validationFailures = [
destination,
message: {
type: 'group',
event: 'purchase',
event_name: 'purchase',
sentAt: '2021-01-25T16:12:02.048Z',
userId: 'john67',
channel: 'mobile',
Expand All @@ -81,7 +81,7 @@ export const validationFailures = [
properties: {
platform: 'snapchat',
ad_unit_id: '2653387',
ad_interaction_time: '1650646278',
ad_interaction_time: '1690867200',
},
anonymousId: 'anon_123',
integrations: {
Expand Down Expand Up @@ -138,7 +138,7 @@ export const validationFailures = [
properties: {
platform: 'snapchat',
ad_unit_id: '2653387',
ad_interaction_time: '1650646278',
ad_interaction_time: '1675094400',
},
anonymousId: 'anon_123',
integrations: {
Expand Down
Loading

0 comments on commit 2cdc522

Please sign in to comment.