From 001d4ed6d4d44be60aa82b8250009f703a065236 Mon Sep 17 00:00:00 2001 From: Jenny <32821331+jenny-s51@users.noreply.github.com> Date: Tue, 12 Oct 2021 11:03:56 -0400 Subject: [PATCH] Create client policy form (#1309) * add create client policy form; WIP add client policy tests checkout realm settings test from master RealmSettingsPage.ts master remove comment and add missing translation fix tests PR feedback from Jon and Erik * rebase * update test file * replace hardcoded URL with path * fix tests * correct tab name * fix duplicates test --- .../integration/realm_settings_test.spec.ts | 33 +-- src/realm-settings/NewClientPolicyForm.tsx | 280 ++++++++++++++++++ src/realm-settings/PoliciesTab.tsx | 83 +++++- src/realm-settings/RealmSettingsSection.css | 8 + src/realm-settings/RealmSettingsTabs.tsx | 2 +- src/realm-settings/help.ts | 3 + src/realm-settings/messages.ts | 17 ++ src/realm-settings/routes.ts | 2 + src/realm-settings/routes/NewClientPolicy.ts | 19 ++ 9 files changed, 416 insertions(+), 31 deletions(-) create mode 100644 src/realm-settings/NewClientPolicyForm.tsx create mode 100644 src/realm-settings/routes/NewClientPolicy.ts diff --git a/cypress/integration/realm_settings_test.spec.ts b/cypress/integration/realm_settings_test.spec.ts index ea1c824604..f62f53bfb8 100644 --- a/cypress/integration/realm_settings_test.spec.ts +++ b/cypress/integration/realm_settings_test.spec.ts @@ -259,20 +259,20 @@ describe("Realm settings tests", () => { }); /*it("delete providers", () => { - sidebarPage.goToRealmSettings(); - const url = `/auth/admin/realms/${realmName}/keys`; - cy.intercept(url).as("load"); + sidebarPage.goToRealmSettings(); + const url = `/auth/admin/realms/${realmName}/keys`; + cy.intercept(url).as("load"); - cy.findByTestId("rs-keys-tab").click(); - cy.findByTestId("rs-providers-tab").click(); + cy.findByTestId("rs-keys-tab").click(); + cy.findByTestId("rs-providers-tab").click(); - cy.wait("@load"); + cy.wait("@load"); - deleteProvider("test_aes-generated"); - deleteProvider("test_ecdsa-generated"); - deleteProvider("test_hmac-generated"); - deleteProvider("test_rsa-generated"); - });*/ + deleteProvider("test_aes-generated"); + deleteProvider("test_ecdsa-generated"); + deleteProvider("test_hmac-generated"); + deleteProvider("test_rsa-generated"); + });*/ it("Test keys", () => { sidebarPage.goToRealmSettings(); goToKeys(); @@ -443,7 +443,7 @@ describe("Realm settings tests", () => { loginPage.logIn(); sidebarPage.goToRealmSettings(); cy.findByTestId("rs-clientPolicies-tab").click(); - cy.findByTestId("rs-profiles-clientPolicies-tab").click(); + cy.findByTestId("rs-policies-clientProfiles-tab").click(); }); it("Go to client policies profiles tab", () => { @@ -484,18 +484,11 @@ describe("Realm settings tests", () => { }); it("Should not create duplicate client profile", () => { - realmSettingsPage.shouldCompleteAndCreateNewClientProfile(); - sidebarPage.goToRealmSettings(); cy.findByTestId("rs-clientPolicies-tab").click(); - cy.findByTestId("rs-profiles-clientPolicies-tab").click(); + cy.findByTestId("rs-policies-clientProfiles-tab").click(); realmSettingsPage.shouldCompleteAndCreateNewClientProfile(); realmSettingsPage.shouldNotCreateDuplicateClientProfile(); - - sidebarPage.goToRealmSettings(); - cy.findByTestId("rs-clientPolicies-tab").click(); - cy.findByTestId("rs-profiles-clientPolicies-tab").click(); - realmSettingsPage.shouldDeleteClientProfileDialog(); }); it("Check deleting newly created client profile from create view via dropdown", () => { diff --git a/src/realm-settings/NewClientPolicyForm.tsx b/src/realm-settings/NewClientPolicyForm.tsx new file mode 100644 index 0000000000..6d1d3f250a --- /dev/null +++ b/src/realm-settings/NewClientPolicyForm.tsx @@ -0,0 +1,280 @@ +import React, { useState } from "react"; +import { + ActionGroup, + AlertVariant, + Button, + ButtonVariant, + Divider, + DropdownItem, + Flex, + FlexItem, + FormGroup, + PageSection, + Text, + TextArea, + TextInput, + TextVariants, + ValidatedOptions, +} from "@patternfly/react-core"; +import { useTranslation } from "react-i18next"; +import { useForm } from "react-hook-form"; +import { FormAccess } from "../components/form-access/FormAccess"; +import { ViewHeader } from "../components/view-header/ViewHeader"; +import { Link, useHistory } from "react-router-dom"; +import { useRealm } from "../context/realm-context/RealmContext"; +import { useAlerts } from "../components/alert/Alerts"; +import { useAdminClient, useFetch } from "../context/auth/AdminClient"; +import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation"; +import { HelpItem } from "../components/help-enabler/HelpItem"; +import { PlusCircleIcon } from "@patternfly/react-icons"; +import "./RealmSettingsSection.css"; +import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; +import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation"; +import { toClientPolicies } from "./routes/ClientPolicies"; + +type NewClientPolicyForm = Required; + +const defaultValues: NewClientPolicyForm = { + name: "", + description: "", + conditions: [], + enabled: true, + profiles: [], +}; + +export const NewClientPolicyForm = () => { + const { t } = useTranslation("realm-settings"); + const { + register, + errors, + handleSubmit, + reset: resetForm, + } = useForm({ + defaultValues, + }); + const { realm } = useRealm(); + const { addAlert, addError } = useAlerts(); + const adminClient = useAdminClient(); + const [policies, setPolicies] = useState([]); + const [ + showAddConditionsAndProfilesForm, + setShowAddConditionsAndProfilesForm, + ] = useState(false); + + const [createdPolicy, setCreatedPolicy] = + useState(); + + const history = useHistory(); + + useFetch( + () => adminClient.clientPolicies.listPolicies(), + (policies) => { + setPolicies(policies.policies ?? []); + }, + [] + ); + + const save = async (form: NewClientPolicyForm) => { + const createdPolicy = { + ...form, + profiles: [], + conditions: [], + }; + + const allPolicies = policies.concat(createdPolicy); + + try { + await adminClient.clientPolicies.updatePolicy({ + policies: allPolicies, + }); + addAlert( + t("realm-settings:createClientPolicySuccess"), + AlertVariant.success + ); + setShowAddConditionsAndProfilesForm(true); + setCreatedPolicy(createdPolicy); + } catch (error) { + addError("realm-settings:createClientProfileError", error); + } + }; + + const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ + titleKey: t("deleteClientProfileConfirmTitle"), + messageKey: t("deleteClientProfileConfirm"), + continueButtonLabel: t("delete"), + continueButtonVariant: ButtonVariant.danger, + onConfirm: async () => { + const updatedPolicies = policies.filter( + (policy) => policy.name !== createdPolicy?.name + ); + + try { + await adminClient.clientPolicies.updatePolicy({ + policies: updatedPolicies, + }); + addAlert(t("deleteClientSuccess"), AlertVariant.success); + history.push(toClientPolicies({ realm })); + } catch (error) { + addError(t("deleteClientError"), error); + } + }, + }); + + return ( + <> + + { + toggleDeleteDialog; + }} + data-testid="deleteClientProfileDropdown" + > + {t("deleteClientProfile")} + , + ] + : undefined + } + /> + + + + + + +