Skip to content

Commit f1183e8

Browse files
Add integration tests for application creation, role creation and API Authorization in sub organization
1 parent bb6f690 commit f1183e8

File tree

10 files changed

+1038
-391
lines changed

10 files changed

+1038
-391
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<parent>
4+
<artifactId>identity-integration-tests</artifactId>
5+
<groupId>org.wso2.is</groupId>
6+
<version>7.1.0-m2-SNAPSHOT</version>
7+
<relativePath>../../pom.xml</relativePath>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>jacoco-report-generator</artifactId>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<artifactId>maven-shade-plugin</artifactId>
15+
<version>${maven-shade-plugin.version}</version>
16+
<executions>
17+
<execution>
18+
<phase>package</phase>
19+
<goals>
20+
<goal>shade</goal>
21+
</goals>
22+
<configuration>
23+
<artifactSet>
24+
<includes>
25+
<include>org.jacoco:org.jacoco.core</include>
26+
<include>org.jacoco:org.jacoco.report</include>
27+
<include>org.codehaus.plexus:plexus-utils</include>
28+
<include>org.ow2.asm:asm</include>
29+
<include>org.ow2.asm:asm-tree</include>
30+
<include>org.ow2.asm:asm-commons</include>
31+
</includes>
32+
</artifactSet>
33+
<transformers>
34+
<transformer>
35+
<mainClass>org.wso2.carbon.identity.jacoco.ReportGenerator</mainClass>
36+
</transformer>
37+
</transformers>
38+
</configuration>
39+
</execution>
40+
</executions>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.wso2.carbon.identity.inbound.auth.sts</groupId>
47+
<artifactId>org.wso2.carbon.identity.sts.passive.stub</artifactId>
48+
<version>5.11.9</version>
49+
<scope>compile</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.wso2.carbon.automationutils</groupId>
53+
<artifactId>org.wso2.carbon.integration.common.extensions</artifactId>
54+
<version>4.5.4</version>
55+
<scope>compile</scope>
56+
</dependency>
57+
</dependencies>
58+
<properties>
59+
<maven.compiler.target>11</maven.compiler.target>
60+
<maven.compiler.source>11</maven.compiler.source>
61+
<maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
62+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
63+
<org.jacoco.version>0.8.12</org.jacoco.version>
64+
<plexus-utils.version>4.0.1</plexus-utils.version>
65+
</properties>
66+
</project>

modules/integration/tests-integration/tests-backend/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@
589589
<groupId>org.apache.maven.plugins</groupId>
590590
<artifactId>maven-compiler-plugin</artifactId>
591591
<configuration>
592-
<source>1.8</source>
593-
<target>1.8</target>
592+
<source>11</source>
593+
<target>11</target>
594594
</configuration>
595595
</plugin>
596596
</plugins>

modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oauth2/OAuth2ServiceAbstractIntegrationTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,43 @@ public void authorizeSystemAPIs(String applicationId, List<String> apiIdentifier
11771177
});
11781178
}
11791179

1180+
/**
1181+
* Authorize list of SYSTEM APIs to an application registered in sub organization.
1182+
*
1183+
* @param applicationId Application id.
1184+
* @param apiIdentifiers API identifiers to authorize.
1185+
* @throws Exception Error occured while authorizing APIs.
1186+
*/
1187+
public void authorizeSystemAPIsToSubOrganizationApp(String applicationId, List<String> apiIdentifiers,
1188+
String switchedM2MToken) {
1189+
1190+
apiIdentifiers.stream().forEach(apiIdentifier -> {
1191+
try {
1192+
List<APIResourceListItem> filteredAPIResource =
1193+
restClient.getAPIResourcesWithFilteringFromSubOrganization("identifier+eq+" + apiIdentifier,
1194+
switchedM2MToken);
1195+
if (filteredAPIResource == null || filteredAPIResource.isEmpty()) {
1196+
return;
1197+
}
1198+
String apiId = filteredAPIResource.get(0).getId();
1199+
// Get API scopes.
1200+
List<ScopeGetModel> apiResourceScopes = restClient.getAPIResourceScopesInSubOrganization(apiId,
1201+
switchedM2MToken);
1202+
AuthorizedAPICreationModel authorizedAPICreationModel = new AuthorizedAPICreationModel();
1203+
authorizedAPICreationModel.setId(apiId);
1204+
authorizedAPICreationModel.setPolicyIdentifier("RBAC");
1205+
apiResourceScopes.forEach(scope -> {
1206+
authorizedAPICreationModel.addScopesItem(scope.getName());
1207+
});
1208+
restClient.addAPIAuthorizationToSubOrgApplication(applicationId, authorizedAPICreationModel,
1209+
switchedM2MToken);
1210+
} catch (Exception e) {
1211+
throw new RuntimeException("Error while authorizing system API " + apiIdentifier + " to application "
1212+
+ applicationId, e);
1213+
}
1214+
});
1215+
}
1216+
11801217
public String getRoleV2ResourceId(String roleName, String audienceType, String OrganizationId) throws Exception {
11811218

11821219
List<String> roles = restClient.getRoles(roleName, audienceType, OrganizationId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package org.wso2.identity.integration.test.rest.api.server.application.management.v1;
2+
3+
import com.nimbusds.oauth2.sdk.AccessTokenResponse;
4+
import com.nimbusds.oauth2.sdk.AuthorizationGrant;
5+
import com.nimbusds.oauth2.sdk.ClientCredentialsGrant;
6+
import com.nimbusds.oauth2.sdk.ResourceOwnerPasswordCredentialsGrant;
7+
import com.nimbusds.oauth2.sdk.Scope;
8+
import com.nimbusds.oauth2.sdk.TokenRequest;
9+
import com.nimbusds.oauth2.sdk.TokenResponse;
10+
import com.nimbusds.oauth2.sdk.auth.ClientAuthentication;
11+
import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic;
12+
import com.nimbusds.oauth2.sdk.auth.Secret;
13+
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
14+
import com.nimbusds.oauth2.sdk.id.ClientID;
15+
import org.json.JSONObject;
16+
import org.testng.Assert;
17+
import org.testng.annotations.AfterClass;
18+
import org.testng.annotations.BeforeClass;
19+
import org.testng.annotations.DataProvider;
20+
import org.testng.annotations.Factory;
21+
import org.testng.annotations.Test;
22+
import org.wso2.carbon.automation.engine.context.TestUserMode;
23+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationResponseModel;
24+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.AssociatedRolesConfig;
25+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.InboundProtocolListItem;
26+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.InboundProtocols;
27+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration;
28+
import org.wso2.identity.integration.test.rest.api.server.organization.management.v1.OrganizationManagementBaseTest;
29+
import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.Audience;
30+
import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.Permission;
31+
import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.RoleV2;
32+
import org.wso2.identity.integration.test.restclients.OAuth2RestClient;
33+
import org.wso2.identity.integration.test.restclients.OrgMgtRestClient;
34+
import org.wso2.identity.integration.test.utils.OAuth2Constant;
35+
36+
import java.net.URI;
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.Collections;
40+
import java.util.List;
41+
42+
public class OrganizationOAuth2ApplicationManagementSuccessTest extends OrganizationManagementBaseTest {
43+
44+
private static final String AUTHORIZED_APIS_JSON = "org-based-authorized-apis.json";
45+
private static final String SUB_ORG_NAME = "subOrg";
46+
47+
private OrgMgtRestClient orgMgtRestClient;
48+
private OAuth2RestClient oAuth2RestClient;
49+
private String subOrgId;
50+
private String switchedM2MToken;
51+
private String subOrgAppToken;
52+
53+
@Factory(dataProvider = "restAPIUserConfigProvider")
54+
public OrganizationOAuth2ApplicationManagementSuccessTest(TestUserMode userMode) throws Exception {
55+
56+
super.init(userMode);
57+
this.context = isServer;
58+
this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName();
59+
this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword();
60+
this.tenant = context.getContextTenant().getDomain();
61+
}
62+
63+
@BeforeClass(alwaysRun = true)
64+
public void initClass() throws Exception {
65+
66+
super.testInit("v1", swaggerDefinition, tenant);
67+
oAuth2RestClient = new OAuth2RestClient(serverURL, tenantInfo);
68+
69+
orgMgtRestClient = new OrgMgtRestClient(isServer, tenantInfo, serverURL,
70+
new JSONObject(readResource(AUTHORIZED_APIS_JSON, this.getClass())));
71+
subOrgId = orgMgtRestClient.addOrganization(SUB_ORG_NAME);
72+
switchedM2MToken = orgMgtRestClient.switchM2MToken(subOrgId);
73+
orgMgtRestClient.addOrganizationUser("sub-org-user", "SubOrgUser@123");
74+
}
75+
76+
@AfterClass(alwaysRun = true)
77+
public void atEnd() throws Exception {
78+
79+
orgMgtRestClient.deleteOrganization(subOrgId);
80+
orgMgtRestClient.closeHttpClient();
81+
oAuth2RestClient.closeHttpClient();
82+
}
83+
84+
@Test
85+
public void testCreateOAuth2ApplicationInOrganization() throws Exception {
86+
87+
String body = readResource("create-basic-oauth2-application.json", this.getClass());
88+
89+
oAuth2RestClient.createApplicationInSubOrganization(body, switchedM2MToken);
90+
System.out.println("Sub Organization Application ID : " + oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP", switchedM2MToken));
91+
String subOrganizationAppId = oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP",
92+
switchedM2MToken);
93+
94+
// Authorizing the APIs to the sub organization application
95+
authorizeSystemAPIsToSubOrganizationApp(oAuth2RestClient, subOrganizationAppId,
96+
new ArrayList<>(Arrays.asList("/o/scim2/Roles", "/o/oauth2/introspect")), switchedM2MToken);
97+
98+
// Creating an application role for the sub organization application
99+
RoleV2 role;
100+
String displayName;
101+
List<String> schemas = Collections.emptyList();
102+
List<Permission> permissions = new ArrayList<>();
103+
permissions.add(new Permission("internal_org_role_mgt_create"));
104+
permissions.add(new Permission("internal_org_role_mgt_view"));
105+
displayName = "Application Role";
106+
Audience roleAudience = new Audience("APPLICATION", subOrganizationAppId);
107+
role = new RoleV2(roleAudience, displayName, permissions, schemas);
108+
oAuth2RestClient.createV2RolesInSubOrganization(role, switchedM2MToken);
109+
ApplicationResponseModel subOrgAppModel = oAuth2RestClient.getSubOrgApplication(subOrganizationAppId,
110+
switchedM2MToken);
111+
112+
// Validate application details
113+
Assert.assertEquals(subOrgAppModel.getName(), "My SAMPLE APP");
114+
115+
// Validate application role audience and roles
116+
AssociatedRolesConfig associatedRolesConfig = subOrgAppModel.getAssociatedRoles();
117+
Assert.assertEquals(associatedRolesConfig.getAllowedAudience().toString(), "APPLICATION");
118+
Assert.assertEquals(associatedRolesConfig.getRoles().get(0).getName(), "Application Role");
119+
120+
// Validate application inbound protocols
121+
List<InboundProtocolListItem> inboundProtocols = subOrgAppModel.getInboundProtocols();
122+
Assert.assertEquals(inboundProtocols.size(), 1);
123+
}
124+
125+
@Test(dependsOnMethods = "testCreateOAuth2ApplicationInOrganization")
126+
public void testIssueAccessTokenFromSubOrgApplicationFromCCGrant() throws Exception {
127+
128+
String subOrganizationAppId = oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP",
129+
switchedM2MToken);
130+
OpenIDConnectConfiguration oidcConfig = oAuth2RestClient.getOIDCInboundDetailsForSubOrgApplications(
131+
subOrganizationAppId, switchedM2MToken);
132+
String subOrgAppClientId = oidcConfig.getClientId();
133+
String clientSecret = oidcConfig.getClientSecret();
134+
135+
// Issue access token from sub organization application
136+
AccessTokenResponse accessTokenResponse = getSubOrgApplicationToken("client_credentials", subOrgAppClientId, clientSecret, subOrgId);
137+
subOrgAppToken = accessTokenResponse.getTokens().getAccessToken().getValue();
138+
Assert.assertNotNull(subOrgAppToken);
139+
String scopes = accessTokenResponse.getTokens().getAccessToken().getScope().toString();
140+
String[] scopeArray = scopes.split(" ");
141+
Assert.assertEquals(scopeArray.length, 6);
142+
Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_create"));
143+
Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_view"));
144+
Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_update"));
145+
Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_delete"));
146+
}
147+
148+
public void testAccessResourcesFromTokensIssuedFromSubOrgApplication() throws Exception {
149+
150+
// Access resources from tokens issued from sub organization application
151+
oAuth2RestClient.getRoles(subOrgAppToken);
152+
}
153+
154+
private AccessTokenResponse getSubOrgApplicationToken(String grantType, String clientId, String clientSecretStr, String orgId) throws Exception {
155+
156+
URI tokenEndpoint = new URI("https://localhost:9853/t/carbon.super/o/" + orgId + "/oauth2/token");
157+
158+
ClientID clientID = new ClientID(clientId);
159+
Secret clientSecret = new Secret(clientSecretStr);
160+
ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret);
161+
162+
AuthorizationGrant authorizationGrant;
163+
switch (grantType) {
164+
case OAuth2Constant.OAUTH2_GRANT_TYPE_CLIENT_CREDENTIALS:
165+
authorizationGrant = new ClientCredentialsGrant();
166+
break;
167+
case OAuth2Constant.OAUTH2_GRANT_TYPE_RESOURCE_OWNER:
168+
authorizationGrant = new ResourceOwnerPasswordCredentialsGrant(null, null);
169+
break;
170+
default:
171+
throw new Exception("Unsupported grant type");
172+
}
173+
Scope scope = new Scope("SYSTEM");
174+
175+
TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, authorizationGrant, scope);
176+
HTTPResponse tokenHTTPResp = request.toHTTPRequest().send();
177+
TokenResponse tokenResponse = TokenResponse.parse(tokenHTTPResp);
178+
return tokenResponse.toSuccessResponse();
179+
}
180+
181+
@DataProvider(name = "restAPIUserConfigProvider")
182+
public static Object[][] restAPIUserConfigProvider() {
183+
184+
return new Object[][]{
185+
{TestUserMode.SUPER_TENANT_ADMIN}
186+
// {TestUserMode.TENANT_ADMIN}
187+
};
188+
}
189+
}

modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/rest/api/server/organization/management/v1/OrganizationManagementBaseTest.java

+54
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
import org.testng.annotations.AfterClass;
3131
import org.testng.annotations.AfterMethod;
3232
import org.testng.annotations.BeforeMethod;
33+
import org.wso2.identity.integration.test.rest.api.server.api.resource.v1.model.APIResourceListItem;
34+
import org.wso2.identity.integration.test.rest.api.server.api.resource.v1.model.ScopeGetModel;
3335
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.AdvancedApplicationConfiguration;
3436
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationModel;
3537
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationSharePOSTRequest;
38+
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.AuthorizedAPICreationModel;
3639
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.InboundProtocols;
3740
import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration;
3841
import org.wso2.identity.integration.test.rest.api.server.common.RESTAPIServerTestBase;
@@ -173,12 +176,26 @@ protected String getAppClientId(String applicationId) throws Exception {
173176
return oidcConfig.getClientId();
174177
}
175178

179+
protected String getSubOrgAppClientId(String applicationId, String switchedToken) throws Exception {
180+
181+
OpenIDConnectConfiguration oidcConfig = oAuth2RestClient.getOIDCInboundDetailsForSubOrgApplications(
182+
applicationId, switchedToken);
183+
return oidcConfig.getClientId();
184+
}
185+
176186
protected String getAppClientSecret(String applicationId) throws Exception {
177187

178188
OpenIDConnectConfiguration oidcConfig = oAuth2RestClient.getOIDCInboundDetails(applicationId);
179189
return oidcConfig.getClientSecret();
180190
}
181191

192+
protected String getSubOrgAppClientSecret(String applicationId, String switchedM2MToken) throws Exception {
193+
194+
OpenIDConnectConfiguration oidcConfig = oAuth2RestClient.getOIDCInboundDetailsForSubOrgApplications(
195+
applicationId, switchedM2MToken);
196+
return oidcConfig.getClientSecret();
197+
}
198+
182199
protected String buildGetRequestURL(String endpointURL, String tenantDomain, List<NameValuePair> queryParams) {
183200

184201
String authorizeEndpoint = getTenantQualifiedURL(endpointURL, tenantDomain);
@@ -259,4 +276,41 @@ protected String createB2BUser(String switchedM2MToken) throws Exception {
259276
Assert.assertNotNull(b2bUserID, "B2B user creation failed.");
260277
return b2bUserID;
261278
}
279+
280+
/**
281+
* Authorize list of SYSTEM APIs to an application registered in sub organization.
282+
*
283+
* @param applicationId Application id.
284+
* @param apiIdentifiers API identifiers to authorize.
285+
* @throws Exception Error occured while authorizing APIs.
286+
*/
287+
public void authorizeSystemAPIsToSubOrganizationApp(OAuth2RestClient restClient, String applicationId, List<String> apiIdentifiers,
288+
String switchedM2MToken) {
289+
290+
apiIdentifiers.stream().forEach(apiIdentifier -> {
291+
try {
292+
List<APIResourceListItem> filteredAPIResource =
293+
restClient.getAPIResourcesWithFilteringFromSubOrganization("identifier+eq+" + apiIdentifier,
294+
switchedM2MToken);
295+
if (filteredAPIResource == null || filteredAPIResource.isEmpty()) {
296+
return;
297+
}
298+
String apiId = filteredAPIResource.get(0).getId();
299+
// Get API scopes.
300+
List<ScopeGetModel> apiResourceScopes = restClient.getAPIResourceScopesInSubOrganization(apiId,
301+
switchedM2MToken);
302+
AuthorizedAPICreationModel authorizedAPICreationModel = new AuthorizedAPICreationModel();
303+
authorizedAPICreationModel.setId(apiId);
304+
authorizedAPICreationModel.setPolicyIdentifier("RBAC");
305+
apiResourceScopes.forEach(scope -> {
306+
authorizedAPICreationModel.addScopesItem(scope.getName());
307+
});
308+
restClient.addAPIAuthorizationToSubOrgApplication(applicationId, authorizedAPICreationModel,
309+
switchedM2MToken);
310+
} catch (Exception e) {
311+
throw new RuntimeException("Error while authorizing system API " + apiIdentifier + " to application "
312+
+ applicationId, e);
313+
}
314+
});
315+
}
262316
}

0 commit comments

Comments
 (0)