From fbe3445c48114aa625d55e6314decfe0ad0255a9 Mon Sep 17 00:00:00 2001 From: Moritz Becker Date: Tue, 9 Oct 2018 08:05:36 +0200 Subject: [PATCH] fix KEYCLOAK-8513 remove data dependency between testUpdateProfile and testGetProfile in org.keycloak.testsuite.account.AccountRestServiceTest --- .../account/AccountRestServiceTest.java | 104 +++++++++++------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountRestServiceTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountRestServiceTest.java index 6f81bacadf..b24ec81d97 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountRestServiceTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/AccountRestServiceTest.java @@ -36,7 +36,9 @@ import org.keycloak.testsuite.util.UserBuilder; import java.io.IOException; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.*; @@ -91,62 +93,80 @@ public class AccountRestServiceTest extends AbstractTestRealmKeycloakTest { @Test public void testUpdateProfile() throws IOException { UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), client).auth(tokenUtil.getToken()).asJson(UserRepresentation.class); - user.setFirstName("Homer"); - user.setLastName("Simpsons"); - user.getAttributes().put("attr1", Collections.singletonList("val1")); - user.getAttributes().put("attr2", Collections.singletonList("val2")); + String originalFirstName = user.getFirstName(); + String originalLastName = user.getLastName(); + String originalEmail = user.getEmail(); + Map> originalAttributes = new HashMap<>(user.getAttributes()); - user = updateAndGet(user); + try { + user.setFirstName("Homer"); + user.setLastName("Simpsons"); + user.getAttributes().put("attr1", Collections.singletonList("val1")); + user.getAttributes().put("attr2", Collections.singletonList("val2")); - assertEquals("Homer", user.getFirstName()); - assertEquals("Simpsons", user.getLastName()); - assertEquals(2, user.getAttributes().size()); - assertEquals(1, user.getAttributes().get("attr1").size()); - assertEquals("val1", user.getAttributes().get("attr1").get(0)); - assertEquals(1, user.getAttributes().get("attr2").size()); - assertEquals("val2", user.getAttributes().get("attr2").get(0)); + user = updateAndGet(user); - // Update attributes - user.getAttributes().remove("attr1"); - user.getAttributes().get("attr2").add("val3"); + assertEquals("Homer", user.getFirstName()); + assertEquals("Simpsons", user.getLastName()); + assertEquals(2, user.getAttributes().size()); + assertEquals(1, user.getAttributes().get("attr1").size()); + assertEquals("val1", user.getAttributes().get("attr1").get(0)); + assertEquals(1, user.getAttributes().get("attr2").size()); + assertEquals("val2", user.getAttributes().get("attr2").get(0)); - user = updateAndGet(user); + // Update attributes + user.getAttributes().remove("attr1"); + user.getAttributes().get("attr2").add("val3"); - assertEquals(1, user.getAttributes().size()); - assertEquals(2, user.getAttributes().get("attr2").size()); - assertThat(user.getAttributes().get("attr2"), containsInAnyOrder("val2", "val3")); + user = updateAndGet(user); - // Update email - user.setEmail("bobby@localhost"); - user = updateAndGet(user); - assertEquals("bobby@localhost", user.getEmail()); + assertEquals(1, user.getAttributes().size()); + assertEquals(2, user.getAttributes().get("attr2").size()); + assertThat(user.getAttributes().get("attr2"), containsInAnyOrder("val2", "val3")); - user.setEmail("john-doh@localhost"); - updateError(user, 409, Messages.EMAIL_EXISTS); + // Update email + user.setEmail("bobby@localhost"); + user = updateAndGet(user); + assertEquals("bobby@localhost", user.getEmail()); - user.setEmail("test-user@localhost"); - user = updateAndGet(user); - assertEquals("test-user@localhost", user.getEmail()); + user.setEmail("john-doh@localhost"); + updateError(user, 409, Messages.EMAIL_EXISTS); - // Update username - user.setUsername("updatedUsername"); - user = updateAndGet(user); - assertEquals("updatedusername", user.getUsername()); + user.setEmail("test-user@localhost"); + user = updateAndGet(user); + assertEquals("test-user@localhost", user.getEmail()); - user.setUsername("john-doh@localhost"); - updateError(user, 409, Messages.USERNAME_EXISTS); + // Update username + user.setUsername("updatedUsername"); + user = updateAndGet(user); + assertEquals("updatedusername", user.getUsername()); - user.setUsername("test-user@localhost"); - user = updateAndGet(user); - assertEquals("test-user@localhost", user.getUsername()); + user.setUsername("john-doh@localhost"); + updateError(user, 409, Messages.USERNAME_EXISTS); - RealmRepresentation realmRep = adminClient.realm("test").toRepresentation(); - realmRep.setEditUsernameAllowed(false); - adminClient.realm("test").update(realmRep); + user.setUsername("test-user@localhost"); + user = updateAndGet(user); + assertEquals("test-user@localhost", user.getUsername()); - user.setUsername("updatedUsername2"); - updateError(user, 400, Messages.READ_ONLY_USERNAME); + RealmRepresentation realmRep = adminClient.realm("test").toRepresentation(); + realmRep.setEditUsernameAllowed(false); + adminClient.realm("test").update(realmRep); + user.setUsername("updatedUsername2"); + updateError(user, 400, Messages.READ_ONLY_USERNAME); + } finally { + RealmRepresentation realmRep = adminClient.realm("test").toRepresentation(); + realmRep.setEditUsernameAllowed(true); + adminClient.realm("test").update(realmRep); + + user.setFirstName(originalFirstName); + user.setLastName(originalLastName); + user.setEmail(originalEmail); + user.setAttributes(originalAttributes); + SimpleHttp.Response response = SimpleHttp.doPost(getAccountUrl(null), client).auth(tokenUtil.getToken()).json(user).asResponse(); + System.out.println(response.asString()); + assertEquals(200, response.getStatus()); + } }