From e7a59f977ff29e53f7b6e7cc39f7f0654aeac120 Mon Sep 17 00:00:00 2001 From: kanishkkatara Date: Tue, 18 Jun 2024 13:09:57 +0530 Subject: [PATCH] chore: added tests for transform batch and code version 0 --- test/__tests__/user_transformation.test.js | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/test/__tests__/user_transformation.test.js b/test/__tests__/user_transformation.test.js index 1abece8f0c..e94df4f4df 100644 --- a/test/__tests__/user_transformation.test.js +++ b/test/__tests__/user_transformation.test.js @@ -1295,6 +1295,97 @@ describe("User transformation", () => { ); expect(output[0].transformedEvent.credentialValue).toEqual(undefined); }); + + it(`Simple ${name} Batch Test with credentials for codeVersion 1`, async () => { + const versionId = randomID(); + + const inputData = require(`./data/${integration}_input_credentials.json`); + + const respBody = { + versionId: versionId, + codeVersion: "1", + name, + code: ` + export function transformBatch(events, metadata) { + events.forEach((event) => { + event.credentialValue1 = credential("key1"); + event.credentialValue2 = credential("key3"); + }); + return events; + } + ` + }; + fetch.mockResolvedValue({ + status: 200, + json: jest.fn().mockResolvedValue(respBody) + }); + + const output = await userTransformHandler(inputData, versionId, []); + expect(fetch).toHaveBeenCalledWith( + `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}` + ); + expect(output[0].transformedEvent.credentialValue1).toEqual("value1"); + expect(output[0].transformedEvent.credentialValue2).toEqual(undefined); + }); + + it(`Simple ${name} Batch Test with credentials without key for codeVersion 1`, async () => { + const versionId = randomID(); + + const inputData = require(`./data/${integration}_input_credentials.json`); + + const respBody = { + versionId: versionId, + codeVersion: "1", + name, + code: ` + export function transformBatch(events, metadata) { + events.forEach((event) => { + event.credentialValue = credential(); + }); + return events; + } + ` + }; + fetch.mockResolvedValue({ + status: 200, + json: jest.fn().mockResolvedValue(respBody) + }); + + const output = await userTransformHandler(inputData, versionId, []); + expect(fetch).toHaveBeenCalledWith( + `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}` + ); + expect(output[0].error).toMatch(/Key should be valid and defined/); + }); + + it(`Simple ${name} Test with credentials for codeVersion 0`, async () => { + const versionId = randomID(); + + const inputData = require(`./data/${integration}_input_credentials.json`); + + const respBody = { + versionId: versionId, + codeVersion: "0", + name, + code: ` + function transform(events) { + events.forEach((event) => { + event.credentialValue = credential + }); + return events; + } + ` + }; + fetch.mockResolvedValue({ + status: 200, + json: jest.fn().mockResolvedValue(respBody) + }); + try { + await userTransformHandler(inputData, versionId, []); + } catch (e) { + expect(e).toEqual('credential is not defined'); + } + }); }); });