Search in sources :

Example 26 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupTest method updateGroup.

@Test
public void updateGroup() {
    RealmResource realm = adminClient.realms().realm("test");
    final String groupName = "group-" + UUID.randomUUID();
    GroupRepresentation group = GroupBuilder.create().name(groupName).singleAttribute("attr1", "attrval1").singleAttribute("attr2", "attrval2").build();
    createGroup(realm, group);
    group = realm.getGroupByPath("/" + groupName);
    Assert.assertNotNull(group);
    assertThat(group.getName(), is(groupName));
    assertThat(group.getAttributes().keySet(), containsInAnyOrder("attr1", "attr2"));
    assertThat(group.getAttributes(), hasEntry(is("attr1"), contains("attrval1")));
    assertThat(group.getAttributes(), hasEntry(is("attr2"), contains("attrval2")));
    final String groupNewName = "group-" + UUID.randomUUID();
    group.setName(groupNewName);
    group.getAttributes().remove("attr1");
    group.getAttributes().get("attr2").add("attrval2-2");
    group.getAttributes().put("attr3", Collections.singletonList("attrval2"));
    realm.groups().group(group.getId()).update(group);
    assertAdminEvents.assertEvent("test", OperationType.UPDATE, AdminEventPaths.groupPath(group.getId()), group, ResourceType.GROUP);
    group = realm.getGroupByPath("/" + groupNewName);
    assertThat(group.getName(), is(groupNewName));
    assertThat(group.getAttributes().keySet(), containsInAnyOrder("attr2", "attr3"));
    assertThat(group.getAttributes(), hasEntry(is("attr2"), containsInAnyOrder("attrval2", "attrval2-2")));
    assertThat(group.getAttributes(), hasEntry(is("attr3"), contains("attrval2")));
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) Test(org.junit.Test)

Example 27 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupTest method groupMembershipUsersOrder.

@Test
public // KEYCLOAK-6300
void groupMembershipUsersOrder() {
    RealmResource realm = adminClient.realms().realm("test");
    GroupRepresentation group = new GroupRepresentation();
    group.setName("group");
    String groupId = createGroup(realm, group).getId();
    List<String> usernames = new ArrayList<>();
    for (int i = 0; i < 9; i++) {
        UserRepresentation user = UserBuilder.create().username("user" + i).build();
        usernames.add(user.getUsername());
        try (Response create = realm.users().create(user)) {
            assertEquals(Status.CREATED, create.getStatusInfo());
            String userAId = ApiUtil.getCreatedId(create);
            realm.users().get(userAId).joinGroup(groupId);
        }
    }
    List<String> memberUsernames = new ArrayList<>();
    for (UserRepresentation member : realm.groups().group(groupId).members(0, 10)) {
        memberUsernames.add(member.getUsername());
    }
    assertArrayEquals("Expected: " + usernames + ", was: " + memberUsernames, usernames.toArray(), memberUsernames.toArray());
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) ArrayList(java.util.ArrayList) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 28 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupTest method deleteRealmWithDefaultGroups.

@Test
public // KEYCLOAK-2700
void deleteRealmWithDefaultGroups() throws IOException {
    RealmRepresentation rep = new RealmRepresentation();
    rep.setRealm("foo");
    GroupRepresentation group = new GroupRepresentation();
    group.setName("default1");
    group.setPath("/default1");
    rep.setGroups(Collections.singletonList(group));
    rep.setDefaultGroups(Collections.singletonList("/default1"));
    adminClient.realms().create(rep);
    adminClient.realm(rep.getRealm()).remove();
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) Test(org.junit.Test)

Example 29 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupTest method createGroupWithEmptyNameShouldFail.

// KEYCLOAK-17581
@Test
public void createGroupWithEmptyNameShouldFail() {
    RealmResource realm = adminClient.realms().realm("test");
    GroupRepresentation group = new GroupRepresentation();
    group.setName("");
    try (Response response = realm.groups().add(group)) {
        if (response.getStatus() != 400) {
            Assert.fail("Creating a group with empty name should fail");
        }
    } catch (Exception expected) {
        Assert.assertNotNull(expected);
    }
    group.setName(null);
    try (Response response = realm.groups().add(group)) {
        if (response.getStatus() != 400) {
            Assert.fail("Creating a group with null name should fail");
        }
    } catch (Exception expected) {
        Assert.assertNotNull(expected);
    }
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) ClientErrorException(javax.ws.rs.ClientErrorException) NotFoundException(javax.ws.rs.NotFoundException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) Test(org.junit.Test)

Example 30 with GroupRepresentation

use of org.keycloak.representations.idm.GroupRepresentation in project keycloak by keycloak.

the class GroupTest method groupMembership.

@Test
public void groupMembership() {
    RealmResource realm = adminClient.realms().realm("test");
    GroupRepresentation group = new GroupRepresentation();
    group.setName("group");
    String groupId = createGroup(realm, group).getId();
    Response response = realm.users().create(UserBuilder.create().username("user-a").build());
    String userAId = ApiUtil.getCreatedId(response);
    response.close();
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.userResourcePath(userAId), ResourceType.USER);
    response = realm.users().create(UserBuilder.create().username("user-b").build());
    String userBId = ApiUtil.getCreatedId(response);
    response.close();
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.userResourcePath(userBId), ResourceType.USER);
    realm.users().get(userAId).joinGroup(groupId);
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.userGroupPath(userAId, groupId), group, ResourceType.GROUP_MEMBERSHIP);
    List<UserRepresentation> members = realm.groups().group(groupId).members(0, 10);
    assertNames(members, "user-a");
    realm.users().get(userBId).joinGroup(groupId);
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.userGroupPath(userBId, groupId), group, ResourceType.GROUP_MEMBERSHIP);
    members = realm.groups().group(groupId).members(0, 10);
    assertNames(members, "user-a", "user-b");
    realm.users().get(userAId).leaveGroup(groupId);
    assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.userGroupPath(userAId, groupId), group, ResourceType.GROUP_MEMBERSHIP);
    members = realm.groups().group(groupId).members(0, 10);
    assertNames(members, "user-b");
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)81 Test (org.junit.Test)62 RealmResource (org.keycloak.admin.client.resource.RealmResource)36 Response (javax.ws.rs.core.Response)24 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)23 List (java.util.List)17 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)17 ProtocolMappersResource (org.keycloak.admin.client.resource.ProtocolMappersResource)14 UserResource (org.keycloak.admin.client.resource.UserResource)13 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 IDToken (org.keycloak.representations.IDToken)10 OAuthClient (org.keycloak.testsuite.util.OAuthClient)10 LinkedList (java.util.LinkedList)8 Before (org.junit.Before)8 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)7 Map (java.util.Map)6 NotFoundException (javax.ws.rs.NotFoundException)6 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)6