From a37e0dae956f2b71694e993928ca0132d0a5bb4b Mon Sep 17 00:00:00 2001 From: alex-slobodian Date: Tue, 5 Nov 2024 18:38:14 +0200 Subject: [PATCH] Add more tests --- src/app.test.tsx | 159 ++++++++++++++++++ .../CertificatesTopbar.test.tsx | 12 +- ...rtificate.test.tsx => certificate.test.ts} | 0 src/utils/download-certificate.test.ts | 51 ++++++ 4 files changed, 213 insertions(+), 9 deletions(-) rename src/utils/{certificate.test.tsx => certificate.test.ts} (100%) create mode 100644 src/utils/download-certificate.test.ts diff --git a/src/app.test.tsx b/src/app.test.tsx index dd3c38c9..9f0ab484 100644 --- a/src/app.test.tsx +++ b/src/app.test.tsx @@ -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("", () => { const providersMock = [ @@ -96,6 +100,135 @@ describe("", () => { }); }); + it("Should open delete certificate dialog", async () => { + vi.mocked(FortifyAPI).mockImplementation( + () => mockFortifyAPIInstance as FortifyAPI + ); + + render(); + + 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(); + + 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(); + + 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(); + + 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(); + + 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 @@ -119,4 +252,30 @@ describe("", () => { expect(screen.queryByText("test 1")).not.toBeInTheDocument(); }); }); + + it("Should handle sorting", async () => { + vi.mocked(FortifyAPI).mockImplementation( + () => mockFortifyAPIInstance as FortifyAPI + ); + + render(); + + 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/); + }); }); diff --git a/src/components/certificates-topbar/CertificatesTopbar.test.tsx b/src/components/certificates-topbar/CertificatesTopbar.test.tsx index 03da8575..4bc8828e 100644 --- a/src/components/certificates-topbar/CertificatesTopbar.test.tsx +++ b/src/components/certificates-topbar/CertificatesTopbar.test.tsx @@ -95,9 +95,7 @@ describe("", () => { const onCreateMock = vi.fn((data) => data); render(); - const newButton = screen.getByRole("button", { name: "New" }); - - await userEvent.click(newButton); + await userEvent.click(screen.getByRole("button", { name: "New" })); expect(screen.getByRole("presentation")).toBeInTheDocument(); @@ -117,9 +115,7 @@ describe("", () => { const onCreateMock = vi.fn((data) => data); render(); - const newButton = screen.getByRole("button", { name: "New" }); - - await userEvent.click(newButton); + await userEvent.click(screen.getByRole("button", { name: "New" })); expect(screen.getByRole("presentation")).toBeInTheDocument(); @@ -138,9 +134,7 @@ describe("", () => { it("Should handle import", async () => { render(); - const newButton = screen.getByRole("button", { name: "New" }); - - await userEvent.click(newButton); + await userEvent.click(screen.getByRole("button", { name: "New" })); expect(screen.getByRole("presentation")).toBeInTheDocument(); diff --git a/src/utils/certificate.test.tsx b/src/utils/certificate.test.ts similarity index 100% rename from src/utils/certificate.test.tsx rename to src/utils/certificate.test.ts diff --git a/src/utils/download-certificate.test.ts b/src/utils/download-certificate.test.ts new file mode 100644 index 00000000..790889ca --- /dev/null +++ b/src/utils/download-certificate.test.ts @@ -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"); + }); +});