From b5003d16aef9ab51d05ab5885bd402e59dfaf8e1 Mon Sep 17 00:00:00 2001 From: Vinay Teki Date: Mon, 4 Nov 2024 20:19:06 +0530 Subject: [PATCH] chore: new flags incorporate env variables via features.ts file --- src/controllers/misc.ts | 2 +- src/features.json | 105 ------------------------- src/features.ts | 116 ++++++++++++++++++++++++++++ src/services/__tests__/misc.test.ts | 40 ++++++++++ src/services/misc.ts | 8 +- test/apitests/service.api.test.ts | 6 +- 6 files changed, 162 insertions(+), 115 deletions(-) delete mode 100644 src/features.json create mode 100644 src/features.ts diff --git a/src/controllers/misc.ts b/src/controllers/misc.ts index e2efdab5db..7f8ec52825 100644 --- a/src/controllers/misc.ts +++ b/src/controllers/misc.ts @@ -21,7 +21,7 @@ export class MiscController { } public static features(ctx: Context) { - ctx.body = MiscService.getFetaures(); + ctx.body = MiscService.getFeatures(); ctx.status = 200; return ctx; } diff --git a/src/features.json b/src/features.json deleted file mode 100644 index 63862eefed..0000000000 --- a/src/features.json +++ /dev/null @@ -1,105 +0,0 @@ -{ - "routerTransform": { - "ACTIVE_CAMPAIGN": true, - "ALGOLIA": true, - "CANDU": true, - "DELIGHTED": true, - "DRIP": true, - "FB_CUSTOM_AUDIENCE": true, - "GA": true, - "GAINSIGHT": true, - "GAINSIGHT_PX": true, - "GOOGLESHEETS": true, - "GOOGLE_ADWORDS_ENHANCED_CONVERSIONS": true, - "GOOGLE_ADWORDS_REMARKETING_LISTS": true, - "GOOGLE_ADWORDS_OFFLINE_CONVERSIONS": true, - "HS": true, - "ITERABLE": true, - "KLAVIYO": true, - "KUSTOMER": true, - "MAILCHIMP": true, - "MAILMODO": true, - "MARKETO": true, - "OMETRIA": true, - "PARDOT": true, - "PINTEREST_TAG": true, - "PROFITWELL": true, - "SALESFORCE": true, - "SALESFORCE_OAUTH": true, - "SALESFORCE_OAUTH_SANDBOX": true, - "SFMC": true, - "SNAPCHAT_CONVERSION": true, - "TIKTOK_ADS": true, - "TRENGO": true, - "YAHOO_DSP": true, - "CANNY": true, - "LAMBDA": true, - "WOOTRIC": true, - "GOOGLE_CLOUD_FUNCTION": true, - "BQSTREAM": true, - "CLICKUP": true, - "FRESHMARKETER": true, - "FRESHSALES": true, - "MONDAY": true, - "CUSTIFY": true, - "USER": true, - "REFINER": true, - "FACEBOOK_OFFLINE_CONVERSIONS": true, - "MAILJET": true, - "SNAPCHAT_CUSTOM_AUDIENCE": true, - "MARKETO_STATIC_LIST": true, - "CAMPAIGN_MANAGER": true, - "SENDGRID": true, - "SENDINBLUE": true, - "ZENDESK": true, - "MP": true, - "TIKTOK_ADS_OFFLINE_EVENTS": true, - "CRITEO_AUDIENCE": true, - "CUSTOMERIO": true, - "BRAZE": true, - "OPTIMIZELY_FULLSTACK": true, - "TWITTER_ADS": true, - "CLEVERTAP": true, - "ORTTO": true, - "GLADLY": true, - "ONE_SIGNAL": true, - "TIKTOK_AUDIENCE": true, - "REDDIT": true, - "THE_TRADE_DESK": true, - "INTERCOM": true, - "NINETAILED": true, - "KOALA": true, - "LINKEDIN_ADS": true, - "BLOOMREACH": true, - "MOVABLE_INK": true, - "EMARSYS": true, - "KODDI": true, - "WUNDERKIND": true, - "CLICKSEND": true, - "ZOHO": true, - "CORDIAL": true, - "X_AUDIENCE": true, - "BLOOMREACH_CATALOG": true, - "SMARTLY": true, - "HTTP": true, - "AMAZON_AUDIENCE": true, - "INTERCOM_V2": true - }, - "regulations": [ - "BRAZE", - "AM", - "INTERCOM", - "CLEVERTAP", - "AF", - "MP", - "GA", - "ITERABLE", - "ENGAGE", - "CUSTIFY", - "SENDGRID", - "SPRIG", - "EMARSYS" - ], - "supportSourceTransformV1": true, - "supportTransformerProxyV1": true -} diff --git a/src/features.ts b/src/features.ts new file mode 100644 index 0000000000..9f60d44483 --- /dev/null +++ b/src/features.ts @@ -0,0 +1,116 @@ +interface FeaturesConfig { + routerTransform: Record; + regulations: string[]; + supportSourceTransformV1: boolean; + supportTransformerProxyV1: boolean; + upgradedToSourceTransformV2?: boolean; +} + +const defaultFeaturesConfig: FeaturesConfig = { + routerTransform: { + ACTIVE_CAMPAIGN: true, + ALGOLIA: true, + CANDU: true, + DELIGHTED: true, + DRIP: true, + FB_CUSTOM_AUDIENCE: true, + GA: true, + GAINSIGHT: true, + GAINSIGHT_PX: true, + GOOGLESHEETS: true, + GOOGLE_ADWORDS_ENHANCED_CONVERSIONS: true, + GOOGLE_ADWORDS_REMARKETING_LISTS: true, + GOOGLE_ADWORDS_OFFLINE_CONVERSIONS: true, + HS: true, + ITERABLE: true, + KLAVIYO: true, + KUSTOMER: true, + MAILCHIMP: true, + MAILMODO: true, + MARKETO: true, + OMETRIA: true, + PARDOT: true, + PINTEREST_TAG: true, + PROFITWELL: true, + SALESFORCE: true, + SALESFORCE_OAUTH: true, + SALESFORCE_OAUTH_SANDBOX: true, + SFMC: true, + SNAPCHAT_CONVERSION: true, + TIKTOK_ADS: true, + TRENGO: true, + YAHOO_DSP: true, + CANNY: true, + LAMBDA: true, + WOOTRIC: true, + GOOGLE_CLOUD_FUNCTION: true, + BQSTREAM: true, + CLICKUP: true, + FRESHMARKETER: true, + FRESHSALES: true, + MONDAY: true, + CUSTIFY: true, + USER: true, + REFINER: true, + FACEBOOK_OFFLINE_CONVERSIONS: true, + MAILJET: true, + SNAPCHAT_CUSTOM_AUDIENCE: true, + MARKETO_STATIC_LIST: true, + CAMPAIGN_MANAGER: true, + SENDGRID: true, + SENDINBLUE: true, + ZENDESK: true, + MP: true, + TIKTOK_ADS_OFFLINE_EVENTS: true, + CRITEO_AUDIENCE: true, + CUSTOMERIO: true, + BRAZE: true, + OPTIMIZELY_FULLSTACK: true, + TWITTER_ADS: true, + CLEVERTAP: true, + ORTTO: true, + GLADLY: true, + ONE_SIGNAL: true, + TIKTOK_AUDIENCE: true, + REDDIT: true, + THE_TRADE_DESK: true, + INTERCOM: true, + NINETAILED: true, + KOALA: true, + LINKEDIN_ADS: true, + BLOOMREACH: true, + MOVABLE_INK: true, + EMARSYS: true, + KODDI: true, + WUNDERKIND: true, + CLICKSEND: true, + ZOHO: true, + CORDIAL: true, + X_AUDIENCE: true, + BLOOMREACH_CATALOG: true, + SMARTLY: true, + HTTP: true, + AMAZON_AUDIENCE: true, + INTERCOM_V2: true, + }, + regulations: [ + 'BRAZE', + 'AM', + 'INTERCOM', + 'CLEVERTAP', + 'AF', + 'MP', + 'GA', + 'ITERABLE', + 'ENGAGE', + 'CUSTIFY', + 'SENDGRID', + 'SPRIG', + 'EMARSYS', + ], + supportSourceTransformV1: true, + supportTransformerProxyV1: true, + upgradedToSourceTransformV2: process.env.UPGRADED_TO_SOURCE_TRANSFORM_V2 === 'true' || false, // redundant but required to show that the default is false +}; + +export default defaultFeaturesConfig; diff --git a/src/services/__tests__/misc.test.ts b/src/services/__tests__/misc.test.ts index 5dcd948b34..f4befd4a04 100644 --- a/src/services/__tests__/misc.test.ts +++ b/src/services/__tests__/misc.test.ts @@ -1,4 +1,5 @@ import { DestHandlerMap } from '../../constants/destinationCanonicalNames'; +import defaultFeaturesConfig from '../../features'; import { MiscService } from '../misc'; describe('Misc tests', () => { @@ -24,3 +25,42 @@ describe('Misc tests', () => { ); }); }); + +describe('Misc | getFeatures', () => { + const originalEnv = process.env; + + beforeEach(() => { + // Reset environment variables and module cache before each test + process.env = { ...originalEnv }; + jest.resetModules(); + }); + + afterAll(() => { + // Restore the original environment variables after all tests + process.env = originalEnv; + }); + + function getMiscService() { + // Re-import config and featuresService after environment variables are set + const { MiscService: miscService } = require('../misc'); + return miscService; + } + + it('should return the default configuration as a JSON string', () => { + const miscService = getMiscService(); + const expectedConfig = JSON.stringify(defaultFeaturesConfig); + const result = miscService.getFeatures(); + expect(result).toBe(expectedConfig); + }); + + it('should return configuration with upgradedToSourceTransformV2 overridden by environment variable', () => { + process.env.UPGRADED_TO_SOURCE_TRANSFORM_V2 = 'true'; + const expectedConfig = { + ...defaultFeaturesConfig, + upgradedToSourceTransformV2: true, + }; + const miscService = getMiscService(); + const result = miscService.getFeatures(); + expect(result).toBe(JSON.stringify(expectedConfig)); + }); +}); diff --git a/src/services/misc.ts b/src/services/misc.ts index 09051edeec..334b54ba17 100644 --- a/src/services/misc.ts +++ b/src/services/misc.ts @@ -1,10 +1,9 @@ /* eslint-disable global-require, import/no-dynamic-require */ -import fs from 'fs'; import { Context } from 'koa'; -import path from 'path'; import { DestHandlerMap } from '../constants/destinationCanonicalNames'; import { getCPUProfile, getHeapProfile } from '../middleware'; import { Metadata } from '../types'; +import defaultFeaturesConfig from '../features'; export class MiscService { public static getDestHandler(dest: string, version: string) { @@ -62,9 +61,8 @@ export class MiscService { return process.env.npm_package_version || 'Version Info not found'; } - public static getFetaures() { - const obj = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../features.json'), 'utf8')); - return JSON.stringify(obj); + public static getFeatures() { + return JSON.stringify(defaultFeaturesConfig); } public static async getCPUProfile(seconds: number) { diff --git a/test/apitests/service.api.test.ts b/test/apitests/service.api.test.ts index 2a0db6978e..9c1d96e7fe 100644 --- a/test/apitests/service.api.test.ts +++ b/test/apitests/service.api.test.ts @@ -8,6 +8,7 @@ import request from 'supertest'; import networkHandlerFactory from '../../src/adapters/networkHandlerFactory'; import { FetchHandler } from '../../src/helpers/fetchHandlers'; import { applicationRoutes } from '../../src/routes'; +import defaultFeaturesConfig from '../../src/features'; let server: any; const OLD_ENV = process.env; @@ -43,12 +44,9 @@ const getDataFromPath = (pathInput) => { describe('features tests', () => { test('successful features response', async () => { - const expectedData = JSON.parse( - fs.readFileSync(path.resolve(__dirname, '../../src/features.json'), 'utf8'), - ); const response = await request(server).get('/features'); expect(response.status).toEqual(200); - expect(JSON.parse(response.text)).toEqual(expectedData); + expect(JSON.parse(response.text)).toEqual(defaultFeaturesConfig); }); test('features regulations should be array', async () => {