From 1442f14c4517da7a14816f96a431abd618b2d722 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Wed, 13 Sep 2023 09:15:55 -0300 Subject: [PATCH] Registration page not showing username when edit username is not enabled Closes #23185 --- .../AbstractUserProfileProvider.java | 3 +- .../testsuite/forms/RegisterTest.java | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/services/src/main/java/org/keycloak/userprofile/AbstractUserProfileProvider.java b/services/src/main/java/org/keycloak/userprofile/AbstractUserProfileProvider.java index 11540446f7..3c33cecc1c 100644 --- a/services/src/main/java/org/keycloak/userprofile/AbstractUserProfileProvider.java +++ b/services/src/main/java/org/keycloak/userprofile/AbstractUserProfileProvider.java @@ -78,7 +78,8 @@ public abstract class AbstractUserProfileProvider KeycloakContext context = session.getContext(); RealmModel realm = context.getRealm(); - if (IDP_REVIEW.equals(c.getContext())) { + if (REGISTRATION_PROFILE.equals(c.getContext()) || REGISTRATION_USER_CREATION.equals(c.getContext()) + || IDP_REVIEW.equals(c.getContext())) { return !realm.isRegistrationEmailAsUsername(); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/RegisterTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/RegisterTest.java index a4fac8f5cb..a091887ed1 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/RegisterTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/RegisterTest.java @@ -34,6 +34,7 @@ import org.keycloak.events.Errors; import org.keycloak.events.EventType; import org.keycloak.models.AuthenticationExecutionModel; import org.keycloak.models.utils.DefaultAuthenticationFlows; +import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.representations.idm.EventRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; @@ -380,6 +381,62 @@ public class RegisterTest extends AbstractTestRealmKeycloakTest { assertNull(user.getAttributes()); } + @Test + public void registerUserSuccessEditUsernameDisabled() { + RealmRepresentation realm = testRealm().toRepresentation(); + Boolean editUsernameAllowed = realm.isEditUsernameAllowed(); + Boolean registrationEmailAsUsername = realm.isRegistrationEmailAsUsername(); + realm.setEditUsernameAllowed(false); + realm.setRegistrationEmailAsUsername(false); + getCleanup().addCleanup(() -> { + realm.setEditUsernameAllowed(editUsernameAllowed); + realm.setRegistrationEmailAsUsername(registrationEmailAsUsername); + testRealm().update(realm); + }); + testRealm().update(realm); + loginPage.open(); + loginPage.clickRegister(); + registerPage.assertCurrent(); + + String username = KeycloakModelUtils.generateId(); + String email = username + "@email.com"; + registerPage.register("firstName", "lastName", email, username, "password", "password"); + + appPage.assertCurrent(); + assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); + + String userId = events.expectRegister(username, email).assertEvent().getUserId(); + assertUserRegistered(userId, username, email); + } + + @Test + public void registerUserSuccessEditUsernameEnabled() { + RealmRepresentation realm = testRealm().toRepresentation(); + Boolean editUsernameAllowed = realm.isEditUsernameAllowed(); + Boolean registrationEmailAsUsername = realm.isRegistrationEmailAsUsername(); + realm.setEditUsernameAllowed(true); + realm.setRegistrationEmailAsUsername(false); + getCleanup().addCleanup(() -> { + realm.setEditUsernameAllowed(editUsernameAllowed); + realm.setRegistrationEmailAsUsername(registrationEmailAsUsername); + testRealm().update(realm); + }); + testRealm().update(realm); + loginPage.open(); + loginPage.clickRegister(); + registerPage.assertCurrent(); + + String username = KeycloakModelUtils.generateId(); + String email = username + "@email.com"; + registerPage.register("firstName", "lastName", email, username, "password", "password"); + + appPage.assertCurrent(); + assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); + + String userId = events.expectRegister(username, email).assertEvent().getUserId(); + assertUserRegistered(userId, username, email); + } + private void assertUserRegistered(String userId, String username, String email) { events.expectLogin().detail("username", username.toLowerCase()).user(userId).assertEvent();