Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Nov 5, 2024
1 parent 1313904 commit a37e0da
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 9 deletions.
159 changes: 159 additions & 0 deletions src/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { FortifyAPI } from "@peculiar/fortify-client-core";
import { App } from "./app";

vi.mock("@peculiar/fortify-client-core");
vi.mock("@peculiar/certificates-viewer-react", () => ({
PeculiarCertificateViewer: () => "x509 certificate viewer component",
PeculiarCsrViewer: () => "CSR certificate viewer component",
}));

describe("<App />", () => {
const providersMock = [
Expand Down Expand Up @@ -96,6 +100,135 @@ describe("<App />", () => {
});
});

it("Should open delete certificate dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

await userEvent.click(screen.getAllByLabelText(/Delete certificate/)[0]);

await waitFor(() => {
expect(screen.getByText(/Delete certificate/)).toBeInTheDocument();
});
expect(
screen.getByText(/Are you sure you want to delete “Certificate test 2”/)
).toBeInTheDocument();
});

it("Should open view certificate details dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

await userEvent.click(
screen.getAllByRole("button", { name: /View details/ })[0]
);

await waitFor(() => {
expect(
screen.getByText(/“Certificate test 2” details/)
).toBeInTheDocument();
});
});

it("Should open import certificate dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

await userEvent.click(
screen.getByRole("menuitem", {
name: /Import certificate/,
})
);

await waitFor(() => {
expect(
screen.getByRole("heading", { name: /Import certificate/ })
).toBeInTheDocument();
});
});

it("Should open create certificate (x509) dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

await userEvent.click(
screen.getByRole("menuitem", {
name: /Create self-signed certificate/,
})
);

await waitFor(() => {
expect(
screen.getByRole("heading", { name: /Create Self-signed certificate/ })
).toBeInTheDocument();
});
});

it("Should open create certificate (CSR) dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

await userEvent.click(
screen.getByRole("menuitem", {
name: /Create certificate signing request \(CSR\)/,
})
);

await waitFor(() => {
expect(
screen.getByRole("heading", {
name: /Create Certificate Signing Request \(CSR\)/,
})
).toBeInTheDocument();
});
});

it("Should handle search & clear search", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
Expand All @@ -119,4 +252,30 @@ describe("<App />", () => {
expect(screen.queryByText("test 1")).not.toBeInTheDocument();
});
});

it("Should handle sorting", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

let cells = screen.getAllByRole("cell");
expect(cells[1]).toHaveTextContent(/Certificate test 2/);
expect(cells[5]).toHaveTextContent(/Certificate test 1/);

await userEvent.click(screen.getByText(/Name/));
expect(global.location.search).toMatch(/sort=label&order=asc/);

cells = screen.getAllByRole("cell");
expect(cells[1]).toHaveTextContent(/Certificate test 1/);
expect(cells[5]).toHaveTextContent(/Certificate test 2/);

await userEvent.click(screen.getByText(/Name/));
expect(global.location.search).toMatch(/sort=label&order=desc/);
});
});
12 changes: 3 additions & 9 deletions src/components/certificates-topbar/CertificatesTopbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ describe("<CertificatesTopbar />", () => {
const onCreateMock = vi.fn((data) => data);
render(<CertificatesTopbar {...defaultProps} onCreate={onCreateMock} />);

const newButton = screen.getByRole("button", { name: "New" });

await userEvent.click(newButton);
await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

Expand All @@ -117,9 +115,7 @@ describe("<CertificatesTopbar />", () => {
const onCreateMock = vi.fn((data) => data);
render(<CertificatesTopbar {...defaultProps} onCreate={onCreateMock} />);

const newButton = screen.getByRole("button", { name: "New" });

await userEvent.click(newButton);
await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

Expand All @@ -138,9 +134,7 @@ describe("<CertificatesTopbar />", () => {
it("Should handle import", async () => {
render(<CertificatesTopbar {...defaultProps} />);

const newButton = screen.getByRole("button", { name: "New" });

await userEvent.click(newButton);
await userEvent.click(screen.getByRole("button", { name: "New" }));

expect(screen.getByRole("presentation")).toBeInTheDocument();

Expand Down
File renamed without changes.
51 changes: 51 additions & 0 deletions src/utils/download-certificate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it, expect, vi } from "vitest";
import { downloadCertificate } from "./download-certificate";
import { CertificateType } from "../types";
import { Download } from "@peculiar/certificates-viewer";

vi.mock("@peculiar/certificates-viewer", () => ({
Download: {
cert: {
asPEM: vi.fn(),
},
csr: {
asPEM: vi.fn(),
},
},
}));

vi.mock("@peculiar/x509", () => ({
X509Certificate: vi.fn().mockImplementation(() => ({
toString: vi.fn().mockReturnValue("mocked-pem-x509"),
})),
Pkcs10CertificateRequest: vi.fn().mockImplementation(() => ({
toString: vi.fn().mockReturnValue("mocked-pem-csr"),
})),
}));

describe("downloadCertificate", () => {
const label = "test-label";
const mockCertRaw = new ArrayBuffer(8);

it("Should handle download x509 certificate as PEM", () => {
downloadCertificate(label, mockCertRaw, "x509");

expect(Download.cert.asPEM).toHaveBeenCalledWith("mocked-pem-x509", label);
});

it("Should handle download CSR as PEM", () => {
downloadCertificate(label, mockCertRaw, "csr");

expect(Download.csr.asPEM).toHaveBeenCalledWith("mocked-pem-csr", label);
});

it("Should throw an error for unsupported certificate type", () => {
expect(() =>
downloadCertificate(
label,
mockCertRaw,
"unsupported-type" as CertificateType
)
).toThrowError("Unsupported certificate type: unsupported-type");
});
});

0 comments on commit a37e0da

Please sign in to comment.