|
| 1 | +package io.quarkiverse.openapi.generator.oidc; |
| 2 | + |
| 3 | +import static io.quarkiverse.openapi.generator.providers.AbstractAuthenticationPropagationHeadersFactory.propagationHeaderName; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.assertj.core.api.Assertions; |
| 12 | +import org.eclipse.microprofile.config.Config; |
| 13 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 14 | +import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.Arguments; |
| 20 | +import org.junit.jupiter.params.provider.MethodSource; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockedStatic; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 25 | + |
| 26 | +import io.quarkiverse.openapi.generator.AuthConfig; |
| 27 | +import io.quarkiverse.openapi.generator.oidc.providers.OAuth2AuthenticationProvider; |
| 28 | +import io.quarkus.oidc.client.Tokens; |
| 29 | +import io.smallrye.mutiny.Uni; |
| 30 | + |
| 31 | +import jakarta.ws.rs.client.ClientRequestContext; |
| 32 | +import jakarta.ws.rs.core.HttpHeaders; |
| 33 | +import jakarta.ws.rs.core.MultivaluedHashMap; |
| 34 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 35 | + |
| 36 | +@ExtendWith(MockitoExtension.class) |
| 37 | +public class ReactiveOAuth2AuthenticationProviderTest { |
| 38 | + private static final String OPEN_API_FILE_SPEC_ID = "open_api_file_spec_id_json"; |
| 39 | + private static final String AUTH_SCHEME_NAME = "auth_scheme_name"; |
| 40 | + |
| 41 | + private static final String ACCESS_TOKEN = "REACTIVE_ACCESS_TOKEN"; |
| 42 | + |
| 43 | + private static final String PROPAGATED_TOKEN = "PROPAGATED_TOKEN"; |
| 44 | + |
| 45 | + private static final String HEADER_NAME = "HEADER_NAME"; |
| 46 | + @Mock |
| 47 | + private ResteasyReactiveClientRequestContext reactiveRequestContext; |
| 48 | + private MultivaluedMap<String, Object> headers; |
| 49 | + |
| 50 | + private ReactiveOidcClientRequestFilterDelegate reactiveDelegate; |
| 51 | + private static final Tokens token = new Tokens(ACCESS_TOKEN, Long.MAX_VALUE, null, "", Long.MAX_VALUE, null, ""); |
| 52 | + private static final Uni<Tokens> uniToken = Uni.createFrom().item(token); |
| 53 | + |
| 54 | + private OAuth2AuthenticationProvider provider; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void setUp() { |
| 58 | + headers = new MultivaluedHashMap<>(); |
| 59 | + Mockito.lenient().doReturn(headers).when(reactiveRequestContext).getHeaders(); |
| 60 | + |
| 61 | + reactiveDelegate = Mockito.mock(ReactiveOidcClientRequestFilterDelegate.class); |
| 62 | + try { |
| 63 | + Mockito.lenient().doCallRealMethod().when(reactiveDelegate).filter(Mockito.any(ClientRequestContext.class)); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + Mockito.lenient().doCallRealMethod().when(reactiveDelegate).filter(reactiveRequestContext); |
| 68 | + Mockito.lenient().when(reactiveDelegate.getTokens()).thenReturn(uniToken); |
| 69 | + |
| 70 | + provider = createReactiveProvider(); |
| 71 | + } |
| 72 | + |
| 73 | + protected OAuth2AuthenticationProvider createReactiveProvider() { |
| 74 | + return new OAuth2AuthenticationProvider(AUTH_SCHEME_NAME, OPEN_API_FILE_SPEC_ID, reactiveDelegate, List.of()); |
| 75 | + } |
| 76 | + |
| 77 | + protected void assertHeader(MultivaluedMap<String, Object> headers, String headerName, String value) { |
| 78 | + Assertions.assertThat(headers.getFirst(headerName)) |
| 79 | + .isNotNull() |
| 80 | + .isEqualTo(value); |
| 81 | + } |
| 82 | + |
| 83 | + static Stream<Arguments> filterWithPropagationTestValues() { |
| 84 | + return Stream.of( |
| 85 | + Arguments.of(null, "Bearer " + PROPAGATED_TOKEN), |
| 86 | + Arguments.of(HEADER_NAME, "Bearer " + PROPAGATED_TOKEN)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void filterReactive() throws IOException { |
| 91 | + filter(provider, "Bearer " + ACCESS_TOKEN); |
| 92 | + } |
| 93 | + |
| 94 | + private void filter(OAuth2AuthenticationProvider provider, String expectedAuthorizationHeader) throws IOException { |
| 95 | + provider.filter(reactiveRequestContext); |
| 96 | + assertHeader(headers, HttpHeaders.AUTHORIZATION, expectedAuthorizationHeader); |
| 97 | + } |
| 98 | + |
| 99 | + @ParameterizedTest |
| 100 | + @MethodSource("filterWithPropagationTestValues") |
| 101 | + void filterWithPropagation(String headerName, |
| 102 | + String expectedAuthorizationHeader) throws IOException { |
| 103 | + String propagatedHeaderName; |
| 104 | + if (headerName == null) { |
| 105 | + propagatedHeaderName = propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME, |
| 106 | + HttpHeaders.AUTHORIZATION); |
| 107 | + } else { |
| 108 | + propagatedHeaderName = propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME, |
| 109 | + HEADER_NAME); |
| 110 | + } |
| 111 | + try (MockedStatic<ConfigProvider> configProviderMocked = Mockito.mockStatic(ConfigProvider.class)) { |
| 112 | + Config mockedConfig = Mockito.mock(Config.class); |
| 113 | + configProviderMocked.when(ConfigProvider::getConfig).thenReturn(mockedConfig); |
| 114 | + |
| 115 | + when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.TOKEN_PROPAGATION), |
| 116 | + Boolean.class)).thenReturn(Optional.of(true)); |
| 117 | + when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME), |
| 118 | + String.class)).thenReturn(Optional.of(headerName == null ? HttpHeaders.AUTHORIZATION : headerName)); |
| 119 | + |
| 120 | + headers.putSingle(propagatedHeaderName, PROPAGATED_TOKEN); |
| 121 | + filter(provider, expectedAuthorizationHeader); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments