diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/AddCertAuthorityModal/AddCertAuthorityModal.tests.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/AddCertAuthorityModal/AddCertAuthorityModal.tests.tsx new file mode 100644 index 0000000000..dd59483d9b --- /dev/null +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/AddCertAuthorityModal/AddCertAuthorityModal.tests.tsx @@ -0,0 +1,70 @@ +import React from "react"; +import { noop } from "lodash"; +import { render, screen } from "@testing-library/react"; + +import { createCustomRenderer, renderWithSetup } from "test/test-utils"; +import createMockConfig from "__mocks__/configMock"; + +import AddCertAuthorityModal from "./AddCertAuthorityModal"; + +describe("AddCertAuthorityModal", () => { + it("renders the Digicert form by default", () => { + render(); + + expect(screen.getByLabelText("Name")).toBeVisible(); + expect(screen.getByLabelText("URL")).toBeVisible(); + expect(screen.getByLabelText("API token")).toBeVisible(); + expect(screen.getByLabelText("Profile GUID")).toBeVisible(); + expect(screen.getByLabelText("Certificate common name (CN)")).toBeVisible(); + expect(screen.getByLabelText("User principal name (UPN)")).toBeVisible(); + expect(screen.getByLabelText("Certificate seat ID")).toBeVisible(); + }); + + it("shows the correct form when the corresponding value in the dropdown is selected.", async () => { + const { user } = renderWithSetup(); + + // this is selecting the custom scep option from the dropdown + await user.click(screen.getByRole("combobox")); + await user.click( + screen.getByRole("option", { + name: "Custom (SCEP: Simple Certificate Enrollment Protocol)", + }) + ); + + expect(screen.getByLabelText("Name")).toBeVisible(); + expect(screen.getByLabelText("SCEP URL")).toBeVisible(); + expect(screen.getByLabelText("Challenge")).toBeVisible(); + }); + + it("does not allow NDES option to be selected if there is already an NDES CA added", async () => { + const customRender = createCustomRenderer({ + context: { + app: { + config: createMockConfig({ + integrations: { + zendesk: [], + jira: [], + ndes_scep_proxy: { + url: "https://ndes.example.com", + admin_url: "https://ndes.example.com/admin", + username: "ndes_user", + password: "ndes_password", + }, + }, + }), + }, + }, + }); + + const { user } = customRender(); + + // testing library does not see options when it is disabled + // so we can just check that its not queryable to confirm that it is disabled + await user.click(screen.getByRole("combobox")); + expect( + screen.queryByRole("option", { + name: "Microsoft NDES (Network Device Enrollment Service)", + }) + ).toBeNull(); + }); +}); diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tests.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tests.tsx new file mode 100644 index 0000000000..78d24f3d96 --- /dev/null +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tests.tsx @@ -0,0 +1,65 @@ +import React from "react"; +import { noop } from "lodash"; +import { render, screen } from "@testing-library/react"; +import { renderWithSetup } from "test/test-utils"; + +import CustomSCEPForm, { ICustomSCEPFormData } from "./CustomSCEPForm"; + +const createTestFormData = (overrides?: Partial) => ({ + name: "TEST_NAME", + scepURL: "https://test.com", + challenge: "test-challenge", + ...overrides, +}); + +describe("CustomSCEPForm", () => { + it("render the custom button text", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeVisible(); + }); + + it("enables and disabled form submittion depending on the form validation", async () => { + const { user } = renderWithSetup( + + ); + + // data is valid, submit should be enabled + expect(screen.getByRole("button", { name: "Submit" })).toBeEnabled(); + + // name input is invalidated, submit should be disabled + await user.clear(screen.getByLabelText("Name")); + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); + + it("disables submit when isSubmitting is set to true", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); +}); diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tsx index 9bfeabd13e..e8956ff152 100644 --- a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tsx +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/CustomSCEPForm/CustomSCEPForm.tsx @@ -52,9 +52,9 @@ const CustomSCEPForm = ({ const [ formValidation, setFormValidation, - ] = useState({ - isValid: false, - }); + ] = useState(() => + validateFormData(formData, validations) + ); const { name, scepURL, challenge } = formData; diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tests.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tests.tsx new file mode 100644 index 0000000000..6e3c0d8450 --- /dev/null +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tests.tsx @@ -0,0 +1,69 @@ +import React from "react"; +import { noop } from "lodash"; +import { render, screen } from "@testing-library/react"; +import { renderWithSetup } from "test/test-utils"; + +import DigicertForm, { IDigicertFormData } from "./DigicertForm"; + +const createTestFormData = (overrides?: Partial) => ({ + name: "TEST_NAME", + url: "https://test.com", + apiToken: "test-api-123", + profileId: "test-id-123", + commonName: "test-common", + userPrincipalName: "test-principal", + certificateSeatId: "test-seat-123", + ...overrides, +}); + +describe("DigicertForm", () => { + it("render the custom button text", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeVisible(); + }); + + it("enables and disables form submission depending on the form validation", async () => { + const { user } = renderWithSetup( + + ); + + // data is valid, submit should be enabled + expect(screen.getByRole("button", { name: "Submit" })).toBeEnabled(); + + // name input is invalidated, submit should be disabled + await user.clear(screen.getByLabelText("Name")); + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); + + it("disables submit when isSubmitting is set to true", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); +}); diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tsx index 48611ec4e3..ac38fc449d 100644 --- a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tsx +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/DigicertForm/DigicertForm.tsx @@ -52,9 +52,7 @@ const DigicertForm = ({ ); const [formValidation, setFormValidation] = useState( - { - isValid: false, - } + () => validateFormData(formData, validations) ); const { diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tests.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tests.tsx new file mode 100644 index 0000000000..58b3085261 --- /dev/null +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tests.tsx @@ -0,0 +1,66 @@ +import React from "react"; +import { noop } from "lodash"; +import { render, screen } from "@testing-library/react"; +import { renderWithSetup } from "test/test-utils"; + +import NDESForm, { INDESFormData } from "./NDESForm"; + +const createTestFormData = (overrides?: Partial) => ({ + scepURL: "https://test.com", + adminURL: "https://test.com", + username: "test user", + password: "password123", + ...overrides, +}); + +describe("NDESForm", () => { + it("render the custom button text", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeVisible(); + }); + + it("enables and disables form submission depending on the form validation", async () => { + const { user } = renderWithSetup( + + ); + + // data is valid, submit should be enabled + expect(screen.getByRole("button", { name: "Submit" })).toBeEnabled(); + + // name input is invalidated, submit should be disabled + await user.clear(screen.getByLabelText("SCEP URL")); + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); + + it("disables submit when isSubmitting is set to true", () => { + render( + + ); + + expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled(); + }); +}); diff --git a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tsx b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tsx index 311d3daf6a..7312f2334f 100644 --- a/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tsx +++ b/frontend/pages/admin/IntegrationsPage/cards/CertificateAuthorities/components/NDESForm/NDESForm.tsx @@ -35,9 +35,9 @@ const NDESForm = ({ onSubmit, onCancel, }: INDESFormProps) => { - const [formValidation, setFormValidation] = useState({ - isValid: false, - }); + const [formValidation, setFormValidation] = useState( + () => validateFormData(formData) + ); const { scepURL, adminURL, username, password } = formData; diff --git a/frontend/test/test-utils.tsx b/frontend/test/test-utils.tsx index d5bfb9fa3f..94c929e92a 100644 --- a/frontend/test/test-utils.tsx +++ b/frontend/test/test-utils.tsx @@ -154,7 +154,6 @@ export const createCustomRenderer = (renderOptions?: ICustomRenderOptions) => { * This is a convenince method that calls the render method from `@testing-library/react` and also * sets up the also `user-events`library and adds the user object to the returned object. */ -// eslint-disable-next-line import/prefer-default-export export const renderWithSetup = (component: JSX.Element) => { return { user: userEvent.setup(),