-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow sub org level application deletion when the application is a su…
…b org app
- Loading branch information
1 parent
3833c81
commit 81768f3
Showing
2 changed files
with
149 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...2/carbon/identity/organization/management/handler/listener/SharedRoleMgtListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2024, 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.organization.management.handler.listener; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.MockitoAnnotations; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig; | ||
import org.wso2.carbon.identity.application.common.model.RoleV2; | ||
import org.wso2.carbon.identity.application.common.model.ServiceProvider; | ||
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; | ||
import org.wso2.carbon.identity.core.util.IdentityTenantUtil; | ||
import org.wso2.carbon.identity.organization.management.handler.internal.OrganizationManagementHandlerDataHolder; | ||
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* Contains unit tests for SharedRoleMgtListener. | ||
*/ | ||
public class SharedRoleMgtListenerTest { | ||
|
||
@Mock | ||
private ApplicationManagementService mockedApplicationManagementService; | ||
|
||
private MockedStatic<OrganizationManagementUtil> organizationManagementUtilMockedStatic; | ||
private MockedStatic<IdentityTenantUtil> identityTenantUtilMockedStatic; | ||
|
||
@BeforeClass | ||
public void setUpClass() { | ||
|
||
MockitoAnnotations.openMocks(this); | ||
OrganizationManagementHandlerDataHolder.getInstance(). | ||
setApplicationManagementService(mockedApplicationManagementService); | ||
organizationManagementUtilMockedStatic = mockStatic(OrganizationManagementUtil.class); | ||
identityTenantUtilMockedStatic = mockStatic(IdentityTenantUtil.class); | ||
} | ||
|
||
@DataProvider(name = "organizationTypeDataProvider") | ||
public Object[][] organizationTypeDataProvider() { | ||
|
||
// Create a ServiceProvider object for a main application. | ||
ServiceProvider serviceProvider = new ServiceProvider(); | ||
serviceProvider.setApplicationName("sampleApplicationName"); | ||
|
||
return new Object[][]{ | ||
{false, null, true}, | ||
{true, serviceProvider, true}, | ||
{true, null, false}, | ||
}; | ||
} | ||
|
||
@DataProvider(name = "applicationDataProvider") | ||
public Object[][] applicationDataProvider() { | ||
|
||
return new Object[][] { | ||
{null}, | ||
{"main-app-id"} | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "organizationTypeDataProvider") | ||
public void testDoPreDeleteApplicationInOrgTypes(boolean isOrganization, ServiceProvider serviceProvider, | ||
boolean expected) throws Exception { | ||
|
||
organizationManagementUtilMockedStatic.when(() -> OrganizationManagementUtil.isOrganization(anyString())) | ||
.thenReturn(isOrganization); | ||
SharedRoleMgtListener sharedRoleMgtListener = new SharedRoleMgtListener(); | ||
if (!isOrganization) { | ||
assertEquals(sharedRoleMgtListener.doPreDeleteApplication("sampleApplicationId", "sampleTenantDomain", | ||
"sampleUsername"), expected); | ||
} else { | ||
when(mockedApplicationManagementService.getServiceProvider("sampleApplicationId", "sampleTenantDomain")) | ||
.thenReturn(serviceProvider); | ||
assertEquals(sharedRoleMgtListener.doPreDeleteApplication("sampleApplicationId", "sampleTenantDomain", | ||
"sampleUsername"), expected); | ||
} | ||
} | ||
|
||
@Test(dataProvider = "applicationDataProvider") | ||
public void testDoPostGetAllowedAudienceForRoleAssociation(String mainAppId) throws Exception { | ||
|
||
AssociatedRolesConfig associatedRolesConfig = new AssociatedRolesConfig(); | ||
RoleV2 roleV2 = new RoleV2(); | ||
roleV2.setName("sampleRoleName"); | ||
associatedRolesConfig.setRoles(new RoleV2[]{roleV2}); | ||
SharedRoleMgtListener sharedRoleMgtListener = new SharedRoleMgtListener(); | ||
when(mockedApplicationManagementService.getMainAppId("1234-1234")).thenReturn(mainAppId); | ||
when(mockedApplicationManagementService.getTenantIdByApp(mainAppId)).thenReturn(12345); | ||
identityTenantUtilMockedStatic.when(() -> IdentityTenantUtil.getTenantDomain(12345)).thenReturn("sampleTenant"); | ||
when(mockedApplicationManagementService.getAllowedAudienceForRoleAssociation(mainAppId, "sampleTenant")).thenReturn("organization"); | ||
assertTrue(sharedRoleMgtListener.doPostGetAllowedAudienceForRoleAssociation(associatedRolesConfig, "1234-1234", | ||
"sampleUsername")); | ||
if (StringUtils.isNotEmpty(mainAppId)) { | ||
assertEquals(associatedRolesConfig.getAllowedAudience(), "organization"); | ||
} | ||
} | ||
} |