Skip to content

Commit

Permalink
Use privilegedCarbonContext and update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiyamSanthosh committed Jan 17, 2024
1 parent c211d0a commit 181bd9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException;
Expand Down Expand Up @@ -165,7 +165,7 @@ public static AuthenticatedUser createLocalAuthenticatedUserFromSubjectIdentifie
authenticatedUser.setUserName(MultitenantUtils.getTenantAwareUsername(authenticatedSubjectIdentifier));
}

String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
authenticatedUser.setTenantDomain(tenantDomain);
authenticatedUser.setAuthenticatedSubjectIdentifier(authenticatedSubjectIdentifier);
authenticatedUser.setUserId(authenticatedUser.getLocalUserIdInternal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.util.AdminServicesUtil;
import org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
Expand Down Expand Up @@ -72,13 +73,15 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;
import static org.wso2.carbon.identity.core.util.IdentityUtil.getLocalGroupsClaimURI;

/**
* This is a test class for {@link PostAuthAssociationHandler}.
*/
@PrepareForTest({FrameworkUtils.class, ConfigurationFacade.class, ClaimMetadataHandler.class, AdminServicesUtil.class
, IdentityTenantUtil.class})
, IdentityTenantUtil.class, PrivilegedCarbonContext.class})
@PowerMockIgnore({"javax.xml.*", "org.mockito.*"})
public class PostAuthAssociationHandlerTest extends AbstractFrameworkTest {

Expand Down Expand Up @@ -142,6 +145,7 @@ protected void setupSuite() throws Exception {
public void testHandleWithAuthenticatedUserWithFederatedIdpAssociatedToSecondaryUserStore(boolean hasSpRoleMapping)
throws Exception {

mockCarbonContextForTenant();
PowerMockito.spy(AdminServicesUtil.class);
PowerMockito.doReturn(null).when(AdminServicesUtil.class, "getUserRealm");
AuthenticationContext context = processAndGetAuthenticationContext(sp, true, true, hasSpRoleMapping);
Expand Down Expand Up @@ -239,4 +243,14 @@ private boolean isSpRoleMappingSuccessful(Map<ClaimMapping, String> authenticate
}
return false;
}

private void mockCarbonContextForTenant() {

mockStatic(PrivilegedCarbonContext.class);
PrivilegedCarbonContext privilegedCarbonContext = mock(PrivilegedCarbonContext.class);
when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
when(privilegedCarbonContext.getTenantDomain()).thenReturn(SUPER_TENANT_DOMAIN_NAME);
when(privilegedCarbonContext.getTenantId()).thenReturn(SUPER_TENANT_ID);
when(privilegedCarbonContext.getUsername()).thenReturn("admin");
}
}

0 comments on commit 181bd9c

Please sign in to comment.