|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package tsptest.armresourceprovider; |
| 5 | + |
| 6 | +import com.azure.core.credential.AccessToken; |
| 7 | +import com.azure.core.credential.TokenCredential; |
| 8 | +import com.azure.core.http.HttpClient; |
| 9 | +import com.azure.core.http.HttpHeaderName; |
| 10 | +import com.azure.core.http.HttpHeaders; |
| 11 | +import com.azure.core.management.AzureEnvironment; |
| 12 | +import com.azure.core.management.profile.AzureProfile; |
| 13 | +import com.azure.core.test.http.MockHttpResponse; |
| 14 | +import tsptest.armresourceprovider.models.Operation; |
| 15 | +import org.junit.jupiter.api.Assertions; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +import java.time.OffsetDateTime; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +public class ArmTests { |
| 25 | + |
| 26 | + private static final String VALID_CHALLENGE_HEADER = "Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", error=\"insufficient_claims\", claims=\"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ==\""; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testCaeBehaviorForBearerTokenAuthenticationPolicy() { |
| 30 | + AtomicInteger tokenGetCount = new AtomicInteger(); |
| 31 | + ArmResourceProviderManager armResourceProviderManager = ArmResourceProviderManager |
| 32 | + .configure() |
| 33 | + .withHttpClient(mockCaeHttpClient(tokenGetCount)) |
| 34 | + .authenticate(mockCaeTokenCredential(tokenGetCount), new AzureProfile(AzureEnvironment.AZURE)); |
| 35 | + Operation operation = armResourceProviderManager.operations() |
| 36 | + .list().stream().iterator().next(); |
| 37 | + Assertions.assertEquals("mockOperation", operation.name()); |
| 38 | + Assertions.assertEquals(2, tokenGetCount.get()); |
| 39 | + } |
| 40 | + |
| 41 | + private HttpClient mockCaeHttpClient(AtomicInteger tokenGetCount) { |
| 42 | + return request -> { |
| 43 | + if (tokenGetCount.get() <= 1) { |
| 44 | + return Mono.just(new MockHttpResponse( |
| 45 | + request, |
| 46 | + 401, |
| 47 | + new HttpHeaders().add(HttpHeaderName.WWW_AUTHENTICATE, VALID_CHALLENGE_HEADER))); |
| 48 | + } |
| 49 | + return Mono.just(new MockHttpResponse(request, 200, mockOperationListResponse())); |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + private Object mockOperationListResponse() { |
| 54 | + return Map.of("value", Collections.singletonList(Map.of("name", "mockOperation", "isDataAction", "false"))); |
| 55 | + } |
| 56 | + |
| 57 | + private TokenCredential mockCaeTokenCredential(AtomicInteger tokenGetCount) { |
| 58 | + return tokenRequestContext -> { |
| 59 | + tokenGetCount.incrementAndGet(); |
| 60 | + return Mono.just(new AccessToken("fake_token", OffsetDateTime.MAX)); |
| 61 | + }; |
| 62 | + } |
| 63 | +} |
0 commit comments