Skip to content

Commit 710c6b1

Browse files
committed
test: add tests for external databricks helpers
1 parent d19af5c commit 710c6b1

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

examples/connect/fastapi/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from fastapi.responses import JSONResponse
1010
from posit.connect.external.databricks import PositCredentialsStrategy
1111

12-
1312
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")
1413
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}"
1514
SQL_HTTP_PATH = os.getenv("DATABRICKS_PATH")

src/posit/connect/external/databricks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CredentialsStrategy(abc.ABC):
1919
https://github.com/databricks/databricks-sql-python/blob/v3.3.0/src/databricks/sql/auth/authenticators.py#L19-L33
2020
https://github.com/databricks/databricks-sdk-py/blob/v0.29.0/databricks/sdk/credentials_provider.py#L44-L54
2121
"""
22+
2223
@abc.abstractmethod
2324
def auth_type(self) -> str:
2425
raise NotImplementedError
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import Dict
2+
from unittest.mock import patch
3+
4+
import responses
5+
from posit.connect import Client
6+
from posit.connect.external.databricks import (
7+
CredentialsProvider,
8+
PositCredentialsProvider,
9+
PositCredentialsStrategy,
10+
)
11+
12+
13+
class mock_strategy:
14+
def auth_type(self) -> str:
15+
return "local"
16+
def __call__(self) -> CredentialsProvider:
17+
def inner() -> Dict[str,str]:
18+
return {"Authorization": "Bearer static-pat-token"}
19+
return inner
20+
21+
22+
def register_mocks():
23+
responses.post(
24+
"https://connect.example/__api__/v1/oauth/integrations/credentials",
25+
match=[
26+
responses.matchers.urlencoded_params_matcher(
27+
{
28+
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
29+
"subject_token_type": "urn:posit:connect:user-session-token",
30+
"subject_token": "cit",
31+
}
32+
)
33+
],
34+
json={
35+
"access_token": "dynamic-viewer-access-token",
36+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
37+
"token_type": "Bearer",
38+
},
39+
)
40+
41+
42+
class TestPositCredentialsHelpers:
43+
@responses.activate
44+
def test_posit_credentials_provider(self):
45+
register_mocks()
46+
47+
client = Client(api_key="12345", url="https://connect.example/")
48+
cp = PositCredentialsProvider(posit_oauth=client.oauth, user_session_token="cit")
49+
assert cp() == {"Authorization": f"Bearer dynamic-viewer-access-token"}
50+
51+
@responses.activate
52+
@patch.dict("os.environ", {"RSTUDIO_PRODUCT": "CONNECT"})
53+
def test_posit_credentials_strategy(self):
54+
register_mocks()
55+
56+
client = Client(api_key="12345", url="https://connect.example/")
57+
cs = PositCredentialsStrategy(local_strategy=mock_strategy(),
58+
user_session_token="cit",
59+
client=client)
60+
cp = cs()
61+
assert cs.auth_type() == "posit-oauth-integration"
62+
assert cp() == {"Authorization": "Bearer dynamic-viewer-access-token"}
63+
64+
def test_posit_credentials_strategy_fallback(self):
65+
# local_strategy is used when the content is running locally
66+
client = Client(api_key="12345", url="https://connect.example/")
67+
cs = PositCredentialsStrategy(local_strategy=mock_strategy(),
68+
user_session_token="cit",
69+
client=client)
70+
cp = cs()
71+
assert cs.auth_type() == "local"
72+
assert cp() == {"Authorization": "Bearer static-pat-token"}

0 commit comments

Comments
 (0)