Skip to content

Commit

Permalink
Moar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Aug 7, 2023
1 parent 99bfcaa commit 5226afa
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -1466,6 +1467,37 @@ public void testGetActiveOnlyApiKeys() throws Exception {

assertResponseContainsApiKeyIds(response, apiKey0.id, apiKey1.id, apiKey2.id);
}

// Active-only throws if used with id for existing but non-active API keys
{
final var request = new Request(HttpGet.METHOD_NAME, "/_security/api_key/");
request.addParameter("id", randomFrom(apiKey0, apiKey2).id);
request.addParameter("active_only", "true");

final ResponseException e = expectThrows(ResponseException.class, () -> adminClient().performRequest(request));

assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(404));
}
{
final var request = new Request(HttpGet.METHOD_NAME, "/_security/api_key/");
request.addParameter("id", apiKey1.id);
request.addParameter("active_only", "true");

final GetApiKeyResponse response = GetApiKeyResponse.fromXContent(getParser(adminClient().performRequest(request)));

assertResponseContainsApiKeyIds(response, apiKey1.id);
}

getSecurityClient().invalidateApiKeys(apiKey1.id);
// Active-only returns empty when no keys found
{
final var request = new Request(HttpGet.METHOD_NAME, "/_security/api_key/");
request.addParameter("active_only", "true");

final GetApiKeyResponse response = GetApiKeyResponse.fromXContent(getParser(adminClient().performRequest(request)));

assertThat(response.getApiKeyInfos(), emptyArray());
}
}

private static void assertResponseContainsApiKeyIds(GetApiKeyResponse response, String... ids) {
Expand Down Expand Up @@ -1653,6 +1685,10 @@ private ObjectPath fetchCrossClusterApiKeyById(String apiKeyId) throws IOExcepti
fetchRequest = new Request("GET", "/_security/api_key");
fetchRequest.addParameter("id", apiKeyId);
fetchRequest.addParameter("with_limited_by", String.valueOf(randomBoolean()));
if (randomBoolean()) {
fetchRequest.addParameter("active_only", "false");
}

} else {
fetchRequest = new Request("GET", "/_security/_query/api_key");
fetchRequest.addParameter("with_limited_by", String.valueOf(randomBoolean()));
Expand Down

0 comments on commit 5226afa

Please sign in to comment.