Skip to content
Snippets Groups Projects
Commit 06a07ac0 authored by Ján Macháček's avatar Ján Macháček
Browse files

Merge branch 'authorization-test' into 'milestone-01'

feat: Authorization test

See merge request xmachac5/pa165-airport-project!13
parents b67f2af5 af51c8d9
No related branches found
No related tags found
No related merge requests found
package cz.muni.fi.pa165.authorization.server;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AuthorizationApplicationTests {
@Test
void contextLoads() {
}
}
package cz.muni.fi.pa165.authorization.server;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.authorization.server.model.LoginResponse;
import cz.muni.fi.pa165.authorization.server.model.UserDto;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class UsersIT {
private static final Logger log = LoggerFactory.getLogger(UsersIT.class);
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
void getUserByIdTest() throws Exception {
log.debug("getUserByIdTest() running");
String response = mockMvc.perform(get("/api/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
.andExpect(jsonPath("$.login").value("john.doe"))
.andExpect(jsonPath("$.email").value("john@example.com"))
.andExpect(jsonPath("$.role").value("manager"))
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
UserDto responseDto = objectMapper.readValue(response, UserDto.class);
assertThat(responseDto.getFirstName()).isEqualTo("John");
assertThat(responseDto.getLastName()).isEqualTo("Doe");
assertThat(responseDto.getLogin()).isEqualTo("john.doe");
assertThat(responseDto.getEmail()).isEqualTo("john@example.com");
}
@Test
void getAllUsers() throws Exception {
log.debug("getAllUsers() running");
String response = mockMvc.perform(get("/api/users/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].firstName").value("John"))
.andExpect(jsonPath("$[0].lastName").value("Doe"))
.andExpect(jsonPath("$[0].login").value("john.doe"))
.andExpect(jsonPath("$[0].email").value("john@example.com"))
.andExpect(jsonPath("$[0].role").value("manager"))
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
UserDto[] responseDtos = objectMapper.readValue(response, UserDto[].class);
assertThat(responseDtos[0].getFirstName()).isEqualTo("John");
assertThat(responseDtos[0].getLastName()).isEqualTo("Doe");
assertThat(responseDtos[0].getLogin()).isEqualTo("john.doe");
assertThat(responseDtos[0].getEmail()).isEqualTo("john@example.com");
}
@Test
void createUser() throws Exception {
log.debug("createUser() running");
String requestBody = """
{
"login": "john.doe",
"firstName": "John",
"lastName": "Doe",
"password": "secretPassword",
"email": "john@example.com",
"role": "manager"
}""";
String response = mockMvc.perform(
put("/api/users/")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1L))
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
.andExpect(jsonPath("$.login").value("john.doe"))
.andExpect(jsonPath("$.email").value("john@example.com"))
.andExpect(jsonPath("$.role").value("manager"))
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
UserDto responseDto = objectMapper.readValue(response, UserDto.class);
assertThat(responseDto.getId()).isEqualTo(1L);
assertThat(responseDto.getFirstName()).isEqualTo("John");
assertThat(responseDto.getLastName()).isEqualTo("Doe");
assertThat(responseDto.getLogin()).isEqualTo("john.doe");
assertThat(responseDto.getEmail()).isEqualTo("john@example.com");
}
@Test
void updateUserById() throws Exception {
log.debug("updateUserByIdTest() running");
String requestBody = """
{
"login": "john.doe",
"firstName": "John",
"lastName": "Doe",
"password": "secretPassword",
"email": "john@example.com",
"role": "manager"
}""";
String response = mockMvc.perform(
put("/api/users/1")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstName").value("John"))
.andExpect(jsonPath("$.lastName").value("Doe"))
.andExpect(jsonPath("$.login").value("john.doe"))
.andExpect(jsonPath("$.email").value("john@example.com"))
.andExpect(jsonPath("$.role").value("manager"))
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
UserDto responseDto = objectMapper.readValue(response, UserDto.class);
assertThat(responseDto.getFirstName()).isEqualTo("John");
assertThat(responseDto.getLastName()).isEqualTo("Doe");
assertThat(responseDto.getLogin()).isEqualTo("john.doe");
assertThat(responseDto.getEmail()).isEqualTo("john@example.com");
}
@Test
void deleteUser() throws Exception {
log.debug("deleteUserBy() running");
String response = mockMvc.perform(
delete("/api/users/1"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
}
@Test
void login() throws Exception {
log.debug("login() running");
String requestBody = """
{
"login": "john.doe",
"password": "password"
}""";
String response = mockMvc.perform(
put("/api/login")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andExpect(jsonPath("$.token").value("token-for-john.doe"))
.andReturn().getResponse().getContentAsString();
log.debug("response: {}", response);
LoginResponse responseDto = objectMapper.readValue(response, LoginResponse.class);
assertThat(responseDto.getToken()).isEqualTo("token-for-john.doe");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment