Skip to content

test(auth): isolate suite from local .env so ANTHROPIC_API_KEY can't leak#67

Merged
silversurfer562 merged 1 commit into
mainfrom
claude/tender-hermann-57df58
Jun 15, 2026
Merged

test(auth): isolate suite from local .env so ANTHROPIC_API_KEY can't leak#67
silversurfer562 merged 1 commit into
mainfrom
claude/tender-hermann-57df58

Conversation

@silversurfer562

Copy link
Copy Markdown
Member

Problem

tests/test_auth_routing.py::TestCliAuthStatus::test_status_warns_when_api_mode_without_key passed in CI but failed locally.

Root cause: the conftest already delenv's ANTHROPIC_API_KEY for every test to keep the suite off the real Anthropic API. But cli.main() calls dotenv.load_dotenv() at startup, and a dev checkout's repo-root .env symlink (attune-author/.env → the sibling attune-ai/.env, which holds a real key) re-injects the key right after the delenv. So auth status saw a key and skipped the expected WARNING line. CI has no .env, so it stayed green — a machine-dependent flake.

Fix

Co-locate a dotenv.load_dotenv no-op with the delenv it completes, in conftest's _lenient_polish_by_default autouse fixture. The delenv was only half the guarantee; this closes the hole at the same altitude so the two can't drift apart.

  • Production code untouchedload_dotenv still runs normally for real CLI users.
  • Escape hatch documented — a test that wants real dotenv loading restores it via its own monkeypatch.setattr.

Verification

Reproduced by creating the .env symlink in the worktree, then:

  • Original failure confirmed (the WARNING assertion failed exactly as reported) with the guard disabled.
  • With the fix: test_auth_routing.py → 32 passed.
  • Full suite with .env present → 1086 passed, 31 skipped.

🤖 Generated with Claude Code

…can't leak ANTHROPIC_API_KEY

The conftest already delenv's ANTHROPIC_API_KEY for every test to keep
the suite from hitting the real Anthropic API. But cli.main() calls
dotenv.load_dotenv() at startup, and a dev checkout's repo-root .env
symlink (-> the sibling project's real key) re-injects the key right
after delenv — reopening the exact hazard the delenv guards against.

This surfaced as test_auth_routing.py::TestCliAuthStatus::
test_status_warns_when_api_mode_without_key passing in CI (no .env) but
failing locally: auth status saw a key and skipped the expected WARNING.

Co-locate a load_dotenv no-op with the delenv it completes so the two
can't drift apart. Production code is untouched; a test that wants real
dotenv loading restores it via its own monkeypatch.setattr.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@silversurfer562 silversurfer562 merged commit 66dfcf9 into main Jun 15, 2026
13 checks passed
@silversurfer562 silversurfer562 deleted the claude/tender-hermann-57df58 branch June 15, 2026 02:30
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