mirror of
https://github.com/keycloak/keycloak.git
synced 2026-05-07 23:50:03 -05:00
KEYCLOAK-1305 Add possibility to change username
This commit is contained in:
+58
-2
@@ -24,11 +24,9 @@ package org.keycloak.testsuite.account;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.keycloak.account.freemarker.model.ApplicationsBean;
|
||||
import org.keycloak.events.Details;
|
||||
import org.keycloak.events.Event;
|
||||
import org.keycloak.events.EventType;
|
||||
@@ -155,6 +153,9 @@ public class AccountTest {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) {
|
||||
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
|
||||
user.setFirstName("Tom");
|
||||
user.setLastName("Brady");
|
||||
user.setEmail("test-user@localhost");
|
||||
|
||||
UserCredentialModel cred = new UserCredentialModel();
|
||||
cred.setType(CredentialRepresentation.PASSWORD);
|
||||
@@ -393,6 +394,61 @@ public class AccountTest {
|
||||
events.expectAccount(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, "test-user@localhost").detail(Details.UPDATED_EMAIL, "new@email.com").assertEvent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changeUsername() {
|
||||
// allow to edit the username in realm
|
||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||
appRealm.setEditUsernameAllowed(true);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
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());
|
||||
|
||||
// All fields are required, so there should be an error when something is missing.
|
||||
profilePage.updateProfile("", "New first", "New last", "new@email.com");
|
||||
|
||||
Assert.assertEquals("Please specify username.", profilePage.getError());
|
||||
Assert.assertEquals("", profilePage.getUsername());
|
||||
Assert.assertEquals("New first", profilePage.getFirstName());
|
||||
Assert.assertEquals("New last", profilePage.getLastName());
|
||||
Assert.assertEquals("new@email.com", profilePage.getEmail());
|
||||
|
||||
events.assertEmpty();
|
||||
|
||||
profilePage.updateProfile("test-user-new@localhost", "New first", "New last", "new@email.com");
|
||||
|
||||
Assert.assertEquals("Your account has been updated.", profilePage.getSuccess());
|
||||
Assert.assertEquals("test-user-new@localhost", profilePage.getUsername());
|
||||
Assert.assertEquals("New first", profilePage.getFirstName());
|
||||
Assert.assertEquals("New last", profilePage.getLastName());
|
||||
Assert.assertEquals("new@email.com", profilePage.getEmail());
|
||||
|
||||
} finally {
|
||||
// reset user for other tests
|
||||
profilePage.updateProfile("test-user@localhost", "Tom", "Brady", "test-user@localhost");
|
||||
events.clear();
|
||||
|
||||
// reset realm
|
||||
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||
appRealm.setEditUsernameAllowed(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setupTotp() {
|
||||
totpPage.open();
|
||||
|
||||
@@ -242,6 +242,7 @@ public class AdminAPITest {
|
||||
if (rep.isRememberMe() != null) Assert.assertEquals(rep.isRememberMe(), storedRealm.isRememberMe());
|
||||
if (rep.isVerifyEmail() != null) Assert.assertEquals(rep.isVerifyEmail(), storedRealm.isVerifyEmail());
|
||||
if (rep.isResetPasswordAllowed() != null) Assert.assertEquals(rep.isResetPasswordAllowed(), storedRealm.isResetPasswordAllowed());
|
||||
if (rep.isEditUsernameAllowed() != null) Assert.assertEquals(rep.isEditUsernameAllowed(), storedRealm.isEditUsernameAllowed());
|
||||
if (rep.getSslRequired() != null) Assert.assertEquals(rep.getSslRequired(), storedRealm.getSslRequired());
|
||||
if (rep.getAccessCodeLifespan() != null) Assert.assertEquals(rep.getAccessCodeLifespan(), storedRealm.getAccessCodeLifespan());
|
||||
if (rep.getAccessCodeLifespanUserAction() != null)
|
||||
|
||||
@@ -71,6 +71,7 @@ public class RealmTest extends AbstractClientTest {
|
||||
rep.setAccessCodeLifespanLogin(1234);
|
||||
rep.setRegistrationAllowed(true);
|
||||
rep.setRegistrationEmailAsUsername(true);
|
||||
rep.setEditUsernameAllowed(true);
|
||||
|
||||
realm.update(rep);
|
||||
|
||||
@@ -81,16 +82,19 @@ public class RealmTest extends AbstractClientTest {
|
||||
assertEquals(1234, rep.getAccessCodeLifespanLogin().intValue());
|
||||
assertEquals(Boolean.TRUE, rep.isRegistrationAllowed());
|
||||
assertEquals(Boolean.TRUE, rep.isRegistrationEmailAsUsername());
|
||||
assertEquals(Boolean.TRUE, rep.isEditUsernameAllowed());
|
||||
|
||||
// second change
|
||||
rep.setRegistrationAllowed(false);
|
||||
rep.setRegistrationEmailAsUsername(false);
|
||||
rep.setEditUsernameAllowed(false);
|
||||
|
||||
realm.update(rep);
|
||||
|
||||
rep = realm.toRepresentation();
|
||||
assertEquals(Boolean.FALSE, rep.isRegistrationAllowed());
|
||||
assertEquals(Boolean.FALSE, rep.isRegistrationEmailAsUsername());
|
||||
assertEquals(Boolean.FALSE, rep.isEditUsernameAllowed());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package org.keycloak.testsuite.admin;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.keycloak.admin.client.resource.IdentityProviderResource;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.representations.idm.ErrorRepresentation;
|
||||
import org.keycloak.representations.idm.FederatedIdentityRepresentation;
|
||||
import org.keycloak.representations.idm.IdentityProviderRepresentation;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
import javax.ws.rs.ClientErrorException;
|
||||
@@ -410,4 +410,78 @@ public class UserTest extends AbstractClientTest {
|
||||
Assert.assertEquals("invalidClientId not enabled", error.getErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithNewUsername() {
|
||||
switchEditUsernameAllowedOn();
|
||||
String id = createUser();
|
||||
|
||||
UserResource user = realm.users().get(id);
|
||||
UserRepresentation userRep = user.toRepresentation();
|
||||
userRep.setUsername("user11");
|
||||
user.update(userRep);
|
||||
|
||||
userRep = realm.users().get(id).toRepresentation();
|
||||
assertEquals("user11", userRep.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithNewUsernameNotPossible() {
|
||||
String id = createUser();
|
||||
|
||||
UserResource user = realm.users().get(id);
|
||||
UserRepresentation userRep = user.toRepresentation();
|
||||
userRep.setUsername("user11");
|
||||
user.update(userRep);
|
||||
|
||||
userRep = realm.users().get(id).toRepresentation();
|
||||
assertEquals("user1", userRep.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithNewUsernameAccessingViaOldUsername() {
|
||||
switchEditUsernameAllowedOn();
|
||||
createUser();
|
||||
|
||||
try {
|
||||
UserResource user = realm.users().get("user1");
|
||||
UserRepresentation userRep = user.toRepresentation();
|
||||
userRep.setUsername("user1");
|
||||
user.update(userRep);
|
||||
|
||||
realm.users().get("user11").toRepresentation();
|
||||
fail("Expected failure");
|
||||
} catch (ClientErrorException e) {
|
||||
assertEquals(404, e.getResponse().getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUserWithExistingUsername() {
|
||||
switchEditUsernameAllowedOn();
|
||||
createUser();
|
||||
|
||||
UserRepresentation userRep = new UserRepresentation();
|
||||
userRep.setUsername("user2");
|
||||
Response response = realm.users().create(userRep);
|
||||
String createdId = ApiUtil.getCreatedId(response);
|
||||
response.close();
|
||||
|
||||
try {
|
||||
UserResource user = realm.users().get(createdId);
|
||||
userRep = user.toRepresentation();
|
||||
userRep.setUsername("user1");
|
||||
user.update(userRep);
|
||||
fail("Expected failure");
|
||||
} catch (ClientErrorException e) {
|
||||
assertEquals(409, e.getResponse().getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private void switchEditUsernameAllowedOn() {
|
||||
RealmRepresentation rep = realm.toRepresentation();
|
||||
rep.setEditUsernameAllowed(true);
|
||||
realm.update(rep);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ public class ModelTest extends AbstractModelTest {
|
||||
realm.setRegistrationAllowed(true);
|
||||
realm.setRegistrationEmailAsUsername(true);
|
||||
realm.setResetPasswordAllowed(true);
|
||||
realm.setEditUsernameAllowed(true);
|
||||
realm.setSslRequired(SslRequired.EXTERNAL);
|
||||
realm.setVerifyEmail(true);
|
||||
realm.setAccessTokenLifespan(1000);
|
||||
@@ -55,6 +56,7 @@ public class ModelTest extends AbstractModelTest {
|
||||
Assert.assertEquals(expected.isRegistrationAllowed(), actual.isRegistrationAllowed());
|
||||
Assert.assertEquals(expected.isRegistrationEmailAsUsername(), actual.isRegistrationEmailAsUsername());
|
||||
Assert.assertEquals(expected.isResetPasswordAllowed(), actual.isResetPasswordAllowed());
|
||||
Assert.assertEquals(expected.isEditUsernameAllowed(), actual.isEditUsernameAllowed());
|
||||
Assert.assertEquals(expected.getSslRequired(), actual.getSslRequired());
|
||||
Assert.assertEquals(expected.isVerifyEmail(), actual.isVerifyEmail());
|
||||
Assert.assertEquals(expected.getAccessTokenLifespan(), actual.getAccessTokenLifespan());
|
||||
|
||||
+20
@@ -35,6 +35,9 @@ public class AccountUpdateProfilePage extends AbstractAccountPage {
|
||||
|
||||
public static String PATH = RealmsResource.accountUrl(UriBuilder.fromUri(Constants.AUTH_SERVER_ROOT)).build("test").toString();
|
||||
|
||||
@FindBy(id = "username")
|
||||
private WebElement usernameInput;
|
||||
|
||||
@FindBy(id = "firstName")
|
||||
private WebElement firstNameInput;
|
||||
|
||||
@@ -74,11 +77,28 @@ public class AccountUpdateProfilePage extends AbstractAccountPage {
|
||||
submitButton.click();
|
||||
}
|
||||
|
||||
public void updateProfile(String username, String firstName, String lastName, String email) {
|
||||
usernameInput.clear();
|
||||
usernameInput.sendKeys(username);
|
||||
firstNameInput.clear();
|
||||
firstNameInput.sendKeys(firstName);
|
||||
lastNameInput.clear();
|
||||
lastNameInput.sendKeys(lastName);
|
||||
emailInput.clear();
|
||||
emailInput.sendKeys(email);
|
||||
|
||||
submitButton.click();
|
||||
}
|
||||
|
||||
public void clickCancel() {
|
||||
cancelButton.click();
|
||||
}
|
||||
|
||||
|
||||
public String getUsername() {
|
||||
return usernameInput.getAttribute("value");
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstNameInput.getAttribute("value");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user