Skip to content

Commit f485df5

Browse files
hectorcast-dbclaude
andcommitted
Add unit test for external browser auth with Azure client ID
Add test to verify that ExternalBrowserCredentialsProvider correctly resolves and passes Azure OIDC endpoints to the OAuthClient when using Azure client ID authentication. The test: - Mocks external browser authentication flow with Azure credentials - Captures the OpenIDConnectEndpoints passed to performBrowserAuth - Verifies the endpoints match the expected Azure OIDC URLs Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent d37c1c5 commit f485df5

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,92 @@ void addOfflineAccessScopeWhenDisableOauthRefreshTokenIsFalse() {
660660
assertTrue(scopes.contains("offline_access"));
661661
assertTrue(scopes.contains("my-test-scope"));
662662
}
663+
664+
@Test
665+
void externalBrowserAuthWithAzureClientIdTest() throws IOException {
666+
// Create mock HTTP client
667+
HttpClient mockHttpClient = Mockito.mock(HttpClient.class);
668+
669+
// Mock token cache
670+
TokenCache mockTokenCache = Mockito.mock(TokenCache.class);
671+
Mockito.doReturn(null).when(mockTokenCache).load();
672+
673+
// Create valid token for browser auth
674+
Token browserAuthToken =
675+
new Token(
676+
"azure_access_token",
677+
"Bearer",
678+
"azure_refresh_token",
679+
Instant.now().plusSeconds(3600));
680+
681+
// Create token source
682+
SessionCredentialsTokenSource browserAuthTokenSource =
683+
new SessionCredentialsTokenSource(
684+
browserAuthToken,
685+
mockHttpClient,
686+
"https://test.azuredatabricks.net/oidc/v1/token",
687+
"test-azure-client-id",
688+
null,
689+
Optional.empty(),
690+
Optional.empty());
691+
692+
CachedTokenSource cachedTokenSource =
693+
new CachedTokenSource.Builder(browserAuthTokenSource).setToken(browserAuthToken).build();
694+
695+
// Create Azure config with Azure client ID
696+
DatabricksConfig config =
697+
new DatabricksConfig()
698+
.setAuthType("external-browser")
699+
.setHost("https://test.azuredatabricks.net")
700+
.setAzureClientId("test-azure-client-id")
701+
.setHttpClient(mockHttpClient);
702+
703+
// Create provider and mock browser auth
704+
ExternalBrowserCredentialsProvider provider =
705+
Mockito.spy(new ExternalBrowserCredentialsProvider(mockTokenCache));
706+
Mockito.doReturn(cachedTokenSource)
707+
.when(provider)
708+
.performBrowserAuth(
709+
any(DatabricksConfig.class),
710+
any(),
711+
any(),
712+
any(TokenCache.class),
713+
any(OpenIDConnectEndpoints.class));
714+
715+
// Spy on config to inject OIDC endpoints
716+
OpenIDConnectEndpoints endpoints =
717+
new OpenIDConnectEndpoints(
718+
"https://test.azuredatabricks.net/oidc/v1/token",
719+
"https://test.azuredatabricks.net/oidc/v1/authorize");
720+
DatabricksConfig spyConfig = Mockito.spy(config);
721+
Mockito.doReturn(endpoints).when(spyConfig).getOidcEndpoints();
722+
723+
// Configure provider
724+
HeaderFactory headerFactory = provider.configure(spyConfig);
725+
assertNotNull(headerFactory);
726+
727+
// Verify headers contain the Azure token
728+
Map<String, String> headers = headerFactory.headers();
729+
assertEquals("Bearer azure_access_token", headers.get("Authorization"));
730+
731+
// Capture and verify the OpenIDConnectEndpoints passed to performBrowserAuth
732+
ArgumentCaptor<OpenIDConnectEndpoints> endpointsCaptor =
733+
ArgumentCaptor.forClass(OpenIDConnectEndpoints.class);
734+
Mockito.verify(provider, Mockito.times(1))
735+
.performBrowserAuth(
736+
any(DatabricksConfig.class),
737+
any(),
738+
any(),
739+
any(TokenCache.class),
740+
endpointsCaptor.capture());
741+
742+
// Verify the captured endpoints match what we expect for Azure
743+
OpenIDConnectEndpoints capturedEndpoints = endpointsCaptor.getValue();
744+
assertNotNull(capturedEndpoints);
745+
assertEquals("https://test.azuredatabricks.net/oidc/v1/token", capturedEndpoints.getTokenEndpoint());
746+
assertEquals("https://test.azuredatabricks.net/oidc/v1/authorize", capturedEndpoints.getAuthorizationEndpoint());
747+
748+
// Verify token was saved
749+
Mockito.verify(mockTokenCache, Mockito.times(1)).save(any(Token.class));
750+
}
663751
}

0 commit comments

Comments
 (0)