Skip to content

Commit

Permalink
test: add tests for external databricks helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkegley committed Aug 6, 2024
1 parent 7a94085 commit 6c0b775
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/connect/fastapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from fastapi.responses import JSONResponse
from posit.connect.external.databricks import PositCredentialsStrategy


DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}"
SQL_HTTP_PATH = os.getenv("DATABRICKS_PATH")
Expand Down
1 change: 1 addition & 0 deletions src/posit/connect/external/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CredentialsStrategy(abc.ABC):
https://github.com/databricks/databricks-sql-python/blob/v3.3.0/src/databricks/sql/auth/authenticators.py#L19-L33
https://github.com/databricks/databricks-sdk-py/blob/v0.29.0/databricks/sdk/credentials_provider.py#L44-L54
"""

@abc.abstractmethod
def auth_type(self) -> str:
raise NotImplementedError
Expand Down
71 changes: 71 additions & 0 deletions tests/posit/connect/external/test_databricks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
from typing import Dict

import responses
from posit.connect import Client
from posit.connect.external.databricks import (
CredentialsProvider,
PositCredentialsProvider,
PositCredentialsStrategy,
)


def register_mocks():
responses.post(
"https://connect.example/__api__/v1/oauth/integrations/credentials",
match=[
responses.matchers.urlencoded_params_matcher(
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"subject_token_type": "urn:posit:connect:user-session-token",
"subject_token": "cit",
}
)
],
json={
"access_token": "dynamic-viewer-access-token",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
},
)

class TestPositCredentialsHelpers:
@responses.activate
def test_posit_credentials_provider(self):
register_mocks()

client = Client(api_key="12345", url="https://connect.example/")
cp = PositCredentialsProvider(posit_oauth=client.oauth, user_session_token="cit")
assert cp() == {"Authorization": f"Bearer dynamic-viewer-access-token"}

@responses.activate
def test_posit_credentials_strategy(self):
register_mocks()

class local_strategy:
def auth_type(self) -> str:
return "local"
def __call__(self) -> CredentialsProvider:
def inner() -> Dict[str,str]:
return {"Authorization": "Bearer static-pat-token"}
return inner

# local_strategy is used when the content is running locally
client = Client(api_key="12345", url="https://connect.example/")
cs = PositCredentialsStrategy(local_strategy=local_strategy(),
user_session_token="cit",
client=client)
cp = cs()
assert cs.auth_type() == "local"
assert cp() == {"Authorization": "Bearer static-pat-token"}

# posit_strategy is used when the content is running on Connect
os.environ["RSTUDIO_PRODUCT"] = "CONNECT"
cs = PositCredentialsStrategy(local_strategy=local_strategy(),
user_session_token="cit",
client=client)
cp = cs()
assert cs.auth_type() == "posit-oauth-integration"
assert cp() == {"Authorization": "Bearer dynamic-viewer-access-token"}
del os.environ["RSTUDIO_PRODUCT"]

0 comments on commit 6c0b775

Please sign in to comment.