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
87 changes: 87 additions & 0 deletions __mocks__/gen-axios.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const axios = require("axios");
const logger = require("../logger");
const { isHttpStatusSuccess } = require("../v0/util");
jest.mock("axios");

/**
* Forms the mock axios client
* This client is used in cases where each response is returned almost immediately
*
* **Limitations**:
* - This mock client would not be useful for scenarios where parallel requests will be made(with randomly responding to requests)
* - This mock client would not be useful in-case there are delays needed in responding to requests
*
* @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 => {
let mockInstance;
switch (resp.type) {
case "constructor":
mockInstance = constructorMock;
break;
case "get":
mockInstance = getMock;
break;
case "delete":
mockInstance = deleteMock;
break;

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

const validateMockAxiosClientReqParams = ({ resp }) => {
let mockInstance;
switch (resp.type) {
case "constructor":
mockInstance = axios;
break;
case "get":
mockInstance = axios.get;
break;
case "delete":
mockInstance = axios.delete;
break;

default:
mockInstance = axios.post;
break;
}
if (Array.isArray(resp?.reqParams)) {
try {
expect(mockInstance).toHaveBeenCalledWith(...resp.reqParams);
} catch (error) {
logger.error(`Validate request parameters error ${resp.type} for mock axios client: ${error}`)
}
}
}

module.exports = { formAxiosMock, validateMockAxiosClientReqParams };
26 changes: 0 additions & 26 deletions __tests__/data/ga_deleteUsers_proxy_input.json

This file was deleted.

8 changes: 0 additions & 8 deletions __tests__/data/ga_deleteUsers_proxy_output.json

This file was deleted.

34 changes: 34 additions & 0 deletions __tests__/deleteUsers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# DeleteUsers Tests

All the tests data for deleteUsers are to be present in __tests__/data/deleteUsers/${destination}/

### Files and their significance
- __`handler_input.json`__ - Input data for `handleDeletionOfUsers` function in `versionedRouter.js`(alias for `_deleteUsers_proxy_input.json`)
- __`handler_output.json`__ - Output of `handleDeletionOfUsers` function in `versionedRouter.js`(alias for `_deleteUsers_proxy_output.json`)
- __`nw_client_data.json`__ - the mock http responses(An example can be seen in the case of `ga`)

### Fields in new files

#### nw_client_data.json

- Type: Array<Array<object>>
- The array of object is how many responses have to be sent back
- Each of the object contains below mentioned fields
- type:
- Indicates what type of http client invocation it is
- Recommended to be sent
- Supported values:
- post
- get
- delete
- constructor
- if nothing is mentioned, `post` is considered by default
- reqParams:
- Type: Array<any>
- Optional
- Indicates the expected arguments that are to be sent to the http client instance
- We would `recommend` to also add this as part of your `nw_client_data.json`
- response:
- Type: object
- Required
- The response that needs to be returned from the http client
50 changes: 50 additions & 0 deletions __tests__/deleteUsers/data/ga/handler_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"getValue": {
"x-rudder-dest-info": "{\"secret\": { \"access_token\": \"valid_token\" }}"
},
"request": {
"body": [
{
"destType": "GA",
"userAttributes": [
{
"userId": "test_user_1"
},
{
"userId": "test_user_2"
}
],
"config": {
"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
}
}
]
}
}
]
15 changes: 15 additions & 0 deletions __tests__/deleteUsers/data/ga/handler_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
[
{
"statusCode": 200,
"status": "successful"
}
],
[
{
"statusCode": 500,
"authErrorCategory": "REFRESH_TOKEN",
"error": "[GA] invalid credentials"
}
]
]
138 changes: 138 additions & 0 deletions __tests__/deleteUsers/data/ga/nw_client_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[
[
{
"type": "post",
"reqParams": [
"https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert",
{
"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"
}
}
],
"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",
"reqParams": [
"https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert",
{
"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"
}
}
],
"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",
"reqParams": [
"https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert",
{
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_3"
},
"webPropertyId": "UA-123456789-6"
},
{
"headers": {
"Authorization": "Bearer expired_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
}
],
"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",
"reqParams": [
"https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert",
{
"kind": "analytics#userDeletionRequest",
"id": {
"type": "USER_ID",
"userId": "test_user_4"
},
"webPropertyId": "UA-123456789-6"
},
{
"headers": {
"Authorization": "Bearer expired_token",
"Accept": "application/json",
"Content-Type": "application/json"
}
}
],
"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"
}
}
]
]
Loading