Skip to content

Commit

Permalink
Update checkNullElementsInArray() unit test to check both error mes…
Browse files Browse the repository at this point in the history
…sage and error code instead of only checking the error code (opensearch-project#1370)

(cherry picked from commit 0a2916b)
  • Loading branch information
afazel committed Aug 12, 2021
1 parent 2f6f501 commit f3eea76
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
import com.amazon.opendistroforelasticsearch.security.auditlog.impl.AuditCategory;
import com.amazon.opendistroforelasticsearch.security.auditlog.impl.AuditMessage;
import com.amazon.opendistroforelasticsearch.security.auditlog.integration.TestAuditlogImpl;
import com.amazon.opendistroforelasticsearch.security.dlic.rest.validation.AbstractConfigurationValidator;
import com.amazon.opendistroforelasticsearch.security.support.ConfigConstants;
import com.amazon.opendistroforelasticsearch.security.test.helper.file.FileHelper;
import com.amazon.opendistroforelasticsearch.security.test.helper.rest.RestHelper.HttpResponse;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import org.apache.http.Header;
import org.apache.http.HttpStatus;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -98,6 +101,15 @@ private void testCrudScenarios(final int expectedStatus, final Header... headers
assertThat(response.getBody(), response.getStatusCode(), equalTo(expectedStatus));
}

private void checkNullElementsInArray(final Header headers) throws Exception{

String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest("_opendistro/_security/api/nodesdn/cluster1", body, headers);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}

@Test
public void testNodesDnApiWithDynamicConfigDisabled() throws Exception {
setup();
Expand Down Expand Up @@ -139,6 +151,12 @@ public void testNodesDnApi() throws Exception {
testCrudScenarios(HttpStatus.SC_OK, nonAdminCredsHeader);
}

{
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;
checkNullElementsInArray(nonAdminCredsHeader);
}

{
// any creds, admin certificate, disallowed key - FORBIDDEN
rh.keystore = "restapi/kirk-keystore.jks";
Expand Down Expand Up @@ -195,15 +213,4 @@ public void testNodesDnApiAuditComplianceLogging() throws Exception {
assertThat(actualCategoryCounts, equalTo(expectedCategoryCounts));
}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest("_opendistro/_security/api/nodesdn", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}

}

0 comments on commit f3eea76

Please sign in to comment.