Search in sources :

Example 76 with GroupRepresentation

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

the class GroupTest method doNotAllowSameGroupNameAtSameLevel.

@Test
public void doNotAllowSameGroupNameAtSameLevel() throws Exception {
    RealmResource realm = adminClient.realms().realm("test");
    GroupRepresentation topGroup = new GroupRepresentation();
    topGroup.setName("top");
    topGroup = createGroup(realm, topGroup);
    GroupRepresentation anotherTopGroup = new GroupRepresentation();
    anotherTopGroup.setName("top");
    Response response = realm.groups().add(anotherTopGroup);
    // conflict status 409 - same name not allowed
    assertEquals(409, response.getStatus());
    GroupRepresentation level2Group = new GroupRepresentation();
    level2Group.setName("level2");
    response = realm.groups().group(topGroup.getId()).subGroup(level2Group);
    response.close();
    // created status
    assertEquals(201, response.getStatus());
    GroupRepresentation anotherlevel2Group = new GroupRepresentation();
    anotherlevel2Group.setName("level2");
    response = realm.groups().group(topGroup.getId()).subGroup(anotherlevel2Group);
    response.close();
    // conflict status 409 - same name not allowed
    assertEquals(409, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) Test(org.junit.Test)

Example 77 with GroupRepresentation

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

the class GroupTest method moveGroups.

@Test
public void moveGroups() {
    RealmResource realm = adminClient.realms().realm("test");
    // Create 2 top level groups "mygroup1" and "mygroup2"
    GroupRepresentation group = GroupBuilder.create().name("mygroup1").build();
    GroupRepresentation group1 = createGroup(realm, group);
    group = GroupBuilder.create().name("mygroup2").build();
    GroupRepresentation group2 = createGroup(realm, group);
    // Move "mygroup2" as child of "mygroup1" . Assert it was moved
    Response response = realm.groups().group(group1.getId()).subGroup(group2);
    Assert.assertEquals(204, response.getStatus());
    response.close();
    // Assert "mygroup2" was moved
    group1 = realm.groups().group(group1.getId()).toRepresentation();
    group2 = realm.groups().group(group2.getId()).toRepresentation();
    assertNames(group1.getSubGroups(), "mygroup2");
    Assert.assertEquals("/mygroup1/mygroup2", group2.getPath());
    assertAdminEvents.clear();
    // Create top level group with the same name
    group = GroupBuilder.create().name("mygroup2").build();
    GroupRepresentation group3 = createGroup(realm, group);
    // Try to move top level "mygroup2" as child of "mygroup1". It should fail as there is already a child group
    // of "mygroup1" with name "mygroup2"
    response = realm.groups().group(group1.getId()).subGroup(group3);
    Assert.assertEquals(409, response.getStatus());
    realm.groups().group(group3.getId()).remove();
    // Move "mygroup2" back under parent
    response = realm.groups().add(group2);
    Assert.assertEquals(204, response.getStatus());
    response.close();
    // Assert "mygroup2" was moved
    group1 = realm.groups().group(group1.getId()).toRepresentation();
    group2 = realm.groups().group(group2.getId()).toRepresentation();
    assertTrue(group1.getSubGroups().isEmpty());
    Assert.assertEquals("/mygroup2", group2.getPath());
}
Also used : Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) Test(org.junit.Test)

Example 78 with GroupRepresentation

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

the class GroupTest method getGroupsWithFullRepresentation.

@Test
public void getGroupsWithFullRepresentation() {
    RealmResource realm = adminClient.realms().realm("test");
    GroupsResource groupsResource = adminClient.realms().realm("test").groups();
    GroupRepresentation group = new GroupRepresentation();
    group.setName("groupWithAttribute");
    Map<String, List<String>> attributes = new HashMap<String, List<String>>();
    attributes.put("attribute1", Arrays.asList("attribute1", "attribute2"));
    group.setAttributes(attributes);
    group = createGroup(realm, group);
    List<GroupRepresentation> groups = groupsResource.groups("groupWithAttribute", 0, 20, false);
    assertFalse(groups.isEmpty());
    assertTrue(groups.get(0).getAttributes().containsKey("attribute1"));
}
Also used : GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) HashMap(java.util.HashMap) RealmResource(org.keycloak.admin.client.resource.RealmResource) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) Test(org.junit.Test)

Example 79 with GroupRepresentation

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

the class GroupTest method testClientRemoveWithClientRoleGroupMapping.

/**
 * KEYCLOAK-2716
 * @throws Exception
 */
@Test
public void testClientRemoveWithClientRoleGroupMapping() throws Exception {
    RealmResource realm = adminClient.realms().realm("test");
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId("foo");
    client.setRootUrl("http://foo");
    client.setProtocol("openid-connect");
    Response response = realm.clients().create(client);
    response.close();
    String clientUuid = ApiUtil.getCreatedId(response);
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.clientResourcePath(clientUuid), client, ResourceType.CLIENT);
    client = realm.clients().findByClientId("foo").get(0);
    RoleRepresentation role = new RoleRepresentation();
    role.setName("foo-role");
    realm.clients().get(client.getId()).roles().create(role);
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.clientRoleResourcePath(clientUuid, "foo-role"), role, ResourceType.CLIENT_ROLE);
    role = realm.clients().get(client.getId()).roles().get("foo-role").toRepresentation();
    GroupRepresentation group = new GroupRepresentation();
    group.setName("2716");
    group = createGroup(realm, group);
    List<RoleRepresentation> list = new LinkedList<>();
    list.add(role);
    realm.groups().group(group.getId()).roles().clientLevel(client.getId()).add(list);
    assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.groupRolesClientRolesPath(group.getId(), clientUuid), list, ResourceType.CLIENT_ROLE_MAPPING);
    realm.clients().get(client.getId()).remove();
    assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.clientResourcePath(clientUuid), ResourceType.CLIENT);
}
Also used : Response(javax.ws.rs.core.Response) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) LinkedList(java.util.LinkedList) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 80 with GroupRepresentation

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

the class RealmRolesTest method before.

@Before
public void before() {
    RoleRepresentation roleA = RoleBuilder.create().name("role-a").description("Role A").attributes(ROLE_A_ATTRIBUTES).build();
    RoleRepresentation roleB = RoleBuilder.create().name("role-b").description("Role B").build();
    // KEYCLOAK-2035
    RoleRepresentation roleWithUsers = RoleBuilder.create().name("role-with-users").description("Role with users").build();
    RoleRepresentation roleWithoutUsers = RoleBuilder.create().name("role-without-users").description("role-without-users").build();
    adminClient.realm(REALM_NAME).roles().create(roleA);
    adminClient.realm(REALM_NAME).roles().create(roleB);
    adminClient.realm(REALM_NAME).roles().create(roleWithUsers);
    adminClient.realm(REALM_NAME).roles().create(roleWithoutUsers);
    ClientRepresentation clientRep = ClientBuilder.create().clientId("client-a").build();
    try (Response response = adminClient.realm(REALM_NAME).clients().create(clientRep)) {
        clientUuid = ApiUtil.getCreatedId(response);
        getCleanup().addClientUuid(clientUuid);
    }
    RoleRepresentation roleC = RoleBuilder.create().name("role-c").description("Role C").build();
    adminClient.realm(REALM_NAME).clients().get(clientUuid).roles().create(roleC);
    for (RoleRepresentation r : adminClient.realm(REALM_NAME).roles().list()) {
        ids.put(r.getName(), r.getId());
    }
    for (RoleRepresentation r : adminClient.realm(REALM_NAME).clients().get(clientUuid).roles().list()) {
        ids.put(r.getName(), r.getId());
    }
    UserRepresentation userRep = new UserRepresentation();
    userRep.setUsername("test-role-member");
    userRep.setEmail("test-role-member@test-role-member.com");
    userRep.setRequiredActions(Collections.<String>emptyList());
    userRep.setEnabled(true);
    adminClient.realm(REALM_NAME).users().create(userRep);
    getCleanup().addRoleId(ids.get("role-a"));
    getCleanup().addRoleId(ids.get("role-b"));
    getCleanup().addRoleId(ids.get("role-c"));
    getCleanup().addRoleId(ids.get("role-with-users"));
    getCleanup().addRoleId(ids.get("role-without-users"));
    getCleanup().addUserId(adminClient.realm(REALM_NAME).users().search(userRep.getUsername()).get(0).getId());
    GroupRepresentation groupRep = new GroupRepresentation();
    groupRep.setName("test-role-group");
    groupRep.setPath("/test-role-group");
    adminClient.realm(REALM_NAME).groups().add(groupRep);
    getCleanup().addGroupId(adminClient.realm(REALM_NAME).groups().groups().get(0).getId());
    resource = adminClient.realm(REALM_NAME).roles();
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath("role-a"), roleA, ResourceType.REALM_ROLE);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath("role-b"), roleB, ResourceType.REALM_ROLE);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath("role-with-users"), roleWithUsers, ResourceType.REALM_ROLE);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.roleResourcePath("role-without-users"), roleWithoutUsers, ResourceType.REALM_ROLE);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.clientResourcePath(clientUuid), clientRep, ResourceType.CLIENT);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.clientRoleResourcePath(clientUuid, "role-c"), roleC, ResourceType.CLIENT_ROLE);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.userResourcePath(adminClient.realm(REALM_NAME).users().search(userRep.getUsername()).get(0).getId()), userRep, ResourceType.USER);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.groupPath(adminClient.realm(REALM_NAME).groups().groups().get(0).getId()), groupRep, ResourceType.GROUP);
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) Response(javax.ws.rs.core.Response) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Before(org.junit.Before)

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