|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2024 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.validator.client.restapi.apis; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.ArgumentMatchers.eq; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST; |
| 22 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_FORBIDDEN; |
| 23 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_INTERNAL_SERVER_ERROR; |
| 24 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_NOT_FOUND; |
| 25 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK; |
| 26 | +import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_UNAUTHORIZED; |
| 27 | +import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.getResponseStringFromMetadata; |
| 28 | +import static tech.pegasys.teku.infrastructure.restapi.MetadataTestUtil.verifyMetadataErrorResponse; |
| 29 | + |
| 30 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.Optional; |
| 33 | +import org.apache.tuweni.bytes.Bytes32; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import tech.pegasys.teku.bls.BLSPublicKey; |
| 36 | +import tech.pegasys.teku.infrastructure.http.HttpErrorResponse; |
| 37 | +import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest; |
| 38 | +import tech.pegasys.teku.spec.TestSpecFactory; |
| 39 | +import tech.pegasys.teku.spec.signatures.Signer; |
| 40 | +import tech.pegasys.teku.spec.util.DataStructureUtil; |
| 41 | +import tech.pegasys.teku.validator.api.Bytes32Parser; |
| 42 | +import tech.pegasys.teku.validator.client.OwnedKeyManager; |
| 43 | +import tech.pegasys.teku.validator.client.Validator; |
| 44 | + |
| 45 | +class GetGraffitiTest { |
| 46 | + private final OwnedKeyManager keyManager = mock(OwnedKeyManager.class); |
| 47 | + private final GetGraffiti handler = new GetGraffiti(keyManager); |
| 48 | + private StubRestApiRequest request; |
| 49 | + |
| 50 | + private final DataStructureUtil dataStructureUtil = |
| 51 | + new DataStructureUtil(TestSpecFactory.createDefault()); |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldGetGraffiti() throws JsonProcessingException { |
| 55 | + final BLSPublicKey publicKey = dataStructureUtil.randomPublicKey(); |
| 56 | + final String stringGraffiti = "Test graffiti"; |
| 57 | + final Bytes32 graffiti = Bytes32Parser.toBytes32(stringGraffiti); |
| 58 | + |
| 59 | + request = |
| 60 | + StubRestApiRequest.builder() |
| 61 | + .metadata(handler.getMetadata()) |
| 62 | + .pathParameter("pubkey", publicKey.toHexString()) |
| 63 | + .build(); |
| 64 | + |
| 65 | + final Validator validator = |
| 66 | + new Validator(publicKey, mock(Signer.class), () -> Optional.of(graffiti)); |
| 67 | + when(keyManager.getValidatorByPublicKey(eq(publicKey))).thenReturn(Optional.of(validator)); |
| 68 | + |
| 69 | + handler.handleRequest(request); |
| 70 | + |
| 71 | + GetGraffiti.GraffitiResponse expectedResponse = |
| 72 | + new GetGraffiti.GraffitiResponse(publicKey, stringGraffiti); |
| 73 | + assertThat(request.getResponseCode()).isEqualTo(SC_OK); |
| 74 | + assertThat(request.getResponseBody()).isEqualTo(expectedResponse); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void shouldGetEmptyGraffiti() throws JsonProcessingException { |
| 79 | + final BLSPublicKey publicKey = dataStructureUtil.randomPublicKey(); |
| 80 | + request = |
| 81 | + StubRestApiRequest.builder() |
| 82 | + .metadata(handler.getMetadata()) |
| 83 | + .pathParameter("pubkey", publicKey.toHexString()) |
| 84 | + .build(); |
| 85 | + |
| 86 | + final Validator validator = new Validator(publicKey, mock(Signer.class), Optional::empty); |
| 87 | + when(keyManager.getValidatorByPublicKey(eq(publicKey))).thenReturn(Optional.of(validator)); |
| 88 | + |
| 89 | + handler.handleRequest(request); |
| 90 | + |
| 91 | + GetGraffiti.GraffitiResponse expectedResponse = new GetGraffiti.GraffitiResponse(publicKey, ""); |
| 92 | + assertThat(request.getResponseCode()).isEqualTo(SC_OK); |
| 93 | + assertThat(request.getResponseBody()).isEqualTo(expectedResponse); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void shouldHandleValidatorNotFound() throws IOException { |
| 98 | + request = |
| 99 | + StubRestApiRequest.builder() |
| 100 | + .metadata(handler.getMetadata()) |
| 101 | + .pathParameter("pubkey", dataStructureUtil.randomPublicKey().toHexString()) |
| 102 | + .build(); |
| 103 | + |
| 104 | + when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.empty()); |
| 105 | + |
| 106 | + handler.handleRequest(request); |
| 107 | + assertThat(request.getResponseCode()).isEqualTo(SC_NOT_FOUND); |
| 108 | + assertThat(request.getResponseBody()) |
| 109 | + .isEqualTo(new HttpErrorResponse(SC_NOT_FOUND, "Validator not found")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void metadata_shouldHandle200() throws JsonProcessingException { |
| 114 | + GetGraffiti.GraffitiResponse response = |
| 115 | + new GetGraffiti.GraffitiResponse(dataStructureUtil.randomPublicKey(), "Test graffiti"); |
| 116 | + final String responseData = getResponseStringFromMetadata(handler, SC_OK, response); |
| 117 | + assertThat(responseData) |
| 118 | + .isEqualTo( |
| 119 | + "{\"data\":{\"pubkey\":" |
| 120 | + + "\"0xa4654ac3105a58c7634031b5718c4880c87300f72091cfbc69fe490b71d93a671e00e80a388e1ceb8ea1de112003e976\"," |
| 121 | + + "\"graffiti\":\"Test graffiti\"}}"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void metadata_shouldHandle400() throws JsonProcessingException { |
| 126 | + verifyMetadataErrorResponse(handler, SC_BAD_REQUEST); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void metadata_shouldHandle401() throws JsonProcessingException { |
| 131 | + verifyMetadataErrorResponse(handler, SC_UNAUTHORIZED); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void metadata_shouldHandle403() throws JsonProcessingException { |
| 136 | + verifyMetadataErrorResponse(handler, SC_FORBIDDEN); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void metadata_shouldHandle500() throws JsonProcessingException { |
| 141 | + verifyMetadataErrorResponse(handler, SC_INTERNAL_SERVER_ERROR); |
| 142 | + } |
| 143 | +} |
0 commit comments