use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class DefaultGroupIDExtractorImplTest method getGroupingIdentifierListTestCase.
@Test
public void getGroupingIdentifierListTestCase() throws UserStoreException {
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
RealmService realmService = Mockito.mock(RealmService.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(tenantManager.getTenantId("carbon.super")).thenReturn(-1234);
Mockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1,org2,org3");
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI)).thenReturn("http://wso2.org/claims/organization");
DefaultGroupIDExtractorImpl defaultGroupIDExtractor = new DefaultGroupIDExtractorImpl();
String[] expectedArr = new String[] { "org1", "org2", "org3" };
Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[0]);
Assert.assertEquals(expectedArr[1], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[1]);
Assert.assertEquals(expectedArr[2], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[2]);
Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1|org2|org3");
Assert.assertEquals("org1|org2|org3", defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn(null);
Assert.assertEquals(0, defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", " + "\"isSuperTenant\":false}").length);
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetDeniedTiers.
@Test
public void testGetDeniedTiers() throws APIManagementException, org.wso2.carbon.user.core.UserStoreException {
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("true", "false");
APIConsumerImpl apiConsumer = new UserAwareAPIConsumerWrapper(userRegistry, apiMgtDAO);
Mockito.when(userRegistry.getUserRealm()).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
String[] currentUserRoles = { "role1", "role2" };
PowerMockito.when(APIUtil.getListOfRoles(Mockito.anyString())).thenReturn(currentUserRoles);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
TierPermissionDTO tierPermissionDTO1 = new TierPermissionDTO();
tierPermissionDTO.setRoles(new String[] { "role1" });
Set<TierPermissionDTO> tierPermissionDTOs = new HashSet<TierPermissionDTO>();
tierPermissionDTOs.add(tierPermissionDTO);
Mockito.when(apiMgtDAO.getThrottleTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
tierPermissionDTO.setRoles(new String[] { "role3" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
Mockito.when(apiMgtDAO.getTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
tierPermissionDTOs.add(tierPermissionDTO1);
tierPermissionDTO1.setRoles(new String[] { "role4" });
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[] { "role1", "role2" });
tierPermissionDTO1.setRoles(new String[] { "role2" });
tierPermissionDTO1.setTierName("Silver");
Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 2);
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIUtil method isUserInRole.
/**
* Check whether the user has the given role
*
* @throws UserStoreException
* @throws APIManagementException
*/
public static boolean isUserInRole(String user, String role) throws UserStoreException, APIManagementException {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(user));
UserRegistrationConfigDTO signupConfig = SelfSignUpUtil.getSignupConfiguration(tenantDomain);
user = SelfSignUpUtil.getDomainSpecificUserName(user, signupConfig);
String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(user);
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
org.wso2.carbon.user.core.UserStoreManager manager = realm.getUserStoreManager();
AbstractUserStoreManager abstractManager = (AbstractUserStoreManager) manager;
return abstractManager.isUserInRole(tenantAwareUserName, role);
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIUtil method createRole.
/**
* Creates a role with a given set of permissions for the specified tenant
*
* @param roleName role name
* @param permissions a set of permissions to be associated with the role
* @param tenantId id of the tenant
* @throws APIManagementException
*/
public static void createRole(String roleName, Permission[] permissions, int tenantId) throws APIManagementException {
try {
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
UserRealm realm;
org.wso2.carbon.user.api.UserRealm tenantRealm;
UserStoreManager manager;
if (tenantId < 0) {
realm = realmService.getBootstrapRealm();
manager = realm.getUserStoreManager();
} else {
tenantRealm = realmService.getTenantUserRealm(tenantId);
manager = tenantRealm.getUserStoreManager();
}
if (!manager.isExistingRole(roleName)) {
if (log.isDebugEnabled()) {
log.debug("Creating role: " + roleName);
}
String tenantAdminName = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getRealmConfiguration().getAdminUserName();
String[] userList = new String[] { tenantAdminName };
manager.addRole(roleName, userList, permissions);
}
} catch (UserStoreException e) {
throw new APIManagementException("Error while creating role: " + roleName, e);
}
}
use of org.wso2.carbon.user.core.UserRealm in project carbon-apimgt by wso2.
the class APIUtilTest method testGetListOfRolesNonSuperTenant.
@Test
public void testGetListOfRolesNonSuperTenant() throws Exception {
int tenantID = 1;
String username = "Kelso";
String[] roles = { "PUBLISHER", "ADMIN", "TEST-ROLE" };
String tenantDomain = "Insta.com";
String tenantAwareUsername = "Insta_User";
PowerMockito.spy(APIUtil.class);
PowerMockito.doReturn(null).when(APIUtil.class, "getValueFromCache", APIConstants.API_USER_ROLE_CACHE, username);
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(username)).thenReturn(tenantDomain);
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);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(MultitenantUtils.getTenantAwareUsername(username)).thenReturn(tenantAwareUsername);
Mockito.when(userStoreManager.getRoleListOfUser(tenantAwareUsername)).thenReturn(roles);
PowerMockito.doNothing().when(APIUtil.class, "addToRolesCache", Mockito.any(), Mockito.any(), Mockito.any());
Assert.assertEquals(roles, APIUtil.getListOfRoles(username));
}
Aggregations