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

Generic filter parameter for the REST findAll interfaces #727

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@
<version>${geojson-jackson.version}</version>
</dependency>

<!-- JsonPath -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
</dependency>

<!-- Keycloak -->
<dependency>
<groupId>org.keycloak</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
package de.terrestris.shogun.boot.controller;

import de.terrestris.shogun.lib.controller.ApplicationController;
import de.terrestris.shogun.lib.enumeration.PermissionCollectionType;
import de.terrestris.shogun.lib.model.Application;
import de.terrestris.shogun.lib.repository.ApplicationRepository;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

public class ApplicationControllerTest extends BaseControllerTest<ApplicationController, ApplicationRepository, Application> {

public void setBaseEntity() {
Expand Down Expand Up @@ -53,4 +62,56 @@ public void insertTestData() {
testData = persistedEntities;
}

@Test
public void findAll_shouldReturnFilteredEntitiesForAdminUsers() throws Exception {
this.mockMvc
.perform(
MockMvcRequestBuilders
.get(basePath + "?filter=$.name == \"Application 1\"")
.with(authentication(getMockAuthentication(this.adminUser)))
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isMap())
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content", hasSize(1)));
}

@Test
public void findAll_shouldReturnFilteredEntitiesForUsersByUserInstancePermission() throws Exception {

userInstancePermissionService.setPermission(testData.get(0), this.user, PermissionCollectionType.READ);
userInstancePermissionService.setPermission(testData.get(1), this.user, PermissionCollectionType.READ);

this.mockMvc
.perform(
MockMvcRequestBuilders
.get(basePath + "?filter=$.name == \"Application 1\"")
.with(authentication(getMockAuthentication(this.user)))
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isMap())
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content", hasSize(1)));
}

@Test
public void findAll_shouldReturnFilteredEntitiesForUsersByGroupInstancePermission() throws Exception {

groupInstancePermissionService.setPermission(testData.get(0), this.group, PermissionCollectionType.READ);
groupInstancePermissionService.setPermission(testData.get(1), this.group, PermissionCollectionType.READ);

this.mockMvc
.perform(
MockMvcRequestBuilders
.get(basePath + "?filter=$.name == \"Application 1\"")
.with(authentication(getMockAuthentication(this.user)))
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isMap())
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content", hasSize(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@
import de.terrestris.shogun.lib.controller.BaseController;
import de.terrestris.shogun.lib.enumeration.PermissionCollectionType;
import de.terrestris.shogun.lib.model.BaseEntity;
import de.terrestris.shogun.lib.model.Group;
import de.terrestris.shogun.lib.model.User;
import de.terrestris.shogun.lib.repository.BaseCrudRepository;
import de.terrestris.shogun.lib.repository.GroupRepository;
import de.terrestris.shogun.lib.repository.UserRepository;
import de.terrestris.shogun.lib.repository.security.permission.GroupClassPermissionRepository;
import de.terrestris.shogun.lib.repository.security.permission.GroupInstancePermissionRepository;
import de.terrestris.shogun.lib.repository.security.permission.UserClassPermissionRepository;
import de.terrestris.shogun.lib.repository.security.permission.UserInstancePermissionRepository;
import de.terrestris.shogun.lib.service.security.permission.GroupInstancePermissionService;
import de.terrestris.shogun.lib.service.security.permission.UserClassPermissionService;
import de.terrestris.shogun.lib.service.security.permission.UserInstancePermissionService;
import de.terrestris.shogun.lib.service.security.provider.GroupProviderService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.IDToken;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.servlet.server.Encoding;
import org.springframework.http.MediaType;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -57,6 +65,7 @@
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
Expand All @@ -79,15 +88,27 @@ public abstract class BaseControllerTest<U extends BaseController, R extends Bas
@Autowired
protected UserRepository userRepository;

@Autowired
protected GroupRepository groupRepository;

@Autowired
protected UserInstancePermissionRepository userInstancePermissionRepository;

@Autowired
protected UserClassPermissionRepository userClassPermissionRepository;

@Autowired
protected GroupInstancePermissionRepository groupInstancePermissionRepository;

@Autowired
protected GroupClassPermissionRepository groupClassPermissionRepository;

@Autowired
protected UserInstancePermissionService userInstancePermissionService;

@Autowired
protected GroupInstancePermissionService groupInstancePermissionService;

@Autowired
protected UserClassPermissionService userClassPermissionService;

Expand All @@ -97,6 +118,9 @@ public abstract class BaseControllerTest<U extends BaseController, R extends Bas
@Autowired
protected ObjectMapper objectMapper;

@MockBean
private GroupProviderService groupProviderService;

protected Class<S> entityClass;

protected String basePath;
Expand All @@ -109,6 +133,8 @@ public abstract class BaseControllerTest<U extends BaseController, R extends Bas

protected User user;

protected Group group;

public void initMockMvc() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(context)
Expand Down Expand Up @@ -149,9 +175,25 @@ public void initUser() {
this.user = userRepository.save(user);
}

public void initGroup() {
Group<GroupRepresentation> group = new Group();
String authProviderId = "c886684b-1a22-4443-b0d8-36c006c19f08";
group.setAuthProviderId(authProviderId);
GroupRepresentation groupRepresentation = new GroupRepresentation();
groupRepresentation.setName("SHOGun Group");
group.setProviderDetails(groupRepresentation);

this.group = groupRepository.save(group);

when(groupProviderService.getGroupsForUser()).thenReturn(List.of(this.group));
when(groupProviderService.findByUser(this.user)).thenReturn(List.of(this.group));
}

public void cleanupPermissions() {
userInstancePermissionRepository.deleteAll();
groupInstancePermissionRepository.deleteAll();
userClassPermissionRepository.deleteAll();
groupClassPermissionRepository.deleteAll();
}

public void deinitAdminUser() {
Expand All @@ -162,6 +204,10 @@ public void deinitUser() {
userRepository.delete(this.user);
}

public void deinitGroup() {
groupRepository.delete(this.group);
}

public void cleanupTestData() {
repository.deleteAll();
}
Expand Down Expand Up @@ -192,6 +238,7 @@ public void setUp() {
initMockMvc();
initAdminUser();
initUser();
initGroup();
setBaseEntity();
setBasePath();
insertTestData();
Expand All @@ -202,6 +249,7 @@ public void teardown() {
cleanupPermissions();
deinitUser();
deinitAdminUser();
deinitGroup();
cleanupTestData();
}

Expand Down
Loading
Loading