Merge pull request elastic/elasticsearch#2616 from javanna/test/nuke_response_body_assertion

Consolidate docs snippets testing in our REST test infra

Original commit: elastic/x-pack-elasticsearch@983ab207bf
This commit is contained in:
Luca Cavanna 2016-07-01 12:09:05 +02:00 committed by GitHub
commit de124baf8d
1 changed files with 11 additions and 7 deletions

View File

@ -11,12 +11,14 @@ import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.security.authc.support.SecuredString; import org.elasticsearch.xpack.security.authc.support.SecuredString;
import org.elasticsearch.xpack.security.authz.InternalAuthorizationService; import org.elasticsearch.xpack.security.authz.InternalAuthorizationService;
import org.elasticsearch.xpack.security.user.AnonymousUser; import org.elasticsearch.xpack.security.user.AnonymousUser;
import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecurityIntegTestCase;
import org.elasticsearch.test.SecuritySettingsSource; import org.elasticsearch.test.SecuritySettingsSource;
import org.elasticsearch.test.rest.json.JsonPath; import org.elasticsearch.test.rest.ObjectPath;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import java.util.Collections; import java.util.Collections;
@ -56,10 +58,11 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase {
new BasicHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.DEFAULT_USER_NAME, new BasicHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.DEFAULT_USER_NAME,
new SecuredString(SecuritySettingsSource.DEFAULT_PASSWORD.toCharArray()))))) { new SecuredString(SecuritySettingsSource.DEFAULT_PASSWORD.toCharArray()))))) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity())); ObjectPath objectPath = ObjectPath.createFromXContent(XContentFactory.xContent(XContentType.JSON),
assertThat(jsonPath.evaluate("username").toString(), equalTo(SecuritySettingsSource.DEFAULT_USER_NAME)); EntityUtils.toString(response.getEntity()));
assertThat(objectPath.evaluate("username").toString(), equalTo(SecuritySettingsSource.DEFAULT_USER_NAME));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> roles = (List<String>) jsonPath.evaluate("roles"); List<String> roles = (List<String>) objectPath.evaluate("roles");
assertThat(roles.size(), is(1)); assertThat(roles.size(), is(1));
assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE)); assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE));
} }
@ -70,10 +73,11 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase {
Collections.emptyMap(), null)) { Collections.emptyMap(), null)) {
if (anonymousEnabled) { if (anonymousEnabled) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity())); ObjectPath objectPath = ObjectPath.createFromXContent(XContentFactory.xContent(XContentType.JSON),
assertThat(jsonPath.evaluate("username").toString(), equalTo("anon")); EntityUtils.toString(response.getEntity()));
assertThat(objectPath.evaluate("username").toString(), equalTo("anon"));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> roles = (List<String>) jsonPath.evaluate("roles"); List<String> roles = (List<String>) objectPath.evaluate("roles");
assertThat(roles.size(), is(2)); assertThat(roles.size(), is(2));
assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE, "foo")); assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE, "foo"));
} else { } else {