Skip to content

Commit

Permalink
Ensure POST invalid data returns 400
Browse files Browse the repository at this point in the history
- added integration test for PostRegisterValidatorTest, showing invalid data coming in, and a resulting 400.
- had to add a default exception handler for `JsonProcessingException`.

fixes Consensys#5571

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone committed May 25, 2022
1 parent e45b717 commit dc8ac9d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
package tech.pegasys.teku.beaconrestapi.v1.validator;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
import static tech.pegasys.teku.spec.schemas.ApiSchemas.SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA;

import java.io.IOException;
import okhttp3.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest;
import tech.pegasys.teku.beaconrestapi.handlers.v1.validator.PostRegisterValidator;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand All @@ -44,15 +49,33 @@ void setup() {
void shouldReturnOk() throws IOException {
final SszList<SignedValidatorRegistration> request =
dataStructureUtil.randomValidatorRegistrations(10);

when(validatorApiChannel.registerValidators(request)).thenReturn(SafeFuture.COMPLETE);

Response response =
try (Response response =
post(
PostRegisterValidator.ROUTE,
JsonUtil.serialize(
request, SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA.getJsonTypeDefinition()));
request, SIGNED_VALIDATOR_REGISTRATIONS_SCHEMA.getJsonTypeDefinition()))) {

assertThat(response.code()).isEqualTo(SC_OK);
}
}

assertThat(response.code()).isEqualTo(SC_OK);
@ParameterizedTest
@ValueSource(strings = {"[{}]", "{}", "invalid"})
void shouldReturnBadRequest(final String body) throws IOException {
when(validatorApiChannel.registerValidators(any())).thenReturn(SafeFuture.COMPLETE);
try (Response response = post(PostRegisterValidator.ROUTE, body)) {
assertThat(response.code()).isEqualTo(SC_BAD_REQUEST);
}
verifyNoInteractions(validatorApiChannel);
}

@Test
void shouldHandleEmptyRequest() throws IOException {
when(validatorApiChannel.registerValidators(any())).thenReturn(SafeFuture.COMPLETE);
try (Response response = post(PostRegisterValidator.ROUTE, "[]")) {
assertThat(response.code()).isEqualTo(SC_OK);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_SERVICE_UNAVAILABLE;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_UNSUPPORTED_MEDIA_TYPE;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Throwables;
import io.javalin.Javalin;
import io.javalin.http.Context;
Expand Down Expand Up @@ -226,6 +227,7 @@ private void addExceptionHandlers() {
app.exception(ServiceUnavailableException.class, this::serviceUnavailable);
app.exception(ContentTypeNotSupportedException.class, this::unsupportedContentType);
app.exception(BadRequestException.class, this::badRequest);
app.exception(JsonProcessingException.class, this::badRequest);
app.exception(IllegalArgumentException.class, this::badRequest);
// Add catch-all handler
app.exception(
Expand Down

0 comments on commit dc8ac9d

Please sign in to comment.