Skip to content

Commit

Permalink
KEYCLOAK-1678 added invalidation tests for groups and roles, added mr…
Browse files Browse the repository at this point in the history
…oe update tests for realm
  • Loading branch information
tkyjovsk committed Feb 16, 2016
1 parent acf8549 commit 65a840b
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 79 deletions.
Expand Up @@ -30,16 +30,18 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;


import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD; import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD;
import org.keycloak.representations.idm.GroupRepresentation;


/** /**
* Created by st on 28.05.15. * Created by st on 28.05.15.
*/ */
public class ApiUtil { public class ApiUtil {


private static final Logger log = Logger.getLogger(ApiUtil.class); private static final Logger log = Logger.getLogger(ApiUtil.class);

public static String getCreatedId(Response response) { public static String getCreatedId(Response response) {
URI location = response.getLocation(); URI location = response.getLocation();
if (location == null) { if (location == null) {
Expand All @@ -48,7 +50,7 @@ public static String getCreatedId(Response response) {
String path = location.getPath(); String path = location.getPath();
return path.substring(path.lastIndexOf('/') + 1); return path.substring(path.lastIndexOf('/') + 1);
} }

public static ClientResource findClientResourceByClientId(RealmResource realm, String clientId) { public static ClientResource findClientResourceByClientId(RealmResource realm, String clientId) {
for (ClientRepresentation c : realm.clients().findAll()) { for (ClientRepresentation c : realm.clients().findAll()) {
if (c.getClientId().equals(clientId)) { if (c.getClientId().equals(clientId)) {
Expand All @@ -57,7 +59,7 @@ public static ClientResource findClientResourceByClientId(RealmResource realm, S
} }
return null; return null;
} }

public static ClientResource findClientResourceByName(RealmResource realm, String name) { public static ClientResource findClientResourceByName(RealmResource realm, String name) {
for (ClientRepresentation c : realm.clients().findAll()) { for (ClientRepresentation c : realm.clients().findAll()) {
if (c.getName().equals(name)) { if (c.getName().equals(name)) {
Expand All @@ -66,7 +68,7 @@ public static ClientResource findClientResourceByName(RealmResource realm, Strin
} }
return null; return null;
} }

public static ClientRepresentation findClientByClientId(RealmResource realm, String clientId) { public static ClientRepresentation findClientByClientId(RealmResource realm, String clientId) {
ClientRepresentation client = null; ClientRepresentation client = null;
for (ClientRepresentation c : realm.clients().findAll()) { for (ClientRepresentation c : realm.clients().findAll()) {
Expand All @@ -76,7 +78,7 @@ public static ClientRepresentation findClientByClientId(RealmResource realm, Str
} }
return client; return client;
} }

public static UserRepresentation findUserByUsername(RealmResource realm, String username) { public static UserRepresentation findUserByUsername(RealmResource realm, String username) {
UserRepresentation user = null; UserRepresentation user = null;
List<UserRepresentation> ur = realm.users().search(username, null, null); List<UserRepresentation> ur = realm.users().search(username, null, null);
Expand All @@ -85,7 +87,7 @@ public static UserRepresentation findUserByUsername(RealmResource realm, String
} }
return user; return user;
} }

public static String createUserWithAdminClient(RealmResource realm, UserRepresentation user) { public static String createUserWithAdminClient(RealmResource realm, UserRepresentation user) {
Response response = realm.users().create(user); Response response = realm.users().create(user);
String createdId = getCreatedId(response); String createdId = getCreatedId(response);
Expand All @@ -98,15 +100,15 @@ public static String createUserAndResetPasswordWithAdminClient(RealmResource rea
resetUserPassword(realm.users().get(id), password, false); resetUserPassword(realm.users().get(id), password, false);
return id; return id;
} }

public static void resetUserPassword(UserResource userResource, String newPassword, boolean temporary) { public static void resetUserPassword(UserResource userResource, String newPassword, boolean temporary) {
CredentialRepresentation newCredential = new CredentialRepresentation(); CredentialRepresentation newCredential = new CredentialRepresentation();
newCredential.setType(PASSWORD); newCredential.setType(PASSWORD);
newCredential.setValue(newPassword); newCredential.setValue(newPassword);
newCredential.setTemporary(temporary); newCredential.setTemporary(temporary);
userResource.resetPassword(newCredential); userResource.resetPassword(newCredential);
} }

public static void assignClientRoles(RealmResource realm, String userId, String clientName, String... roles) { public static void assignClientRoles(RealmResource realm, String userId, String clientName, String... roles) {
String realmName = realm.toRepresentation().getRealm(); String realmName = realm.toRepresentation().getRealm();
String clientId = ""; String clientId = "";
Expand All @@ -118,21 +120,32 @@ public static void assignClientRoles(RealmResource realm, String userId, String


if (!clientId.isEmpty()) { if (!clientId.isEmpty()) {
ClientResource clientResource = realm.clients().get(clientId); ClientResource clientResource = realm.clients().get(clientId);

List<RoleRepresentation> roleRepresentations = new ArrayList<>(); List<RoleRepresentation> roleRepresentations = new ArrayList<>();
for (String roleName : roles) { for (String roleName : roles) {
RoleRepresentation role = clientResource.roles().get(roleName).toRepresentation(); RoleRepresentation role = clientResource.roles().get(roleName).toRepresentation();
roleRepresentations.add(role); roleRepresentations.add(role);
} }

UserResource userResource = realm.users().get(userId); UserResource userResource = realm.users().get(userId);
log.debug("assigning roles: " + Arrays.toString(roles) + " to user: \"" + log.debug("assigning roles: " + Arrays.toString(roles) + " to user: \""
userResource.toRepresentation().getUsername() + "\" of client: \"" + + userResource.toRepresentation().getUsername() + "\" of client: \""
clientName + "\" in realm: \"" + realmName + "\""); + clientName + "\" in realm: \"" + realmName + "\"");
userResource.roles().clientLevel(clientId).add(roleRepresentations); userResource.roles().clientLevel(clientId).add(roleRepresentations);
} else { } else {
log.warn("client with name " + clientName + "doesn't exist in realm " + realmName); log.warn("client with name " + clientName + "doesn't exist in realm " + realmName);
} }
} }


public static boolean groupContainsSubgroup(GroupRepresentation group, GroupRepresentation subgroup) {
boolean contains = false;
for (GroupRepresentation sg : group.getSubGroups()) {
if (subgroup.getId().equals(sg.getId())) {
contains = true;
break;
}
}
return contains;
}

} }
Expand Up @@ -78,7 +78,7 @@ public void failback() {
protected ContainerInfo frontendNode() { protected ContainerInfo frontendNode() {
return suiteContext.getAuthServerInfo(); return suiteContext.getAuthServerInfo();
} }

protected ContainerInfo backendNode(int i) { protected ContainerInfo backendNode(int i) {
return suiteContext.getAuthServerBackendsInfo().get(i); return suiteContext.getAuthServerBackendsInfo().get(i);
} }
Expand Down
@@ -1,9 +1,8 @@
package org.keycloak.testsuite.cluster; package org.keycloak.testsuite.cluster;


import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ReflectionToStringBuilder;
Expand All @@ -16,18 +15,14 @@
/** /**
* *
* @author tkyjovsk * @author tkyjovsk
* @param <T> entity representation
* @param <TR> entity resource
*/ */
public abstract class AbstractInvalidationClusterTest<T> extends AbstractClusterTest { public abstract class AbstractInvalidationClusterTest<T, TR> extends AbstractClusterTest {

private final SecureRandom random = new SecureRandom();

protected String randomString(int length) {
return new BigInteger(130, random).toString(length);
}


protected RealmRepresentation createTestRealmRepresentation() { protected RealmRepresentation createTestRealmRepresentation() {
RealmRepresentation testRealm = new RealmRepresentation(); RealmRepresentation testRealm = new RealmRepresentation();
testRealm.setRealm("test_" + randomString(5)); testRealm.setRealm("test_" + RandomStringUtils.randomAlphabetic(5));
testRealm.setEnabled(true); testRealm.setEnabled(true);
return testRealm; return testRealm;
} }
Expand Down Expand Up @@ -72,6 +67,10 @@ public void crud(boolean backendFailover) {
assertEntityOnSurvivorNodesIsDeleted(testEntity); assertEntityOnSurvivorNodesIsDeleted(testEntity);
} }


protected abstract TR entityResource(T testEntity, ContainerInfo node);

protected abstract TR entityResource(String idOrName, ContainerInfo node);

protected abstract T createEntity(T testEntity, ContainerInfo node); protected abstract T createEntity(T testEntity, ContainerInfo node);


protected abstract T readEntity(T entity, ContainerInfo node); protected abstract T readEntity(T entity, ContainerInfo node);
Expand All @@ -80,20 +79,36 @@ public void crud(boolean backendFailover) {


protected abstract void deleteEntity(T testEntity, ContainerInfo node); protected abstract void deleteEntity(T testEntity, ContainerInfo node);


protected T createEntityOnCurrentFailNode(T testEntity) { protected TR entityResourceOnCurrentFailNode(T testEntity) {
return createEntity(testEntity, getCurrentFailNode()); return entityResource(testEntity, getCurrentFailNode());
}

protected String getEntityType(T entity) {
return entity.getClass().getSimpleName().replace("Representation", "");
}

protected T createEntityOnCurrentFailNode(T entity) {
log.info("Creating " + getEntityType(entity) + " on " + getCurrentFailNode());
return createEntity(entity, getCurrentFailNode());
} }


protected T readEntityOnCurrentFailNode(T entity) { protected T readEntityOnCurrentFailNode(T entity) {
log.debug("Reading " + getEntityType(entity) + " on " + getCurrentFailNode());
return readEntity(entity, getCurrentFailNode()); return readEntity(entity, getCurrentFailNode());
} }


protected T updateEntityOnCurrentFailNode(T entity) { protected T updateEntityOnCurrentFailNode(T entity) {
return updateEntityOnCurrentFailNode(entity, "");
}

protected T updateEntityOnCurrentFailNode(T entity, String updateType) {
log.info("Updating " + getEntityType(entity) + " " + updateType + " on " + getCurrentFailNode());
return updateEntity(entity, getCurrentFailNode()); return updateEntity(entity, getCurrentFailNode());
} }


protected void deleteEntityOnCurrentFailNode(T testEntity) { protected void deleteEntityOnCurrentFailNode(T entity) {
deleteEntity(testEntity, getCurrentFailNode()); log.info("Creating " + getEntityType(entity) + " on " + getCurrentFailNode());
deleteEntity(entity, getCurrentFailNode());
} }


protected abstract T testEntityUpdates(T testEntity, boolean backendFailover); protected abstract T testEntityUpdates(T testEntity, boolean backendFailover);
Expand All @@ -116,15 +131,17 @@ protected void assertEntityOnSurvivorNodesEqualsTo(T testEntityOnFailNode) {
for (ContainerInfo survivorNode : getCurrentSurvivorNodes()) { for (ContainerInfo survivorNode : getCurrentSurvivorNodes()) {
T testEntityOnSurvivorNode = readEntity(testEntityOnFailNode, survivorNode); T testEntityOnSurvivorNode = readEntity(testEntityOnFailNode, survivorNode);
if (EqualsBuilder.reflectionEquals(testEntityOnSurvivorNode, testEntityOnFailNode, excludedComparisonFields)) { if (EqualsBuilder.reflectionEquals(testEntityOnSurvivorNode, testEntityOnFailNode, excludedComparisonFields)) {
log.info("Verification on survivor " + survivorNode + " PASSED"); log.info(String.format("Verification of %s on survivor %s PASSED", getEntityType(testEntityOnFailNode), survivorNode));
} else { } else {
entityDiffers = true; entityDiffers = true;
log.error("Verification on survivor " + survivorNode + " FAILED"); log.error(String.format("Verification of %s on survivor %s FAILED", getEntityType(testEntityOnFailNode), survivorNode));
String tf = ReflectionToStringBuilder.reflectionToString(testEntityOnFailNode, ToStringStyle.SHORT_PREFIX_STYLE); String tf = ReflectionToStringBuilder.reflectionToString(testEntityOnFailNode, ToStringStyle.SHORT_PREFIX_STYLE);
String ts = ReflectionToStringBuilder.reflectionToString(testEntityOnSurvivorNode, ToStringStyle.SHORT_PREFIX_STYLE); String ts = ReflectionToStringBuilder.reflectionToString(testEntityOnSurvivorNode, ToStringStyle.SHORT_PREFIX_STYLE);
log.error("\nEntity on fail node: \n\n" + tf + "\n" log.error(String.format(
+ "\nEntity on survivor node: \n" + ts + "\n" "\nEntity on fail node: \n%s\n"
+ "\nDifference: \n" + StringUtils.difference(tf, ts) + "\n"); + "\nEntity on survivor node: \n%s\n"
+ "\nDifference: \n%s\n",
tf, ts, StringUtils.difference(tf, ts)));
} }
} }
assertFalse(entityDiffers); assertFalse(entityDiffers);
Expand All @@ -136,10 +153,10 @@ private void assertEntityOnSurvivorNodesIsDeleted(T testEntityOnFailNode) {
for (ContainerInfo survivorNode : getCurrentSurvivorNodes()) { for (ContainerInfo survivorNode : getCurrentSurvivorNodes()) {
T testEntityOnSurvivorNode = readEntity(testEntityOnFailNode, survivorNode); T testEntityOnSurvivorNode = readEntity(testEntityOnFailNode, survivorNode);
if (testEntityOnSurvivorNode == null) { if (testEntityOnSurvivorNode == null) {
log.info("Verification of deletion on survivor " + survivorNode + " PASSED"); log.info(String.format("Verification of %s deletion on survivor %s PASSED", getEntityType(testEntityOnFailNode), survivorNode));
} else { } else {
entityExists = true; entityExists = true;
log.error("Verification of deletion on survivor " + survivorNode + " FAILED"); log.error(String.format("Verification of %s deletion on survivor %s FAILED", getEntityType(testEntityOnFailNode), survivorNode));
} }
} }
assertFalse(entityExists); assertFalse(entityExists);
Expand Down
Expand Up @@ -8,7 +8,7 @@
* *
* @author tkyjovsk * @author tkyjovsk
*/ */
public abstract class AbstractInvalidationClusterTestWithTestRealm<T> extends AbstractInvalidationClusterTest<T> { public abstract class AbstractInvalidationClusterTestWithTestRealm<T, TR> extends AbstractInvalidationClusterTest<T, TR> {


protected String testRealmName = null; protected String testRealmName = null;


Expand Down
Expand Up @@ -2,8 +2,10 @@


import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.apache.commons.lang.RandomStringUtils;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import org.junit.Before; import org.junit.Before;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.ClientsResource; import org.keycloak.admin.client.resource.ClientsResource;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.testsuite.admin.ApiUtil; import org.keycloak.testsuite.admin.ApiUtil;
Expand All @@ -13,7 +15,7 @@
* *
* @author tkyjovsk * @author tkyjovsk
*/ */
public class ClientInvalidationClusterTest extends AbstractInvalidationClusterTestWithTestRealm<ClientRepresentation> { public class ClientInvalidationClusterTest extends AbstractInvalidationClusterTestWithTestRealm<ClientRepresentation, ClientResource> {


@Before @Before
public void setExcludedComparisonFields() { public void setExcludedComparisonFields() {
Expand All @@ -23,7 +25,7 @@ public void setExcludedComparisonFields() {
@Override @Override
protected ClientRepresentation createTestEntityRepresentation() { protected ClientRepresentation createTestEntityRepresentation() {
ClientRepresentation client = new ClientRepresentation(); ClientRepresentation client = new ClientRepresentation();
String s = randomString(5); String s = RandomStringUtils.randomAlphabetic(5);
client.setClientId("client_" + s); client.setClientId("client_" + s);
client.setName("name_" + s); client.setName("name_" + s);
return client; return client;
Expand All @@ -33,6 +35,16 @@ protected ClientsResource clients(ContainerInfo node) {
return getAdminClientFor(node).realm(testRealmName).clients(); return getAdminClientFor(node).realm(testRealmName).clients();
} }


@Override
protected ClientResource entityResource(ClientRepresentation client, ContainerInfo node) {
return entityResource(client.getId(), node);
}

@Override
protected ClientResource entityResource(String id, ContainerInfo node) {
return clients(node).get(id);
}

@Override @Override
protected ClientRepresentation createEntity(ClientRepresentation client, ContainerInfo node) { protected ClientRepresentation createEntity(ClientRepresentation client, ContainerInfo node) {
Response response = clients(node).create(client); Response response = clients(node).create(client);
Expand All @@ -46,22 +58,22 @@ protected ClientRepresentation createEntity(ClientRepresentation client, Contain
protected ClientRepresentation readEntity(ClientRepresentation client, ContainerInfo node) { protected ClientRepresentation readEntity(ClientRepresentation client, ContainerInfo node) {
ClientRepresentation u = null; ClientRepresentation u = null;
try { try {
u = clients(node).get(client.getId()).toRepresentation(); u = entityResource(client, node).toRepresentation();
} catch (NotFoundException nfe) { } catch (NotFoundException nfe) {
// exoected when client doesn't exist // expected when client doesn't exist
} }
return u; return u;
} }


@Override @Override
protected ClientRepresentation updateEntity(ClientRepresentation client, ContainerInfo node) { protected ClientRepresentation updateEntity(ClientRepresentation client, ContainerInfo node) {
clients(node).get(client.getId()).update(client); entityResource(client, node).update(client);
return readEntity(client, node); return readEntity(client, node);
} }


@Override @Override
protected void deleteEntity(ClientRepresentation client, ContainerInfo node) { protected void deleteEntity(ClientRepresentation client, ContainerInfo node) {
clients(node).get(client.getId()).remove(); entityResource(client, node).remove();
assertNull(readEntity(client, node)); assertNull(readEntity(client, node));
} }


Expand All @@ -70,12 +82,12 @@ protected ClientRepresentation testEntityUpdates(ClientRepresentation client, bo


// clientId // clientId
client.setClientId(client.getClientId() + "_updated"); client.setClientId(client.getClientId() + "_updated");
client = updateEntity(client, getCurrentFailNode()); client = updateEntityOnCurrentFailNode(client, "clientId");
verifyEntityUpdateDuringFailover(client, backendFailover); verifyEntityUpdateDuringFailover(client, backendFailover);


// name // name
client.setName(client.getName() + "_updated"); client.setName(client.getName() + "_updated");
client = updateEntity(client, getCurrentFailNode()); client = updateEntityOnCurrentFailNode(client, "name");
verifyEntityUpdateDuringFailover(client, backendFailover); verifyEntityUpdateDuringFailover(client, backendFailover);


return client; return client;
Expand Down

0 comments on commit 65a840b

Please sign in to comment.