114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
import pytest
|
|
from pathlib import Path
|
|
|
|
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 auth_client_with_server(tmp_path, monkeypatch):
|
|
db_url = f"sqlite:///{tmp_path/'pages.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()
|
|
|
|
session.add(Server(user_id=user.id, blueprint_id=blueprint.id, name="alpha", port=27015))
|
|
session.flush()
|
|
user_id = user.id
|
|
|
|
client = app.test_client()
|
|
with client.session_transaction() as sess:
|
|
sess["user_id"] = user_id
|
|
return client
|
|
|
|
|
|
@pytest.fixture
|
|
def user_client_and_other_blueprint(tmp_path, monkeypatch):
|
|
db_url = f"sqlite:///{tmp_path/'otherbp.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:
|
|
owner = User(username="owner", password_digest=hash_password("secret"), admin=False)
|
|
other = User(username="other", password_digest=hash_password("secret"), admin=False)
|
|
session.add_all([owner, other])
|
|
session.flush()
|
|
|
|
blueprint = Blueprint(user_id=other.id, name="private", arguments="[]", config="[]")
|
|
session.add(blueprint)
|
|
session.flush()
|
|
|
|
owner_id = owner.id
|
|
blueprint_id = blueprint.id
|
|
|
|
client = app.test_client()
|
|
with client.session_transaction() as sess:
|
|
sess["user_id"] = owner_id
|
|
|
|
return client, blueprint_id
|
|
|
|
|
|
def test_dashboard_is_simple_landing_page(auth_client_with_server) -> None:
|
|
response = auth_client_with_server.get("/dashboard")
|
|
text = response.get_data(as_text=True)
|
|
|
|
assert response.status_code == 200
|
|
assert "Dashboard" in text
|
|
assert "Use the navigation to manage servers, blueprints, and overlays." in text
|
|
assert "alpha" not in text
|
|
|
|
|
|
def test_shell_nav_uses_main_sections(auth_client_with_server) -> None:
|
|
response = auth_client_with_server.get("/dashboard")
|
|
text = response.get_data(as_text=True)
|
|
|
|
assert 'href="/dashboard"' in text
|
|
assert 'href="/servers"' in text
|
|
assert 'href="/blueprints"' in text
|
|
assert 'href="/overlays"' in text
|
|
assert 'action="/logout"' in text
|
|
assert "csrf_token" in text
|
|
|
|
|
|
def test_css_tokens_define_neutral_light_and_dark_theme() -> None:
|
|
css = Path("l4d2web/static/css/tokens.css").read_text()
|
|
|
|
for token in [
|
|
"--color-bg",
|
|
"--color-surface",
|
|
"--color-text",
|
|
"--color-muted",
|
|
"--color-border",
|
|
"--color-link",
|
|
"--space-base",
|
|
"--space-xs",
|
|
"--space-s",
|
|
"--space-m",
|
|
"--space-l",
|
|
"--space-xl",
|
|
"--space-2xl",
|
|
"--radius-s",
|
|
"--radius-m",
|
|
"--line",
|
|
]:
|
|
assert token in css
|
|
assert "prefers-color-scheme: dark" in css
|
|
assert "radial-gradient" not in Path("l4d2web/static/css/layout.css").read_text()
|
|
|
|
|
|
def test_blueprint_page_private_visibility(user_client_and_other_blueprint) -> None:
|
|
client, blueprint_id = user_client_and_other_blueprint
|
|
response = client.get(f"/blueprints/{blueprint_id}")
|
|
assert response.status_code == 403
|