Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ public class PrivilegesEvaluatorTest {
new Role("negated_regex_role").indexPermissions("read").on("/^[a-z].*/").clusterPermissions("cluster_composite_ops")
);

protected final static TestSecurityConfig.User SEARCH_TEMPLATE = new TestSecurityConfig.User("search_template_user").roles(
new Role("search_template_role").indexPermissions("read").on("services")
);

@ClassRule
public static LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS)
.authc(AUTHC_HTTPBASIC_INTERNAL)
.users(NEGATIVE_LOOKAHEAD, NEGATED_REGEX)
.users(NEGATIVE_LOOKAHEAD, NEGATED_REGEX, SEARCH_TEMPLATE)
.build();

@Test
Expand All @@ -68,4 +72,43 @@ public void testRegexPattern() throws Exception {
}

}

@Test
public void testSearchTemplateRequestSuccess() {
try (TestRestClient client = cluster.getRestClient(SEARCH_TEMPLATE)) {
assertThat(
client.getWithJsonBody(
"services/_search/template",
"{\"source\":{\"query\":{\"match\":{\"service\":\"{{service_name}}\"}}},\"params\":{\"service_name\":\"Oracle\"}}"
).getStatusCode(),
equalTo(HttpStatus.SC_OK)
);
}
}

@Test
public void testSearchTemplateRequestUnauthorizedIndex() {
try (TestRestClient client = cluster.getRestClient(SEARCH_TEMPLATE)) {
assertThat(
client.getWithJsonBody(
"movies/_search/template",
"{\"source\":{\"query\":{\"match\":{\"service\":\"{{service_name}}\"}}},\"params\":{\"service_name\":\"Oracle\"}}"
).getStatusCode(),
equalTo(HttpStatus.SC_FORBIDDEN)
);
}
}

@Test
public void testSearchTemplateRequestUnauthorizedAllIndices() {
try (TestRestClient client = cluster.getRestClient(SEARCH_TEMPLATE)) {
assertThat(
client.getWithJsonBody(
"_search/template",
"{\"source\":{\"query\":{\"match\":{\"service\":\"{{service_name}}\"}}},\"params\":{\"service_name\":\"Oracle\"}}"
).getStatusCode(),
equalTo(HttpStatus.SC_FORBIDDEN)
);
}
}
}