Skip to content

Commit

Permalink
Add more tests for useCertificateCreateDialog hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Oct 29, 2024
1 parent e8ef678 commit 2c8db72
Showing 1 changed file with 138 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@ 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,
}),
};
});

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",
Expand All @@ -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,
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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"],
},
});
});

Expand Down Expand Up @@ -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<FortifyAPI> = {
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<FortifyAPI> = {
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();
});
});

0 comments on commit 2c8db72

Please sign in to comment.