Skip to content

Commit

Permalink
feat: ga user delete support (#1531)
Browse files Browse the repository at this point in the history
* feat�: regulation api support for universal analytics initial commit

* modify deleteUsers api to include errorCategory, auth to send delete req to UA

* - correctly getting status code while error is thrown (using ErrorBuilder)
- mock koa context's get method
- implement a callback to get the header value

* adding secret validation

* remove unnecessary code

* add first successful test-case for ga delete support

Signed-off-by: Sai Sankeerth <[email protected]>

* remove comment

* rename variable

* new convention of parsing responses

* feat: move deleteusers tests to a separate file (#1551)

* extending common validations functions in all delete users supported destinations

* add test-case for erreneous scenario in a deleteUser request
- update the error message to include actual destination message for normal errors

* support for exclusion of destination for troubleshooting

Signed-off-by: Sai Sankeerth <[email protected]>
Co-authored-by: Sai Sankeerth <[email protected]>
  • Loading branch information
sanpj2292 and Sai Sankeerth authored Nov 24, 2022
1 parent ddbfef9 commit eb198dd
Show file tree
Hide file tree
Showing 35 changed files with 993 additions and 76 deletions.
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 };
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
],
[
{
"statusCode": 400,
"statusCode": 500,
"error": "androidAppId is required for android_advertising_id type identifier"
}
],
[
{
"statusCode": 400,
"statusCode": 500,
"error": "appleAppId is required for ios_advertising_id type identifier"
}
],
Expand Down
83 changes: 83 additions & 0 deletions __tests__/deleteUsers/data/ga/handler_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[
{
"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
}
}
]
}
},
{
"getValue": {
"x-rudder-dest-info": "{\"secret\": { \"access_token\": \"valid_token_1\" }}"
},
"request": {
"body": [
{
"destType": "GA",
"userAttributes": [
{
"userId": "test_user_5"
},
{
"userId": "test_user_6"
},
{
"userId": "test_user_7"
},
{
"userId": "test_user_8"
},
{
"userId": "test_user_9"
}
],
"config": {
"trackingID": "UA-123456789-7",
"useNativeSDK": false
}
}
]
}
}
]
21 changes: 21 additions & 0 deletions __tests__/deleteUsers/data/ga/handler_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
[
{
"statusCode": 200,
"status": "successful"
}
],
[
{
"statusCode": 500,
"authErrorCategory": "REFRESH_TOKEN",
"error": "[GA] invalid credentials"
}
],
[
{
"statusCode": 403,
"error": "[GA] Error occurred while completing deletion request: [dummy response] The parameter used to query is not correct"
}
]
]
Loading

0 comments on commit eb198dd

Please sign in to comment.