Skip to content

test(auth): cover non-401 token errors and expiry boundaries - #432

Merged
emilyoram merged 3 commits into
mainfrom
test/427-token-endpoint-negative-cases
Nov 23, 2026
Merged

emilyoram merged 3 commits into
mainfrom
test/427-token-endpoint-negative-cases

Conversation

@claude

@claude claude Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Joshua Wulf · Slack thread

Before: The 401 path was well covered, but no test ever made the token endpoint return a 5xx, fail at the transport, or answer 200 with a body that cannot yield a token — and no test ever moved the clock. The raise_for_status branch, the "missing access_token or expires_in" branch, the skew calculation and the 401 cooldown were unasserted in both the sync and the async provider. The two Response(500, ...) occurrences in the file are handler_should_not_run guards; they assert the endpoint is not called and never reach the error path.

After: Every one of those paths is driven and its consequence asserted, for both providers, deterministically.

Twenty-six new tests close the negative-case baseline for token-endpoint errors and expiry boundaries.

How: httpx.MockTransport for the failures, and the providers' already-injectable Clock for the timing, so nothing sleeps and nothing depends on wall-clock time.

  • Non-401 failures — a 500, a 503 and a transport error. The assertion is not that something was raised but what must not happen: the request is retried rather than memoized, and no tarpit file is written. A transient outage must not lock the process out of its own credentials.
  • Malformed 200 bodies — missing access_token, missing expires_in, empty body, empty token, zero lifetime. Each is rejected, and nothing is cached in memory or on disk.
  • Expiry boundaries — a token reused at expires_at - 1 and refetched at expires_at + 1, where expires_at is now + 3600 - max(30, 5%); and a 20-second lifetime, shorter than the 30-second skew floor, clamping to now so the token is never treated as valid, rather than landing in the past and being the same behaviour by accident.
  • The on-disk cache at that boundary — an expired entry replaced by a fresh fetch; an expired entry unlinked even when the refetch fails, so a stale token cannot be served after a restart; and a still-valid entry served without any request at all.
  • The 401 cooldown — asserted on both sides of its boundary: still memoized at 29s, endpoint retried at 31s.

These are tests for a gap, not a fix, so there is no production change and no red/green pair. Instead I checked that they bite, by mutating the runtime and confirming the failures (all reverted): memoizing every status rather than only 401 fails the two non-401 tests; removing the skew fails the expiry tests; and removing the max(0.0, ...) clamp fails the two clamp tests.

After Copilot's review the clamp test now asserts the stored expiry equals the clock's current time, rather than only that every call refetched — which an expiry in the past also satisfies — and the clamp and the three on-disk cache cases gained async counterparts, since the async provider has its own expiry calculation and its own cache loading.

Verification on Python 3.13 (make generate-only, then the Makefile validation steps): ruff check ., ty check and ty check examples/ are clean; pytest tests/acceptance is 868 passed, 1 skipped (842 before). No generated/ or stubs/ change, so this is a single commit.

Known overlap: none in code. #425 adds a separate sentinel-secret file that drives some of the same auth paths for a different purpose (non-disclosure, not behaviour); the two touch different files and can merge in any order.

Closes #427

🤖 Generated with Claude Code

https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

The 401 path was well covered; nothing drove a 5xx, a transport error, or a 200
whose body cannot yield a token, and nothing ever moved the clock. So the
raise_for_status branch, the "missing access_token or expires_in" branch, the
skew calculation and the 401 cooldown were all unasserted in both providers.

These add, for the sync and async providers: a 5xx and a transport error, each
asserted to be retried rather than memoized and to leave no tarpit file behind,
which is the distinction that matters for a non-401; five malformed 200 bodies,
asserted to cache nothing in memory or on disk; and, driven through the injected
clock so they are deterministic, reuse just before the skew-adjusted expiry and
a refetch just after, the clamp that keeps a lifetime shorter than the skew from
landing in the past, an expired file-cached entry replaced and unlinked even when
the refetch fails, a still-valid entry served without a request, and the 401
cooldown on both sides of its boundary.

Checked that they bite: memoizing every status, not only 401, fails the non-401
tests; removing the skew fails the expiry tests.

Closes #427

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

Copilot AI 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.

🟡 Changes recommended

Async expiry and disk-cache behavior lack test coverage, and the short-lifetime test does not verify that the stored expiry is clamped to the current time.

2 open issues
What changed in this PR

Extends the SDK’s OAuth acceptance tests to address the coverage gaps in #427, without changing production code.

Changes:

  • Tests non-401 HTTP errors, transport failures, and malformed token responses for sync and async providers.
  • Uses ManualClock to check token reuse, refresh, and 401 cooldown timing without sleeps.
  • Adds sync-provider tests for short token lifetimes and disk-cache expiry, deletion, and reuse.
File Description
tests/​acceptance/​test_pluggable_auth.py Adds provider helpers, mocked token responses, and deterministic failure and expiry tests.

Review effort: Balanced

Comment thread tests/acceptance/test_pluggable_auth.py Resolved
Comment thread tests/acceptance/test_pluggable_auth.py Resolved
@claude
claude Bot marked this pull request as ready for review November 13, 2026 06:47
@claude
claude Bot requested a review from a team as a code owner November 13, 2026 06:47
@claude
claude Bot marked this pull request as draft November 13, 2026 06:53
…c provider

From Copilot's review. The short-lifetime test asserted only that every call
refetched, which removing max(0.0, ...) also satisfies -- an expiry ten seconds
in the past looks identical from the outside. It now asserts the stored expiry
equals the clock's current time, so the clamp the test is named after is what it
checks; both sync and async fail against that mutant.

The clamp and the three on-disk cache cases were sync-only, although the async
provider has its own expiry calculation and its own cache loading. All four now
have async counterparts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
@claude
claude Bot marked this pull request as ready for review November 13, 2026 07:19
@claude
claude Bot marked this pull request as draft November 13, 2026 07:25
@claude
claude Bot marked this pull request as ready for review November 13, 2026 07:33
@claude
claude Bot requested a balanced review from Copilot November 13, 2026 08:49

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the added OAuth token-failure, expiry, and cache acceptance coverage.

No open issues 🎉

Review effort: Balanced

Resolve the add/add conflict at the end of
tests/acceptance/test_pluggable_auth.py: keep this branch's
token-endpoint failure and expiry-boundary cases, and main's OAuth
User-Agent cases from #477. The two sets are independent; no test
was changed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

Copilot AI 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.

🟢 Approval recommended

There are no active review issues with the OAuth acceptance-test additions.

No open issues 🎉

Review effort: Balanced

@emilyoram
emilyoram merged commit a2da089 into main Nov 23, 2026
27 checks passed
@jwulf jwulf added backport stable/10 Backport to stable/10 after merge backport stable/9 Backport to stable/9 after merge labels Nov 30, 2026
@camunda-sdk-automation

Copy link
Copy Markdown
Contributor

Successfully created backport PR for stable/10:

@camunda-sdk-automation

Copy link
Copy Markdown
Contributor

Successfully created backport PR for stable/9:

jwulf pushed a commit that referenced this pull request Nov 30, 2026
…535)

* test(auth): cover non-401 token errors and expiry boundaries

The 401 path was well covered; nothing drove a 5xx, a transport error, or a 200
whose body cannot yield a token, and nothing ever moved the clock. So the
raise_for_status branch, the "missing access_token or expires_in" branch, the
skew calculation and the 401 cooldown were all unasserted in both providers.

These add, for the sync and async providers: a 5xx and a transport error, each
asserted to be retried rather than memoized and to leave no tarpit file behind,
which is the distinction that matters for a non-401; five malformed 200 bodies,
asserted to cache nothing in memory or on disk; and, driven through the injected
clock so they are deterministic, reuse just before the skew-adjusted expiry and
a refetch just after, the clamp that keeps a lifetime shorter than the skew from
landing in the past, an expired file-cached entry replaced and unlinked even when
the refetch fails, a still-valid entry served without a request, and the 401
cooldown on both sides of its boundary.

Checked that they bite: memoizing every status, not only 401, fails the non-401
tests; removing the skew fails the expiry tests.

Closes #427


Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

* chore(auth): pin the clamped expiry, and mirror the cases on the async provider

From Copilot's review. The short-lifetime test asserted only that every call
refetched, which removing max(0.0, ...) also satisfies -- an expiry ten seconds
in the past looks identical from the outside. It now asserts the stored expiry
equals the clock's current time, so the clamp the test is named after is what it
checks; both sync and async fail against that mutant.

The clamp and the three on-disk cache cases were sync-only, although the async
provider has its own expiry calculation and its own cache loading. All four now
have async counterparts.


Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

---------


(cherry picked from commit a2da089)

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
jwulf added a commit that referenced this pull request Nov 30, 2026
…#432 to stable/9) (#536)

* test(auth): cover non-401 token errors and expiry boundaries (#432)

* test(auth): cover non-401 token errors and expiry boundaries

The 401 path was well covered; nothing drove a 5xx, a transport error, or a 200
whose body cannot yield a token, and nothing ever moved the clock. So the
raise_for_status branch, the "missing access_token or expires_in" branch, the
skew calculation and the 401 cooldown were all unasserted in both providers.

These add, for the sync and async providers: a 5xx and a transport error, each
asserted to be retried rather than memoized and to leave no tarpit file behind,
which is the distinction that matters for a non-401; five malformed 200 bodies,
asserted to cache nothing in memory or on disk; and, driven through the injected
clock so they are deterministic, reuse just before the skew-adjusted expiry and
a refetch just after, the clamp that keeps a lifetime shorter than the skew from
landing in the past, an expired file-cached entry replaced and unlinked even when
the refetch fails, a still-valid entry served without a request, and the 401
cooldown on both sides of its boundary.

Checked that they bite: memoizing every status, not only 401, fails the non-401
tests; removing the skew fails the expiry tests.

Closes #427

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

* chore(auth): pin the clamped expiry, and mirror the cases on the async provider

From Copilot's review. The short-lifetime test asserted only that every call
refetched, which removing max(0.0, ...) also satisfies -- an expiry ten seconds
in the past looks identical from the outside. It now asserts the stored expiry
equals the clock's current time, so the clamp the test is named after is what it
checks; both sync and async fail against that mutant.

The clamp and the three on-disk cache cases were sync-only, although the async
provider has its own expiry calculation and its own cache loading. All four now
have async counterparts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda

---------

Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit a2da089)

* test: use test-local clock in OAuth backport tests (stable/9 lacks runtime clock)

Signed-off-by: Josh Wulf <josh.wulf@camunda.com>

---------

Signed-off-by: Josh Wulf <josh.wulf@camunda.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Josh Wulf <josh.wulf@camunda.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport stable/9 Backport to stable/9 after merge backport stable/10 Backport to stable/10 after merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(auth): no negative-case coverage for non-401 token-endpoint failures or token expiry boundaries

4 participants