Advertisement
bakhridinova

Untitled

May 21st, 2023 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | Software | 0 0
  1. @RestController
  2. @RequiredArgsConstructor
  3. @RequestMapping("/api/certificates")
  4. public class CertificateController {
  5.     private final CertificateService certificateService;
  6.  
  7.     // ...
  8.  
  9.     /**
  10.      * GET endpoint to search for certificates based on search parameters
  11.      *
  12.      * @param page page number requested (default is 0)
  13.      * @param size number of items per page (default is 5)
  14.      * @param searchFilter holding search parameters
  15.      * @return page of certificates based on provided search parameters
  16.      */
  17.     @PostMapping("/search")
  18.     @PreAuthorize("hasAnyRole('USER', 'ADMIN')")
  19.     public Page<CertificateDto> search(@RequestParam(defaultValue = "0") int page,
  20.                                        @RequestParam(defaultValue = "5") int size,
  21.                                        @RequestBody(required = false) SearchFilter searchFilter) {
  22.         return certificateService.findByFilterAndPage(searchFilter, page, size);
  23.     }
  24.  
  25.        // ...
  26. }
  27.  
  28. @WebMvcTest(CertificateController.class)
  29. @ContextConfiguration(classes = AuthenticationManager.class)
  30. class CertificateControllerTest {
  31.     @Autowired
  32.     private MockMvc mockMvc;
  33.  
  34.     @MockBean
  35.     private CertificateService certificateService;
  36.  
  37.     // ...
  38.    
  39.     @Test
  40.     @WithMockUser(roles = "GUEST")
  41.     void searchShouldBeForbiddenForGuest() throws Exception {
  42.         this.mockMvc.perform(post("/api/certificates/search")).andDo(print())
  43.                 .andExpect(isForbidden(true));
  44.     }
  45.  
  46.     @Test
  47.     @WithMockUser(roles = "USER")
  48.     void searchShouldNotBeForbiddenForUser() throws Exception {
  49.         this.mockMvc.perform(post("/api/certificates/search")).andDo(print())
  50.                 .andExpect(isForbidden(false));
  51.     }
  52.  
  53.     @Test
  54.     @WithMockUser(roles = "ADMIN")
  55.     void searchShouldNotBeForbiddenForAdmin() throws Exception {
  56.         this.mockMvc.perform(post("/api/certificates/search")).andDo(print())
  57.                 .andExpect(isForbidden(false));
  58.     }
  59.    
  60.     private ResultMatcher isForbidden(boolean forbidden) {
  61.         if (forbidden) {
  62.             return result -> assertEquals("Status",
  63.                     HttpStatus.FORBIDDEN.value(), result.getResponse().getStatus());
  64.         }
  65.         return result -> assertNotEquals("Status",
  66.                 HttpStatus.FORBIDDEN.value(), result.getResponse().getStatus());
  67.     }
  68.    
  69.     // ...
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement