Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix to update user groups #1189

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fix to update user groups [#1132](https://github.com/adorsys/keycloak-config-cli/issues/1132)

### Added
- improved logging for realm retrieval errors [#1010](https://github.com/adorsys/keycloak-config-cli/issues/1010)
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ private void handleGroups() {
.toList();

handleGroupsToBeAdded(userGroupsToUpdate, existingUserGroups);
handleGroupsToBeRemoved(userGroupsToUpdate, existingUserGroups);

if (importConfigProperties.getManaged().getGroup()
== ImportConfigProperties.ImportManagedProperties.ImportManagedPropertiesValues.FULL) {
handleGroupsToBeRemoved(userGroupsToUpdate, existingUserGroups);
}
}

private void handleGroupsToBeAdded(
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import.behaviors.sync-user-federation=false
import.behaviors.checksum-with-cache-key=true
import.behaviors.checksum-changed=continue
import.managed.authentication-flow=full
import.managed.group=full
import.managed.group=no-delete
import.managed.required-action=full
import.managed.client-scope=full
import.managed.scope-mapping=full
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@

@Test
@Order(78)
void shouldUpdateRealmDeleteGroup() throws IOException {
void shouldUpdateRealmAddedGroup() throws IOException {
GroupRepresentation updatedGroup = tryToLoadGroup("/My Added Group").get();
assertThat(updatedGroup.getName(), Matchers.is(Matchers.equalTo("My Added Group")));

Expand All @@ -1702,7 +1702,7 @@

assertThat(realm.getRealm(), is(REALM_NAME));

assertThat(tryToLoadGroup("/My Added Group").isPresent(), is(false));
assertThat(tryToLoadGroup("/My Added Group").isPresent(), is(true));
assertThat(tryToLoadGroup("/My Group").isPresent(), is(true));
}

Expand All @@ -1720,7 +1720,7 @@
@Test
@Order(99)
void shouldUpdateRealmUpdateGroupAddManySubGroups() throws IOException {
assumeTrue(VersionUtil.ge(KEYCLOAK_VERSION, "23"));

Check failure on line 1723 in src/test/java/de/adorsys/keycloak/config/service/ImportGroupsIT.java

View workflow job for this annotation

GitHub Actions / build-legacy (19.0.3)

de.adorsys.keycloak.config.service.ImportGroupsIT#shouldUpdateRealmUpdateGroupAddManySubGroups: got: <false>, expected: is <true>

Check failure on line 1723 in src/test/java/de/adorsys/keycloak/config/service/ImportGroupsIT.java

View workflow job for this annotation

GitHub Actions / build (21.1.2)

de.adorsys.keycloak.config.service.ImportGroupsIT#shouldUpdateRealmUpdateGroupAddManySubGroups: got: <false>, expected: is <true>

Check failure on line 1723 in src/test/java/de/adorsys/keycloak/config/service/ImportGroupsIT.java

View workflow job for this annotation

GitHub Actions / build (18.0.2)

de.adorsys.keycloak.config.service.ImportGroupsIT#shouldUpdateRealmUpdateGroupAddManySubGroups: got: <false>, expected: is <true>

Check failure on line 1723 in src/test/java/de/adorsys/keycloak/config/service/ImportGroupsIT.java

View workflow job for this annotation

GitHub Actions / build (22.0.4)

de.adorsys.keycloak.config.service.ImportGroupsIT#shouldUpdateRealmUpdateGroupAddManySubGroups: got: <false>, expected: is <true>

doImport("99_update_realm_update_group_add_many_subgroups.json");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void importFederationChangeUserGroupWithReadonlyProvider() throws IOException {
assertThat(user.getFirstName(), is("James"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(1));
assertThat(userGroups, hasSize(2));

GroupRepresentation group = getGroupsByPath(userGroups, "/realm/group2");
assertThat(group, is(notNullValue()));
Expand All @@ -182,7 +182,7 @@ void importFederationRemoveUserGroupWithReadonlyProvider() throws IOException {
assertThat(user.getFirstName(), is("James"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(0));
assertThat(userGroups, hasSize(2));
}

@Test
Expand All @@ -201,7 +201,7 @@ void importFederationUserChangeAttributeWithReadonlyProvider() throws IOExceptio
assertThat(user.getFirstName(), is("James"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(0));
assertThat(userGroups, hasSize(2));
}

private List<GroupRepresentation> getGroupsByUser(UserRepresentation user) {
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/de/adorsys/keycloak/config/service/ImportUsersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ void shouldUpdateRealmUpdateUserChangeGroup() throws IOException {

@Test
@Order(11)
void shouldUpdateRealmUpdateUserRemoveGroup() throws IOException {
// Create Users
void shouldUpdateRealmUpdateUserKeepExistingGroups() throws IOException {
doImport("11_update_realm_update_user_remove_group.json");

final RealmRepresentation realm = keycloakProvider.getInstance().realm(REALM_NAME).toRepresentation();
Expand All @@ -414,10 +413,10 @@ void shouldUpdateRealmUpdateUserRemoveGroup() throws IOException {
assertThat(user.getFirstName(), is("firstName1"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(1));
assertThat(userGroups, hasSize(2)); // User should still be in both groups

GroupRepresentation group1 = getGroupsByPath(userGroups, "/group1");
assertThat(group1, nullValue());
assertThat(group1.getName(), is("group1"));

GroupRepresentation group2 = getGroupsByPath(userGroups, "/group2");
assertThat(group2.getName(), is("group2"));
Expand All @@ -439,16 +438,17 @@ void shouldUpdateRealmUpdateUserAddSubGroup() throws IOException {
assertThat(user.getFirstName(), is("firstName1"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(1));
assertThat(userGroups, hasSize(3)); // User should now be in all groups

GroupRepresentation group1 = getGroupsByPath(userGroups, "/group1/subgroup1");
assertThat(group1.getName(), is("subgroup1"));
GroupRepresentation group2 = getGroupsByPath(userGroups, "/group2");
assertThat(group2.getName(), is("group2"));
}

@Test
@Order(13)
void shouldUpdateRealmUpdateUserChangeSubGroup() throws IOException {
// Create Users
void shouldUpdateRealmUpdateUserAddNewSubGroup() throws IOException {
doImport("13_update_realm_update_user_change_subgroup.json");

final RealmRepresentation realm = keycloakProvider.getInstance().realm(REALM_NAME).toRepresentation();
Expand All @@ -461,7 +461,7 @@ void shouldUpdateRealmUpdateUserChangeSubGroup() throws IOException {
assertThat(user.getFirstName(), is("firstName1"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(2));
assertThat(userGroups, hasSize(4)); // User should now be in all groups

GroupRepresentation group1 = getGroupsByPath(userGroups, "/group1/subgroup1");
assertThat(group1.getName(), is("subgroup1"));
Expand All @@ -472,7 +472,7 @@ void shouldUpdateRealmUpdateUserChangeSubGroup() throws IOException {

@Test
@Order(14)
void shouldUpdateRealmUpdateUserRemoveSubGroup() throws IOException {
void shouldUpdateRealmUpdateUserKeepExistingSubGroups() throws IOException {
doImport("14_update_realm_update_user_remove_subgroup.json");

final RealmRepresentation realm = keycloakProvider.getInstance().realm(REALM_NAME).toRepresentation();
Expand All @@ -485,10 +485,10 @@ void shouldUpdateRealmUpdateUserRemoveSubGroup() throws IOException {
assertThat(user.getFirstName(), is("firstName1"));

List<GroupRepresentation> userGroups = getGroupsByUser(user);
assertThat(userGroups, hasSize(1));
assertThat(userGroups, hasSize(4));

GroupRepresentation group1 = getGroupsByPath(userGroups, "/group1/subgroup1");
assertThat(group1, nullValue());
assertThat(group1.getName(), is("subgroup1"));

GroupRepresentation group2 = getGroupsByPath(userGroups, "/group2/subgroup2");
assertThat(group2.getName(), is("subgroup2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"firstName": "firstName1",
"lastName": "lastName1",
"groups": [
"group2"
"group2",
"group1"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"firstName": "firstName1",
"lastName": "lastName1",
"groups": [
"/group1/subgroup1"
"/group1/subgroup1",
"/group1",
"/group2"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"lastName": "lastName1",
"groups": [
"/group1/subgroup1",
"/group2/subgroup2"
"/group2/subgroup2",
"/group1",
"/group2"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"firstName": "firstName1",
"lastName": "lastName1",
"groups": [
"/group2/subgroup2"
"/group2/subgroup2",
"/group1/subgroup1"
]
},
{
Expand Down
Loading