From 31b203665a3fbea5346ed3c9ca6374f7d6dfe457 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Fri, 27 Jan 2023 17:10:09 +0100 Subject: [PATCH] Added validation on realm name (#4259) --- apps/admin-ui/cypress/e2e/realm_test.spec.ts | 14 ++++++++------ apps/admin-ui/public/resources/en/realm.json | 1 + apps/admin-ui/src/realm-settings/GeneralTab.tsx | 17 +++++++++++++++-- apps/admin-ui/src/realm/add/NewRealmForm.tsx | 10 ++++++++-- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/apps/admin-ui/cypress/e2e/realm_test.spec.ts b/apps/admin-ui/cypress/e2e/realm_test.spec.ts index e76694e5cb..0017931b3b 100644 --- a/apps/admin-ui/cypress/e2e/realm_test.spec.ts +++ b/apps/admin-ui/cypress/e2e/realm_test.spec.ts @@ -15,11 +15,12 @@ const realmSettings = new RealmSettings(); const modalUtils = new ModalUtils(); const testRealmName = - "Test realm " + (Math.random() + 1).toString(36).substring(7); + "Test-realm-" + (Math.random() + 1).toString(36).substring(7); const newRealmName = - "New Test realm " + (Math.random() + 1).toString(36).substring(7); + "New-Test-realm-" + (Math.random() + 1).toString(36).substring(7); const editedRealmName = - "Edited Test realm " + (Math.random() + 1).toString(36).substring(7); + "Edited-Test-realm-" + (Math.random() + 1).toString(36).substring(7); +const testDisabledName = "Test-Disabled"; describe("Realm tests", () => { before(() => { @@ -67,21 +68,22 @@ describe("Realm tests", () => { it("should create Test Disabled realm", () => { sidebarPage.goToCreateRealm(); sidebarPage.waitForPageLoad(); - createRealmPage.fillRealmName("Test Disabled").createRealm(); + + createRealmPage.fillRealmName(testDisabledName).createRealm(); createRealmPage.disableRealm(); masthead.checkNotificationMessage("Realm created successfully"); }); it("Should cancel deleting Test Disabled realm", () => { - sidebarPage.goToRealm("Test Disabled").goToRealmSettings(); + sidebarPage.goToRealm(testDisabledName).goToRealmSettings(); realmSettings.clickActionMenu(); cy.findByText("Delete").click(); modalUtils.cancelModal(); }); it("Should delete Test Disabled realm", () => { - sidebarPage.goToRealm("Test Disabled").goToRealmSettings(); + sidebarPage.goToRealm(testDisabledName).goToRealmSettings(); realmSettings.clickActionMenu(); cy.findByText("Delete").click(); modalUtils.confirmModal(); diff --git a/apps/admin-ui/public/resources/en/realm.json b/apps/admin-ui/public/resources/en/realm.json index bab5038702..cd03883773 100644 --- a/apps/admin-ui/public/resources/en/realm.json +++ b/apps/admin-ui/public/resources/en/realm.json @@ -1,6 +1,7 @@ { "uploadFile": "Upload JSON file", "realmName": "Realm name", + "invalidRealmName": "Realm name can't contain special characters", "enabled": "Enabled", "createRealm": "Create realm", "realmExplain": "A realm manages a set of users, credentials, roles, and groups. A user belongs to and logs into a realm. Realms are isolated from one another and can only manage and authenticate the users that they control.", diff --git a/apps/admin-ui/src/realm-settings/GeneralTab.tsx b/apps/admin-ui/src/realm-settings/GeneralTab.tsx index cd2e19be0f..f80a0f88e7 100644 --- a/apps/admin-ui/src/realm-settings/GeneralTab.tsx +++ b/apps/admin-ui/src/realm-settings/GeneralTab.tsx @@ -48,7 +48,7 @@ export const RealmSettingsGeneralTab = ({ control, handleSubmit, setValue, - formState: { isDirty }, + formState: { isDirty, errors }, } = form; const isFeatureEnabled = useIsFeatureEnabled(); const [open, setOpen] = useState(false); @@ -79,10 +79,23 @@ export const RealmSettingsGeneralTab = ({ className="pf-u-mt-lg" onSubmit={handleSubmit(save)} > - + ( diff --git a/apps/admin-ui/src/realm/add/NewRealmForm.tsx b/apps/admin-ui/src/realm/add/NewRealmForm.tsx index 3aa144fd56..857c10b4b5 100644 --- a/apps/admin-ui/src/realm/add/NewRealmForm.tsx +++ b/apps/admin-ui/src/realm/add/NewRealmForm.tsx @@ -81,13 +81,19 @@ export default function NewRealmForm() { isRequired fieldId="kc-realm-name" validated={errors.realm ? "error" : "default"} - helperTextInvalid={t("common:required")} + helperTextInvalid={errors.realm?.message} >