Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test for application creation using Store APIs with SSO. #329

Merged
merged 2 commits into from
Aug 18, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.List;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -91,6 +92,7 @@ public class SingleSignOnTestCase extends APIMIntegrationBaseTest {
private String apiName = "SingleSignOnAPI";
private String apiVersion = "1.0.0";
private String callbackUrl = "www.youtube.com";
private String testApplicationName = "SSOTestApplication";

private HttpResponse response;
private HttpClient httpClient;
Expand Down Expand Up @@ -151,6 +153,7 @@ public void init() throws APIManagerIntegrationTestException {
@AfterClass(alwaysRun = true)
public void destroy() throws Exception {
super.cleanup();
deleteApplication();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this line before above line, After super.cleanup method couldn't do any of the server requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay. fixed.
Thanks.

}

@Test(description = "Login to publisher using username and password", groups = "wso2.apim.is")
Expand Down Expand Up @@ -569,13 +572,15 @@ private Boolean createAndPublishAPI() throws Exception {
return true;
}

private void createApplication() throws Exception {
@Test(description = "Create an application Using API", groups = "wso2.apim.is")
public void createApplicationTest() throws Exception {

//1
HttpResponse response = sendGetRequest(String.format(httpsStoreUrl + "/site/pages" +
"/applications.jag?tenant=" + storeContext.getSuperTenant().getDomain()));
"/applications.jag?tenant=" + storeContext.getSuperTenant().getDomain()));
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
"Response mismatch not 200");
String csrf = response.getLastHeader("Set-Cookie").getElements()[0].getValue();
EntityUtils.consume(response.getEntity());

//2
Expand All @@ -584,34 +589,73 @@ private void createApplication() throws Exception {
urlParameters.add(new BasicNameValuePair("tenant", storeContext.getSuperTenant().getDomain()));
urlParameters.add(new BasicNameValuePair("limit", "5"));
response = sendPOSTMessage(httpsStoreUrl + "/site/blocks/api/recently-added/ajax/list.jag",
urlParameters);
urlParameters);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
"Response mismatch not 200");
EntityUtils.consume(response.getEntity());


//3
urlParameters.clear();
urlParameters.add(new BasicNameValuePair("action", "sessionCheck"));
response = sendPOSTMessage(httpsStoreUrl + "/site/blocks/user/login/ajax/sessionCheck.jag",
urlParameters);
urlParameters);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
"Response mismatch not 200");
EntityUtils.consume(response.getEntity());

urlParameters.clear();
urlParameters.add(new BasicNameValuePair("action", "addApplication"));
urlParameters.add(new BasicNameValuePair("tier", "Unlimited"));
urlParameters.add(new BasicNameValuePair("callbackUrl", callbackUrl));
urlParameters.add(new BasicNameValuePair("description", "This is platform based application"));
urlParameters.add(new BasicNameValuePair("application", "SSOApplication"));
response = sendPOSTMessage(httpsStoreUrl + "/site/blocks/application/" +
"application-add/ajax/application-add.jag",
urlParameters);
urlParameters.add(new BasicNameValuePair("application", testApplicationName));
response = sendPOSTMessageWithCSRF(httpsStoreUrl + "/site/blocks/application/" +
"application-add/ajax/application-add.jag", urlParameters, csrf);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
"Response mismatch not 200");
boolean errorOccur = getResponseBody(response).contains("\"error\" : true");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about only using boolean error or boolean isError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. is error is more simple and meaningful.
Thanks.

assertFalse(errorOccur, "Error when Application Creation");
EntityUtils.consume(response.getEntity());

response = sendGetRequest(httpsStoreUrl + "/site/blocks/application/"
+ "application-list/ajax/application-list.jag?action=getApplications");
boolean appExist = getResponseBody(response).contains("\"name\" : \"" + testApplicationName + "\"");
assertTrue(appExist, "Application Creattion not succesful");
EntityUtils.consume(response.getEntity());
}

private void deleteApplication() throws Exception {
HttpResponse response = sendGetRequest(String.format(httpsStoreUrl + "/site/pages" +
"/applications.jag?tenant=" + storeContext.getSuperTenant().getDomain()));
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
String csrf = response.getLastHeader("Set-Cookie").getElements()[0].getValue();
EntityUtils.consume(response.getEntity());

urlParameters.clear();
urlParameters.add(new BasicNameValuePair("action", "removeApplication"));
urlParameters.add(new BasicNameValuePair("application", testApplicationName));
response = sendPOSTMessageWithCSRF(httpsStoreUrl + "/site/blocks/application/" +
"application-remove/ajax/application-remove.jag", urlParameters, csrf);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
boolean errorOccur = getResponseBody(response).contains("\"error\" : true");
assertTrue(!errorOccur, "Error on Application deletion");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have use assertFalse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. it is more meaningful.
Thanks.

EntityUtils.consume(response.getEntity());

urlParameters.clear();
urlParameters.add(new BasicNameValuePair("action", "sessionCheck"));
response = sendPOSTMessage(httpsStoreUrl + "/site/blocks/user/login/ajax/sessionCheck.jag", urlParameters);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatusLine().getStatusCode(),
"Response mismatch not 200");
EntityUtils.consume(response.getEntity());

response = sendGetRequest(httpsStoreUrl + "/site/blocks/application/"
+ "application-list/ajax/application-list.jag?action=getApplications");
boolean appExist = getResponseBody(response).contains("\"name\" : \"" + testApplicationName + "\"");
assertFalse(appExist, "Application Deletion not successfull");
EntityUtils.consume(response.getEntity());

}

Expand All @@ -630,6 +674,15 @@ private HttpResponse sendPOSTMessage(String url, List<NameValuePair> urlParamete
return httpClient.execute(post);
}

private HttpResponse sendPOSTMessageWithCSRF(String url, List<NameValuePair> urlParameters, String csrf)
throws Exception {
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
post.addHeader("Referer", url);
post.addHeader("X-CSRFToken", csrf);
post.setEntity(new UrlEncodedFormEntity(urlParameters));
return httpClient.execute(post);
}

private HttpResponse sendRedirectRequest(HttpResponse response) throws IOException {
Header[] headers = response.getAllHeaders();
Expand Down