Skip to content

Commit

Permalink
Merge pull request #16354 from ashanthamara/update_int_tests
Browse files Browse the repository at this point in the history
Update integration tests to use REST APIs in master branch (III)
  • Loading branch information
madurangasiriwardena authored Aug 9, 2023
2 parents 551acac + 0152c09 commit 35f8186
Show file tree
Hide file tree
Showing 42 changed files with 3,034 additions and 2,012 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.json.JSONException;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.extensions.servers.carbonserver.MultipleServersManager;
import org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProvider;
Expand All @@ -46,6 +47,13 @@
import org.wso2.identity.integration.common.utils.CarbonTestServerManager;
import org.wso2.identity.integration.common.utils.ISIntegrationTest;
import org.wso2.identity.integration.test.base.TestDataHolder;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationModel;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationResponseModel;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.SAML2ServiceProvider;
import org.wso2.identity.integration.test.rest.api.server.idp.v1.model.IdentityProviderPOSTRequest;
import org.wso2.identity.integration.test.restclients.IdpMgtRestClient;
import org.wso2.identity.integration.test.restclients.OAuth2RestClient;
import org.wso2.identity.integration.test.utils.CommonConstants;
import org.wso2.identity.integration.test.utils.IdentityConstants;

Expand All @@ -63,6 +71,8 @@ public abstract class AbstractIdentityFederationTestCase extends ISIntegrationTe
private Map<Integer, IdentityProviderMgtServiceClient> identityProviderMgtServiceClients;
private Map<Integer, SAMLSSOConfigServiceClient> samlSSOConfigServiceClients;
private Map<Integer, OauthAdminClient> oauthAdminClients;
private Map<Integer, OAuth2RestClient> applicationManagementRestClients;
private Map<Integer, IdpMgtRestClient> identityProviderMgtRestClients;
protected Map<Integer, AutomationContext> automationContextMap;
private MultipleServersManager manager;
protected static final int DEFAULT_PORT = CommonConstants.IS_DEFAULT_HTTPS_PORT;
Expand All @@ -71,6 +81,8 @@ public void initTest() throws Exception {

super.init();
TestDataHolder testDataHolder = TestDataHolder.getInstance();
applicationManagementRestClients = new HashMap<>();
identityProviderMgtRestClients = new HashMap<>();
applicationManagementServiceClients = new HashMap<>();
identityProviderMgtServiceClients = new HashMap<>();
samlSSOConfigServiceClients = new HashMap<>();
Expand Down Expand Up @@ -137,6 +149,26 @@ public void createServiceClients(int portOffset, String sessionCookie,
}
}

public void createServiceClients(int portOffset, IdentityConstants.ServiceClientType[] adminClients)
throws Exception {

if (adminClients == null) {
return;
}

serverURL = automationContextMap.get(portOffset).getContextUrls().getSecureServiceUrl()
.replace("/services", "");
String serviceUrl = getSecureServiceUrl(portOffset, serverURL);

for (IdentityConstants.ServiceClientType clientType : adminClients) {
if (IdentityConstants.ServiceClientType.APPLICATION_MANAGEMENT.equals(clientType)) {
applicationManagementRestClients.put(portOffset, new OAuth2RestClient(serviceUrl, tenantInfo));
} else if (IdentityConstants.ServiceClientType.IDENTITY_PROVIDER_MGT.equals(clientType)) {
identityProviderMgtRestClients.put(portOffset, new IdpMgtRestClient(serviceUrl, tenantInfo));
}
}
}

public void addServiceProvider(int portOffset, String applicationName) throws Exception {

ServiceProvider serviceProvider = new ServiceProvider();
Expand All @@ -146,6 +178,31 @@ public void addServiceProvider(int portOffset, String applicationName) throws Ex
applicationManagementServiceClients.get(portOffset).createApplication(serviceProvider);
}

public String addApplication(int portOffset, ApplicationModel applicationModel) throws JSONException, IOException {

return applicationManagementRestClients.get(portOffset).createApplication(applicationModel);
}

public ApplicationResponseModel getApplication(int portOffset, String appId) throws Exception {

return applicationManagementRestClients.get(portOffset).getApplication(appId);
}

public OpenIDConnectConfiguration getOIDCInboundDetailsOfApplication(int portOffset, String appId) throws Exception {

return applicationManagementRestClients.get(portOffset).getOIDCInboundDetails(appId);
}

public SAML2ServiceProvider getSAMLInboundDetailsOfApplication(int portOffset, String appId) throws Exception {

return applicationManagementRestClients.get(portOffset).getSAMLInboundDetails(appId);
}

public void deleteApplication(int portOffset, String appId) throws Exception {

applicationManagementRestClients.get(portOffset).deleteApplication(appId);
}

public ServiceProvider getServiceProvider(int portOffset, String applicationName)
throws Exception {

Expand All @@ -169,6 +226,16 @@ public void addIdentityProvider(int portOffset, IdentityProvider identityProvide
identityProviderMgtServiceClients.get(portOffset).addIdP(identityProvider);
}

public String addIdentityProvider(int portOffset, IdentityProviderPOSTRequest idp) throws Exception {

return identityProviderMgtRestClients.get(portOffset).createIdentityProvider(idp);
}

public void deleteIdp(int portOffset, String idpId) throws Exception {

identityProviderMgtRestClients.get(portOffset).deleteIdp(idpId);
}

public IdentityProvider getIdentityProvider(int portOffset, String idPName) throws Exception {

return identityProviderMgtServiceClients.get(portOffset).getIdPByName(idPName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider;
import org.wso2.carbon.identity.user.store.configuration.stub.dto.UserStoreDTO;
import org.wso2.identity.integration.common.clients.UserManagementClient;
import org.wso2.identity.integration.common.clients.user.store.config.UserStoreConfigAdminServiceClient;
import org.wso2.identity.integration.common.utils.UserStoreConfigUtils;
import org.wso2.carbon.automation.test.utils.dbutils.H2DataBaseManager;
import org.wso2.carbon.integration.common.utils.mgt.ServerConfigurationManager;
import org.wso2.identity.integration.test.base.TomcatInitializerTestCase;
import org.wso2.identity.integration.test.oidc.OIDCAbstractIntegrationTest;
import org.wso2.identity.integration.test.oidc.OIDCUtilTest;
import org.wso2.identity.integration.test.oidc.bean.OIDCApplication;
import org.wso2.identity.integration.test.rest.api.server.user.store.v1.model.UserStoreReq;
import org.wso2.identity.integration.test.rest.api.server.user.store.v1.model.UserStoreReq.Property;
import org.wso2.identity.integration.test.rest.api.user.common.model.GroupRequestObject;
import org.wso2.identity.integration.test.rest.api.user.common.model.GroupRequestObject.MemberItem;
import org.wso2.identity.integration.test.rest.api.user.common.model.ListObject;
import org.wso2.identity.integration.test.rest.api.user.common.model.RoleRequestObject;
import org.wso2.identity.integration.test.rest.api.user.common.model.UserObject;
import org.wso2.identity.integration.test.restclients.UserStoreMgtRestClient;
import org.wso2.identity.integration.test.util.Utils;
import org.wso2.identity.integration.test.utils.DataExtractUtil;
import org.wso2.identity.integration.test.utils.OAuth2Constant;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -73,21 +79,30 @@ public class SecondaryStoreUserLoginTestCase extends OIDCAbstractIntegrationTest
private static final String PRIMARY_PASSWORD = "primaryPassword";
private static final String SECONDARY_USERNAME = "secondaryUsername";
private static final String SECONDARY_PASSWORD = "secondaryPassword";
private static final UserStoreConfigUtils USER_STORE_CONFIG_UTILS = new UserStoreConfigUtils();
private static final String PERMISSION_LOGIN = "/permission/admin/login";
private static final String JDBC_CLASS = "org.wso2.carbon.user.core.jdbc.UniqueIDJDBCUserStoreManager";
private static final String DOMAIN_ID = "WSO2TEST.COM";
private static final String PRIMARY_USER_GROUP = "jdbcUserStoreGroup";
private static final String PRIMARY_USER_ROLE = "jdbcUserStoreRole";
private static final String SECONDARY_USER_ROLE = DOMAIN_ID + "/" + "jdbcUserStoreRole";
private static final String SECONDARY_USER_GROUP = DOMAIN_ID + "/" + "jdbcSecondaryUserStoreGroup";
private static final String SECONDARY_USER_ROLE = "jdbcSecondaryUserStoreRole";
private static final String USER_STORE_DB_NAME = "SECONDARY_USER_STORE_DB";
private static final String USER_STORE_TYPE = "VW5pcXVlSURKREJDVXNlclN0b3JlTWFuYWdlcg";
private static final Log LOG = LogFactory.getLog(TomcatInitializerTestCase.class);
private static final String DB_USER_NAME = "wso2automation";
private static final String DB_USER_PASSWORD = "wso2automation";
private OIDCApplication playgroundApp;
private HttpClient client;
private String sessionDataKey;
private UserStoreConfigAdminServiceClient userStoreConfigAdminServiceClient;
private UserManagementClient userMgtClient;
private Tomcat tomcat;
private String clientID;
private UserStoreMgtRestClient userStoreMgtRestClient;
private String userStoreId;
private String secondaryUserStoreRoleId;
private String secondaryUserStoreUserId;
private String primaryUserStoreUserId;
private String primaryUserStoreRoleId;
private String secondaryUserStoreGroupId;
private String primaryUserStoreGroupId;

@DataProvider(name = "userCredentialProvider")
public static Object[][] userCredentialProvider() {
Expand All @@ -99,20 +114,12 @@ public static Object[][] userCredentialProvider() {
public void testInit() throws Exception {

super.init();
userStoreMgtRestClient = new UserStoreMgtRestClient(serverURL, tenantInfo);
addSecondaryJDBCUserStore();

// Register a secondary user store
userStoreConfigAdminServiceClient = new UserStoreConfigAdminServiceClient(backendURL, sessionCookie);
userMgtClient = new UserManagementClient(backendURL, getSessionCookie());
UserStoreDTO userStoreDTO = userStoreConfigAdminServiceClient.createUserStoreDTO(JDBC_CLASS, DOMAIN_ID,
USER_STORE_CONFIG_UTILS.getJDBCUserStoreProperties(USER_STORE_DB_NAME));
userStoreConfigAdminServiceClient.addUserStore(userStoreDTO);
Thread.sleep(5000);
boolean isSecondaryUserStoreDeployed = USER_STORE_CONFIG_UTILS.waitForUserStoreDeployment(
userStoreConfigAdminServiceClient, DOMAIN_ID);
Assert.assertTrue(isSecondaryUserStoreDeployed);
// Creating users in the primary and secondary user stores
addUserIntoJDBCUserStore(PRIMARY_USERNAME, PRIMARY_PASSWORD, false);
addUserIntoJDBCUserStore(SECONDARY_USERNAME, SECONDARY_PASSWORD, true);

// Creating, registering and starting application on tomcat
createAndRegisterPlaygroundApplication();
startTomcat();
Expand Down Expand Up @@ -149,9 +156,13 @@ public void testUserLogin(String username, String password) throws Exception {
public void atEnd() throws Exception {

stopTomcat();
userStoreConfigAdminServiceClient.deleteUserStore(DOMAIN_ID);
userMgtClient.deleteUser(PRIMARY_USERNAME);
userMgtClient.deleteUser(DOMAIN_ID + "/" + SECONDARY_USERNAME);
scim2RestClient.deleteUser(primaryUserStoreUserId);
scim2RestClient.deleteUser(secondaryUserStoreUserId);
scim2RestClient.deleteGroup(primaryUserStoreGroupId);
scim2RestClient.deleteGroup(secondaryUserStoreGroupId);
scim2RestClient.deleteRole(primaryUserStoreRoleId);
scim2RestClient.deleteRole(secondaryUserStoreRoleId);
userStoreMgtRestClient.deleteUserStore(userStoreId);
deleteApplication(playgroundApp);
clear();
}
Expand Down Expand Up @@ -201,25 +212,42 @@ private void createAndRegisterPlaygroundApplication() throws Exception {
playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri);
playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri);
playgroundApp.addRequiredClaim(OIDCUtilTest.lastNameClaimUri);
ServiceProvider serviceProvider = new ServiceProvider();
createApplication(serviceProvider, playgroundApp);

createApplication(playgroundApp);
clientID = playgroundApp.getClientId();
}

private void addUserIntoJDBCUserStore(String username, String password, boolean isSecondaryStoreUser)
throws Exception {

if (isSecondaryStoreUser) {
userMgtClient.addRole(SECONDARY_USER_ROLE, null, new String[]{PERMISSION_LOGIN});
Assert.assertTrue(userMgtClient.roleNameExists(SECONDARY_USER_ROLE), "Role name doesn't exist");
userMgtClient.addUser(DOMAIN_ID + "/" + username, password, new String[]{SECONDARY_USER_ROLE}, null);
Assert.assertTrue(userMgtClient.userNameExists(SECONDARY_USER_ROLE, DOMAIN_ID + "/" + username),
"User is not created.");
secondaryUserStoreUserId = scim2RestClient.createUser(new UserObject()
.userName(DOMAIN_ID + "/" + username)
.password(password));

secondaryUserStoreGroupId = scim2RestClient.createGroup(new GroupRequestObject()
.displayName(SECONDARY_USER_GROUP)
.addMember(new MemberItem().value(secondaryUserStoreUserId)));

secondaryUserStoreRoleId = scim2RestClient.addRole(new RoleRequestObject()
.displayName(SECONDARY_USER_ROLE)
.addPermissions(PERMISSION_LOGIN)
.addUsers(new ListObject().value(secondaryUserStoreUserId))
.addGroups(new ListObject().value(secondaryUserStoreGroupId)));
} else {
userMgtClient.addRole(PRIMARY_USER_ROLE, null, new String[]{PERMISSION_LOGIN});
Assert.assertTrue(userMgtClient.roleNameExists(PRIMARY_USER_ROLE), "Role name doesn't exist");
userMgtClient.addUser(username, password, new String[]{PRIMARY_USER_ROLE}, null);
Assert.assertTrue(userMgtClient.userNameExists(PRIMARY_USER_ROLE, username), "User is not created.");
primaryUserStoreUserId = scim2RestClient.createUser(new UserObject()
.userName(username)
.password(password));

primaryUserStoreGroupId = scim2RestClient.createGroup(new GroupRequestObject()
.displayName(PRIMARY_USER_GROUP)
.addMember(new MemberItem().value(primaryUserStoreUserId)));

primaryUserStoreRoleId = scim2RestClient.addRole(new RoleRequestObject()
.displayName(PRIMARY_USER_ROLE)
.addPermissions(PERMISSION_LOGIN)
.addUsers(new ListObject().value(primaryUserStoreUserId))
.addGroups(new ListObject().value(primaryUserStoreGroupId)));
}
}

Expand All @@ -245,4 +273,59 @@ private void stopTomcat() throws LifecycleException {
tomcat.destroy();
LOG.info("Tomcat server stopped.");
}

private void addSecondaryJDBCUserStore() throws Exception {

//creating database
H2DataBaseManager dbmanager = new H2DataBaseManager("jdbc:h2:" + ServerConfigurationManager.getCarbonHome()
+ "/repository/database/" + USER_STORE_DB_NAME, DB_USER_NAME, DB_USER_PASSWORD);
dbmanager.executeUpdate(new File(ServerConfigurationManager.getCarbonHome() + "/dbscripts/h2.sql"));
dbmanager.disconnect();

// Register a secondary user store
UserStoreReq userStore = new UserStoreReq()
.typeId(USER_STORE_TYPE)
.name(DOMAIN_ID)
.addPropertiesItem(new Property()
.name("driverName")
.value("org.h2.Driver"))
.addPropertiesItem(new Property()
.name("url")
.value("jdbc:h2:./repository/database/" + USER_STORE_DB_NAME))
.addPropertiesItem(new Property()
.name("userName")
.value(DB_USER_NAME))
.addPropertiesItem(new Property()
.name("password")
.value(DB_USER_PASSWORD))
.addPropertiesItem(new Property()
.name("PasswordJavaRegEx")
.value("^[\\S]{5,30}$"))
.addPropertiesItem(new Property()
.name("UsernameJavaRegEx")
.value("^[\\S]{5,30}$"))
.addPropertiesItem(new Property()
.name("Disabled")
.value("false"))
.addPropertiesItem(new Property()
.name("PasswordDigest")
.value("SHA-256"))
.addPropertiesItem(new Property()
.name("StoreSaltedPassword")
.value("true"))
.addPropertiesItem(new Property()
.name("SCIMEnabled")
.value("true"))
.addPropertiesItem(new Property()
.name("CountRetrieverClass")
.value("org.wso2.carbon.identity.user.store.count.jdbc.JDBCUserStoreCountRetriever"))
.addPropertiesItem(new Property()
.name("UserIDEnabled")
.value("true"));

userStoreId = userStoreMgtRestClient.addUserStore(userStore);
Thread.sleep(5000);
boolean isSecondaryUserStoreDeployed = userStoreMgtRestClient.waitForUserStoreDeployment(DOMAIN_ID);
Assert.assertTrue(isSecondaryUserStoreDeployed);
}
}
Loading

0 comments on commit 35f8186

Please sign in to comment.