Skip to content

Commit

Permalink
Fix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpmadhavig committed Aug 23, 2024
1 parent 4e56039 commit 87dc56a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ private static boolean isOmitUsernameInIntrospectionRespForAppTokens(AccessToken
appResidentTenantDomain = IdentityTenantUtil.getTenantDomain(appResidentTenantId);
OAuthAppDO oAuthAppDO = OAuth2Util.getAppInformationByClientId(accessTokenDO.getConsumerKey(),
appResidentTenantDomain);
omitUsernameInIntrospectionRespForAppTokens = oAuthAppDO
.isOmitUsernameInIntrospectionRespForAppTokens();
omitUsernameInIntrospectionRespForAppTokens = Boolean.TRUE.equals(oAuthAppDO
.isOmitUsernameInIntrospectionRespForAppTokens());
} else {
throw new IdentityOAuth2ServerException("Invalid tenant domain found in access token issue for"
+ " client id: " + accessTokenDO.getConsumerKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl;
import org.wso2.carbon.identity.oauth.cache.AppInfoCache;
import org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration;
import org.wso2.carbon.identity.oauth.dao.OAuthAppDO;
import org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO;
import org.wso2.carbon.identity.oauth.internal.OAuthComponentServiceHolder;
import org.wso2.carbon.identity.oauth.tokenprocessor.PlainTextPersistenceProcessor;
import org.wso2.carbon.identity.oauth.tokenprocessor.TokenProvider;
Expand Down Expand Up @@ -142,12 +140,6 @@ public class TokenValidationHandlerTest {
private IdentityProvider identityProvider;
@Mock
private FederatedAuthenticatorConfig federatedAuthenticatorConfig = new FederatedAuthenticatorConfig();
@Mock
OAuthComponentServiceHolder mockOAuthComponentServiceHolder;
@Mock
OAuthConsumerAppDTO mockedOAuthConsumerAppDTO;
@Mock
OAuthAdminServiceImpl mockedOAuthAdminService;
private MockedStatic<LoggerUtils> loggerUtils;

@BeforeMethod
Expand Down Expand Up @@ -308,6 +300,7 @@ public void testBuildIntrospectionResponse(boolean isIDPIdColumnEnabled, String
tokenBinding.setBindingReference("test_binding_reference");
tokenBinding.setBindingValue("R4Hj_0nNdIzVvPdCdsWlxNKm6a74cszp4Za4M1iE8P9");
accessTokenDO.setTokenBinding(tokenBinding);
accessTokenDO.setAppResidentTenantId(-1234);

PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super");
TokenProvider tokenProvider = Mockito.mock(TokenProvider.class);
Expand All @@ -327,46 +320,38 @@ public void testBuildIntrospectionResponse(boolean isIDPIdColumnEnabled, String
OAuthAppDO oAuthAppDO = new OAuthAppDO();
oAuthAppDO.setTokenType("Default");
oAuthAppDO.setApplicationName("testApp");
oAuthAppDO.setOmitUsernameInIntrospectionRespForAppTokens(omitUsernameInIntrospectionRespAppConfig);
AppInfoCache appInfoCache = AppInfoCache.getInstance();
appInfoCache.addToCache("testConsumerKey", oAuthAppDO);
oAuth2TokenValidationRequestDTO.setAccessToken(accessToken);

try (MockedStatic<OAuthComponentServiceHolder> oAuthComponentServiceHolder =
mockStatic(OAuthComponentServiceHolder.class)) {
when(OAuthComponentServiceHolder.getInstance()).thenReturn(mockOAuthComponentServiceHolder);
lenient().when(mockOAuthComponentServiceHolder.getoAuthAdminService())
.thenReturn(mockedOAuthAdminService);
lenient().when(mockedOAuthAdminService.getOAuthApplicationData(anyString(), anyString()))
.thenReturn(mockedOAuthConsumerAppDTO);
lenient().when(mockedOAuthConsumerAppDTO.isOmitUsernameInIntrospectionRespForAppTokens())
.thenReturn(omitUsernameInIntrospectionRespAppConfig);

// Mock server level config value.
when(OAuthServerConfiguration.getInstance()).thenReturn(mockOAuthServerConfiguration);
lenient().when(mockOAuthServerConfiguration
.isRemoveUsernameFromIntrospectionResponseForAppTokensEnabled())
.thenReturn(omitUsernameInIntrospectionRespServerConfig);

oAuth2Util.when(OAuth2Util::getPersistenceProcessor)
.thenReturn(new PlainTextPersistenceProcessor());
oAuth2Util.when(() -> OAuth2Util.getAppInformationByAccessTokenDO(any())).thenReturn(oAuthAppDO);
oAuth2Util.when(() -> OAuth2Util.getAccessTokenExpireMillis(any(), Mockito.anyBoolean()))
.thenReturn(1000L);

OAuth2IntrospectionResponseDTO oAuth2IntrospectionResponseDTO = tokenValidationHandler
.buildIntrospectionResponse(oAuth2TokenValidationRequestDTO);
assertNotNull(oAuth2IntrospectionResponseDTO);
assertEquals(oAuth2IntrospectionResponseDTO.getBindingType(),
OAuth2Constants.TokenBinderType.CERTIFICATE_BASED_TOKEN_BINDER);
assertEquals(oAuth2IntrospectionResponseDTO.getBindingReference(), "test_binding_reference");
assertEquals(oAuth2IntrospectionResponseDTO.getCnfBindingValue(),
"R4Hj_0nNdIzVvPdCdsWlxNKm6a74cszp4Za4M1iE8P9");
if (omitUsernameInIntrospectionRespAppConfig && omitUsernameInIntrospectionRespServerConfig &&
Objects.equals(tokenTypeData, "APPLICATION")) {
assertNull(oAuth2IntrospectionResponseDTO.getUsername());
} else {
assertEquals(oAuth2IntrospectionResponseDTO.getUsername(), authzUser.getUserName());
}
oAuth2Util.when(() -> OAuth2Util.getAppInformationByClientId(anyString(), anyString()))
.thenReturn(oAuthAppDO);
// Mock server level config value.
when(OAuthServerConfiguration.getInstance()).thenReturn(mockOAuthServerConfiguration);
lenient().when(mockOAuthServerConfiguration
.isRemoveUsernameFromIntrospectionResponseForAppTokensEnabled())
.thenReturn(omitUsernameInIntrospectionRespServerConfig);

oAuth2Util.when(OAuth2Util::getPersistenceProcessor)
.thenReturn(new PlainTextPersistenceProcessor());
oAuth2Util.when(() -> OAuth2Util.getAppInformationByAccessTokenDO(any())).thenReturn(oAuthAppDO);
oAuth2Util.when(() -> OAuth2Util.getAccessTokenExpireMillis(any(), Mockito.anyBoolean()))
.thenReturn(1000L);

OAuth2IntrospectionResponseDTO oAuth2IntrospectionResponseDTO = tokenValidationHandler
.buildIntrospectionResponse(oAuth2TokenValidationRequestDTO);
assertNotNull(oAuth2IntrospectionResponseDTO);
assertEquals(oAuth2IntrospectionResponseDTO.getBindingType(),
OAuth2Constants.TokenBinderType.CERTIFICATE_BASED_TOKEN_BINDER);
assertEquals(oAuth2IntrospectionResponseDTO.getBindingReference(), "test_binding_reference");
assertEquals(oAuth2IntrospectionResponseDTO.getCnfBindingValue(),
"R4Hj_0nNdIzVvPdCdsWlxNKm6a74cszp4Za4M1iE8P9");
if (omitUsernameInIntrospectionRespAppConfig && omitUsernameInIntrospectionRespServerConfig &&
Objects.equals(tokenTypeData, "APPLICATION")) {
assertNull(oAuth2IntrospectionResponseDTO.getUsername());
} else {
assertEquals(oAuth2IntrospectionResponseDTO.getUsername(), authzUser.getUserName());
}
}
}
Expand Down

0 comments on commit 87dc56a

Please sign in to comment.