test(auth): isolate suite from local .env so ANTHROPIC_API_KEY can't leak#67
Merged
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
tests/test_auth_routing.py::TestCliAuthStatus::test_status_warns_when_api_mode_without_keypassed in CI but failed locally.Root cause: the conftest already
delenv'sANTHROPIC_API_KEYfor every test to keep the suite off the real Anthropic API. Butcli.main()callsdotenv.load_dotenv()at startup, and a dev checkout's repo-root.envsymlink (attune-author/.env→ the siblingattune-ai/.env, which holds a real key) re-injects the key right after thedelenv. Soauth statussaw a key and skipped the expectedWARNINGline. CI has no.env, so it stayed green — a machine-dependent flake.Fix
Co-locate a
dotenv.load_dotenvno-op with thedelenvit completes, in conftest's_lenient_polish_by_defaultautouse fixture. Thedelenvwas only half the guarantee; this closes the hole at the same altitude so the two can't drift apart.load_dotenvstill runs normally for real CLI users.monkeypatch.setattr.Verification
Reproduced by creating the
.envsymlink in the worktree, then:WARNINGassertion failed exactly as reported) with the guard disabled.test_auth_routing.py→ 32 passed..envpresent → 1086 passed, 31 skipped.🤖 Generated with Claude Code