Skip to content

test: make the sync refresh-margin test actually test the margin - #9

Merged
danieyal merged 2 commits into
masterfrom
test/sync-refresh-margin
Jul 22, 2026
Merged

test: make the sync refresh-margin test actually test the margin#9
danieyal merged 2 commits into
masterfrom
test/sync-refresh-margin

Conversation

@danieyal

@danieyal danieyal commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Tests only. No source changes. Addresses the outside-diff review comment left on #8, which was deliberately deferred to keep that PR single-concern.

The test was vacuous

test_token_manager_refresh_margin never called get_token():

respx_mock.post(token_url).mock(...expires_in=30...)
assert mgr.is_valid is False        # ← no acquisition anywhere

So is_valid was False purely via the tok is None branch. The refresh margin was never involved. Proven twice:

  • It passed with expires_in=3600 — far outside the 60s margin — making 0 calls to the token endpoint.
  • It would have passed with the refresh-ahead logic deleted entirely.

Worth noting: auth.py reported 95% line coverage throughout. Coverage counts executed lines, not meaningful assertions, so the healthiest-looking metric was hiding the untested path.

The fix

Mirrors the async twin fixed in #7 — two mocked responses, two get_token() calls, asserting the second returns the fresh token and route.call_count == 2.

This drives the real path rather than the flag: get_token does not consult the is_valid property, it re-checks is_expired inline, twice (outer check, then again under the lock).

Verified it can fail

With refresh_margin=0 the second get_token() returns the cached STALE token and the new assertions fail:

- FRESH
+ STALE

The old assertion passed either way. Both managers' refresh-ahead behaviour is now covered symmetrically.

Testing

303 tests. ruff check, ruff format --check, mypy src/myinvois all clean.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Strengthened authentication token refresh coverage to verify token reacquisition when a token is nearing expiry, including validity transitions and confirming multiple authentication requests.
    • Improved authentication request assertions by validating the outgoing form payload (client credentials grant).
  • Documentation
    • Updated the current-state notes to better explain the refreshed token test’s coverage and to reflect the strengthened, real token acquisition behavior.

test_token_manager_refresh_margin never called get_token(), so is_valid was
False merely because no token had ever been acquired -- the tok is None
branch. The refresh margin was never exercised. It passed with
expires_in=3600, far outside the 60s margin, having made 0 calls to the token
endpoint, and would have passed with the refresh-ahead logic deleted
entirely.

auth.py reported 95% line coverage throughout, because coverage counts
executed lines rather than meaningful assertions.

Now mirrors the async twin fixed in #7: two mocked responses, two get_token()
calls, asserting the second returns the fresh token and the route was hit
twice. This drives the real path -- get_token does not consult the is_valid
property, it re-checks is_expired inline, twice (outer check, then again
under the lock).

Verified sensitive: with refresh_margin=0 the second get_token returns the
cached STALE token and the new assertions fail. The old assertion passed
either way.

Both managers' refresh-ahead behaviour is now covered symmetrically.

Tests only; no source changes. 303 tests, ruff/format/mypy clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 39cca237-1e61-44bd-a185-79aabb5776ae

📥 Commits

Reviewing files that changed from the base of the PR and between 8283017 and caed4e0.

📒 Files selected for processing (2)
  • AGENTS.md
  • tests/unit/test_auth.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • AGENTS.md
  • tests/unit/test_auth.py

📝 Walkthrough

Walkthrough

Authentication tests now validate the token request payload and exercise refresh-margin token acquisition across successive responses, including token validity transitions and HTTP call counts. AGENTS.md documents that both sync and async tests drive the real token acquisition path.

Changes

Authentication test strengthening

Layer / File(s) Summary
Token request contract assertions
tests/unit/test_auth.py
The acquisition test decodes the outgoing form payload and verifies the OAuth2 client-credentials fields.
Refresh-margin acquisition assertions
tests/unit/test_auth.py, AGENTS.md
The test verifies stale-token handling, fresh-token reacquisition, validity transitions, and two HTTP requests; progress notes describe both sync and async tests exercising real token acquisition.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • danieyal/myinvois-python#7: Adds related async token-manager tests for refresh-margin reacquisition and token acquisition paths.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: strengthening the synchronous refresh-margin test so it actually exercises the margin behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/sync-refresh-margin

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@AGENTS.md`:
- Line 131: Update the AGENTS.md lesson about the refresh-margin tests to remove
the incorrect claim that they are verified with refresh_margin=0. Describe that
the sync test uses the default 60-second margin and accurately summarize the
mutation check that confirms the refresh-ahead behavior.

In `@tests/unit/test_auth.py`:
- Around line 121-136: Update the test around mgr.get_token() and the
respx_mock.post route to inspect the captured outbound token requests, asserting
the expected request headers, form payload, and any applicable query parameters
for both refresh calls. Keep the existing stale-to-fresh response and validity
assertions unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fcc2cdc0-ae94-44ab-bc93-04e2bad84b17

📥 Commits

Reviewing files that changed from the base of the PR and between d62fa7a and 8283017.

📒 Files selected for processing (2)
  • AGENTS.md
  • tests/unit/test_auth.py

Comment thread AGENTS.md Outdated
Comment thread tests/unit/test_auth.py
1. AGENTS.md overstated its own verification. It said both refresh-margin
   twins "are verified against refresh_margin=0", which reads as though a
   test in the suite uses that value. None does -- the committed tests use
   the default 60s margin, and refresh_margin=0 was a throwaway scratch
   mutation run that was deleted. Reworded to say what the tests actually do
   and to flag the mutation check as a manual step to repeat if the
   refresh-ahead logic changes.

   Worth fixing carefully: this file spends several sections warning against
   overstated verification claims, so it should not contain one.

2. The reviewer asked for outbound-request assertions on the refresh-margin
   test. The underlying gap is real -- the sync suite never asserted the
   grant payload, a hole noted when the async twin was fixed in #7 -- but the
   margin test is the wrong home for it. That test was just rewritten to do
   exactly one thing: prove a token expiring inside the margin is
   re-acquired. Loading it with request-shape assertions would blur that.

   Added instead to test_token_manager_acquires_token, mirroring
   test_async_auth.py::test_async_token_manager_acquires_token exactly, so
   both suites now pin the grant form in the same place.

   Verified it catches what it exists for: injecting the typo
   "client_credentials" -> "client_credential" into _build_form fails this
   test and ONLY this test, confirming nothing else covered the payload.

Tests and docs only; no source changes. 303 tests, ruff/format/mypy clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@danieyal
danieyal merged commit 5f1ad7b into master Jul 22, 2026
7 checks passed
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