Skip to content

Commit

Permalink
Define a variable for expected message in checkNullElementsInArray() …
Browse files Browse the repository at this point in the history
…unit test
  • Loading branch information
afazel committed Aug 9, 2021
1 parent 53aaf6e commit e0fc3f1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;
import com.google.common.collect.ImmutableList;

import static org.opensearch.security.dlic.rest.api.RolesApiTest.EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG;

@RunWith(Parameterized.class)
public class ActionGroupsApiTest extends AbstractRestApiUnitTest {

Expand Down Expand Up @@ -385,6 +387,6 @@ public void checkNullElementsInArray() throws Exception{
String body = FileHelper.loadFile("restapi/actiongroup_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/CRUD_UT", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.opensearch.security.dlic.rest.api.RolesApiTest.EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG;

@RunWith(Parameterized.class)
public class NodesDnApiTest extends AbstractRestApiUnitTest {
Expand Down Expand Up @@ -122,7 +123,7 @@ private void checkNullElementsInArray(final Header headers) throws Exception{
String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/nodesdn/cluster1", body, headers);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@RunWith(Parameterized.class)
public class RolesApiTest extends AbstractRestApiUnitTest {

public final static String EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG = "`null` is not allowed as json array element";
private final String ENDPOINT;

public RolesApiTest(String endpoint){
Expand Down Expand Up @@ -532,32 +533,32 @@ public void checkNullElementsInArray() throws Exception{
String body = FileHelper.loadFile("restapi/roles_null_array_element_cluster_permissions.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/roles_null_array_element_index_permissions.json");
response = rh.executePutRequest(ENDPOINT+ "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/roles_null_array_element_tenant_permissions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/roles_null_array_element_index_patterns.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/roles_null_array_element_masked_fields.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/roles_null_array_element_allowed_actions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;
import com.google.common.collect.ImmutableList;

import static org.opensearch.security.dlic.rest.api.RolesApiTest.EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG;

@RunWith(Parameterized.class)
public class RolesMappingApiTest extends AbstractRestApiUnitTest {

Expand Down Expand Up @@ -422,18 +424,18 @@ public void checkNullElementsInArray() throws Exception{
HttpResponse response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_backend_roles.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_hosts.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.google.common.collect.ImmutableList;
import java.util.List;

import static org.opensearch.security.dlic.rest.api.RolesApiTest.EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG;

@RunWith(Parameterized.class)
public class UserApiTest extends AbstractRestApiUnitTest {

Expand Down Expand Up @@ -647,7 +649,7 @@ public void checkNullElementsInArray() throws Exception{
String body = FileHelper.loadFile("restapi/users_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/internalusers/picard", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertTrue(response.getBody().contains("`null` is not allowed as json array element"));
Assert.assertTrue(response.getBody().contains(EXPECTED_NULL_ELEMENT_IN_ARRAY_MSG));
}

}

0 comments on commit e0fc3f1

Please sign in to comment.