|
9 | 9 | import com.azure.identity.util.TestUtils;
|
10 | 10 | import org.junit.jupiter.api.Assertions;
|
11 | 11 | import org.junit.jupiter.api.Test;
|
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.ValueSource; |
12 | 14 | import org.mockito.MockedConstruction;
|
13 | 15 | import reactor.core.publisher.Mono;
|
14 | 16 | import reactor.test.StepVerifier;
|
@@ -68,4 +70,101 @@ public void azurePowerShellCredentialNotInstalledException() {
|
68 | 70 | Assertions.assertNotNull(identityClientMock);
|
69 | 71 | }
|
70 | 72 | }
|
| 73 | + |
| 74 | + @Test |
| 75 | + public void testClaimsChallengeThrowsCredentialUnavailableException() { |
| 76 | + // Test with claims provided |
| 77 | + TokenRequestContext requestWithClaims |
| 78 | + = new TokenRequestContext().addScopes("https://graph.microsoft.com/.default") |
| 79 | + .setClaims("{\"access_token\":{\"essential\":true}}"); |
| 80 | + |
| 81 | + AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().build(); |
| 82 | + |
| 83 | + // Test async version |
| 84 | + StepVerifier.create(credential.getToken(requestWithClaims)) |
| 85 | + .expectErrorMatches(throwable -> throwable instanceof CredentialUnavailableException |
| 86 | + && throwable.getMessage().contains("Claims challenges are not supported") |
| 87 | + && throwable.getMessage().contains("Connect-AzAccount -ClaimsChallenge") |
| 88 | + && throwable.getMessage().contains("access_token")) |
| 89 | + .verify(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testPowerShellClaimsChallengeWithTenantAndScopes() { |
| 94 | + TokenRequestContext requestWithClaims = new TokenRequestContext() |
| 95 | + .addScopes("https://graph.microsoft.com/.default", "https://vault.azure.net/.default") |
| 96 | + .setClaims("{\"access_token\":{\"essential\":true}}") |
| 97 | + .setTenantId("tenant-id-123"); |
| 98 | + |
| 99 | + AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().tenantId("tenant-id-123").build(); |
| 100 | + |
| 101 | + // Test that error message includes tenant and mentions scopes |
| 102 | + StepVerifier.create(credential.getToken(requestWithClaims)) |
| 103 | + .expectErrorMatches(throwable -> throwable instanceof CredentialUnavailableException |
| 104 | + && throwable.getMessage().contains("-Tenant tenant-id-123")) |
| 105 | + .verify(); |
| 106 | + } |
| 107 | + |
| 108 | + @ParameterizedTest |
| 109 | + @ValueSource(strings = { "", " ", "\t", "\n" }) |
| 110 | + public void testEmptyClaimsDoesNotThrowException(String claims) { |
| 111 | + TokenRequestContext request |
| 112 | + = new TokenRequestContext().addScopes("https://graph.microsoft.com/.default").setClaims(claims); |
| 113 | + |
| 114 | + // Mock successful token acquisition for empty claims |
| 115 | + try (MockedConstruction<IdentityClient> identityClientMock |
| 116 | + = mockConstruction(IdentityClient.class, (identityClient, context) -> { |
| 117 | + when(identityClient.authenticateWithAzurePowerShell(request)) |
| 118 | + .thenReturn(TestUtils.getMockAccessToken("token", OffsetDateTime.now().plusHours(1))); |
| 119 | + })) { |
| 120 | + |
| 121 | + AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().build(); |
| 122 | + |
| 123 | + // Should not throw exception for empty/whitespace claims |
| 124 | + StepVerifier.create(credential.getToken(request)) |
| 125 | + .expectNextMatches(accessToken -> "token".equals(accessToken.getToken())) |
| 126 | + .verifyComplete(); |
| 127 | + Assertions.assertNotNull(identityClientMock); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testNullClaimsDoesNotThrowException() { |
| 133 | + TokenRequestContext request |
| 134 | + = new TokenRequestContext().addScopes("https://graph.microsoft.com/.default").setClaims(null); |
| 135 | + |
| 136 | + // Mock successful token acquisition for null claims |
| 137 | + try (MockedConstruction<IdentityClient> identityClientMock |
| 138 | + = mockConstruction(IdentityClient.class, (identityClient, context) -> { |
| 139 | + when(identityClient.authenticateWithAzurePowerShell(request)) |
| 140 | + .thenReturn(TestUtils.getMockAccessToken("token", OffsetDateTime.now().plusHours(1))); |
| 141 | + })) { |
| 142 | + |
| 143 | + AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().build(); |
| 144 | + |
| 145 | + // Should not throw exception for null claims |
| 146 | + StepVerifier.create(credential.getToken(request)) |
| 147 | + .expectNextMatches(accessToken -> "token".equals(accessToken.getToken())) |
| 148 | + .verifyComplete(); |
| 149 | + Assertions.assertNotNull(identityClientMock); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testClaimsChallengeEscapesSingleQuotes() { |
| 155 | + // Test with claims that contain single quotes (needs proper PowerShell escaping) |
| 156 | + TokenRequestContext requestWithClaims |
| 157 | + = new TokenRequestContext().addScopes("https://graph.microsoft.com/.default") |
| 158 | + .setClaims("{\"access_token\":{\"claim\":\"value's test\"}}"); |
| 159 | + |
| 160 | + AzurePowerShellCredential credential = new AzurePowerShellCredentialBuilder().build(); |
| 161 | + |
| 162 | + // Test that single quotes are properly escaped for PowerShell |
| 163 | + StepVerifier.create(credential.getToken(requestWithClaims)) |
| 164 | + .expectErrorMatches(throwable -> throwable instanceof CredentialUnavailableException |
| 165 | + && throwable.getMessage().contains("Connect-AzAccount -ClaimsChallenge") |
| 166 | + && throwable.getMessage().contains("value''s test") // Single quote should be escaped to '' |
| 167 | + ) |
| 168 | + .verify(); |
| 169 | + } |
71 | 170 | }
|
0 commit comments