test: the cookie signer and test modules read the never-back clock too
The docker VM's clock steps back: itsdangerous refused a cookie signed in the next second (a random 401 right after a sign-in), and a test compared an app stamp with the raw wall clock.
This commit is contained in:
@@ -337,20 +337,45 @@ class _NeverBack(_dt.datetime):
|
||||
return t
|
||||
|
||||
|
||||
_last_signature_second = [0]
|
||||
|
||||
|
||||
def _signature_second_never_back(self) -> int:
|
||||
"""The session cookie's signing clock (itsdangerous, whole seconds), never going back: it refuses
|
||||
a signature from the future, so a step back across a second boundary refused the cookie a
|
||||
sign-in had just set. Read through the module at call time, so a probe can step it."""
|
||||
import itsdangerous.timed
|
||||
|
||||
t = max(int(itsdangerous.timed.time.time()), _last_signature_second[0])
|
||||
_last_signature_second[0] = t
|
||||
return t
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clock_never_goes_back(monkeypatch):
|
||||
"""Rows a test makes one after another are stamped in that order. The wall clock of the docker
|
||||
VM the suite runs in steps BACK now and then (measured: 2 steps in 20 s, up to 0.5 s), and a
|
||||
test that makes three files and reads them newest first then got them out of order. Every loaded
|
||||
`app.*` module that took `datetime` from the datetime module reads this clock instead (a sweep,
|
||||
as the limiter reset below does, so a new module is covered the day it lands). A database
|
||||
as the limiter reset below does, so a new module is covered the day it lands) — and so does every
|
||||
test module, so a test comparing a stamp the app wrote with "now" reads the same clock. The
|
||||
session cookie's signature has a clock of its own (`_signature_second_never_back`). A database
|
||||
default (`server_default=func.now()`) is Postgres's clock and is not covered: a test that orders
|
||||
such rows made within a second of each other gives them distinct times itself."""
|
||||
import sys
|
||||
|
||||
from itsdangerous import TimestampSigner
|
||||
|
||||
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
for name, mod in list(sys.modules.items()):
|
||||
if (name == "app" or name.startswith("app.")) and vars(mod).get("datetime") is _dt.datetime:
|
||||
if vars(mod).get("datetime") is not _dt.datetime:
|
||||
continue
|
||||
mod_file = getattr(mod, "__file__", None) or ""
|
||||
if name == "app" or name.startswith("app.") or mod_file.startswith(tests_dir + os.sep):
|
||||
monkeypatch.setattr(mod, "datetime", _NeverBack)
|
||||
# Per test: a cookie never outlives its test, and a probe's stepped clock must not carry over.
|
||||
_last_signature_second[0] = 0
|
||||
monkeypatch.setattr(TimestampSigner, "get_timestamp", _signature_second_never_back)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
||||
Reference in New Issue
Block a user