Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Admin API smoke tests (Anthropic). Read-only — never mutates.23Unmarked (cheap): GET /v1/organizations/me works with a REGULAR key (LIVE_VERIFIED 2026-09-18, HTTP 200), and the4other admin endpoints return 401 with a regular key (ACCOUNT_RESTRICTED) unless ANTHROPIC_ADMIN_KEY is configured.5Marked run_admin_tests (RUN_ADMIN_TESTS=true + ANTHROPIC_ADMIN_KEY): list users/workspaces/api_keys, usage & cost reports.6"""7from __future__ import annotations8import os9from datetime import datetime, timedelta, timezone1011import pytest1213ADMIN_401 = "The Admin API requires an Admin API key or an organization-scoped API key."141516def test_org_me_with_regular_key(anthropic):17 st, body, hdrs = anthropic("GET", "/v1/organizations/me", note="test org me")18 assert st == 200, body19 assert body["type"] == "organization" and body["id"] and body["name"]202122@pytest.mark.parametrize("path", ["/v1/organizations/users?limit=1", "/v1/organizations/workspaces?limit=1",23 "/v1/organizations/api_keys?limit=1", "/v1/organizations/invites?limit=1"])24def test_admin_endpoints_need_admin_key(anthropic, path):25 if os.environ.get("ANTHROPIC_ADMIN_KEY"):26 pytest.skip("admin key configured; see run_admin_tests")27 st, body, _ = anthropic("GET", path, note="test admin 401 with regular key")28 assert st == 401 and body["error"]["type"] == "authentication_error", body29 assert body["error"]["message"] == ADMIN_401303132@pytest.mark.run_admin_tests33@pytest.mark.parametrize("path,keys", [34 ("/v1/organizations/users?limit=1", {"data", "has_more", "first_id", "last_id"}),35 ("/v1/organizations/workspaces?limit=1", {"data", "has_more"}),36 ("/v1/organizations/api_keys?limit=1", {"data", "has_more"}),37 ("/v1/organizations/invites?limit=1", {"data", "has_more"}),38 ("/v1/organizations/rate_limits?limit=1", {"data"}),39])40def test_admin_lists(anthropic, path, keys):41 if not os.environ.get("ANTHROPIC_ADMIN_KEY"):42 pytest.skip("ANTHROPIC_ADMIN_KEY not set")43 st, body, _ = anthropic("GET", path, admin=True, note="test admin list")44 assert st == 200, body45 assert keys <= set(body)464748@pytest.mark.run_admin_tests49def test_usage_and_cost_reports(anthropic):50 if not os.environ.get("ANTHROPIC_ADMIN_KEY"):51 pytest.skip("ANTHROPIC_ADMIN_KEY not set")52 start = (datetime.now(timezone.utc) - timedelta(days=3)).strftime("%Y-%m-%dT00:00:00Z")53 day = (datetime.now(timezone.utc) - timedelta(days=1)).strftime("%Y-%m-%d")54 st, body, _ = anthropic("GET", f"/v1/organizations/usage_report/messages?starting_at={start}&bucket_width=1d&limit=3", admin=True, note="test usage report")55 assert st == 200 and "data" in body and len(body["data"]) <= 356 st, body, _ = anthropic("GET", f"/v1/organizations/cost_report?starting_at={start}&limit=3", admin=True, note="test cost report")57 assert st == 200 and "data" in body58 st, body, _ = anthropic("GET", f"/v1/organizations/usage_report/claude_code?starting_at={day}&limit=1", admin=True, note="test claude code report")59 assert st == 200 and {"data", "has_more", "next_page"} <= set(body)60