-
Notifications
You must be signed in to change notification settings - Fork 90
fix: filter user groups by env scope in query parameter #13890
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
Merged
vikrantgravitee
merged 1 commit into
4.8.x
from
APIM-10981-fix-filter-user-groups-by-environment
Nov 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...nt-rest/src/test/java/io/gravitee/rest/api/management/rest/resource/UserResourceTest.java
phiz71 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.gravitee.rest.api.management.rest.resource; | ||
|
|
||
| import static org.junit.Assert.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import io.gravitee.common.http.HttpStatusCode; | ||
| import io.gravitee.rest.api.model.*; | ||
| import jakarta.ws.rs.core.GenericType; | ||
| import jakarta.ws.rs.core.Response; | ||
| import java.util.*; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| /** | ||
| * Test for UserResource.getUserGroups() method | ||
| */ | ||
| public class UserResourceTest extends AbstractResourceTest { | ||
|
|
||
| private static final String USER_ID = "test-user"; | ||
| private static final String ENV_ID = "test-env"; | ||
|
|
||
| @Before | ||
| public void resetMocks() { | ||
| Mockito.reset(groupService, membershipService); | ||
| } | ||
|
|
||
| @Override | ||
| protected String contextPath() { | ||
| // Matches the REST path you’re testing, e.g. /users/{userId}/groups?environmentId=ENV_ID | ||
| return "users/" + USER_ID + "/groups/"; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnAllGroupsIncludingEnvGroups() { | ||
| GroupEntity group1 = new GroupEntity(); | ||
| group1.setId("group1"); | ||
| group1.setName("Global Group"); | ||
| GroupEntity group2 = new GroupEntity(); | ||
| group2.setId("group2"); | ||
| group2.setName("Env Group"); | ||
|
|
||
| when(groupService.findByUserAndEnvironment(USER_ID, null)).thenReturn(Set.of(group1, group2)); | ||
|
|
||
| final Response response = orgTarget().request().get(); | ||
| assertEquals(HttpStatusCode.OK_200, response.getStatus()); | ||
|
|
||
| List<UserGroupEntity> result = response.readEntity(new GenericType<>() {}); | ||
| assertNotNull(result); | ||
| assertEquals(2, result.size()); | ||
| assertTrue(result.stream().map(UserGroupEntity::getId).toList().containsAll(List.of("group1", "group2"))); | ||
|
|
||
| verify(groupService, atLeastOnce()).findByUserAndEnvironment(USER_ID, null); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnGroupsByEnvironment() { | ||
| GroupEntity group = new GroupEntity(); | ||
| group.setId("env-group"); | ||
| group.setName("Env Group"); | ||
| when(groupService.findByUserAndEnvironment(USER_ID, ENV_ID)).thenReturn(Set.of(group)); | ||
|
|
||
| final Response response = orgTarget().queryParam("environmentId", ENV_ID).request().get(); | ||
| assertEquals(HttpStatusCode.OK_200, response.getStatus()); | ||
|
|
||
| List<UserGroupEntity> result = response.readEntity(new GenericType<>() {}); | ||
| assertNotNull(result); | ||
| assertEquals(1, result.size()); | ||
| assertEquals("env-group", result.get(0).getId()); | ||
| assertEquals("Env Group", result.get(0).getName()); | ||
|
|
||
| verify(groupService, atLeastOnce()).findByUserAndEnvironment(USER_ID, ENV_ID); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnEmptyListWhenNoGroups() { | ||
| when(groupService.findByUserAndEnvironment(USER_ID, null)).thenReturn(Collections.emptySet()); | ||
|
|
||
| final Response response = orgTarget().request().get(); | ||
| assertEquals(HttpStatusCode.OK_200, response.getStatus()); | ||
|
|
||
| List<UserGroupEntity> result = response.readEntity(new GenericType<>() {}); | ||
| assertNotNull(result); | ||
| assertTrue(result.isEmpty()); | ||
|
|
||
| verify(groupService, atLeastOnce()).findByUserAndEnvironment(USER_ID, null); | ||
| verify(membershipService, never()).getRoles(any(), any(), any(), any()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...est/java/io/gravitee/rest/api/service/impl/GroupService_FindByUserAndEnvironmentTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.gravitee.rest.api.service.impl; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.gravitee.repository.exceptions.TechnicalException; | ||
| import io.gravitee.repository.management.api.GroupRepository; | ||
| import io.gravitee.repository.management.model.Group; | ||
| import io.gravitee.rest.api.model.GroupEntity; | ||
| import io.gravitee.rest.api.model.MembershipEntity; | ||
| import io.gravitee.rest.api.model.MembershipMemberType; | ||
| import io.gravitee.rest.api.model.MembershipReferenceType; | ||
| import io.gravitee.rest.api.service.MembershipService; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class GroupService_FindByUserAndEnvironmentTest { | ||
|
|
||
| @InjectMocks | ||
| private final GroupServiceImpl groupService = new GroupServiceImpl(); | ||
vikrantgravitee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Mock | ||
| private GroupRepository groupRepository; | ||
|
|
||
| @Mock | ||
| private MembershipService membershipService; | ||
|
|
||
| @Test | ||
| public void shouldReturnGroupsForUserInEnvironment() throws TechnicalException { | ||
| String userId = "user1"; | ||
| String envId = "env1"; | ||
| Group group1 = Group.builder().id("g1").environmentId(envId).name("Group1").build(); | ||
| Group group2 = Group.builder().id("g2").environmentId("env2").name("Group2").build(); | ||
| Group group3 = Group.builder().id("g3").environmentId(envId).name("Group3").build(); | ||
| Set<String> userGroups = new HashSet<>(Arrays.asList("g1", "g2", "g3")); | ||
| when( | ||
| membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.GROUP) | ||
| ).thenReturn( | ||
| new HashSet<>( | ||
| Arrays.asList( | ||
| MembershipEntity.builder().id("m1").referenceId("g1").build(), | ||
| MembershipEntity.builder().id("m2").referenceId("g2").build(), | ||
| MembershipEntity.builder().id("m3").referenceId("g3").build() | ||
| ) | ||
| ) | ||
| ); | ||
| when(groupRepository.findByIds(userGroups)).thenReturn(Set.of(group1, group2, group3)); | ||
| Set<GroupEntity> result = groupService.findByUserAndEnvironment(userId, envId); | ||
| assertThat(result).hasSize(2); | ||
| assertThat(result.stream().map(GroupEntity::getName)).containsExactlyInAnyOrder("Group1", "Group3"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnEmptyForUserWithNoGroupsInEnv() throws TechnicalException { | ||
| String userId = "user2"; | ||
| String envId = "env1"; | ||
| Group group1 = Group.builder().id("g1").environmentId("env2").name("Group2").build(); | ||
| Set<String> userGroups = Collections.singleton("g1"); | ||
| when( | ||
| membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.GROUP) | ||
| ).thenReturn(new HashSet<>(Collections.singletonList(MembershipEntity.builder().id("m4").referenceId("g1").build()))); | ||
| when(groupRepository.findByIds(userGroups)).thenReturn(Set.of(group1)); | ||
| Set<GroupEntity> result = groupService.findByUserAndEnvironment(userId, envId); | ||
| assertThat(result).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnEmptyForNonExistentUserOrEnv() throws TechnicalException { | ||
| String userId = "nouser"; | ||
| String envId = "noenv"; | ||
| when( | ||
| membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.GROUP) | ||
| ).thenReturn(new HashSet<>()); | ||
| when(groupRepository.findByIds(Collections.emptySet())).thenReturn(Set.of()); | ||
| Set<GroupEntity> result = groupService.findByUserAndEnvironment(userId, envId); | ||
| assertThat(result).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnAllGroupsIfEnvIdIsNull() throws TechnicalException { | ||
| String userId = "user3"; | ||
| Group group1 = Group.builder().id("g1").environmentId("env1").name("Group1").build(); | ||
| Group group2 = Group.builder().id("g2").environmentId("env2").name("Group2").build(); | ||
| Set<String> userGroups = new HashSet<>(Arrays.asList("g1", "g2")); | ||
| when( | ||
| membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userId, MembershipReferenceType.GROUP) | ||
| ).thenReturn( | ||
| new HashSet<>( | ||
| Arrays.asList( | ||
| MembershipEntity.builder().id("m5").referenceId("g1").build(), | ||
| MembershipEntity.builder().id("m6").referenceId("g2").build() | ||
| ) | ||
| ) | ||
| ); | ||
| when(groupRepository.findByIds(userGroups)).thenReturn(Set.of(group1, group2)); | ||
| Set<GroupEntity> result = groupService.findByUserAndEnvironment(userId, null); | ||
| assertThat(result).hasSize(2); | ||
| assertThat(result.stream().map(GroupEntity::getName)).containsExactlyInAnyOrder("Group1", "Group2"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.