|
| 1 | +package com.sap.cloud.sdk.cloudplatform.connectivity; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +import javax.annotation.Nonnull; |
| 12 | + |
| 13 | +import org.junit.jupiter.params.ParameterizedTest; |
| 14 | +import org.junit.jupiter.params.provider.MethodSource; |
| 15 | + |
| 16 | +import com.google.common.net.HttpHeaders; |
| 17 | +import com.sap.cloud.sdk.cloudplatform.requestheader.RequestHeaderAccessor; |
| 18 | + |
| 19 | +class ForwardAuthTokenTest |
| 20 | +{ |
| 21 | + static Collection<TestCase> createTestCases() |
| 22 | + { |
| 23 | + return List |
| 24 | + .of( |
| 25 | + // forwardAuthToken = true && authType = NoAuthentication |
| 26 | + TestCaseBuilder |
| 27 | + .forProperty(DestinationProperty.FORWARD_AUTH_TOKEN.getKeyName(), "true") |
| 28 | + .and(DestinationProperty.AUTH_TYPE.getKeyName(), AuthenticationType.NO_AUTHENTICATION) |
| 29 | + .expectTokenForwarding(), |
| 30 | + // forwardAuthToken = true && unset authType |
| 31 | + TestCaseBuilder |
| 32 | + .forProperty(DestinationProperty.FORWARD_AUTH_TOKEN.getKeyName(), "true") |
| 33 | + .expectTokenForwarding(), |
| 34 | + // forwardAuthToken = false && authType = NoAuthentication |
| 35 | + TestCaseBuilder |
| 36 | + .forProperty(DestinationProperty.FORWARD_AUTH_TOKEN.getKeyName(), "false") |
| 37 | + .and(DestinationProperty.AUTH_TYPE.getKeyName(), AuthenticationType.NO_AUTHENTICATION) |
| 38 | + .expectNoTokenForwarding(), |
| 39 | + // forwardAuthToken = false && unset authType |
| 40 | + TestCaseBuilder |
| 41 | + .forProperty(DestinationProperty.FORWARD_AUTH_TOKEN.getKeyName(), "false") |
| 42 | + .expectNoTokenForwarding(), |
| 43 | + // unset forwardAuthToken && authType = NoAuthentication |
| 44 | + TestCaseBuilder |
| 45 | + .forProperty(DestinationProperty.AUTH_TYPE.getKeyName(), AuthenticationType.NO_AUTHENTICATION) |
| 46 | + .expectNoTokenForwarding(), |
| 47 | + // unset forwardAuthToken && unset authType |
| 48 | + TestCaseBuilder.forNoProperties().expectNoTokenForwarding()); |
| 49 | + } |
| 50 | + |
| 51 | + private enum AssertionType |
| 52 | + { |
| 53 | + TOKEN_IS_FORWARDED(ForwardAuthTokenTest::assertThatTokenIsForwarded), |
| 54 | + TOKEN_IS_NOT_FORWARDED(ForwardAuthTokenTest::assertThatTokenIsNotForwarded); |
| 55 | + |
| 56 | + private final Consumer<HttpDestination> assertion; |
| 57 | + |
| 58 | + AssertionType( final Consumer<HttpDestination> assertion ) |
| 59 | + { |
| 60 | + this.assertion = assertion; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private record TestCase( Map<String, Object> properties, AssertionType forwardingAssertion ) |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | + private static class TestCaseBuilder |
| 69 | + { |
| 70 | + |
| 71 | + private final Map<String, Object> properties = new HashMap<>(); |
| 72 | + |
| 73 | + TestCaseBuilder( String key, Object value ) |
| 74 | + { |
| 75 | + properties.put(key, value); |
| 76 | + } |
| 77 | + |
| 78 | + TestCaseBuilder() |
| 79 | + { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + static TestCaseBuilder forProperty( String key, Object value ) |
| 84 | + { |
| 85 | + return new TestCaseBuilder(key, value); |
| 86 | + } |
| 87 | + |
| 88 | + static TestCaseBuilder forNoProperties() |
| 89 | + { |
| 90 | + return new TestCaseBuilder(); |
| 91 | + } |
| 92 | + |
| 93 | + TestCaseBuilder and( final String key, final Object value ) |
| 94 | + { |
| 95 | + properties.put(key, value); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + TestCase expectTokenForwarding() |
| 100 | + { |
| 101 | + return new TestCase(properties, AssertionType.TOKEN_IS_FORWARDED); |
| 102 | + } |
| 103 | + |
| 104 | + TestCase expectNoTokenForwarding() |
| 105 | + { |
| 106 | + return new TestCase(properties, AssertionType.TOKEN_IS_NOT_FORWARDED); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @ParameterizedTest |
| 111 | + @MethodSource( "createTestCases" ) |
| 112 | + void localDestinationShouldFulfillTestCase( @Nonnull final TestCase testCase ) |
| 113 | + { |
| 114 | + final HttpDestination destination = buildLocalDestination(testCase.properties); |
| 115 | + testCase.forwardingAssertion.assertion.accept(destination); |
| 116 | + } |
| 117 | + |
| 118 | + private HttpDestination buildLocalDestination( Map<String, Object> properties ) |
| 119 | + { |
| 120 | + return DefaultDestination.fromMap(properties).uri("sap.com").build().asHttp(); |
| 121 | + } |
| 122 | + |
| 123 | + private static void assertThatTokenIsNotForwarded( final HttpDestination destination ) |
| 124 | + { |
| 125 | + final Collection<Header> headers = |
| 126 | + RequestHeaderAccessor |
| 127 | + .executeWithHeaderContainer(Map.of(HttpHeaders.AUTHORIZATION, "token"), () -> destination.getHeaders()); |
| 128 | + |
| 129 | + assertThat(headers).isEmpty(); |
| 130 | + } |
| 131 | + |
| 132 | + private static void assertThatTokenIsForwarded( final HttpDestination destination ) |
| 133 | + { |
| 134 | + final Collection<Header> headers = |
| 135 | + RequestHeaderAccessor |
| 136 | + .executeWithHeaderContainer(Map.of(HttpHeaders.AUTHORIZATION, "token"), () -> destination.getHeaders()); |
| 137 | + |
| 138 | + assertThat(headers).containsExactly(new Header(HttpHeaders.AUTHORIZATION, "token")); |
| 139 | + } |
| 140 | +} |
0 commit comments