Skip to content

Commit

Permalink
Add unit tests for ClaimsUtil class
Browse files Browse the repository at this point in the history
  • Loading branch information
SujanSanjula96 committed Jan 26, 2025
1 parent e377de0 commit ce0e6f0
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.oauth2.util;

import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration;
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

/**
* Unit tests for ClaimsUtil class.
*/
public class ClaimsUtilTest {

@Mock
private IdentityProvider mockIdentityProvider;
@Mock
private OAuthTokenReqMessageContext mockTokenReqMsgCtx;
@Mock
private OAuthServerConfiguration mockOAuthServerConfiguration;
@Mock
private ApplicationManagementService mockApplicationManagementService;

private AutoCloseable closeable;

private MockedStatic<OAuthServerConfiguration> oAuthServerConfiguration;
private MockedStatic<OAuth2ServiceComponentHolder> oAuth2ServiceComponentHolder;
private MockedStatic<FrameworkUtils> frameworkUtils;

@BeforeClass
public void setUp() {

closeable = MockitoAnnotations.openMocks(this);

oAuthServerConfiguration = mockStatic(OAuthServerConfiguration.class);
oAuthServerConfiguration.when(OAuthServerConfiguration::getInstance).thenReturn(mockOAuthServerConfiguration);

oAuth2ServiceComponentHolder = mockStatic(OAuth2ServiceComponentHolder.class);
oAuth2ServiceComponentHolder.when(OAuth2ServiceComponentHolder::getApplicationMgtService)
.thenReturn(mockApplicationManagementService);

frameworkUtils = mockStatic(FrameworkUtils.class);
}

@AfterClass
public void tearDown() throws Exception {

closeable.close();
if (oAuthServerConfiguration != null) {
oAuthServerConfiguration.close();
}
if (oAuth2ServiceComponentHolder != null) {
oAuth2ServiceComponentHolder.close();
}
if (frameworkUtils != null) {
frameworkUtils.close();
}
}

@Test
public void testHandleClaimMappingForResolvingIdPGroupsWithProxyAttributesConfig()
throws IdentityException, IdentityApplicationManagementException {

String tenantDomain = "testTenant";
OAuth2AccessTokenReqDTO oAuth2AccessTokenReqDTO = mock(OAuth2AccessTokenReqDTO.class);
when(mockTokenReqMsgCtx.getOauth2AccessTokenReqDTO()).thenReturn(oAuth2AccessTokenReqDTO);
when(oAuth2AccessTokenReqDTO.getTenantDomain()).thenReturn(tenantDomain);

Map<String, String> attributes = new HashMap<>();
attributes.put("groups", "group1,group2");
attributes.put("claim1", "value1");

ServiceProvider serviceProvider = new ServiceProvider();
serviceProvider.setApplicationResourceId("testAppId");
when(mockApplicationManagementService.getServiceProviderNameByClientId(anyString(), anyString(),
anyString())).thenReturn("testApp");
when(mockApplicationManagementService.getApplicationExcludingFileBasedSPs("testApp", tenantDomain)).thenReturn(
serviceProvider);

frameworkUtils.when(() -> FrameworkUtils.getEffectiveIdpGroupClaimUri(any(), anyString())).thenReturn("groups");
frameworkUtils.when(
() -> FrameworkUtils.getAppAssociatedRolesFromFederatedUserAttributes(any(), any(), anyString(),
anyString(), anyString())).thenReturn(Arrays.asList("role1", "role2"));

when(mockOAuthServerConfiguration.isConvertOriginalClaimsFromAssertionsToOIDCDialect()).thenReturn(false);

Map<String, String> mappedAttributes =
ClaimsUtil.handleClaimMapping(mockIdentityProvider, attributes, tenantDomain, mockTokenReqMsgCtx, true);
assertEquals(3, mappedAttributes.size());
String idPMappedUserRolesAttribute = mappedAttributes.get(FrameworkConstants.IDP_MAPPED_USER_ROLES);
assertEquals(idPMappedUserRolesAttribute, "role1,role2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<class name="org.wso2.carbon.identity.oauth2.token.JWTTokenIssuerTest"/>
<class name="org.wso2.carbon.identity.oauth2.token.OauthTokenIssuerImplTest"/>
<class name="org.wso2.carbon.identity.oauth2.util.OAuth2UtilTest"/>
<class name="org.wso2.carbon.identity.oauth2.util.ClaimsUtilTest"/>
<!--<class name="org.wso2.carbon.identity.openidconnect.DefaultIDTokenBuilderTest"/>-->
<class name="org.wso2.carbon.identity.openidconnect.DefaultOIDCClaimsCallbackHandlerTest"/>
<class name="org.wso2.carbon.identity.openidconnect.JWTAccessTokenOIDCClaimsHandler"/>
Expand Down

0 comments on commit ce0e6f0

Please sign in to comment.