From b2b9178bc10959a0a80b4dd0f801156118a3ac0d Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 1 Dec 2020 17:31:55 +0100 Subject: [PATCH] Added missing config tests. Ready to pull. Other resposnes to fix in another task --- .../controllers/ConfigController.java | 2 +- .../controllers/ConfigControllerTest.java | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index f9e3b0e..58b94e1 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -34,7 +34,7 @@ public class ConfigController { private ConfiguratorService contrl; @PostMapping("/config") - // @PreAuthorize("hasRole('ROLE_ADMIN')") + @PreAuthorize("hasRole('ROLE_ADMIN')") @ApiOperation("Imports data to system. To call you need to provide ADMIN token") public ResponseEntity configApp( @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) { diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index 2c445af..9c56c69 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -28,7 +28,6 @@ public class ConfigControllerTest extends AbstractControllerTest { private static final String FILE_NAME = "Zajecia.xlsx"; private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config"; - private static final String TEST_MAIL = "notexisting@mail.domain"; @Autowired private UserService service; @@ -45,12 +44,13 @@ public class ConfigControllerTest extends AbstractControllerTest { @Test public void shouldReturnOKAuthorized() throws Exception { - final User usr = new User(null, null, TEST_MAIL, UserRoles.ADMIN); + final String mail = "shouldReturnOKAuthorized@ConfigController.test"; + final User usr = new User(null, null, mail, UserRoles.ADMIN); this.service.save(usr); final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); final MockMultipartFile file = new MockMultipartFile("file", inputStream); - final String token = this.service.login(TEST_MAIL); + final String token = this.service.login(mail); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token)) @@ -58,4 +58,20 @@ public class ConfigControllerTest extends AbstractControllerTest { } + @Test + public void shouldReturnDenyNoAdminAuthorized() throws Exception { + final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test"; + final User usr = new User(null, null, mail, UserRoles.TEST_USER); + this.service.save(usr); + + final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); + final MockMultipartFile file = new MockMultipartFile("file", inputStream); + final String token = this.service.login(mail); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); + + } + }