From 119c2067b565377494c270802ada69a02eb57fb8 Mon Sep 17 00:00:00 2001 From: strehle Date: Thu, 6 Jul 2023 23:23:43 +0200 Subject: [PATCH] tests --- .../uaa/util/UaaSecurityContextUtilsTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server/src/test/java/org/cloudfoundry/identity/uaa/util/UaaSecurityContextUtilsTest.java diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/util/UaaSecurityContextUtilsTest.java b/server/src/test/java/org/cloudfoundry/identity/uaa/util/UaaSecurityContextUtilsTest.java new file mode 100644 index 00000000000..56c2526fdc6 --- /dev/null +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/util/UaaSecurityContextUtilsTest.java @@ -0,0 +1,47 @@ +package org.cloudfoundry.identity.uaa.util; + +import org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.OAuth2Request; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class UaaSecurityContextUtilsTest { + + private OAuth2Request auth2Request; + + @BeforeEach + void setUp() { + OAuth2Authentication authentication = mock(OAuth2Authentication.class); + SecurityContextHolder.getContext().setAuthentication(authentication); + auth2Request = mock(OAuth2Request.class); + when(auth2Request.getExtensions()).thenReturn(new HashMap<>()); + when(authentication.getOAuth2Request()).thenReturn(auth2Request); + } + + @Test + void getNoClientAuthenticationMethod() { + assertNull(UaaSecurityContextUtils.getClientAuthenticationMethod()); + } + + @Test + void getNullClientAuthenticationMethod() { + SecurityContextHolder.getContext().setAuthentication(null); + assertNull(UaaSecurityContextUtils.getClientAuthenticationMethod()); + } + + @Test + void getClientAuthenticationMethod() { + when(auth2Request.getExtensions()).thenReturn(Map.of(ClaimConstants.CLIENT_AUTH_METHOD, "none")); + assertEquals("none", UaaSecurityContextUtils.getClientAuthenticationMethod()); + } +}