|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.server.authorization.web.authentication;
|
17 | 17 |
|
18 |
| -import java.io.IOException; |
19 |
| - |
20 |
| -import jakarta.servlet.ServletException; |
21 |
| -import jakarta.servlet.http.HttpServletRequest; |
22 |
| -import jakarta.servlet.http.HttpServletResponse; |
23 |
| -import org.junit.jupiter.api.BeforeEach; |
24 | 18 | import org.junit.jupiter.api.Test;
|
| 19 | + |
25 | 20 | import org.springframework.http.HttpStatus;
|
26 | 21 | import org.springframework.http.converter.HttpMessageConverter;
|
27 |
| -import org.springframework.http.server.ServletServerHttpResponse; |
28 | 22 | import org.springframework.mock.web.MockHttpServletRequest;
|
29 | 23 | import org.springframework.mock.web.MockHttpServletResponse;
|
30 | 24 | import org.springframework.security.authentication.BadCredentialsException;
|
31 | 25 | import org.springframework.security.core.AuthenticationException;
|
32 | 26 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
33 | 27 | import org.springframework.security.oauth2.core.OAuth2Error;
|
34 | 28 | import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
| 29 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
35 | 30 |
|
36 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
37 |
| -import static org.mockito.ArgumentMatchers.any; |
38 |
| -import static org.mockito.ArgumentMatchers.eq; |
39 |
| -import static org.mockito.ArgumentMatchers.isNull; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
40 | 33 | import static org.mockito.Mockito.mock;
|
41 |
| -import static org.mockito.Mockito.verify; |
42 | 34 | import static org.mockito.Mockito.verifyNoInteractions;
|
43 | 35 |
|
44 | 36 | /**
|
45 |
| - * Tests for {@link OAuth2ErrorAuthenticationFailureHandler} |
| 37 | + * Tests for {@link OAuth2ErrorAuthenticationFailureHandler}. |
46 | 38 | *
|
47 | 39 | * @author Dmitriy Dubson
|
48 | 40 | */
|
49 | 41 | public class OAuth2ErrorAuthenticationFailureHandlerTests {
|
| 42 | + private final OAuth2ErrorAuthenticationFailureHandler authenticationFailureHandler = new OAuth2ErrorAuthenticationFailureHandler(); |
50 | 43 |
|
51 |
| - private HttpMessageConverter<OAuth2Error> errorHttpMessageConverter; |
52 |
| - |
53 |
| - private HttpServletRequest request; |
54 |
| - |
55 |
| - private HttpServletResponse response; |
56 |
| - |
57 |
| - @BeforeEach |
58 |
| - @SuppressWarnings("unchecked") |
59 |
| - public void setUp() { |
60 |
| - errorHttpMessageConverter = (HttpMessageConverter<OAuth2Error>) mock(HttpMessageConverter.class); |
61 |
| - request = new MockHttpServletRequest(); |
62 |
| - response = new MockHttpServletResponse(); |
| 44 | + @Test |
| 45 | + public void setErrorResponseConverterWhenNullThenThrowIllegalArgumentException() { |
| 46 | + // @formatter:off |
| 47 | + assertThatThrownBy(() -> this.authenticationFailureHandler.setErrorResponseConverter(null)) |
| 48 | + .isInstanceOf(IllegalArgumentException.class) |
| 49 | + .hasMessage("errorResponseConverter cannot be null"); |
| 50 | + // @formatter:on |
63 | 51 | }
|
64 | 52 |
|
65 | 53 | @Test
|
66 |
| - public void onAuthenticationFailure() throws IOException, ServletException { |
67 |
| - OAuth2Error invalidRequestError = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST); |
68 |
| - AuthenticationException authenticationException = new OAuth2AuthenticationException(invalidRequestError); |
69 |
| - OAuth2ErrorAuthenticationFailureHandler handler = new OAuth2ErrorAuthenticationFailureHandler(); |
70 |
| - handler.setErrorHttpResponseConverter(errorHttpMessageConverter); |
| 54 | + public void onAuthenticationFailureWhenValidExceptionThenErrorResponse() throws Exception { |
| 55 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 56 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 57 | + OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST, "error description", "error uri"); |
| 58 | + AuthenticationException authenticationException = new OAuth2AuthenticationException(error); |
71 | 59 |
|
72 |
| - handler.onAuthenticationFailure(request, response, authenticationException); |
| 60 | + this.authenticationFailureHandler.onAuthenticationFailure(request, response, authenticationException); |
73 | 61 |
|
74 |
| - verify(errorHttpMessageConverter).write(eq(invalidRequestError), isNull(), any(ServletServerHttpResponse.class)); |
75 | 62 | assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
|
| 63 | + assertThat(response.getContentAsString()).contains("invalid_request"); |
| 64 | + assertThat(response.getContentAsString()).contains("error description"); |
| 65 | + assertThat(response.getContentAsString()).contains("error uri"); |
76 | 66 | }
|
77 | 67 |
|
78 | 68 | @Test
|
79 |
| - public void onAuthenticationFailure_ifExceptionProvidedIsNotOAuth2AuthenticationException() throws ServletException, IOException { |
80 |
| - OAuth2ErrorAuthenticationFailureHandler handler = new OAuth2ErrorAuthenticationFailureHandler(); |
81 |
| - handler.setErrorHttpResponseConverter(errorHttpMessageConverter); |
| 69 | + public void onAuthenticationFailureWhenInvalidExceptionThenStatusResponse() throws Exception { |
| 70 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 71 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 72 | + AuthenticationException authenticationException = new BadCredentialsException("Not a valid exception."); |
| 73 | + |
| 74 | + HttpMessageConverter<OAuth2Error> errorResponseConverter = mock(HttpMessageConverter.class); |
| 75 | + this.authenticationFailureHandler.setErrorResponseConverter(errorResponseConverter); |
82 | 76 |
|
83 |
| - handler.onAuthenticationFailure(request, response, new BadCredentialsException("Not a valid exception.")); |
| 77 | + this.authenticationFailureHandler.onAuthenticationFailure(request, response, authenticationException); |
84 | 78 |
|
85 |
| - verifyNoInteractions(errorHttpMessageConverter); |
| 79 | + verifyNoInteractions(errorResponseConverter); |
86 | 80 | assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
|
| 81 | + assertThat(response.getContentAsString()).doesNotContain(OAuth2ParameterNames.ERROR); |
| 82 | + assertThat(response.getContentAsString()).doesNotContain(OAuth2ParameterNames.ERROR_DESCRIPTION); |
| 83 | + assertThat(response.getContentAsString()).doesNotContain(OAuth2ParameterNames.ERROR_URI); |
87 | 84 | }
|
88 | 85 |
|
89 | 86 | }
|
0 commit comments