mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-23 15:00:49 +01:00
Due to unexpected and largely unchangeable behavior by both react-router and the browser, square brackets are not correctly encoded in the url when clicking a file link in the source view where the filename contains either of these characters. The source view then tries to use the useSources hook to get the file content but fails, because the path param for the file path it gets from the url has unencoded square brackets in them which are illegal in urls except for declaring IPv6 addresses. We have created a catch for exactly this scenario at the latest possible point before the actual http request is fired, which is in the useSources hook. It seems like the square brackets are the only affected special characters so we force encoding on them specifically. Only the path portion of the URL is checked so the host portion of the url may still contain unencoded square brackets which are left untouched.
239 lines
7.3 KiB
TypeScript
239 lines
7.3 KiB
TypeScript
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
import { File, Repository } from "@scm-manager/ui-types";
|
|
import { useSources } from "./sources";
|
|
import fetchMock from "fetch-mock";
|
|
import createInfiniteCachingClient from "./tests/createInfiniteCachingClient";
|
|
import { act, renderHook } from "@testing-library/react-hooks";
|
|
import createWrapper from "./tests/createWrapper";
|
|
|
|
describe("Test sources hooks", () => {
|
|
const puzzle42: Repository = {
|
|
namespace: "puzzles",
|
|
name: "42",
|
|
type: "git",
|
|
_links: {
|
|
sources: {
|
|
href: "/src",
|
|
},
|
|
},
|
|
};
|
|
|
|
const readmeMd: File = {
|
|
name: "README.md",
|
|
path: "README.md",
|
|
directory: false,
|
|
revision: "abc",
|
|
length: 21,
|
|
description: "Awesome readme",
|
|
_links: {},
|
|
_embedded: {
|
|
children: [],
|
|
},
|
|
};
|
|
|
|
const rootDirectory: File = {
|
|
name: "",
|
|
path: "",
|
|
directory: true,
|
|
revision: "abc",
|
|
_links: {},
|
|
_embedded: {
|
|
children: [readmeMd],
|
|
},
|
|
};
|
|
|
|
const sepecialMd: File = {
|
|
name: "special.md",
|
|
path: "main/special.md",
|
|
directory: false,
|
|
revision: "abc",
|
|
length: 42,
|
|
description: "Awesome special file",
|
|
_links: {},
|
|
_embedded: {
|
|
children: [],
|
|
},
|
|
};
|
|
|
|
const sepecialMdPartial: File = {
|
|
...sepecialMd,
|
|
partialResult: true,
|
|
computationAborted: false,
|
|
};
|
|
|
|
const sepecialMdComputationAborted: File = {
|
|
...sepecialMd,
|
|
partialResult: true,
|
|
computationAborted: true,
|
|
};
|
|
|
|
const mainDirectoryTruncated: File = {
|
|
name: "main",
|
|
path: "main",
|
|
directory: true,
|
|
revision: "abc",
|
|
truncated: true,
|
|
_links: {
|
|
proceed: {
|
|
href: "src/2",
|
|
},
|
|
},
|
|
_embedded: {
|
|
children: [],
|
|
},
|
|
};
|
|
|
|
const mainDirectory: File = {
|
|
...mainDirectoryTruncated,
|
|
truncated: false,
|
|
_embedded: {
|
|
children: [sepecialMd],
|
|
},
|
|
};
|
|
|
|
beforeEach(() => {
|
|
fetchMock.reset();
|
|
});
|
|
|
|
const firstChild = (directory?: File) => {
|
|
if (directory?._embedded?.children && directory._embedded.children.length > 0) {
|
|
return directory._embedded.children[0];
|
|
}
|
|
};
|
|
|
|
describe("useSources tests", () => {
|
|
const testPath = async (path: string, expectedPath: string) => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
fetchMock.getOnce(`/api/v2/src/abc/${expectedPath}?collapse=true`, readmeMd);
|
|
const { result, waitFor } = renderHook(() => useSources(puzzle42, { revision: "abc", path }), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
await waitFor(() => !!result.current.data);
|
|
expect(result.current.data).toEqual(readmeMd);
|
|
};
|
|
|
|
it("should return file from url with revision and path", () => testPath("README.md", "README.md"));
|
|
it("should encode square brackets in path", () =>
|
|
testPath("[...nextauth][...other].ts", "%5B...nextauth%5D%5B...other%5D.ts"));
|
|
it("should not double-encode path", () => testPath("%7BDatei%7D#42.txt", "%7BDatei%7D#42.txt"));
|
|
|
|
it("should return root directory", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
fetchMock.getOnce("/api/v2/src?collapse=true", rootDirectory);
|
|
const { result, waitFor } = renderHook(() => useSources(puzzle42), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
await waitFor(() => !!result.current.data);
|
|
expect(result.current.data).toEqual(rootDirectory);
|
|
});
|
|
|
|
it("should fetch next page", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
fetchMock.getOnce("/api/v2/src?collapse=true", mainDirectoryTruncated);
|
|
fetchMock.getOnce("/api/v2/src/2", mainDirectory);
|
|
const { result, waitFor, waitForNextUpdate } = renderHook(() => useSources(puzzle42), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
await waitFor(() => !!result.current.data);
|
|
|
|
expect(result.current.data).toEqual(mainDirectoryTruncated);
|
|
|
|
await act(() => {
|
|
const { fetchNextPage } = result.current;
|
|
fetchNextPage();
|
|
return waitForNextUpdate();
|
|
});
|
|
await waitFor(() => !result.current.isFetchingNextPage);
|
|
|
|
expect(result.current.data).toEqual(mainDirectory);
|
|
});
|
|
|
|
it("should refetch if partial files exists", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
fetchMock.get(
|
|
"/api/v2/src?collapse=true",
|
|
{
|
|
...mainDirectory,
|
|
_embedded: {
|
|
children: [sepecialMdPartial],
|
|
},
|
|
},
|
|
{
|
|
repeat: 1,
|
|
}
|
|
);
|
|
fetchMock.get(
|
|
"/api/v2/src?collapse=true",
|
|
{
|
|
...mainDirectory,
|
|
_embedded: {
|
|
children: [sepecialMd],
|
|
},
|
|
},
|
|
{
|
|
repeat: 1,
|
|
overwriteRoutes: false,
|
|
}
|
|
);
|
|
|
|
const { result, waitFor } = renderHook(() => useSources(puzzle42, { refetchPartialInterval: 100 }), {
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
});
|
|
|
|
await waitFor(() => !!firstChild(result.current.data));
|
|
expect(firstChild(result.current.data)?.partialResult).toBe(true);
|
|
|
|
await waitFor(() => !firstChild(result.current.data)?.partialResult);
|
|
expect(firstChild(result.current.data)?.partialResult).toBeFalsy();
|
|
});
|
|
|
|
it("should not refetch if computation is aborted", async () => {
|
|
const queryClient = createInfiniteCachingClient();
|
|
fetchMock.getOnce("/api/v2/src/abc/main/special.md?collapse=true", sepecialMdComputationAborted, { repeat: 1 });
|
|
// should never be called
|
|
fetchMock.getOnce("/api/v2/src/abc/main/special.md?collapse=true", sepecialMd, {
|
|
repeat: 1,
|
|
overwriteRoutes: false,
|
|
});
|
|
const { result, waitFor } = renderHook(
|
|
() =>
|
|
useSources(puzzle42, {
|
|
revision: "abc",
|
|
path: "main/special.md",
|
|
refetchPartialInterval: 100,
|
|
}),
|
|
{
|
|
wrapper: createWrapper(undefined, queryClient),
|
|
}
|
|
);
|
|
await waitFor(() => !!result.current.data);
|
|
expect(result.current.data).toEqual(sepecialMdComputationAborted);
|
|
|
|
await new Promise((r) => setTimeout(r, 200));
|
|
expect(result.current.data).toEqual(sepecialMdComputationAborted);
|
|
});
|
|
});
|
|
});
|