feat: add authentication helper and project findAll test
This commit is contained in:
parent
2657b2e4c1
commit
6d5f67f7ca
@ -1,6 +1,8 @@
|
||||
package de.szut.lf8_starter.project;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> {
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package de.szut.lf8_starter.project.utils;
|
||||
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
public interface AuthenticationHelper {
|
||||
|
||||
default String getAuthenticationToken(TestRestTemplate restTemplate) {
|
||||
MultiValueMap<String, String> authParams = new LinkedMultiValueMap<>();
|
||||
authParams.add("grant_type", "password");
|
||||
authParams.add("client_id", "employee-management-service");
|
||||
authParams.add("username", "user");
|
||||
authParams.add("password", "test");
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(authParams, headers);
|
||||
ResponseEntity<String> authResponse = restTemplate.postForEntity("https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token", request, String.class);
|
||||
|
||||
// Extract the token from the response
|
||||
String responseBody = authResponse.getBody();
|
||||
assert responseBody != null;
|
||||
|
||||
return "Bearer " + extractToken(responseBody);
|
||||
}
|
||||
|
||||
private String extractToken(String responseBody) {
|
||||
return responseBody.substring(responseBody.indexOf("access_token\":\"") + 15, responseBody.indexOf("\"", responseBody.indexOf("access_token\":\"") + 15));
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package de.szut.lf8_starter.integration.project;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectRepository;
|
||||
import de.szut.lf8_starter.project.utils.AuthenticationHelper;
|
||||
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.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureMockMvc
|
||||
public class ProjectFindAll implements AuthenticationHelper {
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
void findAllProjects() throws Exception {
|
||||
var project = new ProjectEntity();
|
||||
project.setId(1);
|
||||
project.setComment("comment");
|
||||
project.setContractor(1);
|
||||
project.setContractorName("contractorName");
|
||||
project.setEndDate(LocalDate.of(2024, 1, 1));
|
||||
project.setLeadingEmployee(1);
|
||||
project.setName("name");
|
||||
project.setStartDate(LocalDate.of(2021, 1, 1));
|
||||
project.setEmployees(List.of(1L,2L,3L));
|
||||
this.projectRepository.save(project);
|
||||
|
||||
this.mockMvc.perform(get("/projects").header("Authorization", getAuthenticationToken(restTemplate)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(1)))
|
||||
.andExpect(jsonPath("$[0].id").value(1))
|
||||
.andExpect(jsonPath("$[0].comment").value("comment"))
|
||||
.andExpect(jsonPath("$[0].contractor").value(1))
|
||||
.andExpect(jsonPath("$[0].contractorName").value("contractorName"))
|
||||
.andExpect(jsonPath("$[0].endDate").value("2024-01-01"))
|
||||
.andExpect(jsonPath("$[0].leadingEmployee").value(1))
|
||||
.andExpect(jsonPath("$[0].name").value("name"))
|
||||
.andExpect(jsonPath("$[0].startDate").value("2021-01-01"))
|
||||
.andExpect(jsonPath("$[0].employees").isArray())
|
||||
.andExpect(jsonPath("$[0].employees", hasSize(3)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user