From df7347aa9b867dfd46e0355549bb2c948775f393 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 1 Nov 2022 15:41:32 +0100 Subject: [PATCH] Added check to see if unique (#3642) --- ...ealm_settings_client_policies_test.spec.ts | 11 +++--- .../realm_settings/RealmSettingsPage.ts | 7 ++++ .../realm-settings/NewClientPolicyForm.tsx | 34 ++++++++++++------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/apps/admin-ui/cypress/e2e/realm_settings_client_policies_test.spec.ts b/apps/admin-ui/cypress/e2e/realm_settings_client_policies_test.spec.ts index 7fa89d36e8..0b157f78d4 100644 --- a/apps/admin-ui/cypress/e2e/realm_settings_client_policies_test.spec.ts +++ b/apps/admin-ui/cypress/e2e/realm_settings_client_policies_test.spec.ts @@ -147,13 +147,11 @@ describe("Realm settings client policies tab tests", () => { realmSettingsPage.createNewClientPolicyFromList( "Test", - "Test Again Description" + "Test Again Description", + true ); - cy.wait("@save"); - - // TODO: UNCOMMENT WHEN THE ISSUE 2050 IS FIXED - //realmSettingsPage.checkAlertMessage("Could not create client policy: 'proposed client policy name duplicated.'"); + realmSettingsPage.shouldShowErrorWhenDuplicate(); sidebarPage.goToRealmSettings(); realmSettingsPage @@ -174,10 +172,9 @@ describe("Realm settings client policies tab tests", () => { "Test again", "Test Again Description" ); - masthead.checkNotificationMessage("New policy created", true); + masthead.checkNotificationMessage("New policy created"); sidebarPage.waitForPageLoad(); cy.wait("@save"); - masthead.closeAllAlertMessages(); realmSettingsPage.deleteClientPolicyFromDetails(); modalUtils.confirmModal(); masthead.checkNotificationMessage("Client policy deleted"); diff --git a/apps/admin-ui/cypress/support/pages/admin_console/manage/realm_settings/RealmSettingsPage.ts b/apps/admin-ui/cypress/support/pages/admin_console/manage/realm_settings/RealmSettingsPage.ts index 17bcbbda22..2d7ff265c4 100644 --- a/apps/admin-ui/cypress/support/pages/admin_console/manage/realm_settings/RealmSettingsPage.ts +++ b/apps/admin-ui/cypress/support/pages/admin_console/manage/realm_settings/RealmSettingsPage.ts @@ -801,6 +801,13 @@ export default class RealmSettingsPage extends CommonPage { cy.get("form").should("not.have.text", "Required field"); } + shouldShowErrorWhenDuplicate() { + cy.get("form").should( + "not.have.text", + "The name must be unique within the realm" + ); + } + shouldReloadClientProfileEdits() { cy.get(this.clientProfileTwo).click(); cy.findByTestId(this.newClientProfileNameInput).type("Reloading"); diff --git a/apps/admin-ui/src/realm-settings/NewClientPolicyForm.tsx b/apps/admin-ui/src/realm-settings/NewClientPolicyForm.tsx index fe6b6aa1b8..870301a158 100644 --- a/apps/admin-ui/src/realm-settings/NewClientPolicyForm.tsx +++ b/apps/admin-ui/src/realm-settings/NewClientPolicyForm.tsx @@ -67,12 +67,6 @@ type PolicyDetailAttributes = { export default function NewClientPolicyForm() { const { t } = useTranslation("realm-settings"); - const { - reset: resetForm, - formState: { errors }, - } = useForm({ - defaultValues, - }); const { realm } = useRealm(); const { addAlert, addError } = useAlerts(); const { adminClient } = useAdminClient(); @@ -99,7 +93,10 @@ export default function NewClientPolicyForm() { const { policyName } = useParams(); const navigate = useNavigate(); - const form = useForm({ mode: "onChange" }); + const form = useForm({ + mode: "onChange", + defaultValues, + }); const { handleSubmit } = form; const formValues = form.getValues(); @@ -206,7 +203,7 @@ export default function NewClientPolicyForm() { ); const setupForm = (policy: ClientPolicyRepresentation) => { - resetForm(); + form.reset(); Object.entries(policy).map(([key, value]) => { form.setValue(key, value); }); @@ -465,17 +462,30 @@ export default function NewClientPolicyForm() { label={t("common:name")} fieldId="kc-client-profile-name" isRequired - helperTextInvalid={t("common:required")} + helperTextInvalid={form.errors.name?.message} validated={ - errors.name ? ValidatedOptions.error : ValidatedOptions.default + form.errors.name + ? ValidatedOptions.error + : ValidatedOptions.default } > + policies?.some((policy) => policy.name === value) + ? t("createClientProfileNameHelperText").toString() + : true, + })} type="text" id="kc-client-profile-name" name="name" data-testid="client-policy-name" + validated={ + form.errors.name + ? ValidatedOptions.error + : ValidatedOptions.default + } /> @@ -493,7 +503,7 @@ export default function NewClientPolicyForm() { variant="primary" type="submit" data-testid="saveCreatePolicy" - isDisabled={!formValues.name} + isDisabled={!form.formState.isValid} > {t("common:save")}