Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasasr1 committed Nov 4, 2024
1 parent 552a8b8 commit 64268d0
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.INBOUND_AUTH_TYPE_OAUTH;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.LOGIN_HINT_PARAMETER;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.OIDC_CLAIM_DIALECT_URL;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORGANIZATION_DISCOVERY_TYPE;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_DISCOVERY_PARAMETER;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_DISCOVERY_TYPE_PARAMETER;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_ID_PARAMETER;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_PARAMETER;
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.SELF_REGISTRATION_PARAMETER;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_APPLICATION_NOT_SHARED;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_APPLICATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION_ID;
Expand Down Expand Up @@ -373,6 +376,62 @@ public void testProcessWithValidOrgIdParamSet() throws Exception {
Assert.assertEquals(status, AuthenticatorFlowStatus.INCOMPLETE);
}

@Test
public void testProcessWithSelfRegParam() throws Exception {

when(mockServletRequest.getParameter(SELF_REGISTRATION_PARAMETER)).thenReturn("true");
when(mockAuthenticationContext.getContextIdentifier()).thenReturn(contextIdentifier);
when(mockAuthenticationContext.getExternalIdP()).thenReturn(mockExternalIdPConfig);
when(mockExternalIdPConfig.getName()).thenReturn(AUTHENTICATOR_FRIENDLY_NAME);
when(mockAuthenticationContext.getServiceProviderResourceId()).thenReturn(saasAppResourceId);
when(authenticatorDataHolder.getOrganizationConfigManager().getDiscoveryConfiguration())
.thenReturn(mockDiscoveryConfig);
when(mockAuthenticationContext.getProperty(SELF_REGISTRATION_PARAMETER)).thenReturn("true");
AuthenticatorFlowStatus status = organizationAuthenticator.process(mockServletRequest, mockServletResponse,
mockAuthenticationContext);
Assert.assertEquals(status, AuthenticatorFlowStatus.INCOMPLETE);
}

@Test
public void testProcessWithSelfRegContext() throws Exception {

setMockContextParamForValidOrganization();
when(mockAuthenticationContext.getAuthenticatorProperties()).thenReturn(authenticatorProperties);
when(mockAuthenticationContext.getServiceProviderName()).thenReturn(saasApp);
when(mockAuthenticationContext.getTenantDomain()).thenReturn(saasAppOwnedTenant);
when(authenticatorDataHolder.getOrganizationManager().resolveOrganizationId(anyString()))
.thenReturn(saasAppOwnedOrgId);
when(authenticatorDataHolder.getOrganizationManager().resolveTenantDomain(anyString())).thenReturn(
orgId);
when(authenticatorDataHolder.getOrgApplicationManager()
.resolveSharedApplication(anyString(), anyString(), anyString())).thenReturn(mockServiceProvider);

List<Claim> claims = new ArrayList<>();
claims.add(new Claim(OIDC_CLAIM_DIALECT_URL, "custom", null));
when(authenticatorDataHolder.getClaimMetadataManagementService().getMappedExternalClaimsForLocalClaim(
anyString(), anyString())).thenReturn(claims);
when(mockServiceProvider.getInboundAuthenticationConfig()).thenReturn(mockInboundAuthenticationConfig);
when(mockServiceProvider.getClaimConfig()).thenReturn(mockClaimConfig);

InboundAuthenticationRequestConfig inbound = new InboundAuthenticationRequestConfig();
inbound.setInboundAuthType(INBOUND_AUTH_TYPE_OAUTH);
inbound.setInboundAuthKey(clientId);
InboundAuthenticationRequestConfig[] inbounds = {inbound};
when(mockInboundAuthenticationConfig.getInboundAuthenticationRequestConfigs()).thenReturn(inbounds);

when(authenticatorDataHolder.getOAuthAdminService().getOAuthApplicationData(anyString(), anyString()))
.thenReturn(mockOAuthConsumerAppDTO);
when(mockOAuthConsumerAppDTO.getOauthConsumerSecret()).thenReturn(secretKey);

when(mockAuthenticationContext.getProperty(SELF_REGISTRATION_PARAMETER)).thenReturn("true");
when(mockAuthenticationContext.getProperty(ORG_DISCOVERY_PARAMETER)).thenReturn(userEmailWithValidDomain);
when(mockAuthenticationContext.getProperty(ORGANIZATION_DISCOVERY_TYPE)).thenReturn(emailDomainDiscoveryType);
AuthenticatorFlowStatus status = organizationAuthenticator.process(mockServletRequest, mockServletResponse,
mockAuthenticationContext);
Assert.assertEquals(status, AuthenticatorFlowStatus.INCOMPLETE);

}

@Test(expectedExceptions = {AuthenticationFailedException.class})
public void testInitiateAuthenticationRequestWithoutOrgParameter() throws AuthenticationFailedException {

Expand Down

0 comments on commit 64268d0

Please sign in to comment.