Skip to content

✅ Add tests for OAuth2AuthorizationCodeBearer with auto_error=False#15859

Open
Eikentech wants to merge 2 commits into
fastapi:masterfrom
Eikentech:pr-oauth2-authcode-optional
Open

✅ Add tests for OAuth2AuthorizationCodeBearer with auto_error=False#15859
Eikentech wants to merge 2 commits into
fastapi:masterfrom
Eikentech:pr-oauth2-authcode-optional

Conversation

@Eikentech

@Eikentech Eikentech commented Jun 28, 2026

Copy link
Copy Markdown

Summary

This PR adds the missing optional-mode test (mirroring the existing test_security_oauth2_password_bearer_optional.py) and removes the now-unnecessary # pragma: nocover.

The fix

  1. New test tests/test_security_oauth2_authorization_code_bearer_optional.pyauto_error=False, covering:
    • no Authorization header → returns None (handler returns the "create an account" message),
    • a valid Bearer token → returns the token,
    • a non-Bearer scheme → returns None,
    • the OpenAPI schema (unchanged from the existing auth-code test).
  2. Remove the # pragma: nocover on the return None line, since the branch is now genuinely covered.

Notes

This is a coverage-consistency fix, not a behaviour change — fastapi/security/oauth2.py runtime behaviour is
unchanged; the PR only adds the missing test for an already-documented branch and drops the coverage-exclusion
pragma it was hidden behind.

@codspeed-hq

codspeed-hq Bot commented Jun 28, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing Eikentech:pr-oauth2-authcode-optional (d1c59f7) with master (cecd96d)

Open in CodSpeed

@YuriiMotov YuriiMotov changed the title test: cover OAuth2AuthorizationCodeBearer auto_error=False branch ✅ Add tests for OAuth2AuthorizationCodeBearer with auto_error=False Jun 29, 2026
@YuriiMotov

Copy link
Copy Markdown
Member

@Eikentech, could you please review the PR description and make it shorter?
For now it looks like AI-generated and not properly reviewed by human

@YuriiMotov YuriiMotov added internal Internal changes waiting labels Jun 29, 2026
@Eikentech

Copy link
Copy Markdown
Author

@YuriiMotov , I have updated the PR description, pls check. Thanks

@github-actions github-actions Bot removed the waiting label Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Internal changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants