Skip to content

Commit

Permalink
Merge pull request #16223 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
  • Loading branch information
hwupathum authored Jul 18, 2023
2 parents b111f18 + 3755cd5 commit 6141613
Show file tree
Hide file tree
Showing 61 changed files with 6,746 additions and 1,825 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ISIntegrationTest {
protected Log log = LogFactory.getLog(getClass());
protected AutomationContext isServer;
protected String backendURL;
protected String serverURL;
protected String sessionCookie;
protected Tenant tenantInfo;
protected User userInfo;
Expand All @@ -54,6 +55,7 @@ protected void init() throws Exception {
protected void init(TestUserMode userMode) throws Exception {
isServer = new AutomationContext("IDENTITY", userMode);
backendURL = isServer.getContextUrls().getBackEndUrl();
serverURL = backendURL.replace("services/", "");
loginLogoutClient = new LoginLogoutClient(isServer);
sessionCookie = loginLogoutClient.login();
identityContextUrls = isServer.getContextUrls();
Expand All @@ -66,6 +68,7 @@ protected void init(String instance, String domainKey, String userKey) throws Ex
loginLogoutClient = new LoginLogoutClient(isServer);
sessionCookie = loginLogoutClient.login();
backendURL = isServer.getContextUrls().getBackEndUrl();
serverURL = backendURL.replace("services/", "");
}

protected String login() throws Exception{
Expand All @@ -86,6 +89,7 @@ protected void initPublisher(String productGroupName, String instanceName, TestU
throws XPathExpressionException {
isServer = new AutomationContext(productGroupName, instanceName, userMode);
backendURL = isServer.getContextUrls().getBackEndUrl();
serverURL = backendURL.replace("services/", "");
}

protected String getBackendURL() throws XPathExpressionException {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* 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
*
Expand All @@ -18,7 +18,6 @@

package org.wso2.identity.integration.test.oauth2;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
Expand All @@ -42,23 +41,26 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig;
import org.wso2.carbon.identity.application.common.model.xsd.Property;
import org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider;
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO;
import org.wso2.carbon.um.ws.api.stub.ClaimValue;
import org.wso2.identity.integration.common.clients.application.mgt.ApplicationManagementServiceClient;
import org.wso2.identity.integration.common.clients.oauth.OauthAdminClient;
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.InboundProtocols;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OIDCLogoutConfiguration;
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration;
import org.wso2.identity.integration.test.rest.api.user.common.model.Email;
import org.wso2.identity.integration.test.rest.api.user.common.model.ListObject;
import org.wso2.identity.integration.test.rest.api.user.common.model.PatchOperationRequestObject;
import org.wso2.identity.integration.test.rest.api.user.common.model.RoleItemAddGroupobj;
import org.wso2.identity.integration.test.rest.api.user.common.model.UserObject;
import org.wso2.identity.integration.test.restclients.SCIM2RestClient;
import org.wso2.identity.integration.test.util.Utils;
import org.wso2.identity.integration.test.utils.CommonConstants;
import org.wso2.identity.integration.test.utils.DataExtractUtil;
import org.wso2.identity.integration.test.utils.OAuth2Constant;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -74,23 +76,21 @@ public class OAuth2BackChannelLogoutTestCase extends OAuth2ServiceAbstractIntegr
private RequestConfig requestConfig;
private HttpClient client;
private final String OIDC_APP_NAME = "playground2";
private String oidcAppClientId = "";
private String oidcAppClientSecret = "";
private OauthAdminClient adminClient;
private ApplicationManagementServiceClient applicationManagementServiceClient;
private String oidcAppClientId;
private String sessionDataKeyConsent;
private String sessionDataKey;
private final String CONSENT = "consent";
private final String APPROVE = "approve";
private final String SCOPE_APPROVAL = "scope-approval";
private final String USER_AGENT = "User-Agent";
private final String username;
private final String userPassword;
private final String activeTenant;
private static final String CONSENT = "consent";
private static final String APPROVE = "approve";
private static final String SCOPE_APPROVAL = "scope-approval";
private static final String USER_AGENT = "User-Agent";
private static final String USERS_PATH = "users";

private static final String USER_EMAIL = "abc@wso2.com";
private static final String USERNAME = "testUser";
private static final String PASSWORD = "pass123";
private String applicationId;
private SCIM2RestClient scim2RestClient;
private String userId;

@DataProvider(name = "configProvider")
public static Object[][] configProvider() {
Expand All @@ -101,10 +101,6 @@ public static Object[][] configProvider() {
public OAuth2BackChannelLogoutTestCase(TestUserMode userMode) throws Exception {

super.init(userMode);
AutomationContext context = new AutomationContext("IDENTITY", userMode);
this.username = context.getContextTenant().getTenantAdmin().getUserName();
this.userPassword = context.getContextTenant().getTenantAdmin().getPassword();
this.activeTenant = context.getContextTenant().getDomain();
}

@BeforeClass(alwaysRun = true)
Expand All @@ -123,27 +119,17 @@ public void testInit() throws Exception {
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
scim2RestClient = new SCIM2RestClient(serverURL, tenantInfo);

createOIDCApplication();
createServiceProvider();

remoteUSMServiceClient.addUser(USERNAME, PASSWORD, new String[]{"admin"},
getUserClaims(), "default", true);

addAdminUser();
}

@AfterClass(alwaysRun = true)
public void testCleanUp() throws Exception {

removeApplications();

remoteUSMServiceClient.deleteUser(USERNAME);
}

protected void init() throws Exception {

super.init();
adminClient = new OauthAdminClient(backendURL, sessionCookie);
applicationManagementServiceClient = new ApplicationManagementServiceClient(sessionCookie, backendURL, null);
scim2RestClient.deleteUser(userId);
}

@Test(groups = "wso2.is", description = "Test back channel logout for OIDC.")
Expand All @@ -159,25 +145,38 @@ public void testOIDCLogout() throws IOException {
}

private void createOIDCApplication() throws Exception {
ApplicationModel application = new ApplicationModel();

List<String> grantTypes = new ArrayList<>();
Collections.addAll(grantTypes, OAuth2Constant.OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE);

List<String> callBackUrls = new ArrayList<>();
Collections.addAll(callBackUrls, OAuth2Constant.CALLBACK_URL);

OAuthConsumerAppDTO appDTO = new OAuthConsumerAppDTO();
appDTO.setApplicationName(OIDC_APP_NAME);
appDTO.setCallbackUrl(OAuth2Constant.CALLBACK_URL);
appDTO.setOAuthVersion(OAuth2Constant.OAUTH_VERSION_2);
appDTO.setGrantTypes(OAuth2Constant.OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE);
appDTO.setBackChannelLogoutUrl("http://localhost:" + DEFAULT_TOMCAT_PORT + "/playground2/bclogout");

adminClient.registerOAuthApplicationData(appDTO);
OAuthConsumerAppDTO createdApp = adminClient.getOAuthAppByName(OIDC_APP_NAME);
Assert.assertNotNull(createdApp, "Adding OIDC app failed.");
oidcAppClientId = createdApp.getOauthConsumerKey();
oidcAppClientSecret = createdApp.getOauthConsumerSecret();
OpenIDConnectConfiguration oidcConfig = new OpenIDConnectConfiguration();
oidcConfig.setGrantTypes(grantTypes);
oidcConfig.setCallbackURLs(callBackUrls);
oidcConfig.setLogout(new OIDCLogoutConfiguration().backChannelLogoutUrl("http://localhost:" +
DEFAULT_TOMCAT_PORT + "/playground2/bclogout"));

InboundProtocols inboundProtocolsConfig = new InboundProtocols();
inboundProtocolsConfig.setOidc(oidcConfig);

application.setInboundProtocolConfiguration(inboundProtocolsConfig);
application.setName(OIDC_APP_NAME);

String appId = addApplication(application);
ApplicationResponseModel createdApplication = getApplication(appId);

applicationId = createdApplication.getId();
oidcConfig = getOIDCInboundDetailsOfApplication(applicationId);

oidcAppClientId = oidcConfig.getClientId();
}

private void removeApplications() throws Exception {

adminClient.removeOAuthApplicationData(oidcAppClientId);
applicationManagementServiceClient.deleteApplication(OIDC_APP_NAME);
deleteApp(applicationId);
}

private void initiateOIDCRequest(boolean isCheckLogoutConfirmation) throws IOException {
Expand Down Expand Up @@ -300,7 +299,7 @@ public HttpResponse sendPostRequestWithParameters(List<NameValuePair> urlParamet

private List<NameValuePair> getOIDCInitiationRequestParams() {

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("grantType", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE));
urlParameters.add(new BasicNameValuePair("consumerKey", oidcAppClientId));
urlParameters.add(new BasicNameValuePair("callbackurl", OAuth2Constant.CALLBACK_URL));
Expand All @@ -310,40 +309,20 @@ private List<NameValuePair> getOIDCInitiationRequestParams() {
return urlParameters;
}

private ServiceProvider createServiceProvider() throws Exception {

ServiceProvider serviceProvider = new ServiceProvider();
serviceProvider.setApplicationName(OIDC_APP_NAME);
serviceProvider.setManagementApp(true);
applicationManagementServiceClient.createApplication(serviceProvider);
serviceProvider = applicationManagementServiceClient.getApplication(OIDC_APP_NAME);

InboundAuthenticationRequestConfig requestConfig = new InboundAuthenticationRequestConfig();
requestConfig.setInboundAuthKey(oidcAppClientId);
requestConfig.setInboundAuthType("oauth2");
if (StringUtils.isNotBlank(oidcAppClientSecret)) {
Property property = new Property();
property.setName("oauthConsumerSecret");
property.setValue(oidcAppClientSecret);
Property[] properties = {property};
requestConfig.setProperties(properties);
}
private void addAdminUser() throws Exception {
UserObject userInfo = new UserObject();
userInfo.setUserName(USERNAME);
userInfo.setPassword(PASSWORD);
userInfo.addEmail(new Email().value(USER_EMAIL));

InboundAuthenticationConfig inboundAuthenticationConfig = new InboundAuthenticationConfig();
inboundAuthenticationConfig
.setInboundAuthenticationRequestConfigs(new InboundAuthenticationRequestConfig[]{requestConfig});
serviceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig);
applicationManagementServiceClient.updateApplicationData(serviceProvider);
return serviceProvider;
}
userId = scim2RestClient.createUser(userInfo);
String roleId = scim2RestClient.getRoleIdByName("admin");

protected ClaimValue[] getUserClaims() {
RoleItemAddGroupobj patchRoleItem = new RoleItemAddGroupobj();
patchRoleItem.setOp(RoleItemAddGroupobj.OpEnum.ADD);
patchRoleItem.setPath(USERS_PATH);
patchRoleItem.addValue(new ListObject().value(userId));

ClaimValue[] claimValues = new ClaimValue[1];
ClaimValue email = new ClaimValue();
email.setClaimURI(EMAIL_CLAIM_URI);
email.setValue(USER_EMAIL);
claimValues[0] = email;
return claimValues;
scim2RestClient.updateUserRole(new PatchOperationRequestObject().addOperations(patchRoleItem), roleId);
}
}
Loading

0 comments on commit 6141613

Please sign in to comment.