|
1 | 1 | package io.quarkiverse.openapi.generator.providers; |
2 | 2 |
|
3 | 3 | import static io.quarkiverse.openapi.generator.providers.AbstractAuthenticationPropagationHeadersFactory.propagationHeaderName; |
| 4 | +import static org.mockito.Mockito.when; |
4 | 5 |
|
5 | 6 | import java.io.IOException; |
6 | 7 | import java.util.List; |
| 8 | +import java.util.Optional; |
7 | 9 | import java.util.stream.Stream; |
8 | 10 |
|
| 11 | +import io.quarkiverse.openapi.generator.AuthConfig; |
9 | 12 | import jakarta.ws.rs.core.HttpHeaders; |
10 | 13 |
|
| 14 | +import org.eclipse.microprofile.config.Config; |
| 15 | +import org.eclipse.microprofile.config.ConfigProvider; |
11 | 16 | import org.junit.jupiter.api.Test; |
12 | 17 | import org.junit.jupiter.params.ParameterizedTest; |
13 | 18 | import org.junit.jupiter.params.provider.Arguments; |
14 | 19 | import org.junit.jupiter.params.provider.MethodSource; |
| 20 | +import org.mockito.MockedStatic; |
| 21 | +import org.mockito.Mockito; |
15 | 22 |
|
16 | 23 | class BearerOpenApiSpecProviderTest extends AbstractOpenApiSpecProviderTest<BearerAuthenticationProvider> { |
17 | 24 |
|
18 | 25 | private static final String INCOMING_TOKEN = "INCOMING_TOKEN"; |
19 | 26 |
|
| 27 | + private static final String PROPAGATED_TOKEN = "PROPAGATED_TOKEN"; |
| 28 | + |
20 | 29 | private static final String BEARER_SCHEMA = "bearer"; |
21 | 30 | private static final String CUSTOM_SCHEMA = "custom_scheme"; |
22 | 31 | private static final String HEADER_NAME = "HEADER_NAME"; |
@@ -62,15 +71,25 @@ void filterWithPropagation(String headerName, |
62 | 71 | propagatedHeaderName = propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME, |
63 | 72 | HEADER_NAME); |
64 | 73 | } |
65 | | - headers.putSingle(propagatedHeaderName, INCOMING_TOKEN); |
66 | | - filter(bearerScheme, expectedAuthorizationHeader); |
| 74 | + try (MockedStatic<ConfigProvider> configProviderMocked = Mockito.mockStatic(ConfigProvider.class)) { |
| 75 | + Config mockedConfig = Mockito.mock(Config.class); |
| 76 | + configProviderMocked.when(ConfigProvider::getConfig).thenReturn(mockedConfig); |
| 77 | + |
| 78 | + when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.TOKEN_PROPAGATION), |
| 79 | + Boolean.class)).thenReturn(Optional.of(true)); |
| 80 | + when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME), |
| 81 | + String.class)).thenReturn(Optional.of(headerName == null ? HttpHeaders.AUTHORIZATION: headerName)); |
| 82 | + |
| 83 | + headers.putSingle(propagatedHeaderName, PROPAGATED_TOKEN); |
| 84 | + filter(bearerScheme, expectedAuthorizationHeader); |
| 85 | + } |
67 | 86 | } |
68 | 87 |
|
69 | 88 | static Stream<Arguments> filterWithPropagationTestValues() { |
70 | 89 | return Stream.of( |
71 | | - Arguments.of(null, "bearer", "Bearer " + INCOMING_TOKEN), |
72 | | - Arguments.of(null, CUSTOM_SCHEMA, CUSTOM_SCHEMA + " " + INCOMING_TOKEN), |
73 | | - Arguments.of(HEADER_NAME, "bearer", "Bearer " + INCOMING_TOKEN), |
74 | | - Arguments.of(HEADER_NAME, CUSTOM_SCHEMA, CUSTOM_SCHEMA + " " + INCOMING_TOKEN)); |
| 90 | + Arguments.of(null, "bearer", "Bearer " + PROPAGATED_TOKEN), |
| 91 | + Arguments.of(null, CUSTOM_SCHEMA, CUSTOM_SCHEMA + " " + PROPAGATED_TOKEN), |
| 92 | + Arguments.of(HEADER_NAME, "bearer", "Bearer " + PROPAGATED_TOKEN), |
| 93 | + Arguments.of(HEADER_NAME, CUSTOM_SCHEMA, CUSTOM_SCHEMA + " " + PROPAGATED_TOKEN)); |
75 | 94 | } |
76 | 95 | } |
0 commit comments