Skip to content

Commit

Permalink
Add new VerificationAuthenticatorConfig for verification authenticators
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Oct 25, 2024
1 parent 6b5a326 commit 943d689
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl;
import org.wso2.carbon.identity.application.common.dao.impl.CacheBackedAuthenticatorMgtDAO;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException;
Expand Down Expand Up @@ -124,7 +124,7 @@ public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name) {
public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name, String tenantDomain)
throws AuthenticatorMgtException {

/* First, heck whether an authenticator by the given name is in the system defined authenticators list.
/* First, check whether an authenticator by the given name is in the system defined authenticators list.
If not, check in user defined authenticators. */
for (LocalAuthenticatorConfig localAuthenticator : localAuthenticators) {
if (localAuthenticator.getName().equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Constants for authenticator configuration management service.
*/
public class AuthenticatorMgtConstants {
public class AuthenticatorMgtErrorConstants {

/**
* Error messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtSQLConstants.Column;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtSQLConstants.Query;
import org.wso2.carbon.identity.application.common.dao.AuthenticatorManagementDAO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

package org.wso2.carbon.identity.application.common.model.test;

import org.apache.commons.dbcp.BasicDataSource;
import org.mockito.MockedStatic;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.Property;
Expand All @@ -37,11 +37,10 @@
import org.wso2.carbon.identity.common.testng.WithRegistry;
import org.wso2.carbon.identity.core.internal.IdentityCoreServiceDataHolder;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

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

/**
* This class is a test suite for the ApplicationAuthenticatorServiceTest class.
Expand All @@ -57,8 +56,8 @@ public class ApplicationAuthenticatorServiceTest {

private MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil;
private String tenantDomain;
private static final Map<String, BasicDataSource> dataSourceMap = new HashMap<>();
private ApplicationAuthenticatorService authenticatorService;

private ApplicationAuthenticatorService authenticatorService;;
private LocalAuthenticatorConfig authenticatorConfig1;
private LocalAuthenticatorConfig authenticatorConfig2;
private LocalAuthenticatorConfig nonExistAuthenticatorConfig;
Expand All @@ -74,11 +73,21 @@ public void setUpClass() throws Exception {

tenantDomain = "carbon.super";
authenticatorService = ApplicationAuthenticatorService.getInstance();

authenticatorConfig1 = createAuthenticatorConfig(authenticator1Name, DefinedByType.USER);
authenticatorConfig2 = createAuthenticatorConfig(authenticator2Name, DefinedByType.USER);
nonExistAuthenticatorConfig = createAuthenticatorConfig(nonExistAuthenticator, DefinedByType.USER);
systemAuthenticatorConfig = createAuthenticatorConfig(systemAuthenticator, DefinedByType.SYSTEM);

authenticatorService.addLocalAuthenticator(systemAuthenticatorConfig);
addSystemAuthenticator();
}

private void addSystemAuthenticator() throws AuthenticatorMgtException {

AuthenticatorManagementDAOImpl dao = new AuthenticatorManagementDAOImpl();
dao.addUserDefinedLocalAuthenticator(systemAuthenticatorConfig, MultitenantConstants.SUPER_TENANT_ID,
AuthenticationType.IDENTIFICATION);

}

@DataProvider(name = "authenticatorConfigForCreation")
Expand Down Expand Up @@ -120,8 +129,8 @@ public void testCreateUserDefinedLocalAuthenticatorWithExistingAuthenticator(Loc
expectedExceptionsMessageRegExp = "No operations allowed on system authenticators.")
public void testCreateUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws AuthenticatorMgtException {

authenticatorService.createUserDefinedLocalAuthenticator(
systemAuthenticatorConfig, AuthenticationType.IDENTIFICATION, tenantDomain);
authenticatorService.createUserDefinedLocalAuthenticator(createAuthenticatorConfig(systemAuthenticator + "new",
DefinedByType.SYSTEM), AuthenticationType.IDENTIFICATION, tenantDomain);
}

@DataProvider(name = "authenticatorConfigToModify")
Expand Down Expand Up @@ -164,6 +173,13 @@ public void testUpdateUserDefinedLocalAuthenticatorWithNonExistingAuthenticator(
authenticatorService.updateUserDefinedLocalAuthenticator(nonExistAuthenticatorConfig, tenantDomain);
}

@Test(priority = 6, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No operations allowed on system authenticators.")
public void testUpdateUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws AuthenticatorMgtException {

authenticatorService.updateUserDefinedLocalAuthenticator(systemAuthenticatorConfig, tenantDomain);
}

@DataProvider(name = "authenticatorConfigToRetrieve")
public Object[][] authenticatorConfigToRetrieve() {

Expand All @@ -175,7 +191,7 @@ public Object[][] authenticatorConfigToRetrieve() {
};
}

@Test(priority = 6, dataProvider = "authenticatorConfigToRetrieve")
@Test(priority = 7, dataProvider = "authenticatorConfigToRetrieve")
public void testGetUserDefinedLocalAuthenticator(LocalAuthenticatorConfig configToBeRetrieved,
LocalAuthenticatorConfig expectedConfig, String type) throws AuthenticatorMgtException {

Expand All @@ -194,28 +210,46 @@ public void testGetUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config
}
}

@Test(priority = 7)
@Test(priority = 8)
public void testGetAllUserDefinedLocalAuthenticator() throws AuthenticatorMgtException {

List<LocalAuthenticatorConfig> retrievedConfig = authenticatorService.getLocalAuthenticators(tenantDomain);
Assert.assertEquals(retrievedConfig.size(), 2);
Assert.assertEquals(retrievedConfig.size(), 3);
}

@Test(priority = 9)
public void testGetUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws AuthenticatorMgtException {

LocalAuthenticatorConfig retrievedConfig = authenticatorService.getLocalAuthenticatorByName(
systemAuthenticatorConfig.getName(), tenantDomain);
Assert.assertNotNull(retrievedConfig);
Assert.assertEquals(retrievedConfig.getName(), systemAuthenticatorConfig.getName());
Assert.assertEquals(retrievedConfig.getDisplayName(), systemAuthenticatorConfig.getDisplayName());
Assert.assertEquals(retrievedConfig.getDefinedByType(), DefinedByType.SYSTEM);
Assert.assertEquals(retrievedConfig.getProperties().length, systemAuthenticatorConfig.getProperties().length);
}

@Test(priority = 8, dataProvider = "authenticatorConfigToModify")
@Test(priority = 10, dataProvider = "authenticatorConfigToModify")
public void testDeleteUserDefinedLocalAuthenticator(LocalAuthenticatorConfig config)
throws AuthenticatorMgtException {

authenticatorService.deleteUserDefinedLocalAuthenticator(config.getName(), tenantDomain);
Assert.assertNull(authenticatorService.getLocalAuthenticatorByName(config.getName()));
}

@Test(priority = 9, expectedExceptions = AuthenticatorMgtException.class,
@Test(priority = 11, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No Authenticator is found.")
public void testDeleteUserDefinedLocalAuthenticatorWithNonExistingAuthenticator() throws AuthenticatorMgtException {

authenticatorService.deleteUserDefinedLocalAuthenticator(nonExistAuthenticatorConfig.getName(), tenantDomain);
}

@Test(priority = 12, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "No operations allowed on system authenticators.")
public void testDeleteUserDefinedLocalAuthenticatorWithSystemAuthenticator() throws AuthenticatorMgtException {

authenticatorService.deleteUserDefinedLocalAuthenticator(systemAuthenticatorConfig.getName(), tenantDomain);
}

private LocalAuthenticatorConfig createAuthenticatorConfig(String uniqueIdentifier, DefinedByType definedByType) {

Expand Down

0 comments on commit 943d689

Please sign in to comment.