Added validation on realm name (#4259)

This commit is contained in:
Erik Jan de Wit 2023-01-27 17:10:09 +01:00 committed by GitHub
parent 60d10d88bd
commit 31b203665a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View file

@ -15,11 +15,12 @@ const realmSettings = new RealmSettings();
const modalUtils = new ModalUtils(); const modalUtils = new ModalUtils();
const testRealmName = const testRealmName =
"Test realm " + (Math.random() + 1).toString(36).substring(7); "Test-realm-" + (Math.random() + 1).toString(36).substring(7);
const newRealmName = const newRealmName =
"New Test realm " + (Math.random() + 1).toString(36).substring(7); "New-Test-realm-" + (Math.random() + 1).toString(36).substring(7);
const editedRealmName = 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", () => { describe("Realm tests", () => {
before(() => { before(() => {
@ -67,21 +68,22 @@ describe("Realm tests", () => {
it("should create Test Disabled realm", () => { it("should create Test Disabled realm", () => {
sidebarPage.goToCreateRealm(); sidebarPage.goToCreateRealm();
sidebarPage.waitForPageLoad(); sidebarPage.waitForPageLoad();
createRealmPage.fillRealmName("Test Disabled").createRealm();
createRealmPage.fillRealmName(testDisabledName).createRealm();
createRealmPage.disableRealm(); createRealmPage.disableRealm();
masthead.checkNotificationMessage("Realm created successfully"); masthead.checkNotificationMessage("Realm created successfully");
}); });
it("Should cancel deleting Test Disabled realm", () => { it("Should cancel deleting Test Disabled realm", () => {
sidebarPage.goToRealm("Test Disabled").goToRealmSettings(); sidebarPage.goToRealm(testDisabledName).goToRealmSettings();
realmSettings.clickActionMenu(); realmSettings.clickActionMenu();
cy.findByText("Delete").click(); cy.findByText("Delete").click();
modalUtils.cancelModal(); modalUtils.cancelModal();
}); });
it("Should delete Test Disabled realm", () => { it("Should delete Test Disabled realm", () => {
sidebarPage.goToRealm("Test Disabled").goToRealmSettings(); sidebarPage.goToRealm(testDisabledName).goToRealmSettings();
realmSettings.clickActionMenu(); realmSettings.clickActionMenu();
cy.findByText("Delete").click(); cy.findByText("Delete").click();
modalUtils.confirmModal(); modalUtils.confirmModal();

View file

@ -1,6 +1,7 @@
{ {
"uploadFile": "Upload JSON file", "uploadFile": "Upload JSON file",
"realmName": "Realm name", "realmName": "Realm name",
"invalidRealmName": "Realm name can't contain special characters",
"enabled": "Enabled", "enabled": "Enabled",
"createRealm": "Create realm", "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.", "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.",

View file

@ -48,7 +48,7 @@ export const RealmSettingsGeneralTab = ({
control, control,
handleSubmit, handleSubmit,
setValue, setValue,
formState: { isDirty }, formState: { isDirty, errors },
} = form; } = form;
const isFeatureEnabled = useIsFeatureEnabled(); const isFeatureEnabled = useIsFeatureEnabled();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
@ -79,10 +79,23 @@ export const RealmSettingsGeneralTab = ({
className="pf-u-mt-lg" className="pf-u-mt-lg"
onSubmit={handleSubmit(save)} onSubmit={handleSubmit(save)}
> >
<FormGroup label={t("realmId")} fieldId="kc-realm-id" isRequired> <FormGroup
label={t("realmId")}
fieldId="kc-realm-id"
isRequired
validated={errors.realm ? "error" : "default"}
helperTextInvalid={errors.realm?.message}
>
<Controller <Controller
name="realm" name="realm"
control={control} control={control}
rules={{
required: { value: true, message: t("common:required") },
pattern: {
value: /^[a-zA-Z0-9-_]+$/,
message: t("realm:invalidRealmName"),
},
}}
defaultValue="" defaultValue=""
render={({ field }) => ( render={({ field }) => (
<ClipboardCopy data-testid="realmName" onChange={field.onChange}> <ClipboardCopy data-testid="realmName" onChange={field.onChange}>

View file

@ -81,13 +81,19 @@ export default function NewRealmForm() {
isRequired isRequired
fieldId="kc-realm-name" fieldId="kc-realm-name"
validated={errors.realm ? "error" : "default"} validated={errors.realm ? "error" : "default"}
helperTextInvalid={t("common:required")} helperTextInvalid={errors.realm?.message}
> >
<KeycloakTextInput <KeycloakTextInput
isRequired isRequired
id="kc-realm-name" id="kc-realm-name"
validated={errors.realm ? "error" : "default"} validated={errors.realm ? "error" : "default"}
{...register("realm", { required: true })} {...register("realm", {
required: { value: true, message: t("common:required") },
pattern: {
value: /^[a-zA-Z0-9-_]+$/,
message: t("invalidRealmName"),
},
})}
/> />
</FormGroup> </FormGroup>
<FormGroup label={t("enabled")} fieldId="kc-realm-enabled-switch"> <FormGroup label={t("enabled")} fieldId="kc-realm-enabled-switch">