Skip to content

fix(admin): raise AdminError instead of sys.exit; add admin test suite#228

Open
rylinjames wants to merge 1 commit into
mainfrom
fix/admin-testable-error-handling
Open

fix(admin): raise AdminError instead of sys.exit; add admin test suite#228
rylinjames wants to merge 1 commit into
mainfrom
fix/admin-testable-error-handling

Conversation

@rylinjames

Copy link
Copy Markdown
Collaborator

Audit §3.10 (admin A1/A2/A3/A5) — admin/ had zero tests because the library called sys.exit(2) directly, so nothing could be exercised without subprocess harnesses.

Changes

  • A1 (root cause): get_admin_token / admin_request now raise AdminError(exit_code=2) instead of stderr.write + sys.exit. Each command's main() catches it, prints the message, and returns the code — CLI exit-2-on-failure behavior is preserved, but the functions are now unit-testable and usable as a library.
  • A2: an unsupported HTTP method was raise ValueError inside the network try, so a programmer error got reported as "Could not reach worker". Now validated up front; the network except is narrowed to httpx.HTTPError.
  • A3: warn when the admin bearer token would be sent over cleartext http:// to a non-local host (otherwise the token leaks silently).
  • A5: validate --max-seats >= 1 and --limit >= 1. A4: renamed the license local that shadowed the builtin.

Tests

tests/test_admin.py14 cases: token presence; unsupported-method ≠ network error; 401 / 4xx / network-error wrapping; insecure-endpoint warning (and no warning for localhost); each command's main() success + AdminError exit codes + arg validation. ruff clean.

🤖 Generated with Claude Code

Audit §3.10 A1/A2/A3/A5 — admin/ had zero tests because the library called
sys.exit(2) directly, so nothing could be tested without subprocess harnesses.

- _client.py: get_admin_token / admin_request now raise AdminError(exit_code=2)
  instead of writing to stderr + sys.exit. Each command's main() catches it,
  prints the message, and returns the code — CLI behavior (exit 2 on failure)
  is preserved, but the functions are now unit-testable and library-usable.
- A2: an unsupported HTTP method was `raise ValueError` INSIDE the network
  try-block, so a programmer error got reported as "Could not reach worker".
  Now validated up front; the network except is narrowed to httpx.HTTPError.
- A3: warn when the admin bearer token would be sent over cleartext http:// to
  a non-local host (the token is otherwise leaked silently).
- A5: validate --max-seats >= 1 (issue) and --limit >= 1 (list).
- A4: renamed the `license` local that shadowed the builtin to `lic`.

Added tests/test_admin.py (14 cases): token presence, unsupported-method ≠
network error, 401/4xx/network wrapping, insecure-endpoint warning (and no
warning for localhost), and each command's main() success + AdminError exit
codes + arg validation. ruff clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant