fix KEYCLOAK-8513 remove data dependency between testUpdateProfile and testGetProfile in org.keycloak.testsuite.account.AccountRestServiceTest

This commit is contained in:
Moritz Becker 2018-10-09 08:05:36 +02:00 committed by Stian Thorgersen
parent 5b51c000af
commit fbe3445c48

View file

@ -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<String, List<String>> 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());
}
}