Skip to content

Commit

Permalink
Create RestMatchers
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 30, 2024
1 parent cb06896 commit 40c89ec
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opensearch.test.framework.cluster.LocalCluster;
import org.opensearch.test.framework.cluster.TestRestClient;
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse;
import org.opensearch.test.framework.matcher.RestMatchers;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -108,10 +109,10 @@ public void testPluginShouldNotBeAbleToIndexDocumentIntoSystemIndexRegisteredByO
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
HttpResponse response = client.put("try-create-and-index/" + SYSTEM_INDEX_2);

assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
assertThat(
response.getBody(),
containsString(
response,
RestMatchers.isForbidden(
"/error/root_cause/0/reason",
"no permissions for [] and User [name=plugin:org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1"
)
);
Expand All @@ -123,8 +124,7 @@ public void testPluginShouldBeAbleToCreateSystemIndexButUserShouldNotBeAbleToInd
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
HttpResponse response = client.put("try-create-and-index/" + SYSTEM_INDEX_1 + "?runAs=user");

assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
assertThat(response.getBody(), containsString("no permissions for [] and User [name=admin"));
assertThat(response, RestMatchers.isForbidden("/error/root_cause/0/reason", "no permissions for [] and User [name=admin"));
}
}

Expand All @@ -133,10 +133,10 @@ public void testPluginShouldNotBeAbleToRunClusterActions() {
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
HttpResponse response = client.get("try-cluster-health/plugin");

assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
assertThat(
response.getBody(),
containsString(
response,
RestMatchers.isForbidden(
"/error/root_cause/0/reason",
"no permissions for [cluster:monitor/health] and User [name=plugin:org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1"
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.opensearch.test.framework.matcher;

import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;

import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse;

public class RestMatchers {

private RestMatchers() {}

public static DiagnosingMatcher<HttpResponse> isForbidden(String jsonPointer, String patternString) {
return new DiagnosingMatcher<HttpResponse>() {

@Override
public void describeTo(Description description) {
description.appendText("Response has status 403 Forbidden with a JSON response that has the value ")
.appendValue(patternString)
.appendText(" at ")
.appendValue(jsonPointer);
}

@Override
protected boolean matches(Object item, Description mismatchDescription) {
if (!(item instanceof HttpResponse)) {
mismatchDescription.appendValue(item).appendText(" is not a HttpResponse");
return false;
}

HttpResponse response = (HttpResponse) item;

if (response.getStatusCode() != 403) {
mismatchDescription.appendText("Status is not 403 Forbidden: ").appendText("\n").appendValue(item);
return false;
}

try {
String value = response.getTextFromJsonBody(jsonPointer);

if (value == null) {
mismatchDescription.appendText("Could not find value at " + jsonPointer).appendText("\n").appendValue(item);
return false;
}

if (value.contains(patternString)) {
return true;
} else {
mismatchDescription.appendText("Value at " + jsonPointer + " does not match pattern: " + patternString + "\n")
.appendValue(item);
return false;
}
} catch (Exception e) {
mismatchDescription.appendText("Parsing request body failed with " + e).appendText("\n").appendValue(item);
return false;
}
}
};
}
}

0 comments on commit 40c89ec

Please sign in to comment.