Skip to content

Commit

Permalink
re order test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Thumimku committed Oct 18, 2024
1 parent 9222c0d commit 52d60c6
Showing 1 changed file with 74 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ public void testSendPar() throws Exception {
urlParameters.add(new BasicNameValuePair(OAuth2Constant.OAUTH2_REDIRECT_URI, OAuth2Constant.CALLBACK_URL));
urlParameters.add(new BasicNameValuePair(OAuth2Constant.OAUTH2_RESPONSE_TYPE,
OAuth2Constant.OAUTH2_GRANT_TYPE_CODE));
String response = responsePost(OAuth2Constant.PAR_ENDPOINT, urlParameters);
HttpResponse response = sendPostRequest(OAuth2Constant.PAR_ENDPOINT, urlParameters);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(response);
JSONObject jsonResponse = (JSONObject) parser.parse(responseString);
if (jsonResponse == null) {
throw new Exception("Error occurred while getting the response.");
}
Expand All @@ -141,8 +143,10 @@ public void testSendAuthorize() throws Exception {
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair(REQUEST_URI, requestUri));
urlParameters.add(new BasicNameValuePair(CLIENT_ID_PARAM, consumerKey));
String response = responsePost(OAuth2Constant.AUTHORIZE_ENDPOINT_URL, urlParameters);
Assert.assertNotNull(response, "Authorized response is null");
HttpResponse response = sendPostRequest(OAuth2Constant.PAR_ENDPOINT, urlParameters);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());
Assert.assertNotNull(responseString, "Authorized response is null");
}

@Test(groups = "wso2.is", description = "Send PAR with openid request object", dependsOnMethods =
Expand All @@ -156,9 +160,11 @@ public void testSendParWithRequestObject() throws Exception {
OAuth2Constant.OAUTH2_GRANT_TYPE_CODE));
urlParameters.add(new BasicNameValuePair(OAuth2Constant.OAUTH_OIDC_REQUEST, REQUEST));
urlParameters.add(new BasicNameValuePair(OAuth2Constant.OAUTH2_SCOPE, OAuth2Constant.OAUTH2_SCOPE_OPENID));
String response = responsePost(OAuth2Constant.PAR_ENDPOINT, urlParameters);
HttpResponse response = sendPostRequest(OAuth2Constant.PAR_ENDPOINT, urlParameters);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(response);
JSONObject jsonResponse = (JSONObject) parser.parse(responseString);
if (jsonResponse == null) {
throw new Exception("Error occurred while getting the response.");
}
Expand All @@ -168,80 +174,6 @@ public void testSendParWithRequestObject() throws Exception {
Assert.assertNotNull(expiryTime, "expiry_time is null");
}

private String responsePost(String endpoint, List<NameValuePair> postParameters)
throws Exception {

HttpPost httpPost = new HttpPost(endpoint);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = client.execute(httpPost);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());
return responseString;
}

/**
* Create Application with the given app configurations
*
* @return ApplicationResponseModel
* @throws Exception exception
*/
private ApplicationResponseModel createApp() throws Exception {

ApplicationModel application = new ApplicationModel();

List<String> grantTypes = new ArrayList<>();
Collections.addAll(grantTypes, "authorization_code", "implicit", "password", "client_credentials",
"refresh_token", "urn:ietf:params:oauth:grant-type:saml2-bearer", "iwa:ntlm",
"urn:ietf:params:oauth:grant-type:device_code");

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

OpenIDConnectConfiguration oidcConfig = new OpenIDConnectConfiguration();
oidcConfig.setGrantTypes(grantTypes);
oidcConfig.setCallbackURLs(callBackUrls);
oidcConfig.setPublicClient(true);

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

application.setInboundProtocolConfiguration(inboundProtocolsConfig);
application.setName(OAuth2Constant.OAUTH_APPLICATION_NAME);

String appId = addApplication(application);

return getApplication(appId);
}

private HttpResponse sendPostRequest(String endpoint, List<NameValuePair> parameters) throws Exception {

HttpPost httpPost = new HttpPost(endpoint);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
return client.execute(httpPost);
}

private void assertResponse(HttpResponse response, int expectedStatusCode,
String expectedErrorDescription, String expectedError) throws Exception {

int responseCode = response.getStatusLine().getStatusCode();
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());

JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(responseString);
if (jsonResponse == null) {
throw new Exception("Error occurred while getting the response.");
}

Assert.assertEquals(responseCode, expectedStatusCode, "Response status code does not match.");
Assert.assertEquals(jsonResponse.get("error_description").toString(), expectedErrorDescription,
"Error description is missing or invalid value");
Assert.assertEquals(jsonResponse.get("error").toString(), expectedError,
"Error is missing or invalid value");
}

@Test(groups = "wso2.is", description = "Send authorize user request with invalid client id",
dependsOnMethods = "testSendPar")
public void testSendAuthorizeWithInvalidClient() throws Exception {
Expand Down Expand Up @@ -385,4 +317,66 @@ public void testSendParWithRequestURI() throws Exception {
assertResponse(response, Response.Status.BAD_REQUEST.getStatusCode(),
"Request with request_uri not allowed.", "invalid_request");
}

/**
* Create Application with the given app configurations
*
* @return ApplicationResponseModel
* @throws Exception exception
*/
private ApplicationResponseModel createApp() throws Exception {

ApplicationModel application = new ApplicationModel();

List<String> grantTypes = new ArrayList<>();
Collections.addAll(grantTypes, "authorization_code", "implicit", "password", "client_credentials",
"refresh_token", "urn:ietf:params:oauth:grant-type:saml2-bearer", "iwa:ntlm",
"urn:ietf:params:oauth:grant-type:device_code");

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

OpenIDConnectConfiguration oidcConfig = new OpenIDConnectConfiguration();
oidcConfig.setGrantTypes(grantTypes);
oidcConfig.setCallbackURLs(callBackUrls);
oidcConfig.setPublicClient(true);

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

application.setInboundProtocolConfiguration(inboundProtocolsConfig);
application.setName(OAuth2Constant.OAUTH_APPLICATION_NAME);

String appId = addApplication(application);

return getApplication(appId);
}

private HttpResponse sendPostRequest(String endpoint, List<NameValuePair> parameters) throws Exception {

HttpPost httpPost = new HttpPost(endpoint);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
return client.execute(httpPost);
}

private void assertResponse(HttpResponse response, int expectedStatusCode,
String expectedErrorDescription, String expectedError) throws Exception {

int responseCode = response.getStatusLine().getStatusCode();
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());

JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(responseString);
if (jsonResponse == null) {
throw new Exception("Error occurred while getting the response.");
}

Assert.assertEquals(responseCode, expectedStatusCode, "Response status code does not match.");
Assert.assertEquals(jsonResponse.get("error_description").toString(), expectedErrorDescription,
"Error description is missing or invalid value");
Assert.assertEquals(jsonResponse.get("error").toString(), expectedError,
"Error is missing or invalid value");
}
}

0 comments on commit 52d60c6

Please sign in to comment.