From d217536f2cc8db031268fbb303a8d5bfa41e9763 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Tue, 19 Jan 2021 11:33:14 +0100 Subject: [PATCH] Tours endpoint --- .../controllers/ConfigController.java | 14 ++++ .../responses/models/ConfigTourResponse.java | 26 +++++++ .../controllers/ConfigControllerTest.java | 76 +++++++++++++------ 3 files changed, 92 insertions(+), 24 deletions(-) create mode 100755 restservice/src/main/java/com/plannaplan/responses/models/ConfigTourResponse.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index b4769df..59a5bdd 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -10,6 +10,7 @@ import com.plannaplan.App; import com.plannaplan.entities.User; import com.plannaplan.models.ConfigData; import com.plannaplan.models.TourData; +import com.plannaplan.responses.models.ConfigTourResponse; import com.plannaplan.security.cas.CasUserIdentity; import com.plannaplan.security.cas.CasValidationExcepiton; import com.plannaplan.security.cas.CasValidator; @@ -34,6 +35,7 @@ import org.springframework.web.multipart.MultipartFile; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.springframework.web.bind.annotation.GetMapping; /** * Rest controller to Config related endpoints. More detailed api docs is @@ -167,4 +169,16 @@ public class ConfigController { return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); } } + + /** + * @return api response entity + */ + @GetMapping(path = "/admin/tour") + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ApiOperation("It will return what tour is currently on. You need to provide admin token.") + public ResponseEntity getTourDate() { + final ConfigTourResponse response = new ConfigTourResponse(this.contrl.getCurrentConfig().getCurrentState()); + return new ResponseEntity<>(response, HttpStatus.OK); + } + } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/responses/models/ConfigTourResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/ConfigTourResponse.java new file mode 100755 index 0000000..78d97b4 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/ConfigTourResponse.java @@ -0,0 +1,26 @@ +package com.plannaplan.responses.models; + +import com.plannaplan.types.AppState; + +/** + * Api response for config tours entpoint + */ +public class ConfigTourResponse { + + private String currentTour; + + /** + * @param appState to send as a response + */ + public ConfigTourResponse(AppState appState) { + this.currentTour = appState.toString(); + } + + /** + * @return current app state as string + */ + public String getCurrentTour() { + return currentTour; + } + +} diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index 44275c9..c437a22 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -40,6 +40,7 @@ public class ConfigControllerTest extends AbstractControllerTest { private static final String COURSE_ENDPOINT = "/api/v1/configurator/config/courses"; private static final String TOURS_ENDPOINT = "/api/v1/configurator/config/tours"; private static final String ADMIN_INIT_ENDPOINT = "/api/v1/configurator/admin/init"; + private static final String TOUR_GET_ENDPOINT = "/api/v1/configurator/admin/tour"; private static final String FIRST_TOUR_START = "firstTourBegin"; private static final String FIRST_TOUR_END = "firstTourEnd"; private static final String SECOND_TOUR_START = "secondTourBegin"; @@ -230,19 +231,19 @@ public class ConfigControllerTest extends AbstractControllerTest { @Test @Ignore @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldCreateAdminBecouseOfEmptyDatabase() throws Exception{ + public void shouldCreateAdminBecouseOfEmptyDatabase() throws Exception { // have no idea how to make this test independent from user that run this final String ticket = ""; MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().isOk()); + mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().isOk()); } @Test - public void shouldFailDueToExistingAdmin() throws Exception{ + public void shouldFailDueToExistingAdmin() throws Exception { this.service.save(new User(null, null, "shouldFailDueToExistingAdmin@ConfigController.Test", UserRoles.ADMIN)); final String ticket = "hfewlhfjlewhipfqwehipqwef"; MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().is4xxClientError()); + mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().is4xxClientError()); } @Test @@ -282,10 +283,9 @@ public class ConfigControllerTest extends AbstractControllerTest { final String token = this.service.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020") - .param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020") - .param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token)) - .andExpect(status().isOk()); + mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020") + .param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020") + .header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test @@ -295,10 +295,9 @@ public class ConfigControllerTest extends AbstractControllerTest { final String token = this.service.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020") - .param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020") - .param(SECOND_TOUR_END, "13.12.2020").header("Authorization", "Bearer " + token)) - .andExpect(status().is4xxClientError()); + mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020") + .param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "13.12.2020") + .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @@ -309,10 +308,9 @@ public class ConfigControllerTest extends AbstractControllerTest { final String token = this.service.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020") - .param(FIRST_TOUR_END, "10.12.2020").param(SECOND_TOUR_START, "16.12.2020") - .param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token)) - .andExpect(status().is4xxClientError()); + mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "10.12.2020") + .param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020") + .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @@ -323,10 +321,9 @@ public class ConfigControllerTest extends AbstractControllerTest { final String token = this.service.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020") - .param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "13.12.2020") - .param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token)) - .andExpect(status().is4xxClientError()); + mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020") + .param(SECOND_TOUR_START, "13.12.2020").param(SECOND_TOUR_END, "20.12.2020") + .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @@ -337,10 +334,41 @@ public class ConfigControllerTest extends AbstractControllerTest { final String token = this.service.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020") - .param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020") - .param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token)) + mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020") + .param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020") + .header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); + + } + + @Test + public void shouldReturnOKAuthorizedForGetTours() throws Exception { + final String mail = "shouldReturnOKAuthorizedForGetTours@ConfigController.test"; + final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN)); + + final String token = this.service.login(usr).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk()); + + } + + @Test + public void shouldDenyForGetTours() throws Exception { + final String mail = "shouldDenyForGetTours@ConfigController.test"; + final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER)); + + final String token = this.service.login(usr).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); - } + } + + @Test + public void shouldDenyWithNoToken() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(TOUR_GET_ENDPOINT)).andExpect(status().is4xxClientError()); + + } } \ No newline at end of file