PromucFlow_constructor/app/client/src/ce/api/ApiUtils.test.ts
ChandanBalajiBP 888d9c0223
chore: Send default environmentId in header of all api call (#23745)
## Description
> Send unused_env as environmentId in all API header call.
>
>
#### PR fixes following issue(s)
Fixes #23766
>
#### Type of change
- Chore (housekeeping or task changes that don't impact user perception)
>
>
>
## Testing
>
#### How Has This Been Tested?
This changes is w.r.t #1521 PR in EE. Which will be tested in
regression.
- [x] Regression
>
>
## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flagx

#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Test-plan-implementation#speedbreaker-features-to-consider-for-every-change)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans/_edit#areas-of-interest)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-05-26 15:22:20 +05:30

124 lines
3.9 KiB
TypeScript

import {
apiRequestInterceptor,
apiSuccessResponseInterceptor,
apiFailureResponseInterceptor,
axiosConnectionAbortedCode,
} from "./ApiUtils";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
import type { ActionExecutionResponse } from "api/ActionAPI";
import {
createMessage,
ERROR_0,
SERVER_API_TIMEOUT_ERROR,
} from "@appsmith/constants/messages";
import { ERROR_CODES } from "@appsmith/constants/ApiConstants";
import * as Sentry from "@sentry/react";
describe("axios api interceptors", () => {
describe("Axios api request interceptor", () => {
it("adds timer to the request object", () => {
const request: AxiosRequestConfig = {
url: "https://app.appsmith.com/v1/api/actions/execute",
};
const interceptedRequest = apiRequestInterceptor(request);
expect(interceptedRequest).toHaveProperty("timer");
});
});
describe("Axios api response success interceptor", () => {
it("transforms an action execution response", () => {
const response: AxiosResponse = {
data: "Test data",
headers: {
// @ts-expect-error: content-length should be string
"content-length": 123,
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/actions/execute",
// @ts-expect-error: type mismatch
timer: 0,
},
};
const interceptedResponse: ActionExecutionResponse =
apiSuccessResponseInterceptor(response);
expect(interceptedResponse).toHaveProperty("clientMeta");
expect(interceptedResponse.clientMeta).toHaveProperty("size");
expect(interceptedResponse.clientMeta.size).toBe(123);
expect(interceptedResponse.clientMeta).toHaveProperty("duration");
});
it("just returns the response data for other requests", () => {
const response: AxiosResponse = {
data: "Test data",
headers: {
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/actions",
//@ts-expect-error: type mismatch
timer: 0,
},
};
const interceptedResponse: ActionExecutionResponse =
apiSuccessResponseInterceptor(response);
expect(interceptedResponse).toBe("Test data");
});
});
describe("Api response failure interceptor", () => {
beforeEach(() => {
jest.restoreAllMocks();
});
it("checks for no internet errors", () => {
jest.spyOn(navigator, "onLine", "get").mockReturnValue(false);
const interceptedResponse = apiFailureResponseInterceptor({});
expect(interceptedResponse).rejects.toStrictEqual({
message: createMessage(ERROR_0),
});
});
it.todo("handles axios cancel gracefully");
it("handles timeout errors", () => {
const error = {
code: axiosConnectionAbortedCode,
message: "timeout of 10000ms exceeded",
};
const interceptedResponse = apiFailureResponseInterceptor(error);
expect(interceptedResponse).rejects.toStrictEqual({
message: createMessage(SERVER_API_TIMEOUT_ERROR),
code: ERROR_CODES.REQUEST_TIMEOUT,
});
});
it("checks for response meta", () => {
const sentrySpy = jest.spyOn(Sentry, "captureException");
const response: AxiosResponse = {
data: "Test data",
headers: {
"content-type": "application/json",
},
config: {
url: "https://app.appsmith.com/v1/api/user",
//@ts-expect-error: type mismatch
timer: 0,
},
};
apiSuccessResponseInterceptor(response);
expect(sentrySpy).toHaveBeenCalled();
const interceptedFailureResponse = apiFailureResponseInterceptor({
response,
});
expect(interceptedFailureResponse).rejects.toStrictEqual("Test data");
expect(sentrySpy).toHaveBeenCalled();
});
});
});