From 16282aeb7bad3046349748859e6323c785a98919 Mon Sep 17 00:00:00 2001 From: mposolda Date: Thu, 8 Sep 2016 08:36:31 +0200 Subject: [PATCH] KEYCLOAK-3537 Username not shown when validation error on Account profile page --- .../account/freemarker/model/AccountBean.java | 6 +++++- .../testsuite/account/AccountTest.java | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/services/src/main/java/org/keycloak/forms/account/freemarker/model/AccountBean.java b/services/src/main/java/org/keycloak/forms/account/freemarker/model/AccountBean.java index 857bfc04d9..ceaaf94449 100755 --- a/services/src/main/java/org/keycloak/forms/account/freemarker/model/AccountBean.java +++ b/services/src/main/java/org/keycloak/forms/account/freemarker/model/AccountBean.java @@ -73,7 +73,11 @@ public class AccountBean { } public String getUsername() { - return profileFormData != null ? profileFormData.getFirst("username") : user.getUsername(); + if (profileFormData != null && profileFormData.containsKey("username")) { + return profileFormData.getFirst("username"); + } else { + return user.getUsername(); + } } public String getEmail() { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountTest.java index e4dfe0eea2..35d11d15f4 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountTest.java @@ -377,12 +377,15 @@ public class AccountTest extends TestRealmKeycloakTest { } @Test - public void changeProfile() { + public void changeProfile() throws Exception { + setEditUsernameAllowed(false); + profilePage.open(); loginPage.login("test-user@localhost", "password"); events.expectLogin().client("account").detail(Details.REDIRECT_URI, ACCOUNT_REDIRECT).assertEvent(); + Assert.assertEquals("test-user@localhost", profilePage.getUsername()); Assert.assertEquals("Tom", profilePage.getFirstName()); Assert.assertEquals("Brady", profilePage.getLastName()); Assert.assertEquals("test-user@localhost", profilePage.getEmail()); @@ -391,6 +394,7 @@ public class AccountTest extends TestRealmKeycloakTest { profilePage.updateProfile("", "New last", "new@email.com"); Assert.assertEquals("Please specify first name.", profilePage.getError()); + Assert.assertEquals("test-user@localhost", profilePage.getUsername()); Assert.assertEquals("", profilePage.getFirstName()); Assert.assertEquals("New last", profilePage.getLastName()); Assert.assertEquals("new@email.com", profilePage.getEmail()); @@ -417,6 +421,7 @@ public class AccountTest extends TestRealmKeycloakTest { profilePage.clickCancel(); + Assert.assertEquals("test-user@localhost", profilePage.getUsername()); Assert.assertEquals("Tom", profilePage.getFirstName()); Assert.assertEquals("Brady", profilePage.getLastName()); Assert.assertEquals("test-user@localhost", profilePage.getEmail()); @@ -426,6 +431,7 @@ public class AccountTest extends TestRealmKeycloakTest { profilePage.updateProfile("New first", "New last", "new@email.com"); Assert.assertEquals("Your account has been updated.", profilePage.getSuccess()); + Assert.assertEquals("test-user@localhost", profilePage.getUsername()); Assert.assertEquals("New first", profilePage.getFirstName()); Assert.assertEquals("New last", profilePage.getLastName()); Assert.assertEquals("new@email.com", profilePage.getEmail()); @@ -436,18 +442,21 @@ public class AccountTest extends TestRealmKeycloakTest { // reset user for other tests profilePage.updateProfile("Tom", "Brady", "test-user@localhost"); events.clear(); + + // Revert + setEditUsernameAllowed(true); } - private void setEditUsernameAllowed() { + private void setEditUsernameAllowed(boolean allowed) { RealmRepresentation testRealm = testRealm().toRepresentation(); - testRealm.setEditUsernameAllowed(true); + testRealm.setEditUsernameAllowed(allowed); testRealm().update(testRealm); } @Test public void changeUsername() { // allow to edit the username in realm - setEditUsernameAllowed(); + setEditUsernameAllowed(true); profilePage.open(); loginPage.login("test-user@localhost", "password"); @@ -504,7 +513,7 @@ public class AccountTest extends TestRealmKeycloakTest { @Test public void changeUsernameLoginWithOldUsername() { addUser("change-username", "change-username@localhost"); - setEditUsernameAllowed(); + setEditUsernameAllowed(true); profilePage.open(); loginPage.login("change-username", "password"); @@ -530,7 +539,7 @@ public class AccountTest extends TestRealmKeycloakTest { @Test public void changeEmailLoginWithOldEmail() { addUser("change-email", "change-username@localhost"); - setEditUsernameAllowed(); + setEditUsernameAllowed(true); profilePage.open(); loginPage.login("change-username@localhost", "password");