T13.11: session limit defaults to 8 hours (D25)
This commit is contained in:
66
tests/session_default_check.py
Normal file
66
tests/session_default_check.py
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Is the session limit 8 hours by default? - D25 / T13.11.
|
||||
|
||||
The default of AUTH_SESSION_HOURS is 8. The token expiry and the cookie max-age both
|
||||
follow it, a set value overrides it, and the shipped files name the same default.
|
||||
|
||||
Self-contained: no network, no server, no browser. Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import importlib
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
os.environ["DATABASE_URL"] = "sqlite://"
|
||||
os.environ["AUTH_SECRET_KEY"] = "session-default-check-secret-0123456789abcdef"
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
def load(value):
|
||||
if value is None:
|
||||
os.environ.pop("AUTH_SESSION_HOURS", None)
|
||||
else:
|
||||
os.environ["AUTH_SESSION_HOURS"] = value
|
||||
from server import auth
|
||||
return importlib.reload(auth)
|
||||
|
||||
|
||||
import jwt # noqa: E402
|
||||
|
||||
user = SimpleNamespace(id="u1", username="a", role="project_user", token_version=0)
|
||||
|
||||
auth = load(None)
|
||||
chk("default is 8 hours", auth.SESSION_HOURS == 8, str(auth.SESSION_HOURS))
|
||||
claims = jwt.decode(auth.create_token(user), auth.SECRET_KEY, algorithms=["HS256"])
|
||||
hours = (datetime.fromtimestamp(claims["exp"], timezone.utc) - datetime.fromtimestamp(claims["iat"], timezone.utc)).total_seconds() / 3600
|
||||
chk("a new token lasts 8 hours", abs(hours - 8) < 0.01, str(hours))
|
||||
|
||||
auth = load("2")
|
||||
chk("AUTH_SESSION_HOURS=2 overrides the default", auth.SESSION_HOURS == 2)
|
||||
auth = load("12")
|
||||
chk("AUTH_SESSION_HOURS=12 overrides the default", auth.SESSION_HOURS == 12)
|
||||
load(None)
|
||||
|
||||
def read(p):
|
||||
return open(os.path.join(ROOT, p), encoding="utf-8").read()
|
||||
|
||||
chk("docker-compose.yml defaults to 8", "AUTH_SESSION_HOURS: ${AUTH_SESSION_HOURS:-8}" in read("docker-compose.yml"))
|
||||
chk(".env.example says 8", re.search(r"^# AUTH_SESSION_HOURS=8$", read("server/.env.example"), re.M) is not None)
|
||||
chk("DEPLOY-login-portal.md says 8", "defaults to 8" in read("DEPLOY-login-portal.md"))
|
||||
for p in ("docker-compose.yml", "server/.env.example", "DEPLOY-login-portal.md", "server/auth.py"):
|
||||
chk(f"no leftover default of 12 in {p}", not re.search(r"AUTH_SESSION_HOURS[^\n]{0,30}\b12\b", read(p)))
|
||||
|
||||
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
|
||||
sys.exit(1 if fails else 0)
|
||||
Reference in New Issue
Block a user