Skip to content

Commit 2356b29

Browse files
tests: add context and rekor-api tests (#57)
* tas: migrate tests Signed-off-by: Carlos Feria <[email protected]> * fix: lint and add 1 test Signed-off-by: Carlos Feria <[email protected]> * tests: add context and rekor-api tests Signed-off-by: Carlos Feria <[email protected]> * tests: add context and rekor-api tests Signed-off-by: Carlos Feria <[email protected]> --------- Signed-off-by: Carlos Feria <[email protected]>
1 parent d10059e commit 2356b29

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference types="@vitest/browser/context" />
2+
3+
import { beforeAll, describe, expect, it, vi } from "vitest";
4+
5+
// Mock the ENV module before any imports
6+
vi.mock("@app/env", () => ({
7+
default: {
8+
NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN: "https://example.com",
9+
},
10+
}));
11+
12+
import { fireEvent, render, screen } from "@testing-library/react";
13+
import { RekorClientProvider, useRekorBaseUrl, useRekorClient } from "./context";
14+
15+
const TestConsumerComponent = () => {
16+
useRekorClient();
17+
const [baseUrl, setBaseUrl] = useRekorBaseUrl();
18+
19+
return (
20+
<div>
21+
<button onClick={() => setBaseUrl("https://new.example.com")}>Change Base URL</button>
22+
<p>Base URL: {baseUrl}</p>
23+
</div>
24+
);
25+
};
26+
27+
describe("RekorClientContext", () => {
28+
beforeAll(() => vi.clearAllMocks());
29+
30+
it("provides a RekorClient instance and manages base URL", () => {
31+
render(
32+
<RekorClientProvider>
33+
<TestConsumerComponent />
34+
</RekorClientProvider>
35+
);
36+
37+
expect(screen.getByText(/Base URL: https:\/\/example.com/)).toBeInTheDocument();
38+
39+
fireEvent.click(screen.getByText(/Change Base URL/));
40+
41+
expect(screen.getByText(/Base URL: https:\/\/new.example.com/)).toBeInTheDocument();
42+
});
43+
});

client/src/app/api/context.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ENV from "@app/env";
12
import {
23
createContext,
34
type FunctionComponent,
@@ -15,6 +16,7 @@ export interface RekorClientContext {
1516
setBaseUrl: (_base: string | undefined) => void;
1617
}
1718

19+
/* eslint-disable react-refresh/only-export-components */
1820
export const RekorClientContext = createContext<RekorClientContext | undefined>(undefined);
1921

2022
interface RekorClientProviderProps {
@@ -29,8 +31,8 @@ export const RekorClientProvider: FunctionComponent<PropsWithChildren<RekorClien
2931

3032
useEffect(() => {
3133
if (baseUrl === undefined) {
32-
if (process.env.NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN) {
33-
setBaseUrl(process.env.NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN);
34+
if (ENV.NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN) {
35+
setBaseUrl(ENV.NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN);
3436
} else {
3537
setBaseUrl("https://rekor.sigstore.dev");
3638
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// <reference types="vitest/globals" />
2+
3+
import { renderHook } from "@testing-library/react";
4+
import { vi, type Mock } from "vitest";
5+
import { useRekorSearch } from "./rekor-api";
6+
import { useRekorClient } from "./context";
7+
8+
vi.mock("./context", () => ({
9+
useRekorClient: vi.fn(),
10+
}));
11+
12+
Object.defineProperty(global.self, "crypto", {
13+
value: {
14+
subtle: {
15+
digest: vi.fn().mockImplementation(() => {
16+
const hashBuffer = new ArrayBuffer(32);
17+
const hashArray = new Uint8Array(hashBuffer);
18+
hashArray.fill(0);
19+
return hashBuffer;
20+
}),
21+
},
22+
},
23+
});
24+
25+
describe("useRekorSearch", () => {
26+
it("searches by logIndex", async () => {
27+
const mockGetLogEntryByIndex = vi.fn().mockResolvedValue(0);
28+
29+
(useRekorClient as Mock).mockReturnValue({
30+
entries: { getLogEntryByIndex: mockGetLogEntryByIndex },
31+
});
32+
33+
const { result } = renderHook(() => useRekorSearch());
34+
35+
await result.current({ attribute: "logIndex", query: 123 });
36+
37+
expect(mockGetLogEntryByIndex).toHaveBeenCalledWith({ logIndex: 123 });
38+
});
39+
});

common/src/environment.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export interface ConsoleEnvType {
4444
/** Target URL for the UI server's `/api` proxy */
4545
CONSOLE_API_URL?: string;
4646

47+
/** Rekor Search */
48+
NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN?: string;
49+
4750
/** Location of branding files (relative paths computed from the project source root) */
4851
BRANDING?: string;
4952
}
@@ -72,6 +75,7 @@ export const buildConsoleEnv = ({
7275

7376
UI_INGRESS_PROXY_BODY_SIZE = "500m",
7477
CONSOLE_API_URL,
78+
NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN,
7579
BRANDING,
7680
}: Partial<ConsoleEnvType> = {}): ConsoleEnvType => ({
7781
NODE_ENV,
@@ -88,6 +92,7 @@ export const buildConsoleEnv = ({
8892

8993
UI_INGRESS_PROXY_BODY_SIZE,
9094
CONSOLE_API_URL,
95+
NEXT_PUBLIC_REKOR_DEFAULT_DOMAIN,
9196
BRANDING,
9297
});
9398

0 commit comments

Comments
 (0)