Skip to content

Commit d5e2988

Browse files
fix: set oauth2 auto_error=False so IP whitelist works without token
OAuth2PasswordBearer with auto_error=True rejected tokenless requests before ip_whitelist_or_auth could check the client IP. Set auto_error to always be False so localhost requests bypass auth as intended. Remove test for deleted scheduler.py and clean up dev mode test that no longer needs to patch auto_error. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 99d0b63 commit d5e2988

2 files changed

Lines changed: 2 additions & 16 deletions

File tree

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class UserInDB(User):
168168
hashed_password: str
169169

170170

171-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=not DEV)
171+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=False)
172172

173173

174174
def verify_password(plain_password, hashed_password):

tests/test_unit.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,14 @@ def test_get_current_schedule(test_client, auth_headers):
422422

423423
@pytest.mark.unit
424424
def test_dev_mode_bypasses_auth_for_local_requests(raw_test_client):
425-
"""When DEV=True, localhost requests should not require authentication.
426-
427-
oauth2_scheme is constructed at import time with auto_error=not DEV.
428-
Since DEV is False during test collection, auto_error is True and the
429-
scheme rejects tokenless requests before ip_whitelist_or_auth runs.
430-
We must also patch auto_error on the live instance.
431-
"""
432-
from main import oauth2_scheme
425+
"""When DEV=True, localhost requests should not require authentication."""
433426

434427
mock_schedule_obj = MagicMock(
435428
day="Monday", schedule_time="10:00", enabled=True, snooze_until=None, original_schedule_time="10:00"
436429
)
437430

438431
with (
439432
patch('main.DEV', True),
440-
patch.object(oauth2_scheme, 'auto_error', False),
441433
patch('main.check_and_revert_snooze'),
442434
patch('main.get_schedule', return_value=mock_schedule_obj),
443435
patch('main.db_session') as mock_db_sess,
@@ -859,12 +851,6 @@ def test_no_requests_import_in_sign_jwt(self):
859851
assert "import requests" not in source, "sign_jwt.py still imports requests"
860852
assert "import httpx" in source, "sign_jwt.py should import httpx"
861853

862-
def test_no_requests_import_in_scheduler(self):
863-
"""scheduler should not import the requests library."""
864-
source = (Path(__file__).resolve().parent.parent / "app" / "scheduler.py").read_text()
865-
assert "import requests" not in source, "scheduler.py still imports requests"
866-
assert "import httpx" in source, "scheduler.py should import httpx"
867-
868854
def test_get_access_token_uses_httpx_client(self):
869855
"""get_access_token function body should use httpx.Client."""
870856
source = (Path(__file__).resolve().parent.parent / "app" / "sign_jwt.py").read_text()

0 commit comments

Comments
 (0)