Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move deleteusers tests to a separate file #1551

Merged
merged 12 commits into from
Nov 14, 2022
49 changes: 49 additions & 0 deletions __mocks__/gen-axios.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const axios = require("axios");
const { isHttpStatusSuccess } = require("../v0/util");
jest.mock("axios");

/**
* Forms the mock axios client
* @param {Array<{type: 'constructor'|'get'|'post'|'delete', response: any }>} responsesData
* @returns
*/
const formAxiosMock = responsesData => {
const returnVal = ({ resp, mockInstance }) => {
if (isHttpStatusSuccess(resp.response.status)) {
mockInstance.mockResolvedValueOnce(resp.response);
} else {
mockInstance.mockRejectedValueOnce(resp.response);
}
};

if (Array.isArray(responsesData)) {
const constructorMock = jest.fn();
const postMock = jest.fn();
const getMock = jest.fn();
const deleteMock = jest.fn();
responsesData.flat().forEach(resp => {
switch (resp.type) {
case "constructor":
returnVal({ resp, mockInstance: constructorMock });
break;
case "get":
returnVal({ resp, mockInstance: getMock });
break;
case "delete":
returnVal({ resp, mockInstance: deleteMock });
break;

default:
returnVal({ resp, mockInstance: postMock });
break;
}
});
axios.get = getMock;
axios.post = postMock;
axios.delete = deleteMock;
axios.mockImplementation(constructorMock);
}
return axios;
};

module.exports = formAxiosMock;
26 changes: 25 additions & 1 deletion __tests__/data/ga_deleteUsers_proxy_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@
}
],
"config": {
"trackingID": "UA-12344-556790",
"trackingID": "UA-123456789-5",
"useNativeSDK": false
}
}
]
}
},
{
"getValue": {
"x-rudder-dest-info": "{\"secret\": { \"access_token\": \"expired_token\" }}"
},
"request": {
"body": [
{
"destType": "GA",
"userAttributes": [
{
"userId": "test_user_3"
},
{
"userId": "test_user_4"
}
],
"config": {
"trackingID": "UA-123456789-6",
"useNativeSDK": false
}
}
Expand Down
7 changes: 7 additions & 0 deletions __tests__/data/ga_deleteUsers_proxy_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
"statusCode": 200,
"status": "successful"
}
],
[
{
"statusCode": 500,
"authErrorCategory": "REFRESH_TOKEN",
"error": "[GA] invalid credentials"
}
]
]
40 changes: 40 additions & 0 deletions __tests__/data/ga_deleteUsers_req_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"userAttributes": [
{
"userId": "test_user_1"
},
{
"userId": "test_user_2"
}
],
"config": {
"trackingID": "UA-123456789-5",
"useNativeSDK": false
},
"destInfo": {
"secret": {
"access_token": "valid_token"
}
}
},
{
"userAttributes": [
{
"userId": "test_user_3"
},
{
"userId": "test_user_4"
}
],
"config": {
"trackingID": "UA-123456789-6",
"useNativeSDK": false
},
"destInfo": {
"secret": {
"access_token": "expired_token"
}
}
}
]
66 changes: 66 additions & 0 deletions __tests__/data/ga_deleteUsers_req_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
[
{
"body": {
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_1"
},
"webPropertyId": "UA-123456789-5"
},
"headers": {
"Authorization": "Bearer valid_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
},
{
"body": {
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_2"
},
"webPropertyId": "UA-123456789-5"
},
"headers": {
"Authorization": "Bearer valid_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
}
],
[
{
"body": {
"kind": "analytics#userDeletionRequest",
"webPropertyId": "UA-123456789-6",
"id": {
"type": "USER_ID",
"userId": "test_user_3"
}
},
"headers": {
"Authorization": "Bearer expired_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
},
{
"body": {
"kind": "analytics#userDeletionRequest",
"webPropertyId": "UA-123456789-6",
"id": {
"type": "USER_ID",
"userId": "test_user_4"
}
},
"headers": {
"Authorization": "Bearer expired_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
}
]
]
66 changes: 66 additions & 0 deletions __tests__/data/ga_deleteUsers_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
[
{
"type": "post",
"response": {
"data": {
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_1"
},
"webPropertyId": "UA-123456789-5",
"deletionRequestTime": "2022-11-04T10:39:57.933Z"
},
"status": 200,
"statusText": "OK"
}
},
{
"type": "post",
"response": {
"data": {
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_2"
},
"webPropertyId": "UA-123456789-5",
"deletionRequestTime": "2022-11-04T10:39:57.933Z"
},
"status": 200,
"statusText": "OK"
}
}
],
[
{
"type": "post",
"response": {
"data": {
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
},
"status": 200,
"statusText": "OK"
}
},
{
"type": "post",
"response": {
"data": {
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
},
"status": 200,
"statusText": "OK"
}
}
]
]
113 changes: 113 additions & 0 deletions __tests__/deleteUsers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const name = "DeleteUsers";
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
const logger = require("../logger");
const { mockedAxiosClient } = require("../__mocks__/network");
const formAxiosMock = require("../__mocks__/gen-axios.mock");
const deleteUserDestinations = [
"am",
"braze",
"intercom",
"mp",
"af",
"clevertap",
"engage",
"ga"
];
const { handleDeletionOfUsers } = require("../versionedRouter");
const { default: axios } = require("axios");
const version = "v0";

const buildPrepareDeleteRequestTestCases = () => {
const testCases = deleteUserDestinations
.reduce((acc, currentVal) => {
try {
const delUsers = require(`../${version}/destinations/${currentVal}/deleteUsers`);
const inputJson = require(`./data/${currentVal}_deleteUsers_req_input.json`);
const outputJson = require(`./data/${currentVal}_deleteUsers_req_output.json`);
acc.push({
inputJson,
outputJson,
destination: currentVal,
method: delUsers?.prepareDeleteRequest
});
} catch (error) {
// Do nothing
}
return acc;
}, [])
.map(v => {
const { inputJson, outputJson, method, destination } = v;
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
const cases = inputJson.map((reqInput, ind) => {

return [
destination,
{
input: reqInput,
prepareDeleteRequest: method,
expected: outputJson[ind],
index: ind
}
];
});
return cases;
}).flat();
return testCases;
}
const reqBuildTestCases = buildPrepareDeleteRequestTestCases();

describe("DeleteUsers request build tests", () => {
describe.each(reqBuildTestCases)(
".prepareDeleteRequest(%s)",
(_destination, { input, prepareDeleteRequest, expected, index }) => {
test(`Payload - ${index}`, () => {
expect(
prepareDeleteRequest?.(
input.userAttributes,
input.config,
input.destInfo
)
).toEqual(expected);
});
}
);
});

// delete user tests
deleteUserDestinations.forEach(destination => {
const inputData = require(`./data/${destination}_deleteUsers_proxy_input.json`);
const expectedData = require(`./data/${destination}_deleteUsers_proxy_output.json`);

describe(`${name} Tests: ${destination}`, () => {
beforeAll(() => {
let axiosResponses;
try {
axiosResponses = require(`./data/${destination}_deleteUsers_response.json`);
} catch (error) {
// Do nothing
logger.error(
`Error while reading ${destination}_deleteUsers_response.json: ${error}`
);
}
if (Array.isArray(axiosResponses)) {
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
formAxiosMock(axiosResponses);
} else {
// backward compatibility
jest.mock("axios");
axios.mockImplementation(mockedAxiosClient);
}
});
inputData.forEach((input, index) => {
it(`Payload - ${index}`, async () => {
try {
input.get = jest.fn(destInfoKey => {
return input.getValue && input.getValue[destInfoKey];
});

const output = await handleDeletionOfUsers(input);
expect(output).toEqual(expectedData[index]);
} catch (error) {
expect(error.message).toEqual(expectedData[index].error);
}
});
});
});
});
Loading