load_current_user now treats a session whose pw_changed_at marker is missing, malformed, or older than the user's current password_changed_at as logged-out. Same shape as the existing user.active check. Forced fan-out updates to every test fixture that forges a session via session_transaction(): each now stamps a current pw_changed_at marker. test_deactivated_user_existing_session_invalidated keeps its meaning — the deactivation still flips the user to inactive, and load_current_user rejects the session via the user.active branch before reaching the freshness branch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
import pytest
|
|
from datetime import UTC, datetime
|
|
|
|
from l4d2web.app import create_app
|
|
from l4d2web.auth import hash_password
|
|
from l4d2web.db import init_db, session_scope
|
|
from l4d2web.models import Blueprint, Server, User
|
|
|
|
|
|
@pytest.fixture
|
|
def owner_client_with_server(tmp_path, monkeypatch):
|
|
db_url = f"sqlite:///{tmp_path/'status.db'}"
|
|
monkeypatch.setenv("DATABASE_URL", db_url)
|
|
app = create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"})
|
|
init_db()
|
|
|
|
with session_scope() as session:
|
|
user = User(username="alice", password_digest=hash_password("secret"), admin=False)
|
|
session.add(user)
|
|
session.flush()
|
|
|
|
blueprint = Blueprint(user_id=user.id, name="default", arguments="[]", config="[]")
|
|
session.add(blueprint)
|
|
session.flush()
|
|
|
|
server = Server(user_id=user.id, blueprint_id=blueprint.id, name="alpha", port=27015)
|
|
session.add(server)
|
|
session.flush()
|
|
|
|
user_id = user.id
|
|
server_id = server.id
|
|
|
|
client = app.test_client()
|
|
with client.session_transaction() as sess:
|
|
sess["user_id"] = user_id
|
|
sess["pw_changed_at"] = datetime.now(UTC).isoformat()
|
|
|
|
return client, server_id
|
|
|
|
|
|
def test_owner_can_stream_server_logs(owner_client_with_server, monkeypatch) -> None:
|
|
client, server_id = owner_client_with_server
|
|
|
|
monkeypatch.setattr(
|
|
"l4d2web.services.l4d2_facade.stream_server_logs",
|
|
lambda name, lines=200, follow=True: iter(["first", "second"]),
|
|
)
|
|
|
|
response = client.get(f"/servers/{server_id}/logs/stream")
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_log_stream_translates_heartbeat_to_sse_keepalive(owner_client_with_server, monkeypatch) -> None:
|
|
client, server_id = owner_client_with_server
|
|
|
|
monkeypatch.setattr(
|
|
"l4d2web.services.l4d2_facade.stream_server_logs",
|
|
lambda name, lines=200, follow=True: iter(["first", "", "second"]),
|
|
)
|
|
|
|
response = client.get(f"/servers/{server_id}/logs/stream")
|
|
assert response.status_code == 200
|
|
body = response.get_data(as_text=True)
|
|
assert "data: first\n\n" in body
|
|
assert "data: second\n\n" in body
|
|
assert ": keepalive\n\n" in body
|
|
assert "data: \n\n" not in body
|
|
|
|
|
|
def test_status_precedence() -> None:
|
|
from l4d2web.services.status import compute_display_state
|
|
|
|
assert compute_display_state("start", "stopped") == "starting"
|