diff --git a/wise-api/pom.xml b/wise-api/pom.xml index 549f5e29..aecf4ca6 100644 --- a/wise-api/pom.xml +++ b/wise-api/pom.xml @@ -174,6 +174,13 @@ compile + + org.springframework.security + spring-security-test + 6.2.1 + test + + io.jsonwebtoken diff --git a/wise-api/src/main/java/com/wisemapping/rest/AdminController.java b/wise-api/src/main/java/com/wisemapping/rest/AdminController.java index 10dd3a2b..3c4d1ac3 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/AdminController.java +++ b/wise-api/src/main/java/com/wisemapping/rest/AdminController.java @@ -69,7 +69,7 @@ public class AdminController extends BaseController { @RequestMapping(method = RequestMethod.POST, value = "/users", consumes = {"application/json"}, produces = {"application/json"}) @ResponseStatus(value = HttpStatus.CREATED) - public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws WiseMappingException { + public void createUser(@RequestBody RestUser user, final HttpServletResponse response) throws WiseMappingException { if (user == null) { throw new IllegalArgumentException("User could not be found"); } diff --git a/wise-api/src/main/java/com/wisemapping/security/Utils.java b/wise-api/src/main/java/com/wisemapping/security/Utils.java index f5c68587..5d93831a 100644 --- a/wise-api/src/main/java/com/wisemapping/security/Utils.java +++ b/wise-api/src/main/java/com/wisemapping/security/Utils.java @@ -34,7 +34,6 @@ final public class Utils { return getUser(false); } - @NotNull public static User getUser(boolean forceCheck) { User result = null; final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); diff --git a/wise-api/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java b/wise-api/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java index 689187e5..2a64a3ee 100644 --- a/wise-api/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java +++ b/wise-api/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java @@ -118,14 +118,4 @@ public class RestAccountControllerTest { return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); } - private RestUser createDummyUser() { - final RestUser restUser = new RestUser(); - final String username = "foo-to-delete" + System.nanoTime(); - final String email = username + "@example.org"; - restUser.setEmail(email); - restUser.setFirstname("foo first name"); - restUser.setLastname("foo last name"); - restUser.setPassword("fooPassword"); - return restUser; - } } diff --git a/wise-api/src/test/java/com/wisemapping/test/rest/RestHelper.java b/wise-api/src/test/java/com/wisemapping/test/rest/RestHelper.java index 23bdacc3..d2538f74 100644 --- a/wise-api/src/test/java/com/wisemapping/test/rest/RestHelper.java +++ b/wise-api/src/test/java/com/wisemapping/test/rest/RestHelper.java @@ -1,5 +1,6 @@ package com.wisemapping.test.rest; +import com.wisemapping.rest.model.RestUser; import org.jetbrains.annotations.NotNull; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.client.RestTemplateBuilder; @@ -22,4 +23,15 @@ public class RestHelper { result.setContentType(mediaType); return result; } + + static RestUser createDummyUser() { + final RestUser restUser = new RestUser(); + final String username = "foo-to-delete" + System.nanoTime(); + final String email = username + "@example.org"; + restUser.setEmail(email); + restUser.setFirstname("foo first name"); + restUser.setLastname("foo last name"); + restUser.setPassword("fooPassword"); + return restUser; + } } diff --git a/wise-api/src/test/java/com/wisemapping/test/rest/RestUserControllerTest.java b/wise-api/src/test/java/com/wisemapping/test/rest/RestUserControllerTest.java new file mode 100644 index 00000000..49320e36 --- /dev/null +++ b/wise-api/src/test/java/com/wisemapping/test/rest/RestUserControllerTest.java @@ -0,0 +1,95 @@ +/* + * Copyright [2022] [wisemapping] + * + * Licensed under WiseMapping Public License, Version 1.0 (the "License"). + * It is basically the Apache License, Version 2.0 (the "License") plus the + * "powered by wisemapping" text requirement on every single page; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the license at + * + * http://www.wisemapping.org/license + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.wisemapping.test.rest; + + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wisemapping.config.common.CommonConfig; +import com.wisemapping.config.rest.RestAppConfig; +import com.wisemapping.model.User; +import com.wisemapping.rest.UserController; +import com.wisemapping.rest.model.RestUser; +import com.wisemapping.service.UserService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.web.servlet.MockMvc; + +import static com.wisemapping.test.rest.RestHelper.createDummyUser; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, UserController.class}) +@AutoConfigureMockMvc +public class RestUserControllerTest { + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private MockMvc mockMvc; + + @Autowired + private UserService userService; + + + private RestUser createUser() throws Exception { + final RestUser result = createDummyUser(); + final String userJson = objectMapper.writeValueAsString(result); + + mockMvc.perform( + post("/api/restfull/admin/users"). + contentType(MediaType.APPLICATION_JSON) + .content(userJson) + .with(user("test@wisemapping.org").roles("ADMIN"))) + .andExpect(status().isCreated()); + + // Check dao ... + User userBy = userService.getUserBy(result.getEmail()); + assertTrue(userBy!=null); + return result; + } + + @Test + void resetPasswordInvalidUser() throws Exception { + this.mockMvc.perform + (put("/api/restfull/users/resetPassword?email=doesnotexist@example.com")) + .andDo(print()) + .andExpect(status().is4xxClientError()) + .andExpect(content().string(containsString("The email provided is not a valid user account."))); + } + + @Test + void resetPasswordValidUser() throws Exception { + final RestUser user = createUser(); + this.mockMvc.perform + (put("/api/restfull/users/resetPassword?email=" + user.getEmail())) + .andDo(print()) + .andExpect(status().isOk()); + } +}