Skip to content
Snippets Groups Projects
Commit ce0552c9 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4939 Added test for unauthorized response body

parent 08eebf49
Branches
Tags
No related merge requests found
......@@ -87,9 +87,7 @@ public class SecurityConfiguration {
}
private String getProblemDetailAsString(String requestUri, AuthenticationException authException) throws JsonProcessingException {
var problemDetail = ErrorResponse.builder(authException,
HttpStatus.UNAUTHORIZED,
authException.getLocalizedMessage()).build().getBody();
var problemDetail = ErrorResponse.builder(authException, HttpStatus.UNAUTHORIZED, authException.getLocalizedMessage()).build().getBody();
problemDetail.setInstance(URI.create(requestUri));
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
......
......@@ -25,16 +25,26 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.net.URI;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.web.ErrorResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import de.ozgcloud.common.test.DataITCase;
import lombok.SneakyThrows;
......@@ -88,6 +98,26 @@ public class SecurityConfigurationLocalITCase {
result.andExpect(status().isUnauthorized());
}
@Test
@SneakyThrows
void shouldHaveErrorInfoInBody() {
var ex = new AuthenticationException("Full authentication is required to access this resource") {
};
var expected = getExpectedProblemDetailsAsString("/api", ex);
var result = doPerform("/api");
result.andExpect(content().string(expected));
}
private String getExpectedProblemDetailsAsString(String requestUri, AuthenticationException authException) throws JsonProcessingException {
var problemDetail = ErrorResponse.builder(authException, HttpStatus.UNAUTHORIZED, authException.getLocalizedMessage()).build().getBody();
problemDetail.setInstance(URI.create(requestUri));
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(problemDetail);
}
@SneakyThrows
private ResultActions doPerform(String path) {
return mockMvc.perform(get(path).header("Authorization", "invalid"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment