Add user test.

This commit is contained in:
Paulo Gustavo Veiga 2024-02-04 17:21:51 -08:00
parent 9382fc2995
commit a681cf9b90
6 changed files with 115 additions and 12 deletions

View File

@ -174,6 +174,13 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>6.2.1</version>
<scope>test</scope>
</dependency>
<!-- JWT dependencies -->
<dependency>
<groupId>io.jsonwebtoken</groupId>

View File

@ -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");
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}