-
Notifications
You must be signed in to change notification settings - Fork 1k
Add integration tests for consent webhooks #28119
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
Open
hwupathum
wants to merge
2
commits into
wso2:master
Choose a base branch
from
hwupathum:consent-app-mapping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
...test/java/org/wso2/identity/integration/test/restclients/ConsentManagementRestClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.wso2.identity.integration.test.restclients; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.restassured.http.ContentType; | ||
| import org.apache.commons.codec.binary.Base64; | ||
| import org.apache.http.Header; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.message.BasicHeader; | ||
| import org.wso2.carbon.automation.engine.context.beans.Tenant; | ||
| import org.wso2.carbon.utils.multitenancy.MultitenantConstants; | ||
| import org.wso2.identity.integration.test.utils.OAuth2Constant; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * REST client for consent management API operations used in webhook event tests. | ||
| * | ||
| * <p>Request bodies for element and purpose creation are loaded from the shared JSON | ||
| * resources under {@code org/wso2/identity/integration/test/rest/api/server/consent/management/v2/}. | ||
| */ | ||
| public class ConsentManagementRestClient extends RestBaseClient { | ||
|
|
||
| private static final String CONSENT_ADMIN_API_PATH = "api/identity/consent-mgt/v2.0"; | ||
| private static final String CONSENT_USER_API_PATH = "api/users/v1/me/consents"; | ||
| private static final String ELEMENTS_PATH = "/elements"; | ||
| private static final String PURPOSES_PATH = "/purposes"; | ||
|
|
||
| private static final String RESOURCE_BASE = | ||
| "org/wso2/identity/integration/test/rest/api/server/consent/management/v2/"; | ||
|
|
||
| private final String tenantDomain; | ||
| private final String adminUsername; | ||
| private final String adminPassword; | ||
| private final String adminApiBaseUrl; | ||
| private final String userConsentApiBaseUrl; | ||
|
|
||
| public ConsentManagementRestClient(String serverUrl, Tenant tenantInfo) { | ||
|
|
||
| this.tenantDomain = tenantInfo.getContextUser().getUserDomain(); | ||
| this.adminUsername = tenantInfo.getContextUser().getUserName(); | ||
| this.adminPassword = tenantInfo.getContextUser().getPassword(); | ||
| this.adminApiBaseUrl = buildAdminApiBaseUrl(serverUrl, tenantDomain); | ||
| this.userConsentApiBaseUrl = buildUserApiBaseUrl(serverUrl, tenantDomain); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a PII element using the body from {@code create-element.json}. | ||
| * | ||
| * @return The created element's UUID. | ||
| */ | ||
| public String createElement() throws Exception { | ||
|
|
||
| String body = readResource("create-element.json"); | ||
| try (CloseableHttpResponse response = getResponseOfHttpPost( | ||
| adminApiBaseUrl + ELEMENTS_PATH, body, getAdminHeaders())) { | ||
| return new ObjectMapper().readTree(readResponse(response)).get("id").asText(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a PII element by ID. | ||
| */ | ||
| public void deleteElement(String elementId) throws IOException { | ||
|
|
||
| try (CloseableHttpResponse ignored = getResponseOfHttpDelete( | ||
| adminApiBaseUrl + ELEMENTS_PATH + "/" + elementId, getAdminHeaders())) { | ||
| // expects 204 | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a consent purpose using the body from {@code create-purpose.json}, | ||
| * substituting the element ID placeholder with {@code elementId}. | ||
| * | ||
| * @param elementId UUID of the PII element to attach to the purpose. | ||
| * @return The created purpose's UUID. | ||
| */ | ||
| public String createPurpose(String elementId) throws Exception { | ||
|
|
||
| String body = readResource("create-purpose.json") | ||
| .replace("\"id\": \"1\"", "\"id\": \"" + elementId + "\""); | ||
| try (CloseableHttpResponse response = getResponseOfHttpPost( | ||
| adminApiBaseUrl + PURPOSES_PATH, body, getAdminHeaders())) { | ||
| return new ObjectMapper().readTree(readResponse(response)).get("id").asText(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a consent purpose by ID. | ||
| */ | ||
| public void deletePurpose(String purposeId) throws IOException { | ||
|
|
||
| try (CloseableHttpResponse ignored = getResponseOfHttpDelete( | ||
| adminApiBaseUrl + PURPOSES_PATH + "/" + purposeId, getAdminHeaders())) { | ||
| // expects 204 | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a consent receipt via the user consent API (POST /api/users/v1/me/consents). | ||
| * The authenticated user becomes the consent subject. | ||
| * | ||
| * @param serviceId Service identifier stored in the receipt. | ||
| * @param purposeId UUID of the consent purpose. | ||
| * @param elementId UUID of the PII element to consent to. | ||
| * @param username Username for authenticating the consent subject (not the tenant admin). | ||
| * @param password Password of the consent subject. | ||
| * @return The created consent receipt UUID. | ||
| */ | ||
| public String createConsent(String serviceId, String purposeId, String elementId, | ||
| String username, String password) throws Exception { | ||
|
|
||
| String body = String.format( | ||
| "{\"serviceId\":\"%s\",\"language\":\"en\"," | ||
| + "\"purposes\":[{\"id\":\"%s\",\"elements\":[{\"id\":\"%s\"}]}]}", | ||
| serviceId, purposeId, elementId); | ||
| try (CloseableHttpResponse response = getResponseOfHttpPost( | ||
| userConsentApiBaseUrl, body, getUserHeaders(username, password))) { | ||
| return new ObjectMapper().readTree(readResponse(response)).get("id").asText(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Revokes a consent receipt via the user consent API | ||
| * (POST /api/users/v1/me/consents/{receiptId}/revoke). | ||
| * | ||
| * @param receiptId UUID of the consent receipt to revoke. | ||
| * @param username Username of the consent subject. | ||
| * @param password Password of the consent subject. | ||
| */ | ||
| public void revokeConsent(String receiptId, String username, String password) throws IOException { | ||
|
|
||
| try (CloseableHttpResponse ignored = getResponseOfHttpPost( | ||
| userConsentApiBaseUrl + "/" + receiptId + "/revoke", "", getUserHeaders(username, password))) { | ||
| // expects 200 | ||
| } | ||
| } | ||
|
|
||
| public void closeHttpClient() throws IOException { | ||
|
|
||
| client.close(); | ||
| } | ||
|
|
||
| private String buildAdminApiBaseUrl(String serverUrl, String tenantDomain) { | ||
|
|
||
| if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { | ||
| return serverUrl + CONSENT_ADMIN_API_PATH; | ||
| } | ||
| return serverUrl + TENANT_PATH + tenantDomain + PATH_SEPARATOR + CONSENT_ADMIN_API_PATH; | ||
| } | ||
|
|
||
| private String buildUserApiBaseUrl(String serverUrl, String tenantDomain) { | ||
|
|
||
| if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { | ||
| return serverUrl + CONSENT_USER_API_PATH; | ||
| } | ||
| return serverUrl + TENANT_PATH + tenantDomain + PATH_SEPARATOR + CONSENT_USER_API_PATH; | ||
| } | ||
|
|
||
| private String readResource(String fileName) throws IOException { | ||
|
|
||
| try (InputStream stream = getClass().getClassLoader() | ||
| .getResourceAsStream(RESOURCE_BASE + fileName)) { | ||
| if (stream == null) { | ||
| throw new IOException("Resource not found: " + RESOURCE_BASE + fileName); | ||
| } | ||
| return new String(stream.readAllBytes(), StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
|
|
||
| private Header[] getAdminHeaders() { | ||
|
|
||
| return new Header[]{ | ||
| new BasicHeader(USER_AGENT_ATTRIBUTE, OAuth2Constant.USER_AGENT), | ||
| new BasicHeader(AUTHORIZATION_ATTRIBUTE, BASIC_AUTHORIZATION_ATTRIBUTE + | ||
| Base64.encodeBase64String((adminUsername + ":" + adminPassword).getBytes()).trim()), | ||
| new BasicHeader(CONTENT_TYPE_ATTRIBUTE, String.valueOf(ContentType.JSON)) | ||
| }; | ||
| } | ||
|
|
||
| private Header[] getUserHeaders(String username, String password) { | ||
|
|
||
| return new Header[]{ | ||
| new BasicHeader(USER_AGENT_ATTRIBUTE, OAuth2Constant.USER_AGENT), | ||
| new BasicHeader(AUTHORIZATION_ATTRIBUTE, BASIC_AUTHORIZATION_ATTRIBUTE + | ||
| Base64.encodeBase64String((username + ":" + password).getBytes()).trim()), | ||
| new BasicHeader(CONTENT_TYPE_ATTRIBUTE, String.valueOf(ContentType.JSON)) | ||
| }; | ||
| } | ||
|
|
||
| private String readResponse(CloseableHttpResponse response) throws IOException { | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) { | ||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| sb.append(line); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
No HTTP status validation before parsing response body.
createElement,createPurpose, andcreateConsentall call.get("id").asText()directly on the parsed JSON without checking the response status code first. If the API call fails (e.g., 400/401/500), the response body likely won't contain anidfield, andJsonNode.get("id")returnsnull, causing aNullPointerExceptionon.asText()rather than a clear failure message pointing to the actual HTTP error.🐛 Proposed fix (illustrated for createElement)
public String createElement() throws Exception { String body = readResource("create-element.json"); try (CloseableHttpResponse response = getResponseOfHttpPost( adminApiBaseUrl + ELEMENTS_PATH, body, getAdminHeaders())) { - return new ObjectMapper().readTree(readResponse(response)).get("id").asText(); + String responseBody = readResponse(response); + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 201) { + throw new IOException("Failed to create element. Status: " + statusCode + ", Body: " + responseBody); + } + return new ObjectMapper().readTree(responseBody).get("id").asText(); } }Since this pattern repeats in
createPurposeandcreateConsent, applying the same guard there would improve failure diagnostics.Also applies to: 100-108, 132-143
🤖 Prompt for AI Agents