Skip to content

Commit 6c0b775

Browse files
committed
test: add tests for external databricks helpers
1 parent 7a94085 commit 6c0b775

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
from typing import Dict
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+
def register_mocks():
14+
responses.post(
15+
"https://connect.example/__api__/v1/oauth/integrations/credentials",
16+
match=[
17+
responses.matchers.urlencoded_params_matcher(
18+
{
19+
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
20+
"subject_token_type": "urn:posit:connect:user-session-token",
21+
"subject_token": "cit",
22+
}
23+
)
24+
],
25+
json={
26+
"access_token": "dynamic-viewer-access-token",
27+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
28+
"token_type": "Bearer",
29+
},
30+
)
31+
32+
class TestPositCredentialsHelpers:
33+
@responses.activate
34+
def test_posit_credentials_provider(self):
35+
register_mocks()
36+
37+
client = Client(api_key="12345", url="https://connect.example/")
38+
cp = PositCredentialsProvider(posit_oauth=client.oauth, user_session_token="cit")
39+
assert cp() == {"Authorization": f"Bearer dynamic-viewer-access-token"}
40+
41+
@responses.activate
42+
def test_posit_credentials_strategy(self):
43+
register_mocks()
44+
45+
class local_strategy:
46+
def auth_type(self) -> str:
47+
return "local"
48+
def __call__(self) -> CredentialsProvider:
49+
def inner() -> Dict[str,str]:
50+
return {"Authorization": "Bearer static-pat-token"}
51+
return inner
52+
53+
# local_strategy is used when the content is running locally
54+
client = Client(api_key="12345", url="https://connect.example/")
55+
cs = PositCredentialsStrategy(local_strategy=local_strategy(),
56+
user_session_token="cit",
57+
client=client)
58+
cp = cs()
59+
assert cs.auth_type() == "local"
60+
assert cp() == {"Authorization": "Bearer static-pat-token"}
61+
62+
# posit_strategy is used when the content is running on Connect
63+
os.environ["RSTUDIO_PRODUCT"] = "CONNECT"
64+
cs = PositCredentialsStrategy(local_strategy=local_strategy(),
65+
user_session_token="cit",
66+
client=client)
67+
cp = cs()
68+
assert cs.auth_type() == "posit-oauth-integration"
69+
assert cp() == {"Authorization": "Bearer dynamic-viewer-access-token"}
70+
del os.environ["RSTUDIO_PRODUCT"]
71+

0 commit comments

Comments
 (0)