Skip to content

Commit

Permalink
KEYCLOAK-7991: add pagination params to the RoleResource
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Tudan authored and hmlnarik committed Aug 21, 2018
1 parent 1b88eaf commit b606a25
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -72,10 +73,32 @@ public interface RoleResource {
@Path("composites")
@Consumes(MediaType.APPLICATION_JSON)
void deleteComposites(List<RoleRepresentation> rolesToRemove);


/**
* Get role members
* <p/>
* Returns users that have the given role
*
* @return a list of users with the given role
*/
@GET
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
Set<UserRepresentation> getRoleUserMembers();

/**
* Get role members
* <p/>
* Returns users that have the given role, paginated according to the query parameters
*
* @param firstResult Pagination offset
* @param maxResults Pagination size
* @return a list of users with the given role
*/
@GET
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
Set<UserRepresentation> getRoleUserMembers(@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleResource;
import org.keycloak.admin.client.resource.RolesResource;
import org.keycloak.admin.client.resource.UserResource;
Expand All @@ -40,18 +39,22 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.keycloak.testsuite.Assert.assertNames;
import static org.keycloak.testsuite.auth.page.AuthRealm.TEST;

/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
Expand Down Expand Up @@ -257,5 +260,46 @@ public void roleMembershipAfterUserRemoval() {
assertEquals(0, roleResource.getRoleUserMembers().size());

}


@Test
public void testRoleMembershipWithPagination() {
RoleResource role = resource.get("role-with-users");

// Add a second user
UserRepresentation userRep2 = new UserRepresentation();
userRep2.setUsername("test-role-member2");
userRep2.setEmail("test-role-member2@test-role-member.com");
userRep2.setRequiredActions(Collections.<String>emptyList());
userRep2.setEnabled(true);
adminClient.realm(REALM_NAME).users().create(userRep2);

List<UserRepresentation> users = adminClient.realm(REALM_NAME).users().search("test-role-member", null, null, null, null, null);
assertThat(users, hasSize(2));
for (UserRepresentation userRepFromList : users) {
UserResource user = adminClient.realm(REALM_NAME).users().get(userRepFromList.getId());
UserRepresentation userRep = user.toRepresentation();

RoleResource roleResource = adminClient.realm(REALM_NAME).roles().get(role.toRepresentation().getName());
List<RoleRepresentation> rolesToAdd = new LinkedList<>();
rolesToAdd.add(roleResource.toRepresentation());
adminClient.realm(REALM_NAME).users().get(userRep.getId()).roles().realmLevel().add(rolesToAdd);
}

RoleResource roleResource = adminClient.realm(REALM_NAME).roles().get(role.toRepresentation().getName());
Set<UserRepresentation> roleUserMembers = roleResource.getRoleUserMembers(0, 1);

Set<String> expectedMembers = new HashSet<>();
assertThat(roleUserMembers, hasSize(1));
expectedMembers.add(roleUserMembers.iterator().next().getUsername());

roleUserMembers = roleResource.getRoleUserMembers(1, 1);
assertThat(roleUserMembers, hasSize(1));
expectedMembers.add(roleUserMembers.iterator().next().getUsername());

roleUserMembers = roleResource.getRoleUserMembers(2, 1);
assertThat(roleUserMembers, is(empty()));

assertThat(expectedMembers, containsInAnyOrder("test-role-member", "test-role-member2"));
}

}

0 comments on commit b606a25

Please sign in to comment.