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

Bump axios from 0.27.2 to 1.1.3 in /dashboard #5475

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@tanstack/match-sorter-utils": "^8.5.14",
"@tanstack/react-table": "^8.5.13",
"ajv": "^8.11.0",
"axios": "^0.27.2",
"axios": "^1.1.3",
"connected-react-router": "^6.9.3",
"fast-json-patch": "^3.1.1",
"google-protobuf": "^3.21.2",
Expand Down Expand Up @@ -81,7 +81,6 @@
"@types/js-yaml": "^4.0.5",
"@types/jsonwebtoken": "^8.5.9",
"@types/lodash": "^4.14.186",
"@types/moxios": "^0.4.15",
"@types/qs": "^6.9.7",
"@types/react-copy-to-clipboard": "^5.0.4",
"@types/react-dom": "^18.0.6",
Expand All @@ -92,6 +91,7 @@
"@types/redux-mock-store": "^1.0.3",
"@types/swagger-ui-react": "^4.11.0",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"axios-mock-adapter": "^1.21.2",
"cross-env": "^7.0.3",
"enzyme": "^3.11.0",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -102,7 +102,6 @@
"jest-plugin-context": "^2.9.0",
"mock-socket": "^9.1.5",
"monaco-editor-webpack-plugin": "^7.0.1",
"moxios": "^0.4.0",
"node-polyfill-webpack-plugin": "^2.0.1",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.16",
Expand Down Expand Up @@ -134,7 +133,7 @@
"!src/**/*.d.ts"
],
"transformIgnorePatterns": [
"node_modules/(?!@cds|@clr|@lit|bail|ccount|cds|character-entities|comma-separated-tokens|decode-named-character-reference|escape-string-regexp|hast-util-whitespace|is-plain-obj|lit|lodash-es|markdown-table|mdast-util-definitions|mdast-util-find-and-replace|mdast-util-from-markdown|mdast-util-gfm-autolink-literal|mdast-util-gfm|mdast-util-to-hast|mdast-util-to-markdown|mdast-util-to-string|micromark-core-commonmark|monaco-editor|react-monaco-editor|micromark|parse-entities|property-information|ramda|react-markdown|react-syntax-highlighter|remark-breaks|remark-gfm|remark-parse|remark-rehype|space-separated-tokens|swagger-client|swagger-ui-react|trim-lines|trough|unified|unist-builder|unist-util-generated|unist-util-is|unist-util-position|unist-util-stringify-position|unist-util-visit-parents|unist-util-visit|util-find-and-replace|vfile-message|vfile|.*css)"
"node_modules/(?!@cds|@clr|@lit|axios|bail|ccount|cds|character-entities|comma-separated-tokens|decode-named-character-reference|escape-string-regexp|hast-util-whitespace|is-plain-obj|lit|lodash-es|markdown-table|mdast-util-definitions|mdast-util-find-and-replace|mdast-util-from-markdown|mdast-util-gfm-autolink-literal|mdast-util-gfm|mdast-util-to-hast|mdast-util-to-markdown|mdast-util-to-string|micromark-core-commonmark|monaco-editor|react-monaco-editor|micromark|parse-entities|property-information|ramda|react-markdown|react-syntax-highlighter|remark-breaks|remark-gfm|remark-parse|remark-rehype|space-separated-tokens|swagger-client|swagger-ui-react|trim-lines|trough|unified|unist-builder|unist-util-generated|unist-util-is|unist-util-position|unist-util-stringify-position|unist-util-visit-parents|unist-util-visit|util-find-and-replace|vfile-message|vfile|.*css)"
]
},
"browserslist": {
Expand Down
68 changes: 21 additions & 47 deletions dashboard/src/shared/AxiosInstance.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0

import * as moxios from "moxios";
import MockAdapter from "axios-mock-adapter";
import { IAuthState } from "reducers/auth";
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
Expand All @@ -24,6 +24,7 @@ describe("createAxiosInterceptorWithAuth", () => {
const authToken = "search-google-in-google";

let store: any;
let axiosMock: MockAdapter;

beforeAll(() => {
const state: IAuthState = {
Expand Down Expand Up @@ -53,21 +54,20 @@ describe("createAxiosInterceptorWithAuth", () => {
});

beforeEach(() => {
// Import as "any" to avoid typescript syntax error
moxios.install(axios as any);
axiosMock = new MockAdapter(axios);
});

afterEach(() => {
moxios.uninstall(axios as any);
axiosMock.restore();
store.clearActions();
});

it("includes the auth token if provided", async () => {
moxios.stubRequest(testPath, {});
axiosMock.onGet(testPath).reply(200, {});

await axios.get(testPath);
const request = moxios.requests.mostRecent();
expect(request.headers.Authorization).toBe(`Bearer ${authToken}`);
const request = axiosMock.history.get[0];
expect(request?.headers?.Authorization).toBe(`Bearer ${authToken}`);
});

const testCases = [
Expand All @@ -81,35 +81,23 @@ describe("createAxiosInterceptorWithAuth", () => {

testCases.forEach(t => {
it(`returns a custom message if ${t.code} returned`, async () => {
moxios.stubRequest(testPath, {
response: { message: `Will raise ${t.errorClass.name}` },
status: t.code,
});
axiosMock.onGet(testPath).reply(t.code, { message: `Will raise ${t.errorClass.name}` });
await expect(axios.get(testPath)).rejects.toThrow(`Will raise ${t.errorClass.name}`);
});

it(`returns the custom error ${t.errorClass.name} if ${t.code} returned`, async () => {
moxios.stubRequest(testPath, {
response: {},
status: t.code,
});
axiosMock.onGet(testPath).reply(t.code, {});
await expect(axios.get(testPath)).rejects.toThrowError(t.errorClass);
});
});

it("returns the generic error message otherwise", async () => {
moxios.stubRequest(testPath, {
response: {},
status: 555,
});
axiosMock.onGet(testPath).reply(555, {});
await expect(axios.get(testPath)).rejects.toThrow("Request failed with status code 555");
});

it("returns the response message", async () => {
moxios.stubRequest(testPath, {
response: { message: "this is an error!" },
status: 555,
});
axiosMock.onGet(testPath).reply(555, { message: "this is an error!" });
await expect(axios.get(testPath)).rejects.toThrow("this is an error!");
});

Expand All @@ -126,11 +114,7 @@ describe("createAxiosInterceptorWithAuth", () => {
type: "SET_AUTHENTICATION_SESSION_EXPIRED",
},
];

moxios.stubRequest(testPath, {
response: { message: "Boom!" },
status: 401,
});
axiosMock.onGet(testPath).reply(401, { message: "Boom!" });
await expect(axios.get(testPath)).rejects.toThrow("Boom!");
expect(store.getActions()).toEqual(expectedActions);
expect(Auth.unsetAuthCookie).toHaveBeenCalled();
Expand All @@ -141,7 +125,7 @@ describe("createAxiosInterceptorWithAuth", () => {
Auth.unsetAuthCookie = jest.fn();
const expectedActions = [
{
payload: "not ajson paylod",
payload: "not a json payload",
type: "AUTHENTICATION_ERROR",
},
{
Expand All @@ -150,11 +134,8 @@ describe("createAxiosInterceptorWithAuth", () => {
},
];

moxios.stubRequest(testPath, {
responseText: "not ajson paylod",
status: 401,
});
await expect(axios.get(testPath)).rejects.toThrow("not ajson paylod");
axiosMock.onGet(testPath).reply(401, { message: "not a json payload" });
await expect(axios.get(testPath)).rejects.toThrow("not a json payload");
expect(store.getActions()).toEqual(expectedActions);
expect(Auth.unsetAuthCookie).toHaveBeenCalled();
});
Expand All @@ -179,13 +160,9 @@ describe("createAxiosInterceptorWithAuth", () => {
type: "CLEAR_CLUSTERS",
},
];

moxios.stubRequest(testPath, {
response: {
message:
'{"metadata":{},"status":"Failure","message":"selfsubjectaccessreviews.authorization.k8s.io is forbidden: User "system:anonymous" cannot create resource "selfsubjectaccessreviews" in API group "authorization.k8s.io" at the cluster scope","reason":"Forbidden","details":{"group":"authorization.k8s.io","kind":"selfsubjectaccessreviews"},"code":403} {"namespaces":null}',
},
status: 403,
axiosMock.onGet(testPath).reply(403, {
message:
'{"metadata":{},"status":"Failure","message":"selfsubjectaccessreviews.authorization.k8s.io is forbidden: User "system:anonymous" cannot create resource "selfsubjectaccessreviews" in API group "authorization.k8s.io" at the cluster scope","reason":"Forbidden","details":{"group":"authorization.k8s.io","kind":"selfsubjectaccessreviews"},"code":403} {"namespaces":null}',
});
await expect(axios.get(testPath)).rejects.toThrow(
'{"metadata":{},"status":"Failure","message":"selfsubjectaccessreviews.authorization.k8s.io is forbidden: User "system:anonymous" cannot create resource "selfsubjectaccessreviews" in API group "authorization.k8s.io" at the cluster scope","reason":"Forbidden","details":{"group":"authorization.k8s.io","kind":"selfsubjectaccessreviews"},"code":403} {"namespaces":null}',
Expand All @@ -195,12 +172,9 @@ describe("createAxiosInterceptorWithAuth", () => {
});

it("parses a forbidden response", async () => {
moxios.stubRequest(testPath, {
response: {
message:
'[{"apiGroup": "v1", "resource": "secrets", "namespace": "default", "verbs": ["list", "get"]}]',
},
status: 403,
axiosMock.onGet(testPath).reply(403, {
message:
'[{"apiGroup": "v1", "resource": "secrets", "namespace": "default", "verbs": ["list", "get"]}]',
});
await expect(axios.get(testPath)).rejects.toThrow(
'Forbidden error, missing permissions: apiGroup: "v1", resource: "secrets", action: "list, get", namespace: default',
Expand Down
19 changes: 10 additions & 9 deletions dashboard/src/shared/Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
// SPDX-License-Identifier: Apache-2.0

import axios from "axios";
import * as moxios from "moxios";
import MockAdapter from "axios-mock-adapter";
import Config, { IConfig, SupportedThemes } from "./Config";

describe("Config", () => {
let defaultJSON: IConfig;
let initialEnv: any;
let axiosMock: MockAdapter;

beforeEach(() => {
initialEnv = { ...process.env };
// Import as "any" to avoid typescript syntax error
moxios.install(axios as any);
axiosMock = new MockAdapter(axios);

defaultJSON = require("../../public/config.json");
moxios.stubRequest("config.json", { status: 200, response: { ...defaultJSON } });
axiosMock.onGet("config.json").reply(200, { ...defaultJSON });
});

afterEach(() => {
process.env = initialEnv;
moxios.uninstall(axios as any);
axiosMock.restore();
jest.restoreAllMocks();
});

Expand All @@ -31,19 +32,19 @@ describe("Config", () => {
describe("Themes", () => {
let defaultJSON: IConfig;
let initialEnv: any;
let axiosMock: MockAdapter;

const matchMedia = window.matchMedia;
beforeEach(() => {
initialEnv = { ...process.env };
// Import as "any" to avoid typescript syntax error
moxios.install(axios as any);
axiosMock = new MockAdapter(axios);
defaultJSON = require("../../public/config.json");
moxios.stubRequest("config.json", { status: 200, response: { ...defaultJSON } });
axiosMock.onGet("config.json").reply(200, { ...defaultJSON });
});

afterEach(() => {
process.env = initialEnv;
moxios.uninstall(axios as any);
axiosMock.restore();
window.matchMedia = matchMedia;
jest.restoreAllMocks();
});
Expand Down
40 changes: 18 additions & 22 deletions dashboard/src/shared/Kube.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright 2018-2022 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0

import MockAdapter from "axios-mock-adapter";
import { CanIRequest, CanIResponse } from "gen/kubeappsapis/plugins/resources/v1alpha1/resources";
import * as moxios from "moxios";
import { axiosWithAuth } from "./AxiosInstance";
import { Kube } from "./Kube";
import KubeappsGrpcClient from "./KubeappsGrpcClient";

const clusterName = "cluster-name";

describe("App", () => {
describe("Kube", () => {
let axiosMock: MockAdapter;

beforeEach(() => {
// Import as "any" to avoid typescript syntax error
moxios.install(axiosWithAuth as any);
axiosMock = new MockAdapter(axiosWithAuth);
});
afterEach(() => {
moxios.uninstall(axiosWithAuth as any);
axiosMock.restore();
});

describe("getAPIGroups", () => {
Expand All @@ -34,17 +35,12 @@ describe("App", () => {
},
},
];
beforeEach(() => {
moxios.stubRequest(/.*/, {
// Sample response to /apis
response: { kind: "APIGroupList", apiVersion: "v1", groups },
status: 200,
});
});

it("should request API groups", async () => {
axiosMock.onGet().reply(200, { kind: "APIGroupList", apiVersion: "v1", groups });
expect(await Kube.getAPIGroups(clusterName)).toEqual(groups);
expect(moxios.requests.mostRecent().url).toBe(`api/clusters/${clusterName}/apis`);
const request = axiosMock.history.get[0];
expect(request?.url).toBe(`api/clusters/${clusterName}/apis`);
});
});

Expand Down Expand Up @@ -141,14 +137,14 @@ describe("App", () => {
].forEach(t => {
it(t.description, async () => {
// eslint-disable-next-line redos/no-vulnerable
moxios.stubRequest(/.*api\/v1/, t.apiV1Response);
axiosMock.onGet(/.*api\/v1/).reply(t.apiV1Response.status, t.apiV1Response.response);
const groups: any[] = [];
t.groups.forEach((g: any) => {
groups.push(g.input);
t.groups.forEach((group: any) => {
groups.push(group.input);
// eslint-disable-next-line redos/no-vulnerable
moxios.stubOnce("GET", /.*apis\/.*/, g.apiResponse);
axiosMock.onGet(/.*apis\/.*/).replyOnce(200, group.apiResponse.response);
});
expect(await Kube.getResourceKinds("cluster", groups)).toEqual(t.result);
expect(await Kube.getResourceKinds(clusterName, groups)).toEqual(t.result);
});
});
});
Expand All @@ -170,13 +166,13 @@ describe("App", () => {
jest.spyOn(client, "CanI").mockImplementation(mockClientCanI);
jest.spyOn(Kube, "resourcesServiceClient").mockImplementation(() => client);

const allowed = await Kube.canI("cluster", "v1", "namespaces", "create", "");
const allowed = await Kube.canI(clusterName, "v1", "namespaces", "create", "");
expect(allowed).toBe(true);

expect(Kube.resourcesServiceClient).toHaveBeenCalledWith();
expect(mockClientCanI).toHaveBeenCalledWith({
context: {
cluster: "cluster",
cluster: clusterName,
namespace: "",
},
group: "v1",
Expand All @@ -199,13 +195,13 @@ describe("App", () => {
jest.spyOn(client, "CanI").mockImplementation(mockClientCanI);
jest.spyOn(Kube, "resourcesServiceClient").mockImplementation(() => client);

const allowed = await Kube.canI("cluster", "v1", "secrets", "list", "");
const allowed = await Kube.canI(clusterName, "v1", "secrets", "list", "");
expect(allowed).toBe(false);

expect(Kube.resourcesServiceClient).toHaveBeenCalled();
expect(mockClientCanI).toHaveBeenCalledWith({
context: {
cluster: "cluster",
cluster: clusterName,
namespace: "",
},
group: "v1",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/shared/Kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Kube {
const { data: resourceList } = await axiosWithAuth.get<any>(
url.api.k8s.groupVersion(cluster, groupVersion),
);
resourceList.resources?.forEach((r: any) => addResource(r, groupVersion));
resourceList?.resources?.forEach((r: any) => addResource(r, groupVersion));
}),
);
return result;
Expand Down
14 changes: 0 additions & 14 deletions dashboard/src/shared/PackagesService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
PackageAppVersion,
} from "gen/kubeappsapis/core/packages/v1alpha1/packages";
import { Plugin } from "gen/kubeappsapis/core/plugins/v1alpha1/plugins";
import * as moxios from "moxios";
import { axiosWithAuth } from "./AxiosInstance";
import { KubeappsGrpcClient } from "./KubeappsGrpcClient";
import PackagesService from "./PackagesService";

Expand All @@ -19,18 +17,6 @@ const namespace = "namespace-name";
const defaultPageToken = "defaultPageToken";
const defaultSize = 0;
describe("App", () => {
beforeEach(() => {
// Import as "any" to avoid typescript syntax error
moxios.install(axiosWithAuth as any);
moxios.stubRequest(/.*/, {
response: { data: "ok" },
status: 200,
});
});
afterEach(() => {
moxios.uninstall(axiosWithAuth as any);
jest.restoreAllMocks();
});
describe("getAvailablePackageSummaries", () => {
[
{
Expand Down
Loading