Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip email domain validation for shared user creation #405

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit test for skipping email domain verification for shared user
HasiniSama committed Oct 24, 2024
commit 36b13b1a92170b95f81beaf9a69e9bdac9be7939
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.discovery.service.listener;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.organization.discovery.service.util.TestUtils;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.internal.OrganizationManagementDataHolder;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.Tenant;
import org.wso2.carbon.user.core.tenant.TenantManager;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertTrue;
import static org.wso2.carbon.identity.organization.management.organization.user.sharing.constant.UserSharingConstants.CLAIM_MANAGED_ORGANIZATION;
import static org.wso2.carbon.user.core.UserCoreConstants.DEFAULT_PROFILE;

public class OrganizationDiscoveryUserOperationListenerTest {
HasiniSama marked this conversation as resolved.
Show resolved Hide resolved

private static final String TEST_ORG_ID = "10084a8d-113f-4211-a0d5-efe36b082211";
private static final String TEST_TENANT_DOMAIN = "example.com";
private static final String TEST_USER = "testUser";
private static final Object TEST_CREDENTIALS = "dummyPassword";
private static final int TEST_TENANT_ID = 1234;

@InjectMocks
private OrganizationDiscoveryUserOperationListener organizationDiscoveryUserOperationListener;

@Mock
private UserStoreManager userStoreManager;

@Mock
private RealmService realmService;

@Mock
private TenantManager tenantManager;

@Mock
private Tenant tenant;

@Mock
private OrganizationManager organizationManager;

@BeforeMethod
public void setUp() throws Exception {

MockitoAnnotations.openMocks(this);

TestUtils.initiateH2Base();
TestUtils.mockDataSource();

PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(TEST_TENANT_DOMAIN);
OrganizationManagementDataHolder.getInstance().setRealmService(realmService);
OrganizationManagementDataHolder.getInstance().setOrganizationManager(organizationManager);

when(realmService.getTenantManager()).thenReturn(tenantManager);
when(tenantManager.getTenantId(TEST_TENANT_DOMAIN)).thenReturn(TEST_TENANT_ID);
when(tenantManager.getTenant(TEST_TENANT_ID)).thenReturn(tenant);
when(tenant.getAssociatedOrganizationUUID()).thenReturn(TEST_ORG_ID);
when(organizationManager.getOrganizationDepthInHierarchy(TEST_ORG_ID)).thenReturn(1);
}

@Test
public void testSkipEmailDomainValidationForSharedUserCreation() throws UserStoreException {

Map<String, String> claims = new HashMap<>();
claims.put(CLAIM_MANAGED_ORGANIZATION, TEST_ORG_ID);

boolean result = organizationDiscoveryUserOperationListener.doPreAddUserWithID(TEST_USER, TEST_CREDENTIALS,
new String[]{}, claims, DEFAULT_PROFILE, userStoreManager);

assertTrue("Email domain validation should be skipped for shared user creation.", result);
}

@AfterMethod
public void tearDown() {

PrivilegedCarbonContext.endTenantFlow();
sadilchamishka marked this conversation as resolved.
Show resolved Hide resolved
}
}

Unchanged files with check annotations Beta

return true;
}
if (claims.containsKey(CLAIM_MANAGED_ORGANIZATION)) {
return true;

Check warning on line 85 in components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/listener/OrganizationDiscoveryUserOperationListener.java

Codecov / codecov/patch

components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/listener/OrganizationDiscoveryUserOperationListener.java#L85

Added line #L85 was not covered by tests
}
String organizationId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getOrganizationId();
if (StringUtils.isBlank(organizationId)) {