|
| 1 | +package com.mastercard.developer.interceptor; |
| 2 | + |
| 3 | +import com.mastercard.developer.encryption.EncryptionException; |
| 4 | +import com.mastercard.developer.encryption.FieldLevelEncryptionConfig; |
| 5 | +import com.mastercard.developer.interceptors.FeignFieldLevelEncryptionDecoder; |
| 6 | +import feign.Response; |
| 7 | +import feign.Util; |
| 8 | +import feign.codec.DecodeException; |
| 9 | +import feign.codec.Decoder; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Rule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.rules.ExpectedException; |
| 14 | +import org.mockito.ArgumentCaptor; |
| 15 | + |
| 16 | +import java.lang.reflect.Type; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | + |
| 22 | +import static com.mastercard.developer.test.TestUtils.assertPayloadEquals; |
| 23 | +import static com.mastercard.developer.test.TestUtils.getTestFieldLevelEncryptionConfigBuilder; |
| 24 | +import static org.hamcrest.core.Is.isA; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | + |
| 28 | +public class FeignFieldLevelEncryptionDecoderTest { |
| 29 | + |
| 30 | + @Rule |
| 31 | + public ExpectedException expectedException = ExpectedException.none(); |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testDecode_ShouldDecryptResponsePayloadAndUpdateContentLengthHeader() throws Exception { |
| 35 | + |
| 36 | + // GIVEN |
| 37 | + String encryptedPayload = "{" + |
| 38 | + " \"encryptedData\": {" + |
| 39 | + " \"iv\": \"a32059c51607d0d02e823faecda5fb15\"," + |
| 40 | + " \"encryptedKey\": \"a31cfe7a7981b72428c013270619554c1d645c04b9d51c7eaf996f55749ef62fd7c7f8d334f95913be41ae38c46d192670fd1acb84ebb85a00cd997f1a9a3f782229c7bf5f0fdf49fe404452d7ed4fd41fbb95b787d25893fbf3d2c75673cecc8799bbe3dd7eb4fe6d3f744b377572cdf8aba1617194e10475b6cd6a8dd4fb8264f8f51534d8f7ac7c10b4ce9c44d15066724b03a0ab0edd512f9e6521fdb5841cd6964e457d6b4a0e45ba4aac4e77d6bbe383d6147e751fa88bc26278bb9690f9ee84b17123b887be2dcef0873f4f9f2c895d90e23456fafb01b99885e31f01a3188f0ad47edf22999cc1d0ddaf49e1407375117b5d66f1f185f2b57078d255\"," + |
| 41 | + " \"encryptedValue\": \"21d754bdb4567d35d58720c9f8364075\"," + |
| 42 | + " \"oaepHashingAlgorithm\": \"SHA256\"" + |
| 43 | + " }" + |
| 44 | + "}"; |
| 45 | + FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder() |
| 46 | + .withDecryptionPath("$.encryptedData", "$.data") |
| 47 | + .build(); |
| 48 | + |
| 49 | + Type type = mock(Type.class); |
| 50 | + HashMap<String, Collection<String>> headers = new HashMap<>(); |
| 51 | + headers.put("content-length", Collections.singleton("100")); |
| 52 | + Response response = Response.builder() |
| 53 | + .status(200) |
| 54 | + .headers(headers) |
| 55 | + .body(encryptedPayload, StandardCharsets.UTF_8) |
| 56 | + .build(); |
| 57 | + Decoder delegate = mock(Decoder.class); |
| 58 | + |
| 59 | + // WHEN |
| 60 | + FeignFieldLevelEncryptionDecoder instanceUnderTest = new FeignFieldLevelEncryptionDecoder(config, delegate); |
| 61 | + instanceUnderTest.decode(response, type); |
| 62 | + |
| 63 | + // THEN |
| 64 | + ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class); |
| 65 | + verify(delegate).decode(responseCaptor.capture(), any(Type.class)); |
| 66 | + Response responseValue = responseCaptor.getValue(); |
| 67 | + String payload = Util.toString(responseValue.body().asReader()); |
| 68 | + assertPayloadEquals("{\"data\":\"string\"}", payload); |
| 69 | + Assert.assertEquals(String.valueOf(payload.length()), responseValue.headers().get("Content-Length").toArray()[0]); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testDecode_ShouldDoNothing_WhenNoPayload() throws Exception { |
| 74 | + |
| 75 | + // GIVEN |
| 76 | + FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder().build(); |
| 77 | + Type type = mock(Type.class); |
| 78 | + Response response = mock(Response.class); |
| 79 | + Decoder delegate = mock(Decoder.class); |
| 80 | + when(response.body()).thenReturn(null); |
| 81 | + |
| 82 | + // WHEN |
| 83 | + FeignFieldLevelEncryptionDecoder instanceUnderTest = new FeignFieldLevelEncryptionDecoder(config, delegate); |
| 84 | + instanceUnderTest.decode(response, type); |
| 85 | + |
| 86 | + // THEN |
| 87 | + verify(delegate).decode(any(Response.class), any(Type.class)); |
| 88 | + verify(response).body(); |
| 89 | + verifyNoMoreInteractions(response); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testDecode_ShouldDoNothing_WhenEmptyPayload() throws Exception { |
| 94 | + |
| 95 | + // GIVEN |
| 96 | + FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder().build(); |
| 97 | + Type type = mock(Type.class); |
| 98 | + Response response = mock(Response.class); |
| 99 | + when(response.body()).thenReturn(buildResponseBody("")); |
| 100 | + Decoder delegate = mock(Decoder.class); |
| 101 | + |
| 102 | + // WHEN |
| 103 | + FeignFieldLevelEncryptionDecoder instanceUnderTest = new FeignFieldLevelEncryptionDecoder(config, delegate); |
| 104 | + instanceUnderTest.decode(response, type); |
| 105 | + |
| 106 | + // THEN |
| 107 | + verify(delegate).decode(any(Response.class), any(Type.class)); |
| 108 | + verify(response).body(); |
| 109 | + verifyNoMoreInteractions(response); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testDecode_ShouldThrowDecodeException_WhenDecryptionFails() throws Exception { |
| 114 | + |
| 115 | + // GIVEN |
| 116 | + String encryptedPayload = "{" + |
| 117 | + " \"encryptedData\": {" + |
| 118 | + " \"iv\": \"a2c494ca28dec4f3d6ce7d68b1044cfe\"," + |
| 119 | + " \"encryptedKey\": \"NOT A VALID KEY!\"," + |
| 120 | + " \"encryptedValue\": \"0672589113046bf692265b6ea6088184\"" + |
| 121 | + " }" + |
| 122 | + "}"; |
| 123 | + FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder() |
| 124 | + .withDecryptionPath("$.encryptedData", "$.data") |
| 125 | + .build(); |
| 126 | + Type type = mock(Type.class); |
| 127 | + Response response = mock(Response.class); |
| 128 | + when(response.body()).thenReturn(buildResponseBody(encryptedPayload)); |
| 129 | + Decoder delegate = mock(Decoder.class); |
| 130 | + |
| 131 | + // THEN |
| 132 | + expectedException.expect(DecodeException.class); |
| 133 | + expectedException.expectMessage("Failed to decrypt response!"); |
| 134 | + expectedException.expectCause(isA(EncryptionException.class)); |
| 135 | + |
| 136 | + // WHEN |
| 137 | + FeignFieldLevelEncryptionDecoder instanceUnderTest = new FeignFieldLevelEncryptionDecoder(config, delegate); |
| 138 | + instanceUnderTest.decode(response, type); |
| 139 | + } |
| 140 | + |
| 141 | + private static Response.Body buildResponseBody(String payload) { |
| 142 | + Response response = Response.builder() |
| 143 | + .status(200) |
| 144 | + .headers(new HashMap<String, Collection<String>>()) |
| 145 | + .body(payload, StandardCharsets.UTF_8) |
| 146 | + .build(); |
| 147 | + return response.body(); |
| 148 | + } |
| 149 | +} |
0 commit comments