Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fixes #1278: Updating attributes of a user, which has no attributes set, will work now #1281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void updateUser(UserRepresentation existingUser) {

if (importConfigProperties.getBehaviors().isSkipAttributesForFederatedUser() && patchedUser.getFederationLink() != null) {
patchedUser.setAttributes(null);
} else if (existingUser.getAttributes() != null && userToImport.getAttributes() != null) {
} else {
patchedUser.setAttributes(userToImport.getAttributes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import de.adorsys.keycloak.config.exception.InvalidImportException;
import de.adorsys.keycloak.config.model.RealmImport;
import jakarta.ws.rs.NotAuthorizedException;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.RealmResource;
Expand All @@ -41,6 +42,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SuppressWarnings({"java:S5961"})
Expand Down Expand Up @@ -709,6 +711,35 @@ void shouldRemoveKeycloakDefaultClientLevelRolesFromExistingServiceAccount() thr
assertThat(keycloakNativeClientLevelRoles, empty());
}

@Test
@Order(101)
void shouldUpdateAttributeOfUserWithoutAttributes() throws IOException {
doImport("61.1_create_realm_with_user_without_attributes.json");

RealmRepresentation createdRealm = keycloakProvider.getInstance().realm(REALM_NAME).toRepresentation();

assertThat(createdRealm.getRealm(), is(REALM_NAME));
assertThat(createdRealm.isEnabled(), is(true));

UserRepresentation createdUser = keycloakRepository.getUser(REALM_NAME, "myuser61@mail.de");
assertThat(createdUser.getUsername(), is("myuser61@mail.de"));
assertThat(createdUser.getEmail(), is("myuser61@mail.de"));
assertThat(createdUser.isEnabled(), is(true));
assertThat(createdUser.getFirstName(), is("My firstname"));
assertThat(createdUser.getLastName(), is("My lastname"));

Map<String, List<String>> createdUserAttributes = createdUser.getAttributes();
assertThat(createdUserAttributes, Matchers.nullValue());

doImport("61.2_update_user_set_attributes.json");
UserRepresentation updatedUser = keycloakRepository.getUser(REALM_NAME, "myuser61@mail.de");

Map<String, List<String>> updatedUserAttributes = updatedUser.getAttributes();
assertThat(updatedUserAttributes, Matchers.notNullValue());
assertEquals(1, updatedUserAttributes.size());
assertThat(updatedUserAttributes.get("locale"), contains("de"));
}

private List<GroupRepresentation> getGroupsByUser(UserRepresentation user) {
return keycloakProvider.getInstance().realm(REALM_NAME).users().get(user.getId()).groups();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"enabled": true,
"realm": "realmWithUsers",
"internationalizationEnabled": true,
"users": [
{
"username": "myuser61@mail.de",
"email": "myuser61@mail.de",
"enabled": true,
"firstName": "My firstname",
"lastName": "My lastname"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"enabled": true,
"realm": "realmWithUsers",
"internationalizationEnabled": true,
"users": [
{
"username": "myuser61@mail.de",
"email": "myuser61@mail.de",
"enabled": true,
"firstName": "My firstname",
"lastName": "My lastname",
"attributes": {
"locale": [
"de"
]
}
}
]
}
Loading