Search in sources :

Example 31 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class GroupTest method testBriefRepresentationOnGroupMembers.

@Test
public void testBriefRepresentationOnGroupMembers() {
    RealmResource realm = adminClient.realms().realm("test");
    String groupName = "brief-grouptest-group";
    String userName = "brief-grouptest-user";
    GroupsResource groups = realm.groups();
    try (Response response = groups.add(GroupBuilder.create().name(groupName).build())) {
        String groupId = ApiUtil.getCreatedId(response);
        GroupResource group = groups.group(groupId);
        UsersResource users = realm.users();
        UserRepresentation userRepresentation = UserBuilder.create().username(userName).addAttribute("myattribute", "myvalue").build();
        Response r = users.create(userRepresentation);
        UserResource user = users.get(ApiUtil.getCreatedId(r));
        user.joinGroup(groupId);
        UserRepresentation defaultRepresentation = group.members(null, null).get(0);
        UserRepresentation fullRepresentation = group.members(null, null, false).get(0);
        UserRepresentation briefRepresentation = group.members(null, null, true).get(0);
        assertEquals("full group member representation includes attributes", fullRepresentation.getAttributes(), userRepresentation.getAttributes());
        assertEquals("default group member representation is full", defaultRepresentation.getAttributes(), userRepresentation.getAttributes());
        assertNull("brief group member representation omits attributes", briefRepresentation.getAttributes());
        group.remove();
        user.remove();
    }
}
Also used : Response(javax.ws.rs.core.Response) RealmResource(org.keycloak.admin.client.resource.RealmResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) UserResource(org.keycloak.admin.client.resource.UserResource) GroupResource(org.keycloak.admin.client.resource.GroupResource) GroupsResource(org.keycloak.admin.client.resource.GroupsResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 32 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class UserPolicyManagementTest method testDeleteUser.

@Test
public void testDeleteUser() {
    AuthorizationResource authorization = getClient().authorization();
    UserPolicyRepresentation representation = new UserPolicyRepresentation();
    representation.setName("Realm User Policy");
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.addUser("User D");
    representation.addUser("User E");
    representation.addUser("User F");
    assertCreated(authorization, representation);
    UsersResource users = getRealm().users();
    UserRepresentation user = users.search("User D").get(0);
    users.get(user.getId()).remove();
    representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
    Assert.assertEquals(2, representation.getUsers().size());
    Assert.assertFalse(representation.getUsers().contains(user.getId()));
    user = users.search("User E").get(0);
    users.get(user.getId()).remove();
    representation = authorization.policies().user().findById(representation.getId()).toRepresentation();
    Assert.assertEquals(1, representation.getUsers().size());
    Assert.assertFalse(representation.getUsers().contains(user.getId()));
    user = users.search("User F").get(0);
    users.get(user.getId()).remove();
    try {
        authorization.policies().user().findById(representation.getId()).toRepresentation();
        fail("User policy should be removed");
    } catch (NotFoundException nfe) {
    // ignore
    }
}
Also used : UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UsersResource(org.keycloak.admin.client.resource.UsersResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 33 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class AggregatePolicyManagementTest method configureTest.

@Before
public void configureTest() {
    super.configureTest();
    RealmResource realmResource = testRealmResource();
    RolesResource realmRoles = realmResource.roles();
    realmRoles.create(new RoleRepresentation("Role A", "", false));
    realmRoles.create(new RoleRepresentation("Role B", "", false));
    UsersResource users = realmResource.users();
    users.create(UserBuilder.create().username("user a").build());
    ClientsResource clients = realmResource.clients();
    clients.create(ClientBuilder.create().clientId("client a").build());
    realmResource.groups().add(GroupBuilder.create().name("Group A").build());
    RolePolicyRepresentation policyA = new RolePolicyRepresentation();
    policyA.setName("Policy A");
    policyA.addRole("Role A");
    AuthorizationResource authorization = realmResource.clients().get(newClient.getId()).authorization();
    PoliciesResource policies = authorization.policies();
    RolePoliciesResource roles = policies.role();
    roles.create(policyA);
    RolePolicyRepresentation policyB = new RolePolicyRepresentation();
    policyB.setName("Policy B");
    policyB.addRole("Role B");
    roles.create(policyB);
    UserPolicyRepresentation policyC = new UserPolicyRepresentation();
    policyC.setName("Policy C");
    policyC.addUser("test");
    policies.user().create(policyC).close();
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UsersResource(org.keycloak.admin.client.resource.UsersResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Before(org.junit.Before)

Example 34 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class UserAttributeUpdater method forUserByUsername.

public static UserAttributeUpdater forUserByUsername(RealmResource realm, String userName) {
    UsersResource users = realm.users();
    List<UserRepresentation> foundUsers = users.search(userName).stream().filter(ur -> userName.equalsIgnoreCase(ur.getUsername())).collect(Collectors.toList());
    assertThat(foundUsers, hasSize(1));
    UserResource userRes = users.get(foundUsers.get(0).getId());
    return new UserAttributeUpdater(userRes, realm);
}
Also used : Arrays(java.util.Arrays) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) HashMap(java.util.HashMap) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Keycloak(org.keycloak.admin.client.Keycloak) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) UserModel(org.keycloak.models.UserModel) ServerResourceUpdater.updateViaAddRemove(org.keycloak.testsuite.updaters.ServerResourceUpdater.updateViaAddRemove) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) UserResource(org.keycloak.admin.client.resource.UserResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) UserResource(org.keycloak.admin.client.resource.UserResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 35 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class KcSamlIdPInitiatedSsoTest method testProviderIdpInitiatedLogin.

@Test
public void testProviderIdpInitiatedLogin() throws Exception {
    driver.navigate().to(getSamlIdpInitiatedUrl(REALM_PROV_NAME, "samlbroker"));
    waitForPage("sign in to", true);
    Assert.assertThat("Driver should be on the provider realm page right now", driver.getCurrentUrl(), containsString("/auth/realms/" + REALM_PROV_NAME + "/"));
    log.debug("Logging in");
    accountLoginPage.login(PROVIDER_REALM_USER_NAME, PROVIDER_REALM_USER_PASSWORD);
    waitForPage("update account information", false);
    Assert.assertTrue(updateAccountInformationPage.isCurrent());
    Assert.assertThat("We must be on consumer realm right now", driver.getCurrentUrl(), containsString("/auth/realms/" + REALM_CONS_NAME + "/"));
    log.debug("Updating info on updateAccount page");
    updateAccountInformationPage.updateAccountInformation(CONSUMER_CHOSEN_USERNAME, "test@localhost", "Firstname", "Lastname");
    UsersResource consumerUsers = adminClient.realm(REALM_CONS_NAME).users();
    int userCount = consumerUsers.count();
    Assert.assertTrue("There must be at least one user", userCount > 0);
    List<UserRepresentation> users = consumerUsers.search("", 0, userCount);
    boolean isUserFound = users.stream().anyMatch(user -> user.getUsername().equals(CONSUMER_CHOSEN_USERNAME) && user.getEmail().equals("test@localhost"));
    Assert.assertTrue("There must be user " + CONSUMER_CHOSEN_USERNAME + " in realm " + REALM_CONS_NAME, isUserFound);
    Assert.assertThat(driver.findElement(By.tagName("a")).getAttribute("id"), containsString("account"));
}
Also used : UsersResource(org.keycloak.admin.client.resource.UsersResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Aggregations

UsersResource (org.keycloak.admin.client.resource.UsersResource)36 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)32 Test (org.junit.Test)18 UserResource (org.keycloak.admin.client.resource.UserResource)10 RealmResource (org.keycloak.admin.client.resource.RealmResource)9 Response (javax.ws.rs.core.Response)7 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)7 Map (java.util.Map)6 List (java.util.List)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Before (org.junit.Before)5 UserSessionRepresentation (org.keycloak.representations.idm.UserSessionRepresentation)5 IdentityProviderResource (org.keycloak.admin.client.resource.IdentityProviderResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Matchers.hasSize (org.hamcrest.Matchers.hasSize)3 Assert.assertThat (org.junit.Assert.assertThat)3 RolesResource (org.keycloak.admin.client.resource.RolesResource)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3