Search in sources :

Example 11 with UserRepresentation

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

the class LDAPAccountRestApiTest method testUpdateProfile.

@Test
public void testUpdateProfile() throws IOException {
    UserRepresentation user = getProfile();
    List<String> origLdapId = new ArrayList<>(user.getAttributes().get(LDAPConstants.LDAP_ID));
    List<String> origLdapEntryDn = new ArrayList<>(user.getAttributes().get(LDAPConstants.LDAP_ENTRY_DN));
    Assert.assertEquals(1, origLdapId.size());
    Assert.assertEquals(1, origLdapEntryDn.size());
    Assert.assertThat(user.getAttributes().keySet(), not(contains(KerberosFederationProvider.KERBEROS_PRINCIPAL)));
    // Trying to add KERBEROS_PRINCIPAL should fail (Adding attribute, which was not yet present)
    user.setFirstName("JohnUpdated");
    user.setLastName("DoeUpdated");
    user.singleAttribute(KerberosFederationProvider.KERBEROS_PRINCIPAL, "foo");
    updateProfileExpectError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // The same test, but consider case sensitivity
    user.getAttributes().remove(KerberosFederationProvider.KERBEROS_PRINCIPAL);
    user.singleAttribute("KERberos_principal", "foo");
    updateProfileExpectError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // Trying to update LDAP_ID should fail (Updating existing attribute, which was present)
    user.getAttributes().remove("KERberos_principal");
    user.setFirstName("JohnUpdated");
    user.setLastName("DoeUpdated");
    user.getAttributes().get(LDAPConstants.LDAP_ID).remove(0);
    user.getAttributes().get(LDAPConstants.LDAP_ID).add("123");
    updateProfileExpectError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // Trying to delete LDAP_ID should fail (Removing attribute, which was present here already)
    user.getAttributes().get(LDAPConstants.LDAP_ID).remove(0);
    updateProfileExpectError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // ignore removal for read-only attributes
    user.getAttributes().remove(LDAPConstants.LDAP_ID);
    updateProfileExpectSuccess(user);
    user = getProfile();
    assertFalse(user.getAttributes().get(LDAPConstants.LDAP_ID).isEmpty());
    // Trying to update LDAP_ENTRY_DN should fail
    user.getAttributes().put(LDAPConstants.LDAP_ID, origLdapId);
    user.getAttributes().get(LDAPConstants.LDAP_ENTRY_DN).remove(0);
    user.getAttributes().get(LDAPConstants.LDAP_ENTRY_DN).add("ou=foo,dc=bar");
    updateProfileExpectError(user, 400, Messages.UPDATE_READ_ONLY_ATTRIBUTES_REJECTED);
    // Update firstName and lastName should be fine
    user.getAttributes().put(LDAPConstants.LDAP_ENTRY_DN, origLdapEntryDn);
    updateProfileExpectSuccess(user);
    user = getProfile();
    assertEquals("JohnUpdated", user.getFirstName());
    assertEquals("DoeUpdated", user.getLastName());
    assertEquals(origLdapId, user.getAttributes().get(LDAPConstants.LDAP_ID));
    assertEquals(origLdapEntryDn, user.getAttributes().get(LDAPConstants.LDAP_ENTRY_DN));
    // Revert
    user.setFirstName("John");
    user.setLastName("Doe");
    updateProfileExpectSuccess(user);
}
Also used : ArrayList(java.util.ArrayList) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test)

Example 12 with UserRepresentation

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

the class LDAPAccountRestApiTest method testGetProfile.

@Test
public void testGetProfile() throws IOException {
    UserRepresentation user = getProfile();
    assertEquals("John", user.getFirstName());
    assertEquals("Doe", user.getLastName());
    assertEquals("john@email.org", user.getEmail());
    assertFalse(user.isEmailVerified());
}
Also used : UserRepresentation(org.keycloak.representations.account.UserRepresentation) Test(org.junit.Test)

Example 13 with UserRepresentation

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

the class AccountRestServiceTest method testUpdateProfileEvent.

@Test
public void testUpdateProfileEvent() 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.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").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) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 14 with UserRepresentation

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

the class AccountRestServiceTest method testUpdateSingleField.

@Test
public void testUpdateSingleField() 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(null);
        user.setLastName("Bob");
        user.setEmail(null);
        user.getAttributes().clear();
        user = updateAndGet(user);
        assertEquals(user.getLastName(), "Bob");
        assertNull(user.getFirstName());
        assertNull(user.getEmail());
    } 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 15 with UserRepresentation

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

the class AccountRestServiceTest method testGetUserProfileMetadata_EditUsernameDisallowed.

@Test
public void testGetUserProfileMetadata_EditUsernameDisallowed() throws IOException {
    try {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(false);
        adminClient.realm("test").update(realmRep);
        UserRepresentation user = getUser();
        assertNotNull(user.getUserProfileMetadata());
        UserProfileAttributeMetadata upm = assertUserProfileAttributeMetadata(user, "username", "${username}", true, true);
        // makes sure internal validators are not exposed
        Assert.assertEquals(0, upm.getValidators().size());
        upm = assertUserProfileAttributeMetadata(user, "email", "${email}", true, false);
        Assert.assertEquals(1, upm.getValidators().size());
        Assert.assertTrue(upm.getValidators().containsKey(EmailValidator.ID));
        assertUserProfileAttributeMetadata(user, "firstName", "${firstName}", true, false);
        assertUserProfileAttributeMetadata(user, "lastName", "${lastName}", true, false);
    } finally {
        RealmRepresentation realmRep = testRealm().toRepresentation();
        realmRep.setEditUsernameAllowed(true);
        testRealm().update(realmRep);
    }
}
Also used : RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) 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