Skip to content

Commit 41af0c2

Browse files
added/updated tests
1 parent 6b9654f commit 41af0c2

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/posit/connect/external/connect_api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ class ConnectAPIKeyProvider:
2727
from posit.connect import Client
2828
from posit.connect.external.connect_api import ConnectAPIKeyProvider
2929
30-
client = Client()
31-
3230
app_ui = ui.page_fixed(
3331
ui.h1("My Shiny App"),
3432
# ...
3533
)
3634
3735
def server(input, output, session):
36+
client = Client()
3837
user_session_token = session.http_conn.headers.get("Posit-Connect-User-Session-Token")
3938
provider = ConnectAPIKeyProvider(client, user_session_token)
40-
viewer_api_key = provider.viewer_key
39+
viewer_client = Client(api_key=provider.viewer)
40+
41+
assert client.me() != viewer_client.me()
4142
4243
# your app logic...
4344
@@ -54,7 +55,7 @@ def __init__(
5455
self._user_session_token = user_session_token
5556

5657
@property
57-
def viewer_key(self) -> Optional[str]:
58+
def viewer(self) -> Optional[str]:
5859
"""
5960
The viewer key is retrieved through an OAuth exchange process using the user session token.
6061
The issued API key is associated with the viewer of your app and can be used on their behalf
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest.mock import patch
2+
3+
import responses
4+
5+
from posit.connect import Client
6+
from posit.connect.external.connect_api import ConnectAPIKeyProvider
7+
8+
9+
def register_mocks():
10+
responses.post(
11+
"https://connect.example/__api__/v1/oauth/integrations/credentials",
12+
match=[
13+
responses.matchers.urlencoded_params_matcher(
14+
{
15+
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
16+
"subject_token_type": "urn:posit:connect:user-session-token",
17+
"subject_token": "cit",
18+
"requested_token_type": "urn:posit:connect:api-key"
19+
},
20+
),
21+
],
22+
json={
23+
"access_token": "viewer-api-key",
24+
"issued_token_type": "urn:posit:connect:api-key",
25+
"token_type": "Key",
26+
},
27+
)
28+
29+
30+
class TestConnectAPIKeyProvider:
31+
@responses.activate
32+
@patch.dict("os.environ", {"RSTUDIO_PRODUCT": "CONNECT"})
33+
def test_provider(self):
34+
register_mocks()
35+
36+
client = Client(api_key="12345", url="https://connect.example/")
37+
client._ctx.version = None
38+
auth = ConnectAPIKeyProvider(
39+
client=client,
40+
user_session_token="cit",
41+
)
42+
assert auth.viewer == "viewer-api-key"
43+
44+
def test_provider_fallback(self):
45+
# local_authenticator is used when the content is running locally
46+
client = Client(api_key="12345", url="https://connect.example/")
47+
client._ctx.version = None
48+
auth = ConnectAPIKeyProvider(
49+
client=client,
50+
user_session_token="cit",
51+
)
52+
assert auth.viewer is None

tests/posit/connect/oauth/test_oauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import responses
55

66
from posit.connect import Client
7-
from posit.connect.oauth.oauth import _get_content_session_token
7+
from posit.connect.oauth.oauth import API_KEY_TOKEN_TYPE, _get_content_session_token
88

99

1010
class TestOAuthIntegrations:
@@ -63,7 +63,7 @@ def test_get_credentials_api_key(self):
6363
)
6464
c = Client(api_key="12345", url="https://connect.example/")
6565
c._ctx.version = None
66-
creds = c.oauth.get_credentials("cit")
66+
creds = c.oauth.get_credentials("cit", API_KEY_TOKEN_TYPE)
6767
assert "access_token" in creds
6868
assert creds["access_token"] == "viewer-api-key"
6969
assert "issued_token_type" in creds

0 commit comments

Comments
 (0)