Skip to content

Commit 99d0d05

Browse files
Progress on moving setup into pytest setup
1 parent d29699b commit 99d0d05

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

pins/tests/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import tempfile
34
from pathlib import Path
@@ -12,6 +13,7 @@
1213
RscBoardBuilder,
1314
Snapshot,
1415
rm_env,
16+
set_rsc_api_keys,
1517
)
1618

1719
EXAMPLE_REL_PATH = "pins/tests/pins-compat"
@@ -86,5 +88,38 @@ def tmp_data_dir():
8688
yield Path(tmp_dir)
8789

8890

91+
@pytest.fixture(scope="session", autouse=True)
92+
def rsc_api_keys(request):
93+
if not any(item.get_closest_marker("fs_rsc") for item in request.session.items):
94+
return None
95+
96+
from pins.rsconnect.api import LoginConnectApi, RsConnectApi
97+
98+
extra_users = [
99+
{"username": "susan", "password": "susansusan"},
100+
{"username": "derek", "password": "derekderek"},
101+
]
102+
103+
admin_client = RsConnectApi()
104+
105+
guid = admin_client.get_user()["guid"]
106+
admin_client.query_v1(f"users/{guid}", "PUT", json={"username": "admin"})
107+
108+
api_keys = {"admin": os.getenv("CONNECT_API_KEY")}
109+
110+
for user in extra_users:
111+
admin_client.create_user(
112+
username=user["username"],
113+
password=user["password"],
114+
__confirmed=True,
115+
)
116+
api_keys[user["username"]] = LoginConnectApi(
117+
user["username"], user["password"]
118+
).create_api_key()
119+
120+
set_rsc_api_keys(api_keys)
121+
return api_keys
122+
123+
89124
def pytest_addoption(parser):
90125
parser.addoption("--snapshot-update", action="store_true")

pins/tests/helpers.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
DEFAULT_CREATION_DATE = datetime(2020, 1, 13, 23, 58, 59)
2020

2121
RSC_SERVER_URL = os.getenv("CONNECT_SERVER")
22-
# TODO: should use pkg_resources for this path?
23-
RSC_KEYS_FNAME = "pins/tests/rsconnect_api_keys.json"
22+
_RSC_API_KEYS = None
2423

2524
DATABRICKS_VOLUME = "/Volumes/workshops/my-board/my-volume/test"
2625

@@ -76,12 +75,18 @@ def wrapper(*args, **kwargs):
7675
return wrapper
7776

7877

78+
def set_rsc_api_keys(keys):
79+
global _RSC_API_KEYS
80+
_RSC_API_KEYS = keys
81+
82+
7983
def rsc_from_key(name):
8084
from pins.rsconnect.api import RsConnectApi
8185

82-
with open(RSC_KEYS_FNAME) as f:
83-
api_key = json.load(f)[name]
84-
return RsConnectApi(RSC_SERVER_URL, api_key)
86+
if _RSC_API_KEYS is None:
87+
raise RuntimeError("RSC API keys not initialized. This should be set by the pytest fixture.")
88+
api_key = _RSC_API_KEYS[name]
89+
return RsConnectApi(RSC_SERVER_URL, api_key)
8590

8691

8792
def rsc_fs_from_key(name):

0 commit comments

Comments
 (0)