From 2c8db72f5b2b0531b96dc550deddbd53638a9ea2 Mon Sep 17 00:00:00 2001 From: alex-slobodian Date: Tue, 29 Oct 2024 15:08:39 +0200 Subject: [PATCH] Add more tests for useCertificateCreateDialog hook --- .../useCertificateCreateDialog.test.tsx | 156 ++++++++++++++++-- 1 file changed, 138 insertions(+), 18 deletions(-) diff --git a/src/dialogs/certificate-create-dialog/useCertificateCreateDialog.test.tsx b/src/dialogs/certificate-create-dialog/useCertificateCreateDialog.test.tsx index 345d226..7ca05c9 100644 --- a/src/dialogs/certificate-create-dialog/useCertificateCreateDialog.test.tsx +++ b/src/dialogs/certificate-create-dialog/useCertificateCreateDialog.test.tsx @@ -3,12 +3,14 @@ import { useCertificateCreateDialog } from "./useCertificateCreateDialog"; import type { IProviderInfo, FortifyAPI } from "@peculiar/fortify-client-core"; +const addToastMock = vi.fn(); + vi.mock("@peculiar/react-components", async () => { const actual = await vi.importActual("@peculiar/react-components"); return { ...actual, useToast: () => ({ - addToast: vi.fn(), + addToast: addToastMock, }), }; }); @@ -16,6 +18,18 @@ vi.mock("@peculiar/react-components", async () => { describe("useCertificateCreateDialog", () => { const algorithm = { hash: "SHA-256", signature: "RSASSA-PKCS1-v1_5" }; + const defaultCreateProps = { + subject: { commonName: "example.com" }, + type: "x509", + algorithm, + }; + + const defaultResultMock = { + hashAlgorithm: "SHA-256", + signatureAlgorithm: "RSASSA-PKCS1-v1_5", + subjectName: "commonName=example.com", + }; + const providers = [ { id: "1", @@ -35,7 +49,7 @@ describe("useCertificateCreateDialog", () => { expect(result.current.open).toBeInstanceOf(Function); }); - it("Should open the dialog with x509 type", () => { + it("Should open & close the dialog with x509 type", () => { const { result } = renderHook(() => useCertificateCreateDialog({ providers, @@ -51,6 +65,12 @@ describe("useCertificateCreateDialog", () => { expect(DialogComponent).not.toBeNull(); expect(DialogComponent?.props.type).toBe("x509"); + + act(() => { + DialogComponent?.props.onDialogClose(); + }); + + expect(result.current.dialog()).toBeNull(); }); it("Should open the dialog with csr type", () => { @@ -87,23 +107,24 @@ describe("useCertificateCreateDialog", () => { act(() => { result.current.open("x509"); }); + const DialogComponent = result.current.dialog(); - await act(async () => { - const DialogComponent = result.current.dialog(); + DialogComponent?.props.onProviderSelect("2"); + await act(async () => { DialogComponent && (await DialogComponent.props.onCreateButtonClick({ - subject: { commonName: "example.com" }, - type: "x509", - algorithm, + ...defaultCreateProps, + extendedKeyUsages: ["serverAuth", "clientAuth"], })); }); - expect(onSuccessMock).toHaveBeenCalledWith("1"); - expect(mockFortifyClient.createX509).toHaveBeenCalledWith("1", { - hashAlgorithm: "SHA-256", - signatureAlgorithm: "RSASSA-PKCS1-v1_5", - subjectName: "commonName=example.com", + expect(onSuccessMock).toHaveBeenCalledWith("2"); + expect(mockFortifyClient.createX509).toHaveBeenCalledWith("2", { + ...defaultResultMock, + extensions: { + extendedKeyUsage: ["serverAuth", "clientAuth"], + }, }); }); @@ -131,17 +152,116 @@ describe("useCertificateCreateDialog", () => { DialogComponent && (await DialogComponent.props.onCreateButtonClick({ - subject: { commonName: "example.com" }, + ...defaultCreateProps, type: "csr", - algorithm, })); }); expect(onSuccessMock).toHaveBeenCalledWith("1"); - expect(mockFortifyClient.createPKCS10).toHaveBeenCalledWith("1", { - hashAlgorithm: "SHA-256", - signatureAlgorithm: "RSASSA-PKCS1-v1_5", - subjectName: "commonName=example.com", + expect(mockFortifyClient.createPKCS10).toHaveBeenCalledWith( + "1", + defaultResultMock + ); + }); + + it("Should show error message if certificate creation fails", async () => { + const onSuccessMock = vi.fn(); + const mockFortifyClient: Partial = { + createX509: vi.fn().mockImplementation(() => { + throw new Error("Error"); + }), + }; + + const { result } = renderHook(() => + useCertificateCreateDialog({ + providers, + onSuccess: onSuccessMock, + fortifyClient: mockFortifyClient as FortifyAPI, + currentProviderId: "1", + }) + ); + + act(() => { + result.current.open("x509"); + }); + + await act(async () => { + const DialogComponent = result.current.dialog(); + + DialogComponent && + (await DialogComponent.props.onCreateButtonClick(defaultCreateProps)); + }); + + expect(onSuccessMock).not.toHaveBeenCalled(); + expect(addToastMock).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Can't create certificate", + }) + ); + addToastMock.mockClear(); + }); + + it("Should show error message if pass invalid certificate type", async () => { + const onSuccessMock = vi.fn(); + const typeMock = "unregisterd_type" as "x509"; + const mockFortifyClient: Partial = { + createX509: vi.fn().mockResolvedValue({}), + }; + + const { result } = renderHook(() => + useCertificateCreateDialog({ + providers, + onSuccess: onSuccessMock, + fortifyClient: mockFortifyClient as FortifyAPI, + currentProviderId: "1", + }) + ); + + act(() => { + result.current.open(typeMock); }); + + await act(async () => { + const DialogComponent = result.current.dialog(); + + DialogComponent && + (await DialogComponent.props.onCreateButtonClick({ + ...defaultCreateProps, + type: typeMock, + })); + }); + + expect(onSuccessMock).not.toHaveBeenCalled(); + expect(addToastMock).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Can't create certificate", + }) + ); + addToastMock.mockClear(); + }); + + it("Shouldn't call if fortifyClient not provided", async () => { + const onSuccessMock = vi.fn(); + + const { result } = renderHook(() => + useCertificateCreateDialog({ + providers, + onSuccess: onSuccessMock, + fortifyClient: null, + }) + ); + + act(() => { + result.current.open("x509"); + }); + + const DialogComponent = result.current.dialog(); + await act(async () => { + DialogComponent && + (await DialogComponent.props.onCreateButtonClick(defaultCreateProps)); + }); + + expect(onSuccessMock).not.toHaveBeenCalled(); + expect(addToastMock).not.toHaveBeenCalled(); }); });