Skip to content

Commit

Permalink
Merge pull request #2080 from CrowleyRajapakse/new-main4
Browse files Browse the repository at this point in the history
Adding initial apk agent integration tests
  • Loading branch information
CrowleyRajapakse authored Mar 5, 2024
2 parents 123ecbe + aac22d1 commit 6481d63
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ allow_refresh_tokens = true
iat_validity_period = "1h"
{{- end }}

[oauth.endpoints]
oauth2_jwks_url = "https://apim-wso2am-cp-1-service:9443/oauth2/jwks"

#[apim.publisher]
#{{- if .Values.wso2.apim.configurations.publisher.supportedDocumentTypes }}
#supported_document_types = {{ toJson .Values.wso2.apim.configurations.publisher.supportedDocumentTypes }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,149 @@ public void make_a_api_revision_deployment_request() throws Exception {
Thread.sleep(3000);
}

@When("make the Change Lifecycle request")
public void make_a_change_lifecycle_request() throws Exception {
String apiUUID = sharedContext.getApiUUID();
String payload = "";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getPublisherAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doPost(Utils.getAPIChangeLifecycleURL(apiUUID),
headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);

sharedContext.setResponse(response);
Thread.sleep(3000);
}

@When("make the Application Creation request")
public void make_application_creation_request() throws Exception {
logger.info("Creating an application");
String payload = "{\"name\":\"PetstoreApp\",\"throttlingPolicy\":\"10PerMin\",\"description\":\"test app\",\"tokenType\":\"JWT\",\"groups\":null,\"attributes\":{}}";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doPost(Utils.getApplicationCreateURL(),
headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
logger.info("Response: " + sharedContext.getResponseBody());
sharedContext.setApplicationUUID(Utils.extractApplicationID(sharedContext.getResponseBody()));
Thread.sleep(3000);
}

@When("I have a KeyManager")
public void i_have_a_key_manager() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doGet(Utils.getKeyManagerURL(),
headers);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
sharedContext.setKeyManagerUUID(Utils.extractKeyManagerID(sharedContext.getResponseBody()));
Thread.sleep(3000);
}

@When("make the Generate Keys request")
public void make_generate_keys_request() throws Exception {
String applicationUUID = sharedContext.getApplicationUUID();
String keyManagerUUID = sharedContext.getKeyManagerUUID();
logger.info("Key Manager UUID: " + keyManagerUUID);
logger.info("Application UUID: " + applicationUUID);
String payload = "{\"keyType\":\"PRODUCTION\",\"grantTypesToBeSupported\":[\"password\",\"client_credentials\"]," +
"\"callbackUrl\":\"\",\"additionalProperties\":{\"application_access_token_expiry_time\":\"N/A\"," +
"\"user_access_token_expiry_time\":\"N/A\",\"refresh_token_expiry_time\":\"N/A\"," +
"\"id_token_expiry_time\":\"N/A\",\"pkceMandatory\":\"false\",\"pkceSupportPlain\":\"false\"," +
"\"bypassClientCredentials\":\"false\"},\"keyManager\":\"" + keyManagerUUID +"\"," +
"\"validityTime\":3600,\"scopes\":[\"default\"]}";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

logger.info("Payload: " + payload);

HttpResponse response = sharedContext.getHttpClient().doPost(Utils.getGenerateKeysURL(applicationUUID),
headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
sharedContext.setConsumerSecret(Utils.extractKeys(sharedContext.getResponseBody(), "consumerSecret"));
sharedContext.setConsumerKey(Utils.extractKeys(sharedContext.getResponseBody(), "consumerKey"));
Thread.sleep(3000);
}

@When("make the Subscription request")
public void make_subscription_request() throws Exception {
String applicationUUID = sharedContext.getApplicationUUID();
String apiUUID = sharedContext.getApiUUID();
logger.info("API UUID: " + apiUUID);
logger.info("Application UUID: " + applicationUUID);
String payload = "{\"apiId\":\"" + apiUUID + "\",\"applicationId\":\"" + applicationUUID + "\",\"throttlingPolicy\":\"Gold\"}";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doPost(Utils.getSubscriptionURL(),
headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
Thread.sleep(3000);
}

@When("I get oauth keys for application")
public void get_oauth_keys_for_application() throws Exception {
String applicationUUID = sharedContext.getApplicationUUID();

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doGet(Utils.getOauthKeysURL(applicationUUID),
headers);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
sharedContext.setOauthKeyUUID(Utils.extractOAuthMappingID(sharedContext.getResponseBody()));
Thread.sleep(3000);
}

@When("make the Access Token Generation request")
public void make_access_token_generation_request() throws Exception {
String applicationUUID = sharedContext.getApplicationUUID();
String oauthKeyUUID = sharedContext.getOauthKeyUUID();
String consumerKey = sharedContext.getConsumerKey();
String consumerSecret = sharedContext.getConsumerSecret();
logger.info("Oauth Key UUID: " + oauthKeyUUID);
logger.info("Application UUID: " + applicationUUID);
String payload = "{\"consumerSecret\":\"" + consumerSecret + "\",\"validityPeriod\":3600,\"revokeToken\":null," +
"\"scopes\":[\"write:pets\",\"read:pets\"],\"additionalProperties\":{\"id_token_expiry_time\":3600," +
"\"application_access_token_expiry_time\":3600,\"user_access_token_expiry_time\":3600,\"bypassClientCredentials\":false," +
"\"pkceMandatory\":false,\"pkceSupportPlain\":false,\"refresh_token_expiry_time\":86400}}";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getDevportalAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);

HttpResponse response = sharedContext.getHttpClient().doPost(Utils.getAccessTokenGenerationURL(applicationUUID, oauthKeyUUID),
headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);

sharedContext.setResponse(response);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
sharedContext.setApiAccessToken(Utils.extractKeys(sharedContext.getResponseBody(), "accessToken"));
logger.info("Access Token: " + sharedContext.getApiAccessToken());
Thread.sleep(3000);
}

@When("make the API Deployment request")
public void make_a_api_deployment_request() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ public void theResponseBodyShouldContain(DataTable dataTable) throws IOException
public void theResponseStatusCodeShouldBe(int expectedStatusCode) throws IOException {

int actualStatusCode = sharedContext.getResponse().getStatusLine().getStatusCode();
((CloseableHttpResponse)sharedContext.getResponse()).close();
Assert.assertEquals(actualStatusCode, expectedStatusCode);
}

@Then("I send {string} request to {string} with body {string}")
public void sendHttpRequest(String httpMethod, String url, String body) throws IOException {
sharedContext.addHeader("Authorization", "Bearer " + sharedContext.getApiAccessToken());
body = Utils.resolveVariables(body, sharedContext.getValueStore());
if (sharedContext.getResponse() instanceof CloseableHttpResponse) {
((CloseableHttpResponse) sharedContext.getResponse()).close();
Expand Down Expand Up @@ -316,8 +318,8 @@ public void decode_header_and_validate(String header,String jwksEndpoint, DataTa
}
}

@Given("I have a DCR application for Publisher")
public void iHaveADCRApplicationForPublisher() throws Exception {
@Given("I have a DCR application")
public void iHaveADCRApplication() throws Exception {

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_IDP_HOST);
Expand All @@ -331,39 +333,21 @@ public void iHaveADCRApplicationForPublisher() throws Exception {
" \"saasApp\":true\n" +
" }",
Constants.CONTENT_TYPES.APPLICATION_JSON);
sharedContext.setPublisherBasicAuthToken(Utils.extractBasicToken(httpResponse));
sharedContext.addStoreValue("publisherBasicAuthToken", sharedContext.getPublisherBasicAuthToken());
sharedContext.setBasicAuthToken(Utils.extractBasicToken(httpResponse));
sharedContext.addStoreValue("publisherBasicAuthToken", sharedContext.getBasicAuthToken());
}

@Given("I have a DCR application for Devportal")
public void iHaveADCRApplicationForDevportal() throws Exception {

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_IDP_HOST);
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Basic YWRtaW46YWRtaW4=");

HttpResponse httpResponse = httpClient.doPost(Utils.getDCREndpointURL(), headers, "{\n" +
" \"callbackUrl\":\"www.google.lk\",\n" +
" \"clientName\":\"rest_api_publisher\",\n" +
" \"owner\":\"admin\",\n" +
" \"grantType\":\"client_credentials password refresh_token\",\n" +
" \"saasApp\":true\n" +
" }",
Constants.CONTENT_TYPES.APPLICATION_JSON);
sharedContext.setDevportalBasicAuthToken(Utils.extractBasicToken(httpResponse));
sharedContext.addStoreValue("devportalBasicAuthToken", sharedContext.getDevportalBasicAuthToken());
}

@Given("I have a valid Publisher access token")
public void iHaveValidPublisherAccessToken() throws Exception {

Map<String, String> headers = new HashMap<>();
String basicAuthHeader = "Basic " + sharedContext.getPublisherBasicAuthToken();
String basicAuthHeader = "Basic " + sharedContext.getBasicAuthToken();
logger.info("Basic Auth Header: " + basicAuthHeader);
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_IDP_HOST);
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, basicAuthHeader);

HttpResponse httpResponse = httpClient.doPost(Utils.getTokenEndpointURL(), headers, "grant_type=password&username=admin&password=admin&scope=apim:api_view apim:api_create",
HttpResponse httpResponse = httpClient.doPost(Utils.getTokenEndpointURL(), headers, "grant_type=password&username=admin&password=admin&scope=apim:api_view apim:api_create apim:api_publish",
Constants.CONTENT_TYPES.APPLICATION_X_WWW_FORM_URLENCODED);
logger.info("Response: " + httpResponse);
sharedContext.setPublisherAccessToken(Utils.extractToken(httpResponse));
Expand All @@ -372,16 +356,18 @@ public void iHaveValidPublisherAccessToken() throws Exception {

@Given("I have a valid Devportal access token")
public void iHaveValidDevportalAccessToken() throws Exception {
logger.info("Basic Auth Header: " + sharedContext.getBasicAuthToken());

Map<String, String> headers = new HashMap<>();
String basicAuthHeader = "Basic " + sharedContext.getDevportalBasicAuthToken();
String basicAuthHeader = "Basic " + sharedContext.getBasicAuthToken();
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_IDP_HOST);
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, basicAuthHeader);

HttpResponse httpResponse = httpClient.doPost(Utils.getTokenEndpointURL(), headers, "grant_type=password&username=admin&password=admin&scope=apim:api_view apim:api_create",
Constants.CONTENT_TYPES.APPLICATION_JSON);
HttpResponse httpResponse = httpClient.doPost(Utils.getTokenEndpointURL(), headers, "grant_type=password&username=admin&password=admin&scope=apim:app_manage apim:sub_manage apim:subscribe",
Constants.CONTENT_TYPES.APPLICATION_X_WWW_FORM_URLENCODED);
sharedContext.setDevportalAccessToken(Utils.extractToken(httpResponse));
sharedContext.addStoreValue("devportalAccessToken", sharedContext.getDevportalAccessToken());
logger.info("Devportal Access Token: " + sharedContext.getDevportalAccessToken());
}

// @Given("I have a valid subscription without api deploy permission")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ public class SharedContext {
private SimpleHTTPClient httpClient;
private String publisherAccessToken;
private String devportalAccessToken;
private String publisherBasicAuthToken;
private String devportalBasicAuthToken;
private String basicAuthToken;
private HttpResponse response;
private String responseBody;
private String apiUUID;
private String revisionUUID;
private String applicationUUID;
private String keyManagerUUID;
private String oauthKeyUUID;
private String consumerSecret;
private String consumerKey;
private String apiAccessToken;
private HashMap<String, Object> valueStore = new HashMap<>();
private HashMap<String, String> headers = new HashMap<>();

Expand Down Expand Up @@ -69,24 +74,14 @@ public void setDevportalAccessToken(String accessToken) {
this.devportalAccessToken = accessToken;
}

public String getPublisherBasicAuthToken() {
public String getBasicAuthToken() {

return publisherBasicAuthToken;
return basicAuthToken;
}

public void setPublisherBasicAuthToken(String basicAuthToken) {
public void setBasicAuthToken(String basicAuthToken) {

this.publisherBasicAuthToken = basicAuthToken;
}

public String getDevportalBasicAuthToken() {

return devportalBasicAuthToken;
}

public void setDevportalBasicAuthToken(String basicAuthToken) {

this.devportalBasicAuthToken = basicAuthToken;
this.basicAuthToken = basicAuthToken;
}

public HttpResponse getResponse() {
Expand Down Expand Up @@ -148,4 +143,64 @@ public void setRevisionUUID(String revisionUUID) {

this.revisionUUID = revisionUUID;
}

public String getApplicationUUID() {

return applicationUUID;
}

public void setApplicationUUID(String applicationUUID) {

this.applicationUUID = applicationUUID;
}

public String getKeyManagerUUID() {

return keyManagerUUID;
}

public void setKeyManagerUUID(String keyManagerUUID) {

this.keyManagerUUID = keyManagerUUID;
}

public String getOauthKeyUUID() {

return oauthKeyUUID;
}

public void setOauthKeyUUID(String oauthKeyUUID) {

this.oauthKeyUUID = oauthKeyUUID;
}

public String getConsumerSecret() {

return consumerSecret;
}

public void setConsumerSecret(String consumerSecret) {

this.consumerSecret = consumerSecret;
}

public String getConsumerKey() {

return consumerKey;
}

public void setConsumerKey(String consumerKey) {

this.consumerKey = consumerKey;
}

public String getApiAccessToken() {

return apiAccessToken;
}

public void setApiAccessToken(String apiAccessToken) {

this.apiAccessToken = apiAccessToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Constants {
public static final String DEFAULT_DCR_EP = "client-registration/v0.17/register";
public static final String DEFAULT_API_CONFIGURATOR = "api/configurator/1.0.0/";
public static final String DEFAULT_API_DEPLOYER = "api/am/publisher/v4/";
public static final String DEFAULT_DEVPORTAL = "api/am/devportal/v3/";
public static final String ACCESS_TOKEN = "accessToken";
public static final String EMPTY_STRING = "";
public static final String API_CREATE_SCOPE = "apk:api_create";
Expand Down
Loading

0 comments on commit 6481d63

Please sign in to comment.