Skip to content

Commit

Permalink
Fixing data provider in the integration tests (#21204)
Browse files Browse the repository at this point in the history
Fixing data provider
  • Loading branch information
indeewari authored Oct 1, 2024
1 parent a4865ac commit a067345
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
Expand All @@ -62,6 +61,7 @@
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -151,9 +151,6 @@ public class FederatedTokenSharingIDPConfigDisabledTestCase extends AbstractIden
private static final String SECONDARY_IS_LOGOUT_ENDPOINT = "https://localhost:9854/oidc/logout";
private static final String SECONDARY_IS_AUTHORIZE_ENDPOINT = "https://localhost:9854/oauth2/authorize";
private static final String HTTPS_LOCALHOST_SERVICES = "https://localhost:%s/";
private static final String TRUE = "true";
private static final String SCOPES_APPROVED_FOR_TOKEN_SHARING =
"https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar";
private static final String SCOPES_REQUESTED_FOR_TOKEN_SHARING =
"https://www.googleapis.com/auth/calendar.readonly";
private String secondaryISAppId;
Expand All @@ -164,10 +161,7 @@ public class FederatedTokenSharingIDPConfigDisabledTestCase extends AbstractIden
private String username;
private String userPassword;
private AutomationContext context;
private String sessionDataKey;

// Application Native Authentication related attributes
private String appId;
private String flowId;
private String flowStatus;
private String authenticatorId;
Expand All @@ -179,14 +173,11 @@ public class FederatedTokenSharingIDPConfigDisabledTestCase extends AbstractIden
private static final int PORT_OFFSET_0 = 0;
private static final int PORT_OFFSET_1 = 1;
CookieStore cookieStore;
private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private CloseableHttpClient client;
private String primaryISIdpId;
private String primaryISAppId;
private SCIM2RestClient scim2RestClient;
private String secondaryISUserId;
private AuthorizationCode authorizationCode;

@DataProvider(name = "configProvider")
public static Object[][] configProvider() {
Expand Down Expand Up @@ -218,21 +209,25 @@ public void initTest() throws Exception {
createIDPInPrimaryIS();//Google IDP in IS
createApplicationInPrimaryIS();// CallMeName app in IS

scim2RestClient = new SCIM2RestClient(getSecondaryISURI(), tenantInfo);
addUserToSecondaryIS();
}

@BeforeMethod(alwaysRun = true)
public void initTestRun() {

cookieStore = new BasicCookieStore();
cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
client = HttpClientBuilder.create()
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore)
.build();

scim2RestClient = new SCIM2RestClient(getSecondaryISURI(), tenantInfo);
addUserToSecondaryIS();
}

@AfterClass(alwaysRun = true)
Expand All @@ -253,9 +248,8 @@ public void endTest() throws Exception {
username = null;
userPassword = null;
context = null;
sessionDataKey = null;

appId = null;
// Application Native Authentication related attributes
flowId = null;
flowStatus = null;
authenticatorId = null;
Expand All @@ -268,7 +262,7 @@ public void endTest() throws Exception {
client.close();
scim2RestClient.closeHttpClient();
} catch (Exception e) {
log.error("Failure occured due to :" + e.getMessage(), e);
log.error("Failure occurred due to :" + e.getMessage(), e);
throw e;
}
}
Expand All @@ -277,12 +271,12 @@ public void endTest() throws Exception {
public static Object[][] federatedTokenSharingParams() {

return new Object[][]{
{" when IDP config is disabled, application requested the federated token", "true", "true"},
{" when IDP config is disabled, application did not request the federated token", "true", "false"}
{" when IDP config is disabled, application requested the federated token", "true"},
{" when IDP config is disabled, application did not request the federated token", "false"}
};
}

@Test(groups = "wso2.is", description = "Send init authorize POST request to primary IDP.", dataProvider = "testFederatedTokenSharingDataProvider")
@Test(groups = "wso2.is", description = "Send init authorize POST request to primary IDP.", dataProvider = "federatedTokenSharingParams")
public void testFederatedTokenSharing(String errorMessage,
String shareTokenQueryParameter) throws Exception {

Expand All @@ -296,16 +290,7 @@ public void testFederatedTokenSharing(String errorMessage,
authenticatePrimaryIDPWithFederatedResponse();

// Send get access token request.
ClientID clientID = new ClientID(appClientID);
Secret clientSecret = new Secret(appClientSecret);
ClientSecretBasic clientSecretBasic = new ClientSecretBasic(clientID, clientSecret);

URI callbackURI = new URI(PRIMARY_IS_IDP_CALLBACK_URL);
authorizationCode = new AuthorizationCode(code);
AuthorizationCodeGrant authorizationCodeGrant = new AuthorizationCodeGrant(authorizationCode, callbackURI);

TokenRequest tokenReq = new TokenRequest(new URI(PRIMARY_IS_TOKEN_URL), clientSecretBasic,
authorizationCodeGrant);
TokenRequest tokenReq = getTokenRequest();

HTTPResponse tokenHTTPResp = tokenReq.toHTTPRequest().send();
Assert.assertNotNull(tokenHTTPResp, "Access token http response is null." + errorMessage);
Expand All @@ -328,6 +313,20 @@ public void testFederatedTokenSharing(String errorMessage,
"The OIDC token response have federated tokens" + errorMessage);
}

private TokenRequest getTokenRequest() throws URISyntaxException {

ClientID clientID = new ClientID(appClientID);
Secret clientSecret = new Secret(appClientSecret);
ClientSecretBasic clientSecretBasic = new ClientSecretBasic(clientID, clientSecret);

URI callbackURI = new URI(PRIMARY_IS_IDP_CALLBACK_URL);
AuthorizationCode authorizationCode = new AuthorizationCode(code);
AuthorizationCodeGrant authorizationCodeGrant = new AuthorizationCodeGrant(authorizationCode, callbackURI);

return new TokenRequest(new URI(PRIMARY_IS_TOKEN_URL), clientSecretBasic,
authorizationCodeGrant);
}

private void authorizePrimaryIDP(String shareTokenQueryParameter, String errorMessage)
throws IOException, ParseException, URISyntaxException {

Expand Down Expand Up @@ -433,14 +432,12 @@ private String getLocationHeaderValue(HttpResponse response) {
* @param client - http client
* @param sessionDataKeyConsent - session consent data
* @return http response
* @throws ClientProtocolException ClientProtocolException
* @throws java.io.IOException java.io.IOException
*/
private HttpResponse sendApprovalPost(HttpClient client, String sessionDataKeyConsent)
throws ClientProtocolException,
IOException {
throws IOException {

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("sessionDataKey", sessionDataKeyConsent));

return sendPostRequestWithParameters(client, urlParameters,
Expand Down Expand Up @@ -611,28 +608,26 @@ private HttpResponse sendLoginPost(HttpClient client, String sessionDataKey) thr
urlParameters.add(new BasicNameValuePair("sessionDataKey", sessionDataKey));
log.info(">>> sendLoginPost:sessionDataKey: " + sessionDataKey);

HttpResponse response = sendPostRequestWithParameters(client, urlParameters, SECONDARY_IS_IDP_CALLBACK_URL);
return response;
return sendPostRequestWithParameters(client, urlParameters, SECONDARY_IS_IDP_CALLBACK_URL);
}

private HttpResponse sendPostRequestWithParameters(HttpClient client, List<NameValuePair> urlParameters, String url)
throws ClientProtocolException, IOException {
throws IOException {

HttpPost request = new HttpPost(url);
request.setHeader("User-Agent", OAuth2Constant.USER_AGENT);
request.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(request);
return response;
return client.execute(request);
}

/**
* Builds a list of OAuth 2.0 parameters required for initiating the authorization process.
* The method constructs and returns a list of parameters necessary for initiating the OAuth 2.0 authorization process.
*
* @param consumerKey The client's unique identifier in the OAuth 2.0 system
* @param shareTokenQueryParameter
* @return A list of NameValuePair representing the OAuth 2.0 parameters
* @param consumerKey The client's unique identifier in the OAuth 2.0 system.
* @param shareTokenQueryParameter Whether the application requests the federated token.
* @return A list of NameValuePair representing the OAuth 2.0 parameters.
*/
private List<NameValuePair> buildOAuth2Parameters(String consumerKey, String callBackUrl,
String shareTokenQueryParameter) {
Expand Down Expand Up @@ -734,8 +729,7 @@ private JSONObject getJsonObject(HttpResponse response) throws IOException, Pars
String responseString = EntityUtils.toString(response.getEntity(), UTF_8);
EntityUtils.consume(response.getEntity());
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(responseString);
return json;
return (JSONObject) parser.parse(responseString);
}

private List<NameValuePair> getNameValuePairsForExternalFederation() {
Expand Down Expand Up @@ -783,7 +777,7 @@ private void validateSecondaryISFederationResponse(HttpResponse response) throws

private String generateAuthReqBody() {

String body = "{\n" +
return "{\n" +
" \"flowId\": \"" + flowId + "\",\n" +
" \"selectedAuthenticator\": {\n" +
" \"authenticatorId\": \"" + authenticatorId + "\",\n" +
Expand All @@ -793,7 +787,6 @@ private String generateAuthReqBody() {
" }\n" +
" }\n" +
"}";
return body;
}

}
Loading

0 comments on commit a067345

Please sign in to comment.