use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIUtilTest method testHasPermission.
@Test
public void testHasPermission() throws Exception {
int tenantId = 2;
String userNameWithoutChange = "Drake";
String permission = APIConstants.Permissions.API_PUBLISH;
System.setProperty(CARBON_HOME, "");
PowerMockito.spy(APIUtil.class);
PowerMockito.doReturn(false).when(APIUtil.class, "isPermissionCheckDisabled");
PowerMockito.doReturn(1).when(APIUtil.class, "getValueFromCache", APIConstants.API_PUBLISHER_ADMIN_PERMISSION_CACHE, userNameWithoutChange);
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(userNameWithoutChange)).thenReturn(tenantDomain);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(CarbonContext.class);
PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
RealmService realmService = Mockito.mock(RealmService.class);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
UserRealm userRealm = Mockito.mock(UserRealm.class);
Mockito.when(realmService.getTenantUserRealm(tenantId)).thenReturn(userRealm);
org.wso2.carbon.user.api.AuthorizationManager authorizationManager = Mockito.mock(org.wso2.carbon.user.api.AuthorizationManager.class);
Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authorizationManager);
Mockito.when(authorizationManager.isUserAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
org.wso2.carbon.user.core.UserRealm userRealm2 = Mockito.mock(org.wso2.carbon.user.core.UserRealm.class);
Mockito.when(ServiceReferenceHolder.getUserRealm()).thenReturn((userRealm2));
PowerMockito.mockStatic(AuthorizationManager.class);
AuthorizationManager authorizationManager1 = Mockito.mock(AuthorizationManager.class);
Mockito.when(AuthorizationManager.getInstance()).thenReturn(authorizationManager1);
Mockito.when(authorizationManager1.isUserAuthorized(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Log logMock = Mockito.mock(Log.class);
PowerMockito.mockStatic(LogFactory.class);
Mockito.when(LogFactory.getLog(any(Class.class))).thenReturn(logMock);
boolean expectedResult = APIUtil.hasPermission(userNameWithoutChange, permission);
Assert.assertEquals(true, expectedResult);
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIUtilTest method testIsRoleNameExist.
@Test
public void testIsRoleNameExist() throws Exception {
String userName = "John";
String roleName = "developer";
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.isExistingRole(roleName)).thenReturn(true);
Mockito.when(userStoreManager.isExistingRole("NonExistingDomain/role")).thenThrow(UserStoreException.class);
Mockito.when(userStoreManager.isExistingRole("NonExistingDomain/")).thenThrow(UserStoreException.class);
Assert.assertTrue(APIUtil.isRoleNameExist(userName, roleName));
Assert.assertFalse(APIUtil.isRoleNameExist(userName, "NonExistingDomain/role"));
Assert.assertFalse(APIUtil.isRoleNameExist(userName, "NonExistingDomain/"));
// allow adding empty role
Assert.assertTrue(APIUtil.isRoleNameExist(userName, ""));
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class StandaloneAuthorizationManagerClientTestCase method setup.
@Before
public void setup() throws Exception {
ServiceReferenceHolderMockCreator serviceReferenceHolderMockCreator = new ServiceReferenceHolderMockCreator(4444);
serviceReferenceHolder = serviceReferenceHolderMockCreator.getMock();
Mockito.when(serviceReferenceHolder.getUserRealm()).thenReturn(userRealm);
Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authorizationManager);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class UserSignUpWSWorkflowExecutorTest method init.
@Before
public void init() throws Exception {
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
RealmService realmService = Mockito.mock(RealmService.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
userStoreManager = Mockito.mock(UserStoreManager.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
serviceClient = Mockito.mock(ServiceClient.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(apiManagerConfiguration);
PowerMockito.mockStatic(SelfSignUpUtil.class);
PowerMockito.mockStatic(CarbonUtils.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.when(APIUtil.isAnalyticsEnabled()).thenReturn(true);
PowerMockito.doNothing().when(CarbonUtils.class, "setBasicAccessSecurityHeaders", Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any());
PowerMockito.when(serviceReferenceHolder.getContextService()).thenReturn(configurationContextService);
PowerMockito.when(configurationContextService.getClientConfigContext()).thenReturn(configurationContext);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.whenNew(ServiceClient.class).withAnyArguments().thenReturn(serviceClient);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
Mockito.when(realmService.getTenantUserRealm(tenantID)).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
apiMgtDAO = TestUtils.getApiMgtDAO();
userSignUpWSWorkflowExecutor = new UserSignUpWSWorkflowExecutor();
workflowDTO = new WorkflowDTO();
workflowDTO.setCallbackUrl(callBackURL);
workflowDTO.setTenantDomain(tenantDomain);
workflowDTO.setExternalWorkflowReference(externalWFReference);
workflowDTO.setWorkflowReference(testUsername + "@carbon.super");
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class SelfSignUpUtil method isUserNameWithAllowedDomainName.
/**
* Check whether user can signup to the tenant domain
*
* @param userName - The user name
* @param realm - The realm
* @return - A boolean value
* @throws APIManagementException
*/
public static boolean isUserNameWithAllowedDomainName(String userName, UserRealm realm) throws APIManagementException {
int index;
index = userName.indexOf('/');
// Check whether we have a secondary UserStoreManager setup.
if (index > 0) {
// Using the short-circuit. User name comes with the domain name.
try {
return !realm.getRealmConfiguration().isRestrictedDomainForSlefSignUp(userName.substring(0, index));
} catch (UserStoreException e) {
throw new APIManagementException(e.getMessage(), e);
}
}
return true;
}
Aggregations