New endpints tests

This commit is contained in:
Filip Izydorczyk 2020-12-10 16:50:47 +01:00
parent 831358128e
commit a6acab932c
3 changed files with 59 additions and 1 deletions

View File

@ -40,7 +40,7 @@ public class AssignmentsController extends TokenBasedController {
private AssignmentService assignmentService;
@GetMapping("/user")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided.")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided. This method is depreaceted. Use `/api/v1/commisions/user/schedule` instead.")
@Deprecated
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));

View File

@ -158,6 +158,11 @@ public class CommisionController extends TokenBasedController {
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignmentsDeanery(
@PathVariable(name = "id") Long userId) throws Exception {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
if (user.getRole() == UserRoles.DEANERY) {
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {

View File

@ -43,6 +43,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule";
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
@ -190,6 +191,58 @@ public class CommisionControllerTest extends AbstractControllerTest {
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturn4xxInScheduleEndpoint() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOkInScheduleEdnpoint() throws Exception {
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldReturnOkInScheduleEdnpointByDeanery() throws Exception {
this.checkUsers();
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + student.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailInScheduleEdnpointByOtherStudent() throws Exception {
this.checkUsers();
final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final User otherStudent = this.service.checkForUser(TEST_COMMISIONS_OTHER_STUDENT_EMAIL, null);
final String token = this.service.login(student).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherStudent.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldFailInScheduleEdnpointByDeaneryDeaneryRelation() throws Exception {
this.checkUsers();
final User deanery = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final User otherDeanery = this.service.checkForUser(TEST_COMMISIONS_OTHER_DEANERY_EMAIL, null);
final String token = this.service.login(deanery).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherDeanery.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().is4xxClientError());
}
private void checkUsers() {
if (CommisionControllerTest.user == null) {
CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT);