From 510c9bd856937369456d80948ac9c3aa4f05375f Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Mon, 21 Oct 2024 15:17:19 +0200 Subject: [PATCH] Added server-side pagination API --- client/package.json | 6 +- client/src/tabs/UserRoles.js | 2 +- client/yarn.lock | 57 +++++----- .../main/java/access/api/UserController.java | 18 +-- .../java/access/api/UserRoleController.java | 32 +++++- .../internal/InternalInviteController.java | 2 +- .../access/repository/RoleRepository.java | 29 +++++ .../access/repository/UserRepository.java | 16 ++- .../access/repository/UserRoleRepository.java | 54 +++++++++ .../access/api/UserRoleControllerTest.java | 103 ++++++++++++++++++ .../access/repository/RoleRepositoryTest.java | 30 ++++- .../access/repository/UserRepositoryTest.java | 4 +- .../repository/UserRoleRepositoryTest.java | 56 ++++++++++ server/src/test/resources/queries.sql | 22 ++++ 14 files changed, 378 insertions(+), 53 deletions(-) create mode 100644 server/src/test/java/access/repository/UserRoleRepositoryTest.java create mode 100644 server/src/test/resources/queries.sql diff --git a/client/package.json b/client/package.json index 3f518757..8a7f8aa4 100644 --- a/client/package.json +++ b/client/package.json @@ -3,10 +3,10 @@ "version": "0.1.0", "private": true, "dependencies": { - "@surfnet/sds": "^0.0.113", - "dompurify": "^3.1.6", + "@surfnet/sds": "^0.0.114", + "dompurify": "^3.1.7", "i18n-js": "^4.4.3", - "isomorphic-dompurify": "^2.15.0", + "isomorphic-dompurify": "^2.16.0", "js-cookie": "^3.0.5", "lodash.debounce": "^4.0.8", "luxon": "^3.5.0", diff --git a/client/src/tabs/UserRoles.js b/client/src/tabs/UserRoles.js index 675ae715..66a5c7ba 100644 --- a/client/src/tabs/UserRoles.js +++ b/client/src/tabs/UserRoles.js @@ -185,7 +185,7 @@ export const UserRoles = ({role, guests, userRoles}) => { const displayEndDate = userRole => { const allowed = allowedToRenewUserRole(user, userRole, false); - if (allowed && userRole.authority !== AUTHORITIES.GUEST) { + if (allowed && !guests) { return ( > search(@RequestParam(value = "query") String q } @GetMapping("search-paginated") - public ResponseEntity> searchPaginated(@RequestParam(value = "query") String query, - @RequestParam(value = "pageNumber", required = false, defaultValue = "0") int pageNumber, - @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize, - @RequestParam(value = "sort", required = false, defaultValue = "id") String sort, - @RequestParam(value = "sortDirection", required = false, defaultValue = "ASC") String sortDirection, - @Parameter(hidden = true) User user) { + public ResponseEntity> searchPaginated(@RequestParam(value = "query", required = false, defaultValue = "") String query, + @RequestParam(value = "pageNumber", required = false, defaultValue = "0") int pageNumber, + @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize, + @RequestParam(value = "sort", required = false, defaultValue = "id") String sort, + @RequestParam(value = "sortDirection", required = false, defaultValue = "ASC") String sortDirection, + @Parameter(hidden = true) User user) { LOG.debug(String.format("/search-paginated for user %s", user.getEduPersonPrincipalName())); UserPermissions.assertSuperUser(user); if (query.equals("owl")) { @@ -157,8 +157,10 @@ public ResponseEntity> searchPaginated(@RequestParam(value = "query") return ResponseEntity.ok(new PageImpl<>(content, pageRequest, content.size())); } Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.fromString(sortDirection), sort)); - Page users = userRepository.searchByPage(query.replaceAll("@", " ") + "*", pageable); - return ResponseEntity.ok(users); + Page> page = StringUtils.hasText(query) ? + userRepository.searchByPage(pageable) : + userRepository.searchByPageWithKeyword(query.replaceAll("@", " ") + "*", pageable) ; + return ResponseEntity.ok(page); } @GetMapping("search-by-application") diff --git a/server/src/main/java/access/api/UserRoleController.java b/server/src/main/java/access/api/UserRoleController.java index 97b0cc2f..f7fa90f2 100644 --- a/server/src/main/java/access/api/UserRoleController.java +++ b/server/src/main/java/access/api/UserRoleController.java @@ -19,6 +19,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; @@ -91,7 +95,34 @@ public ResponseEntity>> consequencesDeleteRole(@PathVar return ResponseEntity.ok(res); } + @GetMapping("/search/{roleId}/{guests}") + public ResponseEntity> searchPaginated(@PathVariable("roleId") Long roleId, + @PathVariable("guests") boolean guests, + @RequestParam(value = "query", required = false, defaultValue = "") String query, + @RequestParam(value = "pageNumber", required = false, defaultValue = "0") int pageNumber, + @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize, + @RequestParam(value = "sort", required = false, defaultValue = "id") String sort, + @RequestParam(value = "sortDirection", required = false, defaultValue = "ASC") String sortDirection, + @Parameter(hidden = true) User user) { + LOG.debug(String.format("/search for user %s", user.getEduPersonPrincipalName())); + Role role = roleRepository.findById(roleId).orElseThrow(() -> new NotFoundException("Role not found")); + + UserPermissions.assertRoleAccess(user, role, Authority.INVITER); + + Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.fromString(sortDirection), sort)); + Page> page; + if (StringUtils.hasText(query)) { + page = guests ? + userRoleRepository.searchGuestsByPageWithKeyword(roleId, query, pageable) : + userRoleRepository.searchNonGuestsByPageWithKeyword(roleId, query, pageable); + } else { + page = guests ? + userRoleRepository.searchGuestsByPage(roleId, pageable) : + userRoleRepository.searchNonGuestsByPage(roleId, pageable); + } + return ResponseEntity.ok(page); + } @PostMapping("user_role_provisioning") @Operation(summary = "Add Role to a User", description = "Provision the User if the User is unknown and add the Role(s)") @@ -176,5 +207,4 @@ public ResponseEntity deleteUserRole(@PathVariable("id") Long id, return Results.deleteResult(); } - } diff --git a/server/src/main/java/access/internal/InternalInviteController.java b/server/src/main/java/access/internal/InternalInviteController.java index d9fbefdd..a9cd9dde 100644 --- a/server/src/main/java/access/internal/InternalInviteController.java +++ b/server/src/main/java/access/internal/InternalInviteController.java @@ -255,7 +255,7 @@ public ResponseEntity deleteRole(@PathVariable("id") Long id, @PostMapping("/invitations") @PreAuthorize("hasRole('SP_DASHBOARD')") @Operation(summary = "Invite member for existing Role", - description = "Invite a member for an existing role. An invitation email will be send", + description = "Invite a member for an existing role. An invitation email will be sent. Do not forget to set to ", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( useParameterTypeSchema = true, content = {@Content(examples = {@ExampleObject(value = """ diff --git a/server/src/main/java/access/repository/RoleRepository.java b/server/src/main/java/access/repository/RoleRepository.java index c5e5e77a..b49aa76b 100644 --- a/server/src/main/java/access/repository/RoleRepository.java +++ b/server/src/main/java/access/repository/RoleRepository.java @@ -1,11 +1,14 @@ package access.repository; import access.model.Role; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Map; import java.util.Optional; @Repository @@ -16,6 +19,32 @@ public interface RoleRepository extends JpaRepository { nativeQuery = true) List search(String keyWord, int limit); + + @Query(value = """ + SELECT r.id, r.name, r.description, a.manage_id, a.manage_type, + (SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount + FROM roles r INNER JOIN application_usages au on au.role_id = r.id + INNER JOIN applications a on au.application_id = a.id + """, + countQuery = """ + SELECT COUNT(r.id) FROM roles r + """, + nativeQuery = true) + Page> searchByPage(Pageable pageable); + + @Query(value = """ + SELECT r.id, r.name, r.description, a.manage_id, a.manage_type, + (SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount + FROM roles r INNER JOIN application_usages au on au.role_id = r.id + INNER JOIN applications a on au.application_id = a.id + WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE) + """, + countQuery = """ + SELECT COUNT(r.id) FROM roles r WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE) + """, + nativeQuery = true) + Page> searchByPageWithKeyword(String keyword, Pageable pageable); + List findByApplicationUsagesApplicationManageId(String manageId); List findByOrganizationGUID(String organizationGUID); diff --git a/server/src/main/java/access/repository/UserRepository.java b/server/src/main/java/access/repository/UserRepository.java index d553fc06..c97dd8c9 100644 --- a/server/src/main/java/access/repository/UserRepository.java +++ b/server/src/main/java/access/repository/UserRepository.java @@ -45,10 +45,22 @@ public interface UserRepository extends JpaRepository { nativeQuery = true) List> searchByApplication(List manageIdentifiers, String keyWord, int limit); - @Query(value = "SELECT * FROM users WHERE MATCH (given_name, family_name, email) against (?1 IN BOOLEAN MODE)", + @Query(value = """ + select u.name, u.email, u.schac_home_organization, u.sub, u.super_user, u.institution_admin, + (SELECT GROUP_CONCAT(DISTINCT ur.authority) FROM user_roles ur WHERE ur.user_id = u.id) AS authority from users u + """, + countQuery = "SELECT count(*) FROM users", + nativeQuery = true) + Page> searchByPage(Pageable pageable ); + + @Query(value = """ + select u.name, u.email, u.schac_home_organization, u.sub, u.super_user, u.institution_admin, + (SELECT GROUP_CONCAT(DISTINCT ur.authority) FROM user_roles ur WHERE ur.user_id = u.id) AS authority + from users u WHERE MATCH (given_name, family_name, email) against (?1 IN BOOLEAN MODE) + """, countQuery = "SELECT count(*) FROM users WHERE MATCH (given_name, family_name, email) against (?1 IN BOOLEAN MODE)", nativeQuery = true) - Page searchByPage(String keyWord, Pageable pageable ); + Page> searchByPageWithKeyword(String keyWord, Pageable pageable ); @Query(value = "SELECT u.id, u.email, u.name, u.schac_home_organization, u.created_at, u.last_activity, " + "ur.authority, r.name AS role_name, r.id AS role_id, ur.end_date " + diff --git a/server/src/main/java/access/repository/UserRoleRepository.java b/server/src/main/java/access/repository/UserRoleRepository.java index 3227a843..dfeed2b1 100644 --- a/server/src/main/java/access/repository/UserRoleRepository.java +++ b/server/src/main/java/access/repository/UserRoleRepository.java @@ -2,6 +2,8 @@ import access.model.Role; import access.model.UserRole; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -11,6 +13,7 @@ import java.time.Instant; import java.util.List; +import java.util.Map; @Repository public interface UserRoleRepository extends JpaRepository { @@ -32,4 +35,55 @@ public interface UserRoleRepository extends JpaRepository { @Query(value = "UPDATE user_roles SET expiry_notifications= ?1 WHERE id = ?2", nativeQuery = true) @Transactional(isolation = Isolation.SERIALIZABLE) void updateExpiryNotifications(Integer expiryNotifications, Long id); + + + @Query(value = """ + SELECT ur.authority, ur.end_date, ur.created_at, u.name, u.email, u.schac_home_organization + FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND (ur.authority = 'GUEST' OR ur.guest_role_included ) + """, + countQuery = """ + SELECT COUNT(ur.id) FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND (ur.authority = 'GUEST' OR ur.guest_role_included ) + """, + nativeQuery = true) + Page> searchGuestsByPage(Long roleId, Pageable pageable); + + @Query(value = """ + SELECT ur.authority, ur.end_date, ur.created_at, u.name, u.email, u.schac_home_organization + FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND (ur.authority = 'GUEST' OR ur.guest_role_included ) + AND MATCH (u.given_name, u.family_name, u.email) AGAINST (?2 IN BOOLEAN MODE) + """, + countQuery = """ + SELECT COUNT(ur.id) FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND (ur.authority = 'GUEST' OR ur.guest_role_included ) + AND MATCH (u.given_name, u.family_name, u.email) AGAINST (?2 IN BOOLEAN MODE) + """, + nativeQuery = true) + Page> searchGuestsByPageWithKeyword(Long roleId, String keyWord, Pageable pageable); + + @Query(value = """ + SELECT ur.authority, ur.end_date, ur.created_at, u.name, u.email, u.schac_home_organization + FROM user_roles ur INNER JOIN users u on u.id = ur.user_id WHERE ur.role_id = ?1 AND ur.authority <> 'GUEST' + """, + countQuery = """ + SELECT COUNT(ur.id) FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND ur.authority <> 'GUEST' + """, + nativeQuery = true) + Page> searchNonGuestsByPage(Long roleId, Pageable pageable); + + @Query(value = """ + SELECT ur.authority, ur.end_date, ur.created_at, u.name, u.email, u.schac_home_organization + FROM user_roles ur INNER JOIN users u on u.id = ur.user_id WHERE ur.role_id = ?1 + AND ur.authority <> 'GUEST' AND MATCH (u.given_name, u.family_name, u.email) AGAINST (?2 IN BOOLEAN MODE) + """, + countQuery = """ + SELECT COUNT(ur.id) FROM user_roles ur INNER JOIN users u on u.id = ur.user_id + WHERE ur.role_id = ?1 AND ur.authority <> 'GUEST' + AND MATCH (u.given_name, u.family_name, u.email) AGAINST (?2 IN BOOLEAN MODE) + """, + nativeQuery = true) + Page> searchNonGuestsByPageWithKeyword(Long roleId, String keyWord, Pageable pageable); } diff --git a/server/src/test/java/access/api/UserRoleControllerTest.java b/server/src/test/java/access/api/UserRoleControllerTest.java index 0b57dfee..d3d755e9 100644 --- a/server/src/test/java/access/api/UserRoleControllerTest.java +++ b/server/src/test/java/access/api/UserRoleControllerTest.java @@ -9,6 +9,7 @@ import io.restassured.common.mapper.TypeRef; import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import java.time.Instant; @@ -44,6 +45,108 @@ void byRole() throws Exception { assertNotNull(userRoles.get(0).getUserInfo().get("name")); } + @Test + void searchGuestsByPage() throws Exception { + AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", INVITER_WIKI_SUB); + + Role role = roleRepository.findByName("Wiki").get(); + Map userRoles = given() + .when() + .filter(accessCookieFilter.cookieFilter()) + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .queryParams(Map.of( + "pageNumber", 0, + "pageSize", 1, + "sort", "end_date", + "sortDirection", Sort.Direction.DESC + )) + .pathParams("roleId", role.getId()) + .pathParams("guests", true) + .get("/api/v1/user_roles/search/{roleId}/{guests}") + .as(new TypeRef<>() { + }); + assertEquals(2, userRoles.get("totalElements")); + assertEquals(1, ((List) userRoles.get("content")).size()); + } + + @Test + void searchNonGuestsByPage() throws Exception { + AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", INVITER_WIKI_SUB); + + Role role = roleRepository.findByName("Wiki").get(); + Map userRoles = given() + .when() + .filter(accessCookieFilter.cookieFilter()) + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .queryParams(Map.of( + "pageNumber", 0, + "pageSize", 1, + "sort", "end_date", + "sortDirection", Sort.Direction.DESC + )) + .pathParams("roleId", role.getId()) + .pathParams("guests", false) + .get("/api/v1/user_roles/search/{roleId}/{guests}") + .as(new TypeRef<>() { + }); + assertEquals(2, userRoles.get("totalElements")); + assertEquals(1, ((List) userRoles.get("content")).size()); + } + + @Test + void searchGuestsByPageWithKeyword() throws Exception { + AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", INVITER_WIKI_SUB); + + Role role = roleRepository.findByName("Wiki").get(); + Map userRoles = given() + .when() + .filter(accessCookieFilter.cookieFilter()) + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .queryParams(Map.of( + "query", "mary", + "pageNumber", 0, + "pageSize", 1, + "sort", "end_date", + "sortDirection", Sort.Direction.DESC + )) + .pathParams("roleId", role.getId()) + .pathParams("guests", true) + .get("/api/v1/user_roles/search/{roleId}/{guests}") + .as(new TypeRef<>() { + }); + assertEquals(1, userRoles.get("totalElements")); + assertEquals(1, ((List) userRoles.get("content")).size()); + } + + @Test + void searchNonGuestsByPageWithKeyword() throws Exception { + AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", INVITER_WIKI_SUB); + + Role role = roleRepository.findByName("Wiki").get(); + Map userRoles = given() + .when() + .filter(accessCookieFilter.cookieFilter()) + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .queryParams(Map.of( + "query", "doe", + "pageNumber", 0, + "pageSize", 1, + "sort", "end_date", + "sortDirection", Sort.Direction.DESC + )) + .pathParams("roleId", role.getId()) + .pathParams("guests", false) + .get("/api/v1/user_roles/search/{roleId}/{guests}") + .as(new TypeRef<>() { + }); + assertEquals(2, userRoles.get("totalElements")); + assertEquals(1, ((List) userRoles.get("content")).size()); + } + @Test void byRoleNotFound() throws Exception { AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", MANAGE_SUB); diff --git a/server/src/test/java/access/repository/RoleRepositoryTest.java b/server/src/test/java/access/repository/RoleRepositoryTest.java index a89becc5..f2ed6dac 100644 --- a/server/src/test/java/access/repository/RoleRepositoryTest.java +++ b/server/src/test/java/access/repository/RoleRepositoryTest.java @@ -3,8 +3,12 @@ import access.AbstractTest; import access.model.Role; import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -17,10 +21,28 @@ void search() { } @Test - void findByOrganizationGUID_ApplicationUsagesApplicationManageId() { - //mysql> select r.id, r.name,r.organization_guid, a.manage_id, a.manage_type from roles r - // inner join application_usages au on au.role_id = r.id - // inner join applications a on a.id = au.application_id; + void searchByPage() { + PageRequest pageRequest = PageRequest.of(3, 1, Sort.by(Sort.Direction.DESC, "name")); + Page> page = roleRepository.searchByPage(pageRequest); + assertEquals(6L, page.getTotalElements()); + assertEquals(1, page.getContent().size()); + assertEquals("Research", page.getContent().get(0).get("name")); } + @Test + void searchByPageWithKeyword() { + PageRequest pageRequest = PageRequest.of(1, 3, Sort.by(Sort.Direction.DESC, "name")); + Page> page = roleRepository.searchByPageWithKeyword("desc*", pageRequest); + assertEquals(6L, page.getTotalElements()); + assertEquals(3, page.getContent().size()); + } + + @Test + void searchByPageWiki() { + PageRequest pageRequest = PageRequest.of(0, 15, Sort.by(Sort.Direction.ASC, "description")); + Page> page = roleRepository.searchByPageWithKeyword("wiki*", pageRequest); + assertEquals(1L, page.getTotalElements()); + assertEquals(1, page.getContent().size()); + assertEquals(3L, page.getContent().get(0).get("userRoleCount")); + } } \ No newline at end of file diff --git a/server/src/test/java/access/repository/UserRepositoryTest.java b/server/src/test/java/access/repository/UserRepositoryTest.java index e4a99466..8f204176 100644 --- a/server/src/test/java/access/repository/UserRepositoryTest.java +++ b/server/src/test/java/access/repository/UserRepositoryTest.java @@ -42,7 +42,7 @@ void search() { @Test void searchByPage() { PageRequest pageRequest = PageRequest.of(1, 3, Sort.by(Sort.Direction.DESC, "given_name")); - Page users = userRepository.searchByPage("exam*", pageRequest); + Page> users = userRepository.searchByPageWithKeyword("exam*", pageRequest); assertEquals(6L, users.getTotalElements()); assertEquals(3, users.getContent().size()); } @@ -50,7 +50,7 @@ void searchByPage() { @Test void searchByPageSingleResult() { PageRequest pageRequest = PageRequest.of(0, 3, Sort.by(Sort.Direction.DESC, "given_name")); - Page users = userRepository.searchByPage("mary*", pageRequest); + Page> users = userRepository.searchByPageWithKeyword("mary*", pageRequest); assertEquals(1L, users.getTotalElements()); assertEquals(1, users.getContent().size()); } diff --git a/server/src/test/java/access/repository/UserRoleRepositoryTest.java b/server/src/test/java/access/repository/UserRoleRepositoryTest.java new file mode 100644 index 00000000..f1c735e3 --- /dev/null +++ b/server/src/test/java/access/repository/UserRoleRepositoryTest.java @@ -0,0 +1,56 @@ +package access.repository; + +import access.AbstractTest; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class UserRoleRepositoryTest extends AbstractTest { + + @SneakyThrows + @Test + void searchGuestsByPage() { + PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "authority")); + Long roleID = roleRepository.findByName("Wiki").get().getId(); + Page> userRoles = userRoleRepository.searchGuestsByPage(roleID, pageRequest); + assertEquals(2L, userRoles.getTotalElements()); + assertEquals(1, userRoles.getContent().size()); + } + + @SneakyThrows + @Test + void searchGuestsByPageWithKeyword() { + PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "u.email")); + Long roleID = roleRepository.findByName("Wiki").get().getId(); + Page> userRoles = userRoleRepository.searchGuestsByPageWithKeyword(roleID, "doe*", pageRequest); + assertEquals(2L, userRoles.getTotalElements()); + assertEquals(1, userRoles.getContent().size()); + } + + @SneakyThrows + @Test + void searchNonGuestsByPage() { + PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "authority")); + Long roleID = roleRepository.findByName("Wiki").get().getId(); + Page> userRoles = userRoleRepository.searchNonGuestsByPage(roleID, pageRequest); + assertEquals(2L, userRoles.getTotalElements()); + assertEquals(1, userRoles.getContent().size()); + } + + @SneakyThrows + @Test + void searchNonGuestsByPageWithKeyword() { + PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "u.email")); + Long roleID = roleRepository.findByName("Wiki").get().getId(); + Page> userRoles = userRoleRepository.searchNonGuestsByPageWithKeyword(roleID, "doe*", pageRequest); + assertEquals(2L, userRoles.getTotalElements()); + assertEquals(1, userRoles.getContent().size()); + } +} \ No newline at end of file diff --git a/server/src/test/resources/queries.sql b/server/src/test/resources/queries.sql new file mode 100644 index 00000000..cd8d3007 --- /dev/null +++ b/server/src/test/resources/queries.sql @@ -0,0 +1,22 @@ +--Users +select u.name, u.email, u.schac_home_organization, u.sub, u.super_user, u.institution_admin, + (SELECT ur.authority FROM user_roles ur WHERE ur.user_id = u.id LIMIT 1) AS authority +from users u +where MATCH (u.given_name, u.family_name, u.email) AGAINST ('doe' IN BOOLEAN MODE) order by u.name; + +---Roles +SELECT r.id, r.name, r.description, (SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount, + a.manage_id, a.manage_type +FROM roles r + INNER JOIN application_usages au on au.role_id = r.id + INNER JOIN applications a on au.application_id = a.id +WHERE MATCH (name, description) against ("de*" IN BOOLEAN MODE) ORDER BY userRoleCount DESC LIMIT 5; + +--UserRoles +select ur.authority, ur.end_date, ur.created_at, u.name, u.email, u.schac_home_organization FROM user_roles ur + INNER JOIN users u on u.id = ur.user_id WHERE + (ur.authority = 'GUEST' OR ur.guest_role_included ) AND + ur.role_id = 43229 AND + MATCH (u.given_name, u.family_name, u.email) AGAINST ('d*' IN BOOLEAN MODE) ; + +