50 lines
2.0 KiB
Java
50 lines
2.0 KiB
Java
package pl.edu.amu.wmi.bookapi.fixtures.api;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
import org.springframework.test.web.servlet.ResultActions;
|
|
|
|
import java.net.URI;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
public class UserControllerRequests {
|
|
|
|
private final MockMvc mvc;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
public UserControllerRequests(MockMvc mvc, ObjectMapper objectMapper) {
|
|
this.mvc = mvc;
|
|
this.objectMapper = objectMapper;
|
|
}
|
|
|
|
public ResultActions registerUser(String userName, String password) throws Exception {
|
|
return mvc.perform(post(URI.create("/users/sign-up"))
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\n" +
|
|
" \"username\": " + objectMapper.writeValueAsString(userName) + ",\n" +
|
|
" \"password\": " + objectMapper.writeValueAsString(password) + "\n" +
|
|
"}"));
|
|
}
|
|
|
|
public ResultActions loginUser(String userName, String password) throws Exception {
|
|
return mvc.perform(post(URI.create("/users/login"))
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\n" +
|
|
" \"username\": " + objectMapper.writeValueAsString(userName) + ",\n" +
|
|
" \"password\": " + objectMapper.writeValueAsString(password) + "\n" +
|
|
"}"))
|
|
.andDo(print());
|
|
}
|
|
|
|
public String loginAsUserAndReturnAuthorizationHeader(String username, String password) throws Exception {
|
|
return loginUser(username, password)
|
|
.andExpect(status().isOk())
|
|
.andReturn().getResponse().getHeader("Authorization");
|
|
}
|
|
|
|
}
|