Keep login sessions persistent across browser restarts

This commit is contained in:
Peter Stockings
2026-10-22 23:07:46 +11:00
parent d660b86bb0
commit b8eb6d10f8
4 changed files with 45 additions and 2 deletions
+30
View File
@@ -69,6 +69,36 @@ def login(client, name="runner"):
return response
def test_login_persists_and_logout_clears_cookie(app, monkeypatch):
import time
client = app.test_client()
response = login(client)
assert "Expires=" in response.headers["Set-Cookie"]
cookie = client.get_cookie(app.config["SESSION_COOKIE_NAME"])
restarted = app.test_client()
restarted.set_cookie(cookie.key, cookie.value)
now = time.time()
monkeypatch.setattr(time, "time", lambda: now + 400 * 86400)
response = restarted.get("/dashboard")
assert response.status_code == 200
assert "Expires=" in response.headers["Set-Cookie"]
assert restarted.post("/logout").status_code == 302
assert restarted.get_cookie(cookie.key) is None
assert restarted.get("/dashboard").status_code == 302
def test_existing_login_becomes_persistent(app):
client = app.test_client()
with client.session_transaction() as session:
session["_user_id"] = "1"
response = client.get("/dashboard")
assert response.status_code == 200
assert "Expires=" in response.headers["Set-Cookie"]
with client.session_transaction() as session:
assert session.permanent
def add_history(count, visibility="private"):
from datetime import timedelta