Realm settings(login tab): Add login tests (#2260)

This commit is contained in:
Jenny 2022-03-28 11:34:07 -04:00 committed by GitHub
parent de32d5d163
commit 3efffab55d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 40 deletions

View file

@ -63,14 +63,16 @@ describe("Realm settings tabs tests", () => {
sidebarPage.goToRealmSettings(); sidebarPage.goToRealmSettings();
cy.findByTestId("rs-login-tab").click(); cy.findByTestId("rs-login-tab").click();
realmSettingsPage.toggleSwitch(realmSettingsPage.userRegSwitch); realmSettingsPage.toggleSwitch(realmSettingsPage.userRegSwitch);
realmSettingsPage.toggleSwitch(realmSettingsPage.forgotPwdSwitch); realmSettingsPage.toggleSwitch(realmSettingsPage.forgotPwdSwitch);
realmSettingsPage.toggleSwitch(realmSettingsPage.rememberMeSwitch); realmSettingsPage.toggleSwitch(realmSettingsPage.rememberMeSwitch);
});
it("Check login tab values", () => { realmSettingsPage.toggleSwitch(realmSettingsPage.loginWithEmailSwitch);
sidebarPage.goToRealmSettings();
cy.findByTestId("rs-login-tab").click();
realmSettingsPage.toggleSwitch(realmSettingsPage.duplicateEmailsSwitch);
// Check values
cy.findByTestId(realmSettingsPage.userRegSwitch).should("have.value", "on"); cy.findByTestId(realmSettingsPage.userRegSwitch).should("have.value", "on");
cy.findByTestId(realmSettingsPage.forgotPwdSwitch).should( cy.findByTestId(realmSettingsPage.forgotPwdSwitch).should(
"have.value", "have.value",
@ -80,6 +82,24 @@ describe("Realm settings tabs tests", () => {
"have.value", "have.value",
"on" "on"
); );
cy.findByTestId(realmSettingsPage.emailAsUsernameSwitch).should(
"have.value",
"off"
);
cy.findByTestId(realmSettingsPage.loginWithEmailSwitch).should(
"have.value",
"off"
);
cy.findByTestId(realmSettingsPage.duplicateEmailsSwitch).should(
"have.value",
"on"
);
cy.findByTestId(realmSettingsPage.verifyEmailSwitch).should(
"have.value",
"off"
);
}); });
it("Go to email tab", () => { it("Go to email tab", () => {

View file

@ -150,7 +150,7 @@ export const RealmSettingsGeneralTab = ({
fieldLabelId="realm-settings:userManagedAccess" fieldLabelId="realm-settings:userManagedAccess"
/> />
} }
fieldId="kc-user-manged-access" fieldId="kc-user-managed-access"
> >
<Controller <Controller
name="userManagedAccessAllowed" name="userManagedAccessAllowed"

View file

@ -1,35 +1,55 @@
import React from "react"; import React from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { FormGroup, PageSection, Switch } from "@patternfly/react-core"; import {
AlertVariant,
FormGroup,
PageSection,
Switch,
} from "@patternfly/react-core";
import { FormAccess } from "../components/form-access/FormAccess"; import { FormAccess } from "../components/form-access/FormAccess";
import { HelpItem } from "../components/help-enabler/HelpItem"; import { HelpItem } from "../components/help-enabler/HelpItem";
import { FormPanel } from "../components/scroll-form/FormPanel"; import { FormPanel } from "../components/scroll-form/FormPanel";
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import { Controller, useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form";
import { useAdminClient } from "../context/auth/AdminClient";
import { useAlerts } from "../components/alert/Alerts";
import { useRealm } from "../context/realm-context/RealmContext";
type RealmSettingsLoginTabProps = { type RealmSettingsLoginTabProps = {
save: (realm: RealmRepresentation) => void;
realm: RealmRepresentation; realm: RealmRepresentation;
refresh: () => void; refresh: () => void;
}; };
export const RealmSettingsLoginTab = ({ export const RealmSettingsLoginTab = ({
save,
realm, realm,
refresh, refresh,
}: RealmSettingsLoginTabProps) => { }: RealmSettingsLoginTabProps) => {
const { t } = useTranslation("realm-settings"); const { t } = useTranslation("realm-settings");
const form = useForm<RealmRepresentation>({ mode: "onChange" }); const form = useForm<RealmRepresentation>({ mode: "onChange" });
const { addAlert, addError } = useAlerts();
const adminClient = useAdminClient();
const { realm: realmName } = useRealm();
const updateSwitchValue = ( const updateSwitchValue = async (
onChange: (newValue: boolean) => void, onChange: (newValue: boolean) => void,
value: boolean, value: boolean
name: string
) => { ) => {
save({ ...realm, [name as keyof typeof realm]: value });
onChange(value); onChange(value);
refresh(); const switchValues = form.getValues();
try {
await adminClient.realms.update(
{
realm: realmName,
},
switchValues
);
addAlert(t("deleteClientPolicySuccess"), AlertVariant.success);
refresh();
} catch (error) {
addError(t("deleteClientPolicyError"), error);
}
}; };
return ( return (
@ -58,12 +78,12 @@ export const RealmSettingsLoginTab = ({
<Switch <Switch
id="kc-user-reg-switch" id="kc-user-reg-switch"
data-testid="user-reg-switch" data-testid="user-reg-switch"
name="registrationAllowed" value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "registrationAllowed"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}
@ -89,11 +109,12 @@ export const RealmSettingsLoginTab = ({
id="kc-forgot-pw-switch" id="kc-forgot-pw-switch"
data-testid="forgot-pw-switch" data-testid="forgot-pw-switch"
name="resetPasswordAllowed" name="resetPasswordAllowed"
value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "resetPasswordAllowed"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}
@ -118,12 +139,12 @@ export const RealmSettingsLoginTab = ({
<Switch <Switch
id="kc-remember-me-switch" id="kc-remember-me-switch"
data-testid="remember-me-switch" data-testid="remember-me-switch"
name="rememberMe" value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "rememberMe"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}
@ -152,16 +173,12 @@ export const RealmSettingsLoginTab = ({
<Switch <Switch
id="kc-email-as-username-switch" id="kc-email-as-username-switch"
data-testid="email-as-username-switch" data-testid="email-as-username-switch"
name="registrationEmailAsUsername" value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue( updateSwitchValue(onChange, value);
onChange,
value,
"registrationEmailAsUsername"
);
}} }}
/> />
)} )}
@ -186,12 +203,12 @@ export const RealmSettingsLoginTab = ({
<Switch <Switch
id="kc-login-with-email-switch" id="kc-login-with-email-switch"
data-testid="login-with-email-switch" data-testid="login-with-email-switch"
name="loginWithEmailAllowed" value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "loginWithEmailAllowed"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}
@ -218,18 +235,13 @@ export const RealmSettingsLoginTab = ({
data-testid="duplicate-emails-switch" data-testid="duplicate-emails-switch"
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
name="duplicateEmailsAllowed"
isChecked={ isChecked={
form.getValues().duplicateEmailsAllowed && form.getValues().duplicateEmailsAllowed &&
!form.getValues().loginWithEmailAllowed && !form.getValues().loginWithEmailAllowed &&
!form.getValues().registrationEmailAsUsername !form.getValues().registrationEmailAsUsername
} }
onChange={(value) => { onChange={(value) => {
updateSwitchValue( updateSwitchValue(onChange, value);
onChange,
value,
"duplicateEmailsAllowed"
);
}} }}
isDisabled={ isDisabled={
form.getValues().loginWithEmailAllowed || form.getValues().loginWithEmailAllowed ||
@ -259,11 +271,12 @@ export const RealmSettingsLoginTab = ({
id="kc-verify-email-switch" id="kc-verify-email-switch"
data-testid="verify-email-switch" data-testid="verify-email-switch"
name="verifyEmail" name="verifyEmail"
value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "verifyEmail"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}
@ -295,12 +308,12 @@ export const RealmSettingsLoginTab = ({
<Switch <Switch
id="kc-edit-username-switch" id="kc-edit-username-switch"
data-testid="edit-username-switch" data-testid="edit-username-switch"
name="editUsernameAllowed" value={value ? "on" : "off"}
label={t("common:on")} label={t("common:on")}
labelOff={t("common:off")} labelOff={t("common:off")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
updateSwitchValue(onChange, value, "editUsernameAllowed"); updateSwitchValue(onChange, value);
}} }}
/> />
)} )}

View file

@ -278,11 +278,7 @@ export const RealmSettingsTabs = ({
data-testid="rs-login-tab" data-testid="rs-login-tab"
{...route("login")} {...route("login")}
> >
<RealmSettingsLoginTab <RealmSettingsLoginTab refresh={refresh} realm={realm} />
refresh={refresh}
save={save}
realm={realm}
/>
</Tab> </Tab>
<Tab <Tab
title={<TabTitleText>{t("email")}</TabTitleText>} title={<TabTitleText>{t("email")}</TabTitleText>}