Search in sources :

Example 1 with UserProfileAttributeMetadata

use of org.keycloak.representations.account.UserProfileAttributeMetadata 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 2 with UserProfileAttributeMetadata

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

the class AccountRestServiceWithUserProfileTest method testGetUserProfileMetadata_EditUsernameDisallowed.

@Test
@Override
public void testGetUserProfileMetadata_EditUsernameDisallowed() throws IOException {
    try {
        RealmRepresentation realmRep = adminClient.realm("test").toRepresentation();
        realmRep.setEditUsernameAllowed(false);
        adminClient.realm("test").update(realmRep);
        setUserProfileConfiguration(UP_CONFIG_FOR_METADATA);
        UserRepresentation user = getUser();
        assertNotNull(user.getUserProfileMetadata());
        assertUserProfileAttributeMetadata(user, "username", "${username}", true, true);
        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"));
    } 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) VerifyProfileTest(org.keycloak.testsuite.forms.VerifyProfileTest)

Example 3 with UserProfileAttributeMetadata

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

the class AccountRestServiceTest method assertUserProfileAttributeMetadata.

protected UserProfileAttributeMetadata assertUserProfileAttributeMetadata(UserRepresentation user, String attName, String displayName, boolean required, boolean readOnly) {
    UserProfileAttributeMetadata uam = getUserProfileAttributeMetadata(user, attName);
    assertNotNull(uam);
    assertEquals("Unexpected display name for attribute " + uam.getName(), displayName, uam.getDisplayName());
    assertEquals("Unexpected required flag for attribute " + uam.getName(), required, uam.isRequired());
    assertEquals("Unexpected readonly flag for attribute " + uam.getName(), readOnly, uam.isReadOnly());
    return uam;
}
Also used : UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata)

Example 4 with UserProfileAttributeMetadata

use of org.keycloak.representations.account.UserProfileAttributeMetadata 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

UserProfileAttributeMetadata (org.keycloak.representations.account.UserProfileAttributeMetadata)4 Test (org.junit.Test)3 UserRepresentation (org.keycloak.representations.account.UserRepresentation)3 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)2 VerifyProfileTest (org.keycloak.testsuite.forms.VerifyProfileTest)2 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)1