Skip to content

Commit 9b778fa

Browse files
committed
fmt and fix impor
1 parent f752885 commit 9b778fa

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
lines changed

databricks/sdk/_base_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import requests
88
import requests.adapters
9+
import urllib.parse
910

1011
from . import useragent
1112
from .casing import Casing

databricks/sdk/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import requests
1111

1212
from . import useragent
13+
from ._base_client import fix_host_if_needed
1314
from .clock import Clock, RealClock
1415
from .credentials_provider import CredentialsStrategy, DefaultCredentials
1516
from .environments import (ALL_ENVS, AzureEnvironment, Cloud,
1617
DatabricksEnvironment, get_environment_for_hostname)
1718
from .oauth import (OidcEndpoints, Token, get_account_endpoints,
1819
get_azure_entra_id_workspace_endpoints,
1920
get_workspace_endpoints)
20-
from ._base_client import fix_host_if_needed
2121

2222
logger = logging.getLogger('databricks.sdk')
2323

tests/test_oauth.py

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,78 @@
1-
from databricks.sdk.oauth import OidcEndpoints, TokenCache, get_workspace_endpoints, get_azure_entra_id_workspace_endpoints, get_account_endpoints
21
from 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

79
def 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

1518
def 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

2326
def 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

3235
def 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+
4250
def 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

6883
def 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

7996
def 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

Comments
 (0)