Skip to content

Commit 3f34f79

Browse files
committed
cocalc-api/test: make this work for project and account based api keys
1 parent 1227210 commit 3f34f79

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/python/cocalc-api/tests/conftest.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,30 @@ def validate_api_key_config(hub):
126126
For account-scoped keys, requires COCALC_PROJECT_ID to be set.
127127
For project-scoped keys, no additional configuration needed.
128128
"""
129+
scope = None
130+
hub_error = None
131+
132+
# First, try the hub endpoint (works only for account-scoped keys)
129133
try:
130134
scope = hub.system.test()
131135
except Exception as e:
132-
pytest.fail(f"Failed to determine API key scope: {e}")
136+
hub_error = e
137+
138+
# If hub check failed, fall back to the project endpoint so project-scoped keys work
139+
if scope is None:
140+
try:
141+
project_client = Project(
142+
api_key=hub.api_key,
143+
host=hub.host,
144+
project_id=os.environ.get("COCALC_PROJECT_ID"),
145+
)
146+
scope = project_client.system.test()
147+
except Exception as project_error:
148+
pytest.fail(
149+
"Failed to determine API key scope using both hub and project endpoints:\n"
150+
f" hub error: {hub_error}\n"
151+
f" project error: {project_error}"
152+
)
133153

134154
is_account_scoped = "account_id" in scope
135155
is_project_scoped = "project_id" in scope

0 commit comments

Comments
 (0)