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 6eefa53
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
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
@@ -0,0 +1,126 @@
/*
* 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.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
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.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.idp.mgt.IdentityProviderManager;

import java.util.List;

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;

@Test
public class ApplicationAuthenticatorManagerTest {

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 final MockedStatic<ActionExecutorConfig> mockedActionExecutorConfig =
mockStatic(ActionExecutorConfig.class);
private final ActionExecutorConfig actionExecutorConfig = mock(ActionExecutorConfig.class);

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 {

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

mockedActionExecutorConfig.when(ActionExecutorConfig::getInstance).thenReturn(actionExecutorConfig);

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()});
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNotNullUserDefinedAuthenticatorService() {

when(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION)).thenReturn(true);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(3, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() {

FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null);
when(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION)).thenReturn(true);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}

@Test
public void testGetAllAuthenticatorsWithAuthenticationActionTypeDisabled() throws Exception {

when(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION)).thenReturn(false);
List<ApplicationAuthenticator> result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN);
assertEquals(1, result.size());
}
}
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 6eefa53

Please sign in to comment.