Skip to content

Commit

Permalink
Merge pull request #406 from sadilchamishka/improve-unit-tests
Browse files Browse the repository at this point in the history
Add unit test for validate invite for sub org console role
  • Loading branch information
sadilchamishka authored Oct 25, 2024
2 parents 4946a0a + 8c75c3d commit 67c14ca
Showing 1 changed file with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@
import org.wso2.carbon.identity.recovery.util.Utils;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.role.v2.mgt.core.model.Role;
import org.wso2.carbon.identity.role.v2.mgt.core.model.RoleBasicInfo;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.common.Group;
import org.wso2.carbon.user.core.service.RealmService;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -116,6 +119,9 @@ public class InvitationCoreServiceImplTest {
private UserRealm userRealm;
@Mock
private AbstractUserStoreManager userStoreManager;
@Mock
private RoleManagementService roleManagementService;

@Mock
private IdentityEventService identityEventService;

Expand Down Expand Up @@ -164,6 +170,11 @@ public void setUp() throws Exception {
when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
when(userStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(userStoreManager);
doNothing().when(identityEventService).handleEvent(isA(Event.class));
when(userStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(userStoreManager);

UserInvitationMgtDataHolder.getInstance().setRoleManagementService(roleManagementService);
when(roleManagementService.getRoleWithoutUsers(anyString(), anyString())).thenReturn(buildRoleInfo());
when(roleManagementService.isExistingRole(anyString(), anyString())).thenReturn(true);
}

private Role buildRoleInfo() {
Expand All @@ -184,6 +195,7 @@ public void tearDown() throws Exception {
Mockito.reset(userRealm);
Mockito.reset(userStoreManager);
Mockito.reset(identityEventService);
Mockito.reset(roleManagementService);
}


Expand All @@ -204,9 +216,6 @@ public void testGetInvitation() throws Exception {

identityUtil.when(() -> IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234);
identityDBUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(getConnection());
RoleManagementService roleManagementService = mock(RoleManagementService.class);
UserInvitationMgtDataHolder.getInstance().setRoleManagementService(roleManagementService);
when(roleManagementService.getRoleWithoutUsers(anyString(), anyString())).thenReturn(buildRoleInfo());
OrganizationManager organizationManager = mock(OrganizationManager.class);
UserInvitationMgtDataHolder.getInstance().setOrganizationManagerService(organizationManager);
when(organizationManager.resolveTenantDomain(anyString())).thenReturn("carbon.super");
Expand Down Expand Up @@ -349,22 +358,22 @@ public Object[][] inviteNotificationManagingData() {

return new Object[][]{
{
true, "false", "true", true
true, "false", "true", true, true
},
{
true, "false", "false", true
true, "false", "false", true, false
},
{
false, null, "false", true
true, "true", "true", false, true
},
{
true, "true", "false", false
true, "true", "false", false, false
},
{
true, "true", "true", false
false, null, "false", true, true
},
{
false, null, "true", false
false, null, "true", false, false
}
};
}
Expand All @@ -373,7 +382,8 @@ public Object[][] inviteNotificationManagingData() {
public void testConfirmationCodeReturnOnInviteCreation(boolean setNotificationManagingProperty,
String propertyValue,
String isNotificationManagedInternallyForOrg,
boolean isConfirmationCodeReturnInResponse)
boolean isConfirmationCodeReturnInResponse,
boolean inviteConsoleRole)
throws Exception {

String username = "alex";
Expand All @@ -388,7 +398,14 @@ public void testConfirmationCodeReturnOnInviteCreation(boolean setNotificationMa
InvitationDO invitation = new InvitationDO();
invitation.setUsernamesList(Collections.singletonList(username));
invitation.setUserDomain(userStoreDomain);
invitation.setRoleAssignments(null);

if (inviteConsoleRole) {
RoleAssignments roleAssignments = buildRoleAssignments(roleList);
roleAssignments.setRole(roleList[0]);
invitation.setRoleAssignments(new RoleAssignments[]{roleAssignments});
invitation.setGroupAssignments(new GroupAssignments[]{buildGroupAssignments(groupList)});
}

invitation.setUserRedirectUrl("https://localhost:8080/travel-manager-001/invitations/accept");
if (setNotificationManagingProperty) {
invitation.setInvitationProperties(
Expand Down Expand Up @@ -423,18 +440,29 @@ public void testConfirmationCodeReturnOnInviteCreation(boolean setNotificationMa

UserRealm userRealmParentOrg = mock(UserRealm.class);
AbstractUserStoreManager userStoreManagerParentOrg = mock(AbstractUserStoreManager.class);
AbstractUserStoreManager secondaryUserStoreManagerParentOrg = mock(AbstractUserStoreManager.class);

if (inviteConsoleRole) {
RoleBasicInfo roleBasicInfo = new RoleBasicInfo();
roleBasicInfo.setAudienceName("Console");
List<Group> groupsList = new ArrayList<>();
groupsList.add(new Group("123", "adminGroup"));
when(userStoreManagerParentOrg.getGroupListOfUser(anyString(), any(), any()))
.thenReturn(groupsList);
when(roleManagementService.getRoleListOfGroups(any(), anyString()))
.thenReturn(Collections.singletonList(roleBasicInfo));
}

mockParentOrgDetails(userStoreManagerParentOrg, userStoreQualifiedUsername, userId, userRealmParentOrg,
realmService, tenantDomainOfParentOrg, organizationManager, parentOrgId, identityDBUtil);
when(userStoreManagerParentOrg.getSecondaryUserStoreManager(anyString()))
.thenReturn(userStoreManagerParentOrg);
.thenReturn(secondaryUserStoreManagerParentOrg);

UserRealm userRealmSubOrg = mock(UserRealm.class);
AbstractUserStoreManager userStoreManagerSubOrg = mock(AbstractUserStoreManager.class);
mockSubOrgDetails(userStoreManagerSubOrg, userStoreQualifiedUsername, userRealmSubOrg, realmService,
tenantDomainOfSubOrg, organizationManager, subOrgId, identityDBUtil);

when(userStoreManagerParentOrg.getSecondaryUserStoreManager(anyString()))
.thenReturn(userStoreManagerParentOrg);
when(userStoreManagerSubOrg.isGroupExist(anyString())).thenReturn(true);

orgSharedUserUtil.when(() -> OrganizationSharedUserUtil
.getUserManagedOrganizationClaim(userStoreManagerSubOrg, userId)).thenReturn(parentOrgId);
Expand Down

0 comments on commit 67c14ca

Please sign in to comment.