From 37745b9c68ae841456e0c0e9c13a341c30337c0b Mon Sep 17 00:00:00 2001 From: strehle Date: Fri, 7 Jul 2023 08:41:11 +0200 Subject: [PATCH] more tests --- ...entParametersAuthenticationFilterTest.java | 55 +++++++++++++++++++ ...ncedAuthorizationCodeTokenGranterTest.java | 15 +++++ 2 files changed, 70 insertions(+) diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/ClientParametersAuthenticationFilterTest.java b/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/ClientParametersAuthenticationFilterTest.java index 871958cf69c..13497ebf619 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/ClientParametersAuthenticationFilterTest.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/ClientParametersAuthenticationFilterTest.java @@ -26,6 +26,7 @@ import java.io.IOException; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -83,5 +84,59 @@ public void testStoreClientAuthenticationMethod() throws IOException, ServletExc verifyNoInteractions(authenticationEntryPoint); verify(chain).doFilter(request, response); + verify(authenticationDetails, atLeast(1)).getAuthenticationMethod(); + } + + @Test + public void testStoreClientAuthenticationMethodNoDetails() throws IOException, ServletException { + ClientParametersAuthenticationFilter filter = new ClientParametersAuthenticationFilter(); + + AuthenticationEntryPoint authenticationEntryPoint = mock(AuthenticationEntryPoint.class); + filter.setAuthenticationEntryPoint(authenticationEntryPoint); + AuthenticationManager clientAuthenticationManager = mock(AuthenticationManager.class); + filter.setClientAuthenticationManager(clientAuthenticationManager); + + Authentication authentication = mock(Authentication.class); + MockHttpServletRequest request = new MockHttpServletRequest(); + when(clientAuthenticationManager.authenticate(Mockito.any())).thenReturn(authentication); + when(authentication.isAuthenticated()).thenReturn(true); + when(authentication.getDetails()).thenReturn(null); + + MockFilterChain chain = mock(MockFilterChain.class); + request.addHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); + MockHttpServletResponse response = new MockHttpServletResponse(); + + filter.doFilter(request, response, chain); + + verifyNoInteractions(authenticationEntryPoint); + verify(chain).doFilter(request, response); + } + + @Test + public void testStoreClientAuthenticationMethodNoMethod() throws IOException, ServletException { + ClientParametersAuthenticationFilter filter = new ClientParametersAuthenticationFilter(); + + AuthenticationEntryPoint authenticationEntryPoint = mock(AuthenticationEntryPoint.class); + filter.setAuthenticationEntryPoint(authenticationEntryPoint); + AuthenticationManager clientAuthenticationManager = mock(AuthenticationManager.class); + filter.setClientAuthenticationManager(clientAuthenticationManager); + + Authentication authentication = mock(Authentication.class); + MockHttpServletRequest request = new MockHttpServletRequest(); + UaaAuthenticationDetails authenticationDetails = mock(UaaAuthenticationDetails.class); + when(clientAuthenticationManager.authenticate(Mockito.any())).thenReturn(authentication); + when(authentication.isAuthenticated()).thenReturn(true); + when(authentication.getDetails()).thenReturn(authenticationDetails); + when(authenticationDetails.getAuthenticationMethod()).thenReturn(null); + + MockFilterChain chain = mock(MockFilterChain.class); + request.addHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); + MockHttpServletResponse response = new MockHttpServletResponse(); + + filter.doFilter(request, response, chain); + + verifyNoInteractions(authenticationEntryPoint); + verify(chain).doFilter(request, response); + verify(authenticationDetails).getAuthenticationMethod(); } } diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/oauth/token/PkceEnhancedAuthorizationCodeTokenGranterTest.java b/server/src/test/java/org/cloudfoundry/identity/uaa/oauth/token/PkceEnhancedAuthorizationCodeTokenGranterTest.java index 77fb53a532d..84d0cf067e3 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/oauth/token/PkceEnhancedAuthorizationCodeTokenGranterTest.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/oauth/token/PkceEnhancedAuthorizationCodeTokenGranterTest.java @@ -26,7 +26,10 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atMost; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class PkceEnhancedAuthorizationCodeTokenGranterTest { @@ -94,5 +97,17 @@ void getOAuth2AuthenticationMethod() throws PkceValidationException { when(oAuth2Request.getExtensions()).thenReturn(authMap); when(oAuth2Request.createOAuth2Request(any())).thenReturn(oAuth2Request); assertNotNull(granter.getOAuth2Authentication(requestingClient, tokenRequest)); + verify(oAuth2Request, times(2)).getExtensions(); + } + + @Test + void getOAuth2AuthenticationNoMethod() throws PkceValidationException { + HashMap authMap = new HashMap(); + authMap.put(ClaimConstants.CLIENT_AUTH_METHOD, null); + when(pkceValidationService.checkAndValidate(any(), any(), any())).thenReturn(true); + when(oAuth2Request.getExtensions()).thenReturn(authMap); + when(oAuth2Request.createOAuth2Request(any())).thenReturn(oAuth2Request); + assertNotNull(granter.getOAuth2Authentication(requestingClient, tokenRequest)); + verify(oAuth2Request, atMost(1)).getExtensions(); } } \ No newline at end of file