1- from databricks .sdk .oauth import OidcEndpoints , TokenCache , get_workspace_endpoints , get_azure_entra_id_workspace_endpoints , get_account_endpoints
21from databricks .sdk ._base_client import _BaseClient
3- from .clock import FakeClock
2+ from databricks .sdk .oauth import (OidcEndpoints , TokenCache ,
3+ get_account_endpoints ,
4+ get_workspace_endpoints )
45
6+ from .clock import FakeClock
57
68
79def test_token_cache_unique_filename_by_host ():
8- common_args = dict (
9- client_id = "abc" ,
10- redirect_url = "http://localhost:8020" ,
11- oidc_endpoints = OidcEndpoints ("http://localhost:1234" , "http://localhost:1234" ))
12- assert TokenCache (host = "http://localhost:" , ** common_args ).filename != TokenCache ("https://bar.cloud.databricks.com" , ** common_args ).filename
10+ common_args = dict (client_id = "abc" ,
11+ redirect_url = "http://localhost:8020" ,
12+ oidc_endpoints = OidcEndpoints ("http://localhost:1234" , "http://localhost:1234" ))
13+ assert TokenCache (host = "http://localhost:" ,
14+ ** common_args ).filename != TokenCache ("https://bar.cloud.databricks.com" ,
15+ ** common_args ).filename
1316
1417
1518def test_token_cache_unique_filename_by_client_id ():
16- common_args = dict (
17- host = "http://localhost:" ,
18- redirect_url = "http://localhost:8020" ,
19- oidc_endpoints = OidcEndpoints ( "http://localhost:1234 " , "http://localhost:1234" ))
20- assert TokenCache ( client_id = "abc" , ** common_args ). filename != TokenCache ( client_id = "def" , ** common_args ).filename
19+ common_args = dict (host = "http://localhost:" ,
20+ redirect_url = "http://localhost:8020 " ,
21+ oidc_endpoints = OidcEndpoints ( "http://localhost:1234" , "http://localhost:1234" ))
22+ assert TokenCache ( client_id = "abc " , ** common_args ). filename != TokenCache ( client_id = "def" ,
23+ ** common_args ).filename
2124
2225
2326def test_token_cache_unique_filename_by_scopes ():
24- common_args = dict (
25- host = "http://localhost: " ,
26- client_id = "abc " ,
27- redirect_url = "http://localhost:8020" ,
28- oidc_endpoints = OidcEndpoints ( "http://localhost:1234" , "http://localhost:1234" ))
29- assert TokenCache ( scopes = [ "foo" ], ** common_args ). filename != TokenCache ( scopes = [ "bar" ], ** common_args ).filename
27+ common_args = dict (host = "http://localhost:" ,
28+ client_id = "abc " ,
29+ redirect_url = "http://localhost:8020 " ,
30+ oidc_endpoints = OidcEndpoints ( "http://localhost:1234" , "http://localhost:1234" ))
31+ assert TokenCache ( scopes = [ "foo" ], ** common_args ). filename != TokenCache ( scopes = [ "bar" ],
32+ ** common_args ).filename
3033
3134
3235def test_account_oidc_endpoints (requests_mock ):
33- requests_mock .get ("https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
34- json = {"authorization_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/authorize" ,
35- "token_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/token" })
36+ requests_mock .get (
37+ "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
38+ json = {
39+ "authorization_endpoint" :
40+ "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/authorize" ,
41+ "token_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/token"
42+ })
3643 client = _BaseClient (clock = FakeClock ())
3744 endpoints = get_account_endpoints ("accounts.cloud.databricks.com" , "abc-123" , client = client )
3845 assert endpoints == OidcEndpoints (
3946 "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/authorize" ,
4047 "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/token" )
4148
49+
4250def test_account_oidc_endpoints_retry_on_429 (requests_mock ):
4351 request_count = 0
4452
4553 def nth_request (n ):
54+
4655 def observe_request (_request ):
4756 nonlocal request_count
4857 is_match = request_count == n
4958 if is_match :
5059 request_count += 1
5160 return is_match
61+
5262 return observe_request
5363
54- requests_mock .get ("https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
55- additional_matcher = nth_request (0 ),
56- status_code = 429 )
57- requests_mock .get ("https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
58- additional_matcher = nth_request (1 ),
59- json = {"authorization_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/authorize" ,
60- "token_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/token" })
64+ requests_mock .get (
65+ "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
66+ additional_matcher = nth_request (0 ),
67+ status_code = 429 )
68+ requests_mock .get (
69+ "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/.well-known/oauth-authorization-server" ,
70+ additional_matcher = nth_request (1 ),
71+ json = {
72+ "authorization_endpoint" :
73+ "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/authorize" ,
74+ "token_endpoint" : "https://accounts.cloud.databricks.com/oidc/accounts/abc-123/oauth/token"
75+ })
6176 client = _BaseClient (clock = FakeClock ())
6277 endpoints = get_account_endpoints ("accounts.cloud.databricks.com" , "abc-123" , client = client )
6378 assert endpoints == OidcEndpoints (
@@ -67,36 +82,42 @@ def observe_request(_request):
6782
6883def test_workspace_oidc_endpoints (requests_mock ):
6984 requests_mock .get ("https://my-workspace.cloud.databricks.com/.well-known/oauth-authorization-server" ,
70- json = {"authorization_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
71- "token_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/token" })
85+ json = {
86+ "authorization_endpoint" :
87+ "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
88+ "token_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/token"
89+ })
7290 client = _BaseClient (clock = FakeClock ())
7391 endpoints = get_workspace_endpoints ("my-workspace.cloud.databricks.com" , client = client )
74- assert endpoints == OidcEndpoints (
75- "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
76- "https://my-workspace.cloud.databricks.com/oidc/oauth/token" )
92+ assert endpoints == OidcEndpoints ("https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
93+ "https://my-workspace.cloud.databricks.com/oidc/oauth/token" )
7794
7895
7996def test_workspace_oidc_endpoints_retry_on_429 (requests_mock ):
8097 request_count = 0
8198
8299 def nth_request (n ):
100+
83101 def observe_request (_request ):
84102 nonlocal request_count
85103 is_match = request_count == n
86104 if is_match :
87105 request_count += 1
88106 return is_match
107+
89108 return observe_request
90109
91110 requests_mock .get ("https://my-workspace.cloud.databricks.com/.well-known/oauth-authorization-server" ,
92111 additional_matcher = nth_request (0 ),
93112 status_code = 429 )
94113 requests_mock .get ("https://my-workspace.cloud.databricks.com/.well-known/oauth-authorization-server" ,
95114 additional_matcher = nth_request (1 ),
96- json = {"authorization_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
97- "token_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/token" })
115+ json = {
116+ "authorization_endpoint" :
117+ "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
118+ "token_endpoint" : "https://my-workspace.cloud.databricks.com/oidc/oauth/token"
119+ })
98120 client = _BaseClient (clock = FakeClock ())
99121 endpoints = get_workspace_endpoints ("my-workspace.cloud.databricks.com" , client = client )
100- assert endpoints == OidcEndpoints (
101- "https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
102- "https://my-workspace.cloud.databricks.com/oidc/oauth/token" )
122+ assert endpoints == OidcEndpoints ("https://my-workspace.cloud.databricks.com/oidc/oauth/authorize" ,
123+ "https://my-workspace.cloud.databricks.com/oidc/oauth/token" )
0 commit comments