Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 26, 2025
1 parent 1533048 commit 98a9317
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

package org.wso2.carbon.identity.application.authentication.framework.internal.core;

import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.util.ActionExecutorConfig;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.idp.mgt.IdentityProviderManager;

import java.util.ArrayList;
Expand All @@ -39,6 +38,9 @@ public class ApplicationAuthenticatorManager {
private static final ApplicationAuthenticatorManager instance = new ApplicationAuthenticatorManager();
private final List<ApplicationAuthenticator> systemDefinedAuthenticators = new ArrayList<>();

private static final String AUTHENTICATION_ACTION_ENABLED_PROP =
"Actions.Types.Authentication.Enable";

public static ApplicationAuthenticatorManager getInstance() {

return instance;
Expand Down Expand Up @@ -100,7 +102,7 @@ public List<ApplicationAuthenticator> getAllAuthenticators(String tenantDomain)

List<ApplicationAuthenticator> allAuthenticators = new ArrayList<>(systemDefinedAuthenticators);

if (!ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION) ||
if (!isAuthenticationActionEnabled() ||
FrameworkServiceDataHolder.getInstance().getUserDefinedAuthenticatorService() == null) {
return allAuthenticators;
}
Expand Down Expand Up @@ -143,7 +145,7 @@ public ApplicationAuthenticator getApplicationAuthenticatorByName(String authent
}
}

if (!ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION) ||
if (!isAuthenticationActionEnabled() ||
FrameworkServiceDataHolder.getInstance().getUserDefinedAuthenticatorService() == null) {
return null;
}
Expand Down Expand Up @@ -172,4 +174,10 @@ public ApplicationAuthenticator getApplicationAuthenticatorByName(String authent
throw new RuntimeException("Error while getting the authenticator for the name: " + authenticatorName, e);
}
}

private boolean isAuthenticationActionEnabled() {

return Boolean.parseBoolean((String) IdentityConfigParser.getInstance()
.getConfiguration().get(AUTHENTICATION_ACTION_ENABLED_PROP));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,19 @@ public String getI18nKey() {

return this.name + ".authenticator";
}

public static class MockLocalAuthenticator extends MockAuthenticator implements LocalApplicationAuthenticator {

public MockLocalAuthenticator(String name) {
super(name);
}
}

public static class MockFederatedAuthenticator extends MockAuthenticator
implements FederatedApplicationAuthenticator {

public MockFederatedAuthenticator(String name) {
super(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.wso2.carbon.identity.application.common.model.JustInTimeProvisioningConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.core.internal.IdentityCoreServiceComponent;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManagerImpl;
Expand All @@ -69,6 +70,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -105,6 +107,9 @@ public class JITProvisioningPostAuthenticationHandlerTest extends AbstractFramew
private MockedStatic<CarbonUtils> carbonUtils;
private MockedStatic<PrivilegedCarbonContext> privilegedCarbonContextMockedStatic;

private IdentityConfigParser mockIdentityConfigParser;
private MockedStatic<IdentityConfigParser> identityConfigParser;

@BeforeClass
protected void setupSuite() throws XMLStreamException, IdentityProviderManagementException {

Expand All @@ -130,6 +135,11 @@ protected void setupSuite() throws XMLStreamException, IdentityProviderManagemen
sp = getTestServiceProvider("default-sp-1.xml");
carbonUtils = mockStatic(CarbonUtils.class);
privilegedCarbonContextMockedStatic = mockStatic(PrivilegedCarbonContext.class);

mockIdentityConfigParser = mock(IdentityConfigParser.class);
identityConfigParser = mockStatic(IdentityConfigParser.class);
identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser);
setAuthenticatorActionEnableStatus(false);
}

@AfterClass
Expand All @@ -138,6 +148,7 @@ protected void cleanup() {
configurationFacade.close();
carbonUtils.close();
privilegedCarbonContextMockedStatic.close();
identityConfigParser.close();
}

@Test(description = "This test case tests the Post JIT provisioning handling flow without an authenticated user")
Expand Down Expand Up @@ -325,4 +336,11 @@ private void initAuthenticators() {
authenticatorManager.addSystemDefinedAuthenticator(new MockAuthenticator("HwkMockAuthenticator"));
authenticatorManager.addSystemDefinedAuthenticator(new MockAuthenticator("FptMockAuthenticator"));
}

private void setAuthenticatorActionEnableStatus(boolean isEnabled) {

Map<String, Object> configMap = new HashMap<>();
configMap.put("Actions.Types.Authentication.Enable", Boolean.toString(isEnabled));
when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2025, 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.application.authentication.framework.internal.core;

import org.mockito.MockedStatic;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.MockAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.UserDefinedAuthenticatorService;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.idp.mgt.IdentityProviderManager;

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

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@Listeners(MockitoTestNGListener.class)
public class ApplicationAuthenticatorManagerTest extends AbstractFrameworkTest {

private final ApplicationAuthenticatorManager applicationAuthenticatorService =
ApplicationAuthenticatorManager.getInstance();

private static final String SYSTEM_DEFINED_AUTHENTICATOR_NAME = "BasicAuthenticator";
private static final String USER_DEFINED_LOCAL_AUTHENTICATOR_NAME = "UserDefinedLocalMockAuthenticator";
private static final String USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME = "UserDefinedFederatedMockAuthenticator";
private static final String TENANT_DOMAIN = "carbon.super";

private static final ApplicationAuthenticator systemDefinedAuthenticator =
new MockAuthenticator(SYSTEM_DEFINED_AUTHENTICATOR_NAME);
private static final LocalApplicationAuthenticator userDefinedLocalAuthenticator =
new MockAuthenticator.MockLocalAuthenticator(USER_DEFINED_LOCAL_AUTHENTICATOR_NAME);
private static final FederatedApplicationAuthenticator userDefinedFederatedAuthenticator =
new MockAuthenticator.MockFederatedAuthenticator(USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME);

private static UserDefinedAuthenticatorService userDefinedAuthenticatorService =
mock(UserDefinedAuthenticatorService.class);

private IdentityConfigParser mockIdentityConfigParser;
private MockedStatic<IdentityConfigParser> identityConfigParser;

private final MockedStatic<ApplicationAuthenticatorService> mockedAuthenticationService =
mockStatic(ApplicationAuthenticatorService.class);
private final ApplicationAuthenticatorService authenticatorService = mock(ApplicationAuthenticatorService.class);

private final MockedStatic<IdentityProviderManager> mockedIdentityProviderManager =
mockStatic(IdentityProviderManager.class);
private final IdentityProviderManager identityProviderManager = mock(IdentityProviderManager.class);

@BeforeClass
public void setUp() throws Exception {

mockIdentityConfigParser = mock(IdentityConfigParser.class);
identityConfigParser = mockStatic(IdentityConfigParser.class);

removeAllSystemDefinedAuthenticators();
applicationAuthenticatorService.addSystemDefinedAuthenticator(systemDefinedAuthenticator);
when(userDefinedAuthenticatorService.getUserDefinedLocalAuthenticator(any())).thenReturn(
userDefinedLocalAuthenticator);
when(userDefinedAuthenticatorService.getUserDefinedFederatedAuthenticator(any())).thenReturn(
userDefinedFederatedAuthenticator);
FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService);

identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser);

mockedAuthenticationService.when(ApplicationAuthenticatorService::getInstance).thenReturn(authenticatorService);
when(authenticatorService.getAllUserDefinedLocalAuthenticators(TENANT_DOMAIN)).thenReturn(
List.of(new UserDefinedLocalAuthenticatorConfig(
AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION)));

mockedIdentityProviderManager.when(IdentityProviderManager::getInstance).thenReturn(identityProviderManager);
when(identityProviderManager.getAllFederatedAuthenticators(TENANT_DOMAIN)).thenReturn(new
FederatedAuthenticatorConfig[]{new UserDefinedFederatedAuthenticatorConfig()});
}

@AfterClass
public void tearDown() {

identityConfigParser.close();
mockedAuthenticationService.close();
mockedIdentityProviderManager.close();
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNotNullUserDefinedAuthenticatorService() {

setAuthenticatorActionEnableStatus(true);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(3, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() {

FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null);
setAuthenticatorActionEnableStatus(true);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthenticationActionTypeDisabled() {

setAuthenticatorActionEnableStatus(false);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}

private void setAuthenticatorActionEnableStatus(boolean isEnabled) {

Map<String, Object> configMap = new HashMap<>();
configMap.put("Actions.Types.Authentication.Enable", Boolean.toString(isEnabled));
when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<class name="org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityResponseFactoryTest"/>

<class name="org.wso2.carbon.identity.application.authentication.framework.internal.impl.AuthenticationMethodNameTranslatorImplTest"/>
<class name="org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManagerTest"/>

<class name="org.wso2.carbon.identity.application.authentication.framework.services.PostAuthenticationMgtServiceTest"/>
<class name="org.wso2.carbon.identity.application.authentication.framework.services.ConditionalAuthenticationMgtServiceTest"/>
Expand Down

0 comments on commit 98a9317

Please sign in to comment.