From a6acab932c520c1e35dd005957ad93616b732e93 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Thu, 10 Dec 2020 16:50:47 +0100 Subject: [PATCH] New endpints tests --- .../controllers/AssignmentsController.java | 2 +- .../controllers/CommisionController.java | 5 ++ .../controllers/CommisionControllerTest.java | 53 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java index 06313e8..8a0a786 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -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> getCurrentAssignments() throws Exception { User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found")); diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 5b08dc8..2388f12 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -158,6 +158,11 @@ public class CommisionController extends TokenBasedController { public ResponseEntity> 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 com = this.commisionService.getNewestCommision(user); if (com.isPresent()) { diff --git a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java index f426785..5ff2377 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java @@ -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);