Skip to content

Commit

Permalink
Add unit tests for user def auth mgt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 3, 2024
1 parent b652192 commit 9acc1e1
Show file tree
Hide file tree
Showing 7 changed files with 3,447 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.action.management</artifactId>
Expand Down Expand Up @@ -90,7 +105,11 @@
org.apache.axiom.*; version="${axiom.osgi.version.range}",
org.apache.commons.logging; version="${import.package.version.commons.logging}",
org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}",
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.apache.commons.collections; version="${commons-collections.wso2.osgi.version.range}",
org.wso2.carbon.database.utils.jdbc; version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.database.utils.jdbc.exceptions; version="${org.wso2.carbon.database.utils.version.range}",

org.apache.axis2.*; version="${axis2.osgi.version.range}",

Expand All @@ -106,6 +125,7 @@
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.central.log.mgt.*; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.action.management.*;version="${carbon.identity.package.import.version.range}",

com.fasterxml.jackson.annotation; version="${com.fasterxml.jackson.annotation.version.range}"
</Import-Package>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.application.common.model.test.util;

import org.wso2.carbon.identity.action.management.model.Action;
import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig;

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

public class ActionMgtTestUtil {

public static final String ASSOCIATED_ACTION_ID = "Dummy_Action_ID";

public static Action createAction(EndpointConfig endpointConfig) {

Action.ActionResponseBuilder actionResponseBuilder = new Action.ActionResponseBuilder();
actionResponseBuilder.id(ASSOCIATED_ACTION_ID);
actionResponseBuilder.name("SampleAssociatedAction");
actionResponseBuilder.type(Action.ActionTypes.AUTHENTICATION);
actionResponseBuilder.description("SampleDescription");
actionResponseBuilder.status(Action.Status.ACTIVE);
actionResponseBuilder.endpoint(endpointConfig);
return actionResponseBuilder.build();
}

public static EndpointConfig createEndpointConfig(String uri, String username, String password) {

EndpointConfig.EndpointConfigBuilder endpointConfigBuilder = new EndpointConfig.EndpointConfigBuilder();
endpointConfigBuilder.uri(uri);
endpointConfigBuilder.authentication(
new Authentication.BasicAuthBuilder(username, password).build());
return endpointConfigBuilder.build();
}

public static IdentityProvider createIdPWithUserDefinedFederatedAuthenticatorConfig(String idpName,
EndpointConfig endpointConfig) {

// Initialize Test Identity Provider 4 with custom user defined federated authenticator.
IdentityProvider newUserDefinedIdp = new IdentityProvider();
newUserDefinedIdp.setIdentityProviderName(idpName);

UserDefinedFederatedAuthenticatorConfig userDefinedFederatedAuthenticatorConfig = new
UserDefinedFederatedAuthenticatorConfig();
userDefinedFederatedAuthenticatorConfig.setDisplayName("DisplayName1");
userDefinedFederatedAuthenticatorConfig.setName("customFedAuthenticator");
userDefinedFederatedAuthenticatorConfig.setEnabled(true);
userDefinedFederatedAuthenticatorConfig.setEndpointConfig(
buildUserDefinedAuthenticatorEndpointConfig(endpointConfig));
Property property = new Property();
property.setName("actionId");
property.setValue(ASSOCIATED_ACTION_ID);
property.setConfidential(false);
userDefinedFederatedAuthenticatorConfig.setProperties(new Property[]{property});
newUserDefinedIdp.setFederatedAuthenticatorConfigs(
new FederatedAuthenticatorConfig[]{userDefinedFederatedAuthenticatorConfig});
newUserDefinedIdp.setDefaultAuthenticatorConfig(userDefinedFederatedAuthenticatorConfig);
return newUserDefinedIdp;
}

public static UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig(
EndpointConfig endpointConfig) {

UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder =
new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder();
endpointConfigBuilder.uri(endpointConfig.getUri());
endpointConfigBuilder.authenticationType(endpointConfig.getAuthentication().getType().getName());
Map<String, String> propMap = new HashMap<>();
endpointConfig.getAuthentication().getProperties()
.forEach(prop -> propMap.put(prop.getName(), prop.getValue()));
endpointConfigBuilder.authenticationProperties(propMap);
return endpointConfigBuilder.build();
}
}
Loading

0 comments on commit 9acc1e1

Please sign in to comment.