students endpoint

This commit is contained in:
Filip Izydorczyk 2020-12-07 22:21:34 +01:00
parent 82200acc06
commit 48394d3583
2 changed files with 47 additions and 5 deletions

View File

@ -36,10 +36,19 @@ public class UsersController {
@GetMapping("/student/search")
@PreAuthorize("hasRole('ROLE_DEANERY')")
@ApiOperation(value = "Serch for user by providing query. If query is empty it will return all students. You need token with DEANERY role to call this")
public ResponseEntity<List<UserResponse>> configApp(
public ResponseEntity<List<UserResponse>> searchForStudent(
@RequestParam("query") @ApiParam(value = "Query to filter all students. If empty will match everyone") String query) {
final List<User> searches = this.userService.searchForStudents(query);
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@GetMapping("/students")
@PreAuthorize("hasRole('ROLE_DEANERY')")
@ApiOperation(value = "Gets all students. You need token with DEANERY role to call this")
public ResponseEntity<List<UserResponse>> getAllStudents() {
final List<User> searches = this.userService.searchForStudents("");
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -22,11 +22,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@ContextConfiguration
public class UsersControllerTest extends AbstractControllerTest {
private static final String ENDPOINT = "/api/v1/users/student/search";
private static final String SEARCH_ENDPOINT = "/api/v1/users/student/search";
private static final String ALL_USERS_ENDPOINT = "/api/v1/users/students";
@Autowired
private UserService service;
/* SEARCH_ENDPOINT */
@Test
public void shouldRestrun200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
@ -34,14 +37,14 @@ public class UsersControllerTest extends AbstractControllerTest {
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
mockMvc.perform(get(SEARCH_ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldRestrunForbiden() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
mockMvc.perform(get(SEARCH_ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
@ -51,7 +54,37 @@ public class UsersControllerTest extends AbstractControllerTest {
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).header("Authorization", "Bearer " + token))
mockMvc.perform(get(SEARCH_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
/* ALL_USERS_ENDPOINT */
@Test
public void shouldRestrunAllStudents200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrunAllStudents200OK.test";
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldRestrunAllStudentsForbiden() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ALL_USERS_ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyAllStudentsTryByStudent() throws Exception {
final String email = "notexistingassignmentuser@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.STUDENT));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
}