test: fix shared state leak and fragile expiry assertion in oauth tests

This commit is contained in:
2026-05-08 13:30:16 -04:00
parent dbb779b013
commit 82cbce9615
2 changed files with 13 additions and 4 deletions
+12 -4
View File
@@ -1,3 +1,7 @@
import time as time_mod
import pytest
from hbd.server import oauth
@@ -14,6 +18,13 @@ CFG_ON = {
CFG_PARTIAL = {"oauth": {"gitea": {"url": "https://git.example.com"}}}
@pytest.fixture(autouse=True)
def clear_oauth_states():
oauth._states.clear()
yield
oauth._states.clear()
def test_is_enabled_when_all_keys_present():
assert oauth.is_enabled(CFG_ON) is True
@@ -26,9 +37,6 @@ def test_is_enabled_false_when_partial_config():
assert oauth.is_enabled(CFG_PARTIAL) is False
import time as time_mod
def test_make_state_returns_unique_tokens():
s1 = oauth.make_state()
s2 = oauth.make_state()
@@ -54,5 +62,5 @@ def test_validate_state_unknown():
def test_validate_state_expired(monkeypatch):
state = oauth.make_state()
# Wind expiry into the past
monkeypatch.setitem(oauth._states, state, time_mod.time() - 1)
monkeypatch.setitem(oauth._states, state, time_mod.time() - 1000)
assert oauth.validate_state(state) is False