Tours endpoint
This commit is contained in:
		| @@ -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<ConfigTourResponse> getTourDate() { | ||||
|         final ConfigTourResponse response = new ConfigTourResponse(this.contrl.getCurrentConfig().getCurrentState()); | ||||
|         return new ResponseEntity<>(response, HttpStatus.OK); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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,7 +231,7 @@ 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(); | ||||
| @@ -238,7 +239,7 @@ public class ConfigControllerTest extends AbstractControllerTest { | ||||
|     } | ||||
|  | ||||
|     @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(); | ||||
| @@ -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()); | ||||
|  | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user