Search in sources :

Example 1 with UserRepresentation

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

the class AccountRestServiceWithUserProfileTest method testUpdateProfileEventWithAdditionalAttributesAuditing.

@Test
public void testUpdateProfileEventWithAdditionalAttributesAuditing() throws IOException {
    setUserProfileConfiguration("{\"attributes\": [" + "{\"name\": \"firstName\"," + PERMISSIONS_ALL + ", \"required\": {}}," + "{\"name\": \"lastName\"," + PERMISSIONS_ALL + ", \"required\": {}}," + "{\"name\": \"attr1\"," + PERMISSIONS_ALL + "}," + "{\"name\": \"attr2\"," + PERMISSIONS_ALL + "}" + "]}");
    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.setEmail("bobby@localhost");
        user.setFirstName("Homer");
        user.setLastName("Simpsons");
        user.getAttributes().put("attr1", Collections.singletonList("val1"));
        user.getAttributes().put("attr2", Collections.singletonList("val2"));
        user = updateAndGet(user);
        // skip login to the REST API event
        events.poll();
        events.expectAccount(EventType.UPDATE_PROFILE).user(user.getId()).detail(Details.CONTEXT, UserProfileContext.ACCOUNT.name()).detail(Details.PREVIOUS_EMAIL, originalEmail).detail(Details.UPDATED_EMAIL, "bobby@localhost").detail(Details.PREVIOUS_FIRST_NAME, originalFirstName).detail(Details.PREVIOUS_LAST_NAME, originalLastName).detail(Details.UPDATED_FIRST_NAME, "Homer").detail(Details.UPDATED_LAST_NAME, "Simpsons").detail(Details.PREF_UPDATED + "attr2", "val2").assertEvent();
        events.assertEmpty();
    } 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) VerifyProfileTest(org.keycloak.testsuite.forms.VerifyProfileTest)

Example 2 with UserRepresentation

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

the class AccountRestServiceWithUserProfileTest method testGetUserProfileMetadata_EditUsernameAllowed.

@Test
@Override
public void testGetUserProfileMetadata_EditUsernameAllowed() throws IOException {
    setUserProfileConfiguration(UP_CONFIG_FOR_METADATA);
    UserRepresentation user = getUser();
    assertNotNull(user.getUserProfileMetadata());
    assertUserProfileAttributeMetadata(user, "username", "${username}", true, false);
    assertUserProfileAttributeMetadata(user, "email", "${email}", true, false);
    UserProfileAttributeMetadata uam = assertUserProfileAttributeMetadata(user, "firstName", "${profile.firstName}", false, false);
    assertNull(uam.getAnnotations());
    Map<String, Object> vc = assertValidatorExists(uam, "length");
    assertEquals(255, vc.get("max"));
    uam = assertUserProfileAttributeMetadata(user, "lastName", "Last name", true, false);
    assertNotNull(uam.getAnnotations());
    assertEquals(3, uam.getAnnotations().size());
    assertAnnotationValue(uam, "formHintKey", "userEmailFormFieldHint");
    assertAnnotationValue(uam, "anotherKey", 10);
    assertUserProfileAttributeMetadata(user, "attr_with_scope_selector", "attr_with_scope_selector", false, false);
    assertUserProfileAttributeMetadata(user, "attr_required", "attr_required", true, false);
    assertUserProfileAttributeMetadata(user, "attr_required_by_role", "attr_required_by_role", true, false);
    assertUserProfileAttributeMetadata(user, "attr_required_by_scope", "attr_required_by_scope", false, false);
    assertUserProfileAttributeMetadata(user, "attr_not_required_due_to_role", "attr_not_required_due_to_role", false, false);
    assertUserProfileAttributeMetadata(user, "attr_readonly", "attr_readonly", false, true);
    assertNull(getUserProfileAttributeMetadata(user, "attr_no_permission"));
}
Also used : UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) VerifyProfileTest(org.keycloak.testsuite.forms.VerifyProfileTest)

Example 3 with UserRepresentation

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

the class AccountRestServiceTest method testProfilePermissions.

@Test
public void testProfilePermissions() throws IOException {
    TokenUtil noaccessToken = new TokenUtil("no-account-access", "password");
    TokenUtil viewToken = new TokenUtil("view-account-access", "password");
    // Read with no access
    assertEquals(403, SimpleHttp.doGet(getAccountUrl(null), httpClient).header("Accept", "application/json").auth(noaccessToken.getToken()).asStatus());
    // Update with no access
    assertEquals(403, SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(noaccessToken.getToken()).json(new UserRepresentation()).asStatus());
    // Update with read only
    assertEquals(403, SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(viewToken.getToken()).json(new UserRepresentation()).asStatus());
}
Also used : TokenUtil(org.keycloak.testsuite.util.TokenUtil) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 4 with UserRepresentation

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

the class AccountRestServiceTest method testUpdateProfileCannotChangeThroughAttributes.

@Test
public void testUpdateProfileCannotChangeThroughAttributes() throws IOException {
    UserRepresentation user = getUser();
    String originalUsername = user.getUsername();
    Map<String, List<String>> originalAttributes = new HashMap<>(user.getAttributes());
    try {
        user.getAttributes().put("username", Collections.singletonList("Username"));
        user.getAttributes().put("attr2", Collections.singletonList("val2"));
        user = updateAndGet(user);
        assertEquals(user.getUsername(), originalUsername);
    } finally {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(true);
        adminClient.realm("test").update(realmRep);
        user.setUsername(originalUsername);
        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 5 with UserRepresentation

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

the class AccountRestServiceTest method testUpdateProfileWithRegistrationEmailAsUsername.

// KEYCLOAK-7572
@Test
public void testUpdateProfileWithRegistrationEmailAsUsername() throws IOException {
    RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
    realmRep.setRegistrationEmailAsUsername(true);
    adminClient.realm("test").update(realmRep);
    UserRepresentation user = getUser();
    String originalFirstname = user.getFirstName();
    try {
        user.setFirstName("Homer1");
        user = updateAndGet(user);
        assertEquals("Homer1", user.getFirstName());
    } finally {
        user.setFirstName(originalFirstname);
        int status = SimpleHttp.doPost(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).json(user).asStatus();
        assertEquals(204, status);
    }
}
Also used : RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

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