Skip to content

Commit

Permalink
Merge pull request #20679 from HasiniSama/meta-attribute-get
Browse files Browse the repository at this point in the history
Integration tests for Organization /meta-attribute GET API
  • Loading branch information
AnuradhaSK committed Jul 24, 2024
2 parents b869b87 + 2d9b535 commit 54710c1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.identity.integration.test.rest.api.server.organization.management.v1;

import io.restassured.response.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -114,6 +115,7 @@ public void testGetM2MAccessToken() throws Exception {
public void testSelfOnboardOrganization(String requestBodyPath) throws Exception {

String body = readResource(requestBodyPath);
body = body.replace("${parentId}", StringUtils.EMPTY);
Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body, m2mToken);
response.then()
.log().ifValidationFails()
Expand Down Expand Up @@ -146,6 +148,24 @@ public void testGetOrganizationsWithUnsupportedAttribute() {
}

@Test(dependsOnMethods = "testGetOrganizationsWithUnsupportedAttribute")
public void testGetOrganizationsMetaAttributesWithInvalidOperator() {

String filterQuery = "?filter=attributes ca C&limit=10&recursive=false";
String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/meta-attributes" + filterQuery;
Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken);
validateErrorResponseWithoutTraceId(response, HttpStatus.SC_BAD_REQUEST, "ORG-60059");
}

@Test(dependsOnMethods = "testGetOrganizationsMetaAttributesWithInvalidOperator")
public void testGetOrganizationsMetaAttributesWithUnsupportedAttribute() {

String filterQuery = "?filter=attribute co S&limit=10&recursive=false";
String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/meta-attributes" + filterQuery;
Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken);
validateErrorResponseWithoutTraceId(response, HttpStatus.SC_BAD_REQUEST, "ORG-60023");
}

@Test(dependsOnMethods = "testGetOrganizationsMetaAttributesWithUnsupportedAttribute")
public void testGetDiscoveryConfigWithoutAddingConfig() {

String endpointURL = ORGANIZATION_CONFIGS_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.identity.integration.test.rest.api.server.organization.management.v1;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.nimbusds.oauth2.sdk.AccessTokenResponse;
import com.nimbusds.oauth2.sdk.AuthorizationGrant;
import com.nimbusds.oauth2.sdk.ClientCredentialsGrant;
Expand All @@ -41,6 +43,7 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -81,6 +84,7 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.testng.Assert.assertNotNull;
import static org.wso2.identity.integration.test.restclients.RestBaseClient.API_SERVER_PATH;
import static org.wso2.identity.integration.test.restclients.RestBaseClient.CONTENT_TYPE_ATTRIBUTE;
import static org.wso2.identity.integration.test.restclients.RestBaseClient.ORGANIZATION_PATH;
import static org.wso2.identity.integration.test.restclients.RestBaseClient.TENANT_PATH;
Expand All @@ -92,6 +96,7 @@
public class OrganizationManagementSuccessTest extends OrganizationManagementBaseTest {

private String organizationID;
private String childOrganizationID;
private String selfServiceAppId;
private String selfServiceAppClientId;
private String selfServiceAppClientSecret;
Expand Down Expand Up @@ -223,6 +228,7 @@ public void getM2MAccessToken() throws Exception {
public void testSelfOnboardOrganization() throws IOException {

String body = readResource("add-greater-hospital-organization-request-body.json");
body = body.replace("${parentId}", StringUtils.EMPTY);
Response response = getResponseOfPostWithOAuth2(ORGANIZATION_MANAGEMENT_API_BASE_PATH, body, m2mToken);
response.then()
.log().ifValidationFails()
Expand Down Expand Up @@ -253,18 +259,19 @@ public void testGetOrganization() {
public Object[][] dataProviderForFilterOrganizations() {

return new Object[][] {
{"?filter=name co G&limit=10&recursive=false", false, false},
{"?filter=attributes.Country co S&limit=10&recursive=false", true, false},
{"?filter=attributes.Country eq Sri Lanka&limit=10&recursive=false", true, false},
{"?filter=attributes.Country eq Sri Lanka and name co Greater&limit=10&recursive=false", true, false},
{"?filter=attributes.Country eq USA&limit=10&recursive=false", false, true}
{"name co G", false, false},
{"attributes.Country co S", true, false},
{"attributes.Country eq Sri Lanka", true, false},
{"attributes.Country eq Sri Lanka and name co Greater", true, false},
{"attributes.Country eq USA", false, true}
};
}

@Test(dependsOnMethods = "testGetOrganization", dataProvider = "dataProviderForFilterOrganizations")
public void testFilterOrganizations(String filterQuery, boolean expectAttributes, boolean expectEmptyList) {

String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + filterQuery;
String query = "?filter=" + filterQuery + "&limit=1&recursive=false";
String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + query;
Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken);
Assert.assertNotNull(response.asString());
response.then()
Expand Down Expand Up @@ -381,6 +388,65 @@ public void unShareB2BApplication() throws JSONException {
}

@Test(dependsOnMethods = "unShareB2BApplication")
public void testOnboardChildOrganization() throws IOException {

String body = readResource("add-smaller-hospital-organization-request-body.json");
body = body.replace("${parentId}", organizationID);
HttpPost request = new HttpPost(serverURL + TENANT_PATH + tenant + PATH_SEPARATOR + ORGANIZATION_PATH
+ API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH);
Header[] headerList = new Header[3];
headerList[0] = new BasicHeader("Authorization", "Bearer " + switchedM2MToken);
headerList[1] = new BasicHeader(CONTENT_TYPE_ATTRIBUTE, "application/json");
headerList[2] = new BasicHeader(HttpHeaders.ACCEPT, "application/json");
request.setHeaders(headerList);
request.setEntity(new StringEntity(body));
HttpResponse response = client.execute(request);
Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED);

String jsonResponse = EntityUtils.toString(response.getEntity());
JsonObject responseObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
childOrganizationID = responseObject.get("id").getAsString();
assertNotNull(childOrganizationID);
}

@DataProvider(name = "dataProviderForGetOrganizationsMetaAttributes")
public Object[][] dataProviderForGetOrganizationsMetaAttributes() {

return new Object[][] {
{"attributes eq Country", false, false},
{"attributes sw C and attributes ew try", false, false},
{"attributes eq Region", true, false},
{"attributes co A", true, true},
};
}

@Test(dependsOnMethods = "testOnboardChildOrganization",
dataProvider = "dataProviderForGetOrganizationsMetaAttributes")
public void testGetOrganizationsMetaAttributes(String filter, boolean isRecursive, boolean expectEmptyList) {

String query = "?filter=" + filter + "&limit=1&recursive=" + isRecursive;
String endpointURL = ORGANIZATION_MANAGEMENT_API_BASE_PATH + "/meta-attributes" + query;
Response response = getResponseOfGetWithOAuth2(endpointURL, m2mToken);
Assert.assertNotNull(response.asString());
response.then()
.log().ifValidationFails()
.assertThat()
.statusCode(HttpStatus.SC_OK);
if (expectEmptyList) {
response.then()
.assertThat().body(equalTo("{}"));
} else if (isRecursive) {
response.then()
.body("attributes.size()", equalTo(1))
.body("attributes[0]", equalTo("Region"));
} else {
response.then()
.body("attributes.size()", equalTo(1))
.body("attributes[0]", equalTo("Country"));
}
}

@Test(dependsOnMethods = "testGetOrganizationsMetaAttributes")
public void testAddDiscoveryConfig() throws IOException {

String endpointURL = ORGANIZATION_CONFIGS_API_BASE_PATH + ORGANIZATION_DISCOVERY_API_PATH;
Expand Down Expand Up @@ -505,6 +571,19 @@ public void testDeleteDiscoveryConfig() {
}

@Test(dependsOnMethods = "testDeleteDiscoveryConfig")
public void testDeleteChildOrganization() throws IOException {

HttpDelete request = new HttpDelete(serverURL + TENANT_PATH + tenant + PATH_SEPARATOR + ORGANIZATION_PATH
+ API_SERVER_PATH + ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR
+ childOrganizationID);
Header[] headerList = new Header[1];
headerList[0] = new BasicHeader("Authorization", "Bearer " + switchedM2MToken);
request.setHeaders(headerList);
HttpResponse response = client.execute(request);
Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_NO_CONTENT);
}

@Test(dependsOnMethods = "testDeleteChildOrganization")
public void testDisablingOrganization() throws IOException {

String endpoint = ORGANIZATION_MANAGEMENT_API_BASE_PATH + PATH_SEPARATOR + organizationID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class RestBaseClient {
public static final String CONTENT_TYPE_ATTRIBUTE = "Content-Type";
public static final String AUTHORIZATION_ATTRIBUTE = "Authorization";
public static final String USER_AGENT_ATTRIBUTE = "User-Agent";
public static final String API_SERVER_PATH = "api/server/v1";
public static final String TENANT_PATH = "t/";
public static final String ORGANIZATION_PATH = "o/";
public static final String PATH_SEPARATOR = "/";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "Greater Hospital",
"description": "Hospital chain in South Asia",
"parentId": "${parentId}",
"attributes": [
{
"key": "Country",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "Smaller Hospital",
"description": "Hospital chain in South Asia",
"parentId": "${parentId}",
"attributes": [
{
"key": "Country",
"key": "Region",
"value": "Sri Lanka"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"/api/server/v1/organizations": [
"internal_organization_view",
"internal_organization_create"
],
],
"/api/server/v1/organizations/discovery": [
"internal_organization_discovery_view",
"internal_organization_discovery_update",
Expand All @@ -16,7 +16,7 @@
"/scim2/Users": [
"internal_user_mgt_view",
"internal_user_mgt_create"
],
],
"/o/scim2/Users": [
"internal_org_user_mgt_create",
"internal_org_user_mgt_list"
Expand All @@ -27,5 +27,9 @@
],
"/o/api/server/v1/applications": [
"internal_org_application_mgt_view"
],
"/o/api/server/v1/organizations": [
"internal_org_organization_create",
"internal_org_organization_delete"
]
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@
<!-- Identity REST API feature -->
<identity.api.dispatcher.version>2.0.17</identity.api.dispatcher.version>
<identity.user.api.version>1.3.38</identity.user.api.version>
<identity.server.api.version>1.2.211</identity.server.api.version>
<identity.server.api.version>1.2.212</identity.server.api.version>

<identity.agent.sso.version>5.5.9</identity.agent.sso.version>
<identity.tool.samlsso.validator.version>5.5.8</identity.tool.samlsso.validator.version>
Expand Down

0 comments on commit 54710c1

Please sign in to comment.