KEYCLOAK-8613 Group Membership Pagination
This commit is contained in:
1 parent
39bf08e1b9
commit
85f11873c3
19 files changed
+922
-180
No files matched your search
+157
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright 2018 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.keycloak.testsuite.admin.user;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Test;
|
||||
import org.keycloak.admin.client.resource.RealmResource;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.events.admin.OperationType;
|
||||
import org.keycloak.events.admin.ResourceType;
|
||||
import org.keycloak.representations.idm.GroupRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
|
||||
import org.keycloak.testsuite.admin.AbstractAdminTest;
|
||||
import org.keycloak.testsuite.admin.ApiUtil;
|
||||
import org.keycloak.testsuite.federation.DummyUserFederationProvider;
|
||||
import org.keycloak.testsuite.federation.DummyUserFederationProviderFactory;
|
||||
import org.keycloak.testsuite.runonserver.RunOnServerDeployment;
|
||||
import org.keycloak.testsuite.util.AdminEventPaths;
|
||||
import org.keycloak.testsuite.util.UserBuilder;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.keycloak.testsuite.Assert.assertNames;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:volker.suschke@bosch-si.com">Volker Suschke</a>
|
||||
* @author <a href="mailto:leon.graser@bosch-si.com">Leon Graser</a>
|
||||
*/
|
||||
public class UserGroupMembershipTest extends AbstractAdminTest {
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
return RunOnServerDeployment.create(
|
||||
AbstractAdminTest.class,
|
||||
AbstractTestRealmKeycloakTest.class,
|
||||
DummyUserFederationProviderFactory.class, DummyUserFederationProvider.class,
|
||||
UserResource.class);
|
||||
}
|
||||
|
||||
public String createUser() {
|
||||
return createUser("user1", "user1@localhost");
|
||||
}
|
||||
|
||||
public String createUser(String username, String email) {
|
||||
UserRepresentation user = new UserRepresentation();
|
||||
user.setUsername(username);
|
||||
user.setEmail(email);
|
||||
user.setRequiredActions(Collections.emptyList());
|
||||
user.setEnabled(true);
|
||||
|
||||
return createUser(user);
|
||||
}
|
||||
|
||||
private String createUser(UserRepresentation userRep) {
|
||||
Response response = realm.users().create(userRep);
|
||||
String createdId = ApiUtil.getCreatedId(response);
|
||||
response.close();
|
||||
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userResourcePath(createdId), userRep, ResourceType.USER);
|
||||
|
||||
getCleanup().addUserId(createdId);
|
||||
|
||||
return createdId;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyCreateUser() {
|
||||
createUser();
|
||||
}
|
||||
|
||||
private GroupRepresentation createGroup(RealmResource realm, GroupRepresentation group) {
|
||||
Response response = realm.groups().add(group);
|
||||
String groupId = ApiUtil.getCreatedId(response);
|
||||
getCleanup().addGroupId(groupId);
|
||||
response.close();
|
||||
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.groupPath(groupId), group, ResourceType.GROUP);
|
||||
|
||||
// Set ID to the original rep
|
||||
group.setId(groupId);
|
||||
return group;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupMembershipPaginated() {
|
||||
Response response = realm.users().create(UserBuilder.create().username("user-a").build());
|
||||
String userId = ApiUtil.getCreatedId(response);
|
||||
response.close();
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userResourcePath(userId), ResourceType.USER);
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
GroupRepresentation group = new GroupRepresentation();
|
||||
group.setName("group-" + i);
|
||||
String groupId = createGroup(realm, group).getId();
|
||||
realm.users().get(userId).joinGroup(groupId);
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userGroupPath(userId, groupId), group, ResourceType.GROUP_MEMBERSHIP);
|
||||
}
|
||||
|
||||
List<GroupRepresentation> groups = realm.users().get(userId).groups(5, 6);
|
||||
assertEquals(groups.size(), 5);
|
||||
assertNames(groups, "group-5","group-6","group-7","group-8","group-9");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupMembershipSearch() {
|
||||
Response response = realm.users().create(UserBuilder.create().username("user-b").build());
|
||||
String userId = ApiUtil.getCreatedId(response);
|
||||
response.close();
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userResourcePath(userId), ResourceType.USER);
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
GroupRepresentation group = new GroupRepresentation();
|
||||
group.setName("group-" + i);
|
||||
String groupId = createGroup(realm, group).getId();
|
||||
realm.users().get(userId).joinGroup(groupId);
|
||||
assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userGroupPath(userId, groupId), group, ResourceType.GROUP_MEMBERSHIP);
|
||||
}
|
||||
|
||||
List<GroupRepresentation> groups = realm.users().get(userId).groups("-3", 0, 10);
|
||||
assertEquals(1, groups.size());
|
||||
assertNames(groups, "group-3");
|
||||
|
||||
List<GroupRepresentation> groups2 = realm.users().get(userId).groups("1", 0, 10);
|
||||
assertEquals(2, groups2.size());
|
||||
assertNames(groups2, "group-1", "group-10");
|
||||
|
||||
List<GroupRepresentation> groups3 = realm.users().get(userId).groups("1", 2, 10);
|
||||
assertEquals(0, groups3.size());
|
||||
|
||||
List<GroupRepresentation> groups4 = realm.users().get(userId).groups("gr", 2, 10);
|
||||
assertEquals(8, groups4.size());
|
||||
|
||||
List<GroupRepresentation> groups5 = realm.users().get(userId).groups("Gr", 2, 10);
|
||||
assertEquals(8, groups5.size());
|
||||
}
|
||||
|
||||
}
|
||||
+95
@@ -128,6 +128,21 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
UserModel johnDb = session.userLocalStorage().getUserByUsername("johnkeycloak", appRealm);
|
||||
Set<GroupModel> johnDbGroups = johnDb.getGroups();
|
||||
Assert.assertEquals(2, johnDbGroups.size());
|
||||
|
||||
Set<GroupModel> johnDbGroupsWithGr = johnDb.getGroups("Gr", 0, 10);
|
||||
Assert.assertEquals(2, johnDbGroupsWithGr.size());
|
||||
|
||||
Set<GroupModel> johnDbGroupsWithGr2 = johnDb.getGroups("Gr", 1, 10);
|
||||
Assert.assertEquals(1, johnDbGroupsWithGr2.size());
|
||||
|
||||
Set<GroupModel> johnDbGroupsWithGr3 = johnDb.getGroups("Gr", 0, 1);
|
||||
Assert.assertEquals(1, johnDbGroupsWithGr3.size());
|
||||
|
||||
Set<GroupModel> johnDbGroupsWith12 = johnDb.getGroups("12", 0, 10);
|
||||
Assert.assertEquals(1, johnDbGroupsWith12.size());
|
||||
|
||||
long dbGroupCount = johnDb.getGroupsCount();
|
||||
Assert.assertEquals(2, dbGroupCount);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -145,10 +160,24 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
|
||||
Set<GroupModel> johnGroups = john.getGroups();
|
||||
Assert.assertEquals(2, johnGroups.size());
|
||||
long groupCount = john.getGroupsCount();
|
||||
Assert.assertEquals(2, groupCount);
|
||||
Assert.assertTrue(johnGroups.contains(group1));
|
||||
Assert.assertFalse(johnGroups.contains(group11));
|
||||
Assert.assertTrue(johnGroups.contains(group12));
|
||||
|
||||
Set<GroupModel> johnGroupsWithGr = john.getGroups("gr", 0, 10);
|
||||
Assert.assertEquals(2, johnGroupsWithGr.size());
|
||||
|
||||
Set<GroupModel> johnGroupsWithGr2 = john.getGroups("gr", 1, 10);
|
||||
Assert.assertEquals(1, johnGroupsWithGr2.size());
|
||||
|
||||
Set<GroupModel> johnGroupsWithGr3 = john.getGroups("gr", 0, 1);
|
||||
Assert.assertEquals(1, johnGroupsWithGr3.size());
|
||||
|
||||
Set<GroupModel> johnGroupsWith12 = john.getGroups("12", 0, 10);
|
||||
Assert.assertEquals(1, johnGroupsWith12.size());
|
||||
|
||||
// 4 - Check through userProvider
|
||||
List<UserModel> group1Members = session.users().getGroupMembers(appRealm, group1, 0, 10);
|
||||
List<UserModel> group11Members = session.users().getGroupMembers(appRealm, group11, 0, 10);
|
||||
@@ -170,6 +199,9 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
|
||||
johnGroups = john.getGroups();
|
||||
Assert.assertEquals(0, johnGroups.size());
|
||||
|
||||
groupCount = john.getGroupsCount();
|
||||
Assert.assertEquals(0, groupCount);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -211,6 +243,21 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
Assert.assertTrue(maryGroups.contains(group1));
|
||||
Assert.assertTrue(maryGroups.contains(group11));
|
||||
Assert.assertTrue(maryGroups.contains(group12));
|
||||
|
||||
long groupCount = mary.getGroupsCount();
|
||||
Assert.assertEquals(5, groupCount);
|
||||
|
||||
Set<GroupModel> maryGroupsWithGr = mary.getGroups("gr", 0, 10);
|
||||
Assert.assertEquals(5, maryGroupsWithGr.size());
|
||||
|
||||
Set<GroupModel> maryGroupsWithGr2 = mary.getGroups("gr", 1, 10);
|
||||
Assert.assertEquals(4, maryGroupsWithGr2.size());
|
||||
|
||||
Set<GroupModel> maryGroupsWithGr3 = mary.getGroups("gr", 0, 1);
|
||||
Assert.assertEquals(1, maryGroupsWithGr3.size());
|
||||
|
||||
Set<GroupModel> maryGroupsWith12 = mary.getGroups("12", 0, 10);
|
||||
Assert.assertEquals(2, maryGroupsWith12.size());
|
||||
});
|
||||
|
||||
// Assert that access through DB will have just DB mapped groups
|
||||
@@ -230,6 +277,21 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
Assert.assertFalse(maryDBGroups.contains(group11));
|
||||
Assert.assertTrue(maryDBGroups.contains(group12));
|
||||
|
||||
Set<GroupModel> maryDBGroupsWithGr = maryDB.getGroups("Gr", 0, 10);
|
||||
Assert.assertEquals(3, maryDBGroupsWithGr.size());
|
||||
|
||||
Set<GroupModel> maryDBGroupsWithGr2 = maryDB.getGroups("Gr", 1, 10);
|
||||
Assert.assertEquals(2, maryDBGroupsWithGr2.size());
|
||||
|
||||
Set<GroupModel> maryDBGroupsWithGr3 = maryDB.getGroups("Gr", 0, 1);
|
||||
Assert.assertEquals(1, maryDBGroupsWithGr3.size());
|
||||
|
||||
Set<GroupModel> maryDBGroupsWith12 = maryDB.getGroups("12", 0, 10);
|
||||
Assert.assertEquals(2, maryDBGroupsWith12.size());
|
||||
|
||||
long dbGroupCount = maryDB.getGroupsCount();
|
||||
Assert.assertEquals(3, dbGroupCount);
|
||||
|
||||
// Test the group mapping available for group12
|
||||
List<UserModel> group12Members = session.users().getGroupMembers(appRealm, group12, 0, 10);
|
||||
Assert.assertEquals(1, group12Members.size());
|
||||
@@ -321,6 +383,21 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
Assert.assertTrue(robGroups.contains(group11));
|
||||
Assert.assertTrue(robGroups.contains(group12));
|
||||
|
||||
Set<GroupModel> robGroupsWithGr = rob.getGroups("Gr", 0, 10);
|
||||
Assert.assertEquals(4, robGroupsWithGr.size());
|
||||
|
||||
Set<GroupModel> robGroupsWithGr2 = rob.getGroups("Gr", 1, 10);
|
||||
Assert.assertEquals(3, robGroupsWithGr2.size());
|
||||
|
||||
Set<GroupModel> robGroupsWithGr3 = rob.getGroups("Gr", 0, 1);
|
||||
Assert.assertEquals(1, robGroupsWithGr3.size());
|
||||
|
||||
Set<GroupModel> robGroupsWith12 = rob.getGroups("12", 0, 10);
|
||||
Assert.assertEquals(2, robGroupsWith12.size());
|
||||
|
||||
long dbGroupCount = rob.getGroupsCount();
|
||||
Assert.assertEquals(4, dbGroupCount);
|
||||
|
||||
// Delete some group mappings in LDAP and check that it doesn't have any effect and user still has groups
|
||||
LDAPObject ldapGroup = groupMapper.loadLDAPGroupByName("group11");
|
||||
groupMapper.deleteGroupMappingInLDAP(robLdap, ldapGroup);
|
||||
@@ -510,6 +587,24 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
|
||||
Assert.assertTrue(groups.contains(group31));
|
||||
Assert.assertTrue(groups.contains(group32));
|
||||
Assert.assertTrue(groups.contains(group4));
|
||||
|
||||
long groupsCount = john.getGroupsCount();
|
||||
Assert.assertEquals(4, groupsCount);
|
||||
|
||||
Set<GroupModel> groupsWith3v1 = john.getGroups("3", 0, 10);
|
||||
Assert.assertEquals(2, groupsWith3v1.size());
|
||||
|
||||
Set<GroupModel> groupsWith3v2 = john.getGroups("3", 1, 10);
|
||||
Assert.assertEquals(1, groupsWith3v2.size());
|
||||
|
||||
Set<GroupModel> groupsWith3v3 = john.getGroups("3", 1, 1);
|
||||
Assert.assertEquals(1, groupsWith3v3.size());
|
||||
|
||||
Set<GroupModel> groupsWith3v4 = john.getGroups("3", 1, 0);
|
||||
Assert.assertEquals(0, groupsWith3v4.size());
|
||||
|
||||
Set<GroupModel> groupsWithKeycloak = john.getGroups("Keycloak", 0, 10);
|
||||
Assert.assertEquals(0, groupsWithKeycloak.size());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user