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());
}
}
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"));
}
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());
}
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());
}
}
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);
}
}
Aggregations