Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strehle committed Jul 7, 2023
1 parent df727dc commit 37745b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 37745b9

Please sign in to comment.