Added check to see if unique (#3642)

This commit is contained in:
Erik Jan de Wit 2022-11-01 15:41:32 +01:00 committed by GitHub
parent 7f8cb11988
commit df7347aa9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 19 deletions

View file

@ -147,13 +147,11 @@ describe("Realm settings client policies tab tests", () => {
realmSettingsPage.createNewClientPolicyFromList( realmSettingsPage.createNewClientPolicyFromList(
"Test", "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(); sidebarPage.goToRealmSettings();
realmSettingsPage realmSettingsPage
@ -174,10 +172,9 @@ describe("Realm settings client policies tab tests", () => {
"Test again", "Test again",
"Test Again Description" "Test Again Description"
); );
masthead.checkNotificationMessage("New policy created", true); masthead.checkNotificationMessage("New policy created");
sidebarPage.waitForPageLoad(); sidebarPage.waitForPageLoad();
cy.wait("@save"); cy.wait("@save");
masthead.closeAllAlertMessages();
realmSettingsPage.deleteClientPolicyFromDetails(); realmSettingsPage.deleteClientPolicyFromDetails();
modalUtils.confirmModal(); modalUtils.confirmModal();
masthead.checkNotificationMessage("Client policy deleted"); masthead.checkNotificationMessage("Client policy deleted");

View file

@ -801,6 +801,13 @@ export default class RealmSettingsPage extends CommonPage {
cy.get("form").should("not.have.text", "Required field"); 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() { shouldReloadClientProfileEdits() {
cy.get(this.clientProfileTwo).click(); cy.get(this.clientProfileTwo).click();
cy.findByTestId(this.newClientProfileNameInput).type("Reloading"); cy.findByTestId(this.newClientProfileNameInput).type("Reloading");

View file

@ -67,12 +67,6 @@ type PolicyDetailAttributes = {
export default function NewClientPolicyForm() { export default function NewClientPolicyForm() {
const { t } = useTranslation("realm-settings"); const { t } = useTranslation("realm-settings");
const {
reset: resetForm,
formState: { errors },
} = useForm<NewClientPolicyForm>({
defaultValues,
});
const { realm } = useRealm(); const { realm } = useRealm();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
const { adminClient } = useAdminClient(); const { adminClient } = useAdminClient();
@ -99,7 +93,10 @@ export default function NewClientPolicyForm() {
const { policyName } = useParams<EditClientPolicyParams>(); const { policyName } = useParams<EditClientPolicyParams>();
const navigate = useNavigate(); const navigate = useNavigate();
const form = useForm<ClientPolicyRepresentation>({ mode: "onChange" }); const form = useForm<NewClientPolicyForm>({
mode: "onChange",
defaultValues,
});
const { handleSubmit } = form; const { handleSubmit } = form;
const formValues = form.getValues(); const formValues = form.getValues();
@ -206,7 +203,7 @@ export default function NewClientPolicyForm() {
); );
const setupForm = (policy: ClientPolicyRepresentation) => { const setupForm = (policy: ClientPolicyRepresentation) => {
resetForm(); form.reset();
Object.entries(policy).map(([key, value]) => { Object.entries(policy).map(([key, value]) => {
form.setValue(key, value); form.setValue(key, value);
}); });
@ -465,17 +462,30 @@ export default function NewClientPolicyForm() {
label={t("common:name")} label={t("common:name")}
fieldId="kc-client-profile-name" fieldId="kc-client-profile-name"
isRequired isRequired
helperTextInvalid={t("common:required")} helperTextInvalid={form.errors.name?.message}
validated={ validated={
errors.name ? ValidatedOptions.error : ValidatedOptions.default form.errors.name
? ValidatedOptions.error
: ValidatedOptions.default
} }
> >
<KeycloakTextInput <KeycloakTextInput
ref={form.register({ required: true })} ref={form.register({
required: { value: true, message: t("common:required") },
validate: (value) =>
policies?.some((policy) => policy.name === value)
? t("createClientProfileNameHelperText").toString()
: true,
})}
type="text" type="text"
id="kc-client-profile-name" id="kc-client-profile-name"
name="name" name="name"
data-testid="client-policy-name" data-testid="client-policy-name"
validated={
form.errors.name
? ValidatedOptions.error
: ValidatedOptions.default
}
/> />
</FormGroup> </FormGroup>
<FormGroup label={t("common:description")} fieldId="kc-description"> <FormGroup label={t("common:description")} fieldId="kc-description">
@ -493,7 +503,7 @@ export default function NewClientPolicyForm() {
variant="primary" variant="primary"
type="submit" type="submit"
data-testid="saveCreatePolicy" data-testid="saveCreatePolicy"
isDisabled={!formValues.name} isDisabled={!form.formState.isValid}
> >
{t("common:save")} {t("common:save")}
</Button> </Button>