Search in sources :

Example 16 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testGetUserProfileWithoutMetadata.

@Test
public void testGetUserProfileWithoutMetadata() throws IOException {
    UserRepresentation user = getUser(false);
    assertNull(user.getUserProfileMetadata());
}
Also used : UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 17 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testGetUserProfileMetadata_EditUsernameAllowed.

@Test
public void testGetUserProfileMetadata_EditUsernameAllowed() throws IOException {
    UserRepresentation user = getUser();
    assertNotNull(user.getUserProfileMetadata());
    assertUserProfileAttributeMetadata(user, "username", "${username}", true, false);
    assertUserProfileAttributeMetadata(user, "email", "${email}", true, false);
    assertUserProfileAttributeMetadata(user, "firstName", "${firstName}", true, false);
    assertUserProfileAttributeMetadata(user, "lastName", "${lastName}", true, false);
}
Also used : UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 18 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method testUpdateProfile.

@Test
public void testUpdateProfile() throws IOException {
    UserRepresentation user = getUser();
    String originalUsername = user.getUsername();
    String originalFirstName = user.getFirstName();
    String originalLastName = user.getLastName();
    String originalEmail = user.getEmail();
    Map<String, List<String>> originalAttributes = new HashMap<>(user.getAttributes());
    try {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setRegistrationEmailAsUsername(false);
        adminClient.realm("test").update(realmRep);
        user.setFirstName("Homer");
        user.setLastName("Simpsons");
        user.getAttributes().put("attr1", Collections.singletonList("val1"));
        user.getAttributes().put("attr2", Collections.singletonList("val2"));
        user = updateAndGet(user);
        assertEquals("Homer", user.getFirstName());
        assertEquals("Simpsons", user.getLastName());
        assertEquals(2, user.getAttributes().size());
        assertEquals(1, user.getAttributes().get("attr1").size());
        assertEquals("val1", user.getAttributes().get("attr1").get(0));
        assertEquals(1, user.getAttributes().get("attr2").size());
        assertEquals("val2", user.getAttributes().get("attr2").get(0));
        // Update attributes
        user.getAttributes().remove("attr1");
        user.getAttributes().get("attr2").add("val3");
        user = updateAndGet(user);
        if (isDeclarativeUserProfile()) {
            assertEquals(2, user.getAttributes().size());
            assertTrue(user.getAttributes().get("attr1").isEmpty());
        } else {
            assertEquals(1, user.getAttributes().size());
        }
        assertEquals(2, user.getAttributes().get("attr2").size());
        assertThat(user.getAttributes().get("attr2"), containsInAnyOrder("val2", "val3"));
        // Update email
        user.setEmail("bobby@localhost");
        user = updateAndGet(user);
        assertEquals("bobby@localhost", user.getEmail());
        user.setEmail("john-doh@localhost");
        updateError(user, 409, Messages.EMAIL_EXISTS);
        user.setEmail("test-user@localhost");
        user = updateAndGet(user);
        assertEquals("test-user@localhost", user.getEmail());
        user.setUsername("john-doh@localhost");
        updateError(user, 409, Messages.USERNAME_EXISTS);
        user.setUsername("test-user@localhost");
        user = updateAndGet(user);
        assertEquals("test-user@localhost", user.getUsername());
        realmRep.setRegistrationEmailAsUsername(true);
        adminClient.realm("test").update(realmRep);
        user.setUsername("updatedUsername");
        user = updateAndGet(user);
        assertEquals("test-user@localhost", user.getUsername());
        realmRep.setRegistrationEmailAsUsername(false);
        adminClient.realm("test").update(realmRep);
        user.setUsername("updatedUsername");
        user = updateAndGet(user);
        assertEquals("updatedusername", user.getUsername());
        realmRep.setEditUsernameAllowed(false);
        realmRep.setRegistrationEmailAsUsername(false);
        adminClient.realm("test").update(realmRep);
        user.setUsername("updatedUsername2");
        updateError(user, 400, Messages.READ_ONLY_USERNAME);
    } finally {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(true);
        adminClient.realm("test").update(realmRep);
        user.setUsername(originalUsername);
        user.setFirstName(originalFirstName);
        user.setLastName(originalLastName);
        user.setEmail(originalEmail);
        user.setAttributes(originalAttributes);
        SimpleHttp.Response response = SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).json(user).asResponse();
        System.out.println(response.asString());
        assertEquals(204, response.getStatus());
    }
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) HashMap(java.util.HashMap) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) List(java.util.List) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 19 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceReadOnlyAttributesTest method testAccountUpdateAttributeExpectSuccess.

private void testAccountUpdateAttributeExpectSuccess(String attrName) throws IOException {
    // Attribute not yet supposed to be on the user
    UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    Assert.assertThat(user.getAttributes().keySet(), not(contains(attrName)));
    // Assert not possible to add the attribute to the user
    user.singleAttribute(attrName, "foo");
    user = updateAndGet(user);
    // Update attribute of the user with account REST to the same value (Case when we are updating existing attribute) - should be fine as our attribute is not changed
    user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    Assert.assertEquals("foo", user.getAttributes().get(attrName).get(0));
    user.singleAttribute("someOtherAttr", "foo");
    user = updateAndGet(user);
    // Update attribute of the user with account REST (Case when we are updating existing attribute
    user.singleAttribute(attrName, "foo-updated");
    user = updateAndGet(user);
    // Remove attribute from the user with account REST (Case when we are removing existing attribute)
    user.getAttributes().remove(attrName);
    user = updateAndGet(user);
    // Revert
    user.getAttributes().remove("foo");
    user.getAttributes().remove("someOtherAttr");
    user = updateAndGet(user);
}
Also used : UserRepresentation(org.keycloak.representations.account.UserRepresentation)

Example 20 with UserRepresentation

use of org.keycloak.representations.account.UserRepresentation in project keycloak by keycloak.

the class AccountRestServiceReadOnlyAttributesTest method testAccountUpdateAttributeExpectFailure.

private void testAccountUpdateAttributeExpectFailure(String attrName, boolean deniedForAdminAsWell) throws IOException {
    // Attribute not yet supposed to be on the user
    UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    Assert.assertThat(user.getAttributes().keySet(), not(contains(attrName)));
    // Assert not possible to add the attribute to the user
    user.singleAttribute(attrName, "foo");
    updateError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // Add the attribute to the user with admin REST (Case when we are adding new attribute)
    UserResource adminUserResource = null;
    org.keycloak.representations.idm.UserRepresentation adminUserRep = null;
    try {
        adminUserResource = ApiUtil.findUserByUsernameId(testRealm(), user.getUsername());
        adminUserRep = adminUserResource.toRepresentation();
        adminUserRep.singleAttribute(attrName, "foo");
        adminUserResource.update(adminUserRep);
        if (deniedForAdminAsWell) {
            Assert.fail("Not expected to update attribute " + attrName + " by admin REST API");
        }
    } catch (BadRequestException bre) {
        if (!deniedForAdminAsWell) {
            Assert.fail("Was expected to update attribute " + attrName + " by admin REST API");
        }
        return;
    }
    // Update attribute of the user with account REST to the same value (Case when we are updating existing attribute) - should be fine as our attribute is not changed
    user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
    Assert.assertEquals("foo", user.getAttributes().get(attrName).get(0));
    user.singleAttribute("someOtherAttr", "foo");
    user = updateAndGet(user);
    // Update attribute of the user with account REST (Case when we are updating existing attribute
    user.singleAttribute(attrName, "foo-updated");
    updateError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // Ignore removal of read-only attributes
    user.getAttributes().remove(attrName);
    user = updateAndGet(user);
    assertTrue(user.getAttributes().containsKey(attrName));
    // Revert with admin REST
    adminUserRep.getAttributes().remove(attrName);
    adminUserRep.getAttributes().remove("someOtherAttr");
    adminUserResource.update(adminUserRep);
}
Also used : UserResource(org.keycloak.admin.client.resource.UserResource) BadRequestException(javax.ws.rs.BadRequestException) UserRepresentation(org.keycloak.representations.account.UserRepresentation)

Aggregations

UserRepresentation (org.keycloak.representations.account.UserRepresentation)21 Test (org.junit.Test)18 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)11 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)9 SimpleHttp (org.keycloak.broker.provider.util.SimpleHttp)6 HashMap (java.util.HashMap)5 List (java.util.List)5 UserProfileAttributeMetadata (org.keycloak.representations.account.UserProfileAttributeMetadata)3 VerifyProfileTest (org.keycloak.testsuite.forms.VerifyProfileTest)3 UserResource (org.keycloak.admin.client.resource.UserResource)2 RealmModel (org.keycloak.models.RealmModel)2 ArrayList (java.util.ArrayList)1 BadRequestException (javax.ws.rs.BadRequestException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 UserModel (org.keycloak.models.UserModel)1 TokenUtil (org.keycloak.testsuite.util.TokenUtil)1 UserProfile (org.keycloak.userprofile.UserProfile)1