Skip to content

Commit 5a418c4

Browse files
haraldschillyclaude
andcommitted
cocalc-api/test: fix edge cases in api and improve testing
This commit updates the conftest.py test fixtures to properly support both account-scoped and project-scoped API keys during testing. The validate_api_key_config fixture now: - First attempts to detect scope via hub.system.test() (account-scoped keys) - Falls back to project.system.test() if hub fails (project-scoped keys) - Reports both errors if both endpoints fail, for better debugging Also clarifies that the hub endpoint's system.test() is for account-scoped keys only, with clear comments directing project-scoped key users to the project endpoint. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ab4ab26 commit 5a418c4

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/packages/server/conat/api/system.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export function ping() {
1818
return { now: Date.now() };
1919
}
2020

21-
export async function test({
22-
account_id,
23-
}: { account_id?: string } = {}) {
21+
export async function test({ account_id }: { account_id?: string } = {}) {
2422
// Return API key scope information and server time
2523
// The authFirst decorator determines the scope from the API key and injects account_id.
24+
// Note: This endpoint only accepts account-scoped keys (see packages/next/pages/api/conat/hub.ts).
25+
// For project-scoped keys, use the project endpoint (packages/next/pages/api/conat/project.ts).
2626
const response: { account_id: string; server_time: number } = {
2727
account_id: account_id ?? "",
2828
server_time: Date.now(),

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)