106 lines
5.0 KiB
Python
106 lines
5.0 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Einstellungen > Gestaltung über die Oberfläche (CI)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from helpers import new_client, token_for
|
|
from PIL import Image
|
|
from test_forms import participant
|
|
from test_mail import admin
|
|
|
|
pytestmark = pytest.mark.db
|
|
|
|
|
|
def png(color: tuple[int, int, int, int] = (31, 95, 174, 255)) -> bytes:
|
|
buf = io.BytesIO()
|
|
Image.new("RGBA", (300, 100), color).save(buf, "PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def save(client: Any, **fields: str) -> Any:
|
|
data = {"name": "Feuerwehr Test", "mode": "auto", "primary": "#c00000",
|
|
"accent": "#ffcc00", "background": "#f6f7f9", "text": "#1c1e21",
|
|
"background_default": "on", "text_default": "on", **fields}
|
|
return client.post("/admin/appearance",
|
|
data={"csrf_token": token_for(client, "/admin/appearance"), **data})
|
|
|
|
|
|
def test_name_colours_and_footer(app: Any) -> None:
|
|
root = admin(app)
|
|
guest = new_client(app)
|
|
assert "/branding/theme.css" not in guest.get("/login").text
|
|
resp = save(root, imprint_url="https://fw.example/impressum",
|
|
privacy_url="https://fw.example/datenschutz", footer_text="Freiwillige <b>",
|
|
welcome="Hallo <i>Welt</i>")
|
|
assert resp.status_code == 303 and "settings_saved" in resp.headers["location"]
|
|
page = guest.get("/login").text
|
|
assert "Sign in · Feuerwehr Test" in page and "/branding/theme.css?v=" in page
|
|
assert 'href="https://fw.example/impressum"' in page and "Legal notice" in page
|
|
assert "Freiwillige <b>" in page
|
|
assert "Hallo <i>Welt</i>" in guest.get("/").text
|
|
css = guest.get("/branding/theme.css")
|
|
assert css.status_code == 200 and css.headers["content-type"].startswith("text/css")
|
|
assert "--sb-primary:#c00000" in css.text and "--sb-accent:#ffcc00" in css.text
|
|
assert "--sb-bg:#f6f7f9" in css.text # Standard angehakt: unverändert
|
|
# Gelbe Akzentfarbe: zu wenig Kontrast, Warnung ohne Speicherverbot
|
|
assert "accent colour has too little contrast" in root.get("/admin/appearance").text
|
|
|
|
|
|
def test_invalid_input_is_not_saved(app: Any) -> None:
|
|
root = admin(app)
|
|
resp = save(root, name="", imprint_url="javascript:alert(1)")
|
|
assert resp.status_code == 400
|
|
assert "Please enter a name" in resp.text and "must start with https://" in resp.text
|
|
assert "/branding/theme.css" not in new_client(app).get("/login").text
|
|
|
|
|
|
def test_font_export_import_and_reset(app: Any) -> None:
|
|
root = admin(app)
|
|
save(root, mode="dark", css=".brand-name { letter-spacing: 1px }")
|
|
csrf = token_for(root, "/admin/appearance")
|
|
resp = root.post("/admin/appearance/font", data={"csrf_token": csrf},
|
|
files={"font": ("x.svg", b"<svg/>", "image/svg+xml")})
|
|
assert "error=font" in resp.headers["location"]
|
|
csrf = token_for(root, "/admin/appearance")
|
|
root.post("/admin/appearance/font", data={"csrf_token": csrf},
|
|
files={"font": ("a.woff2", b"wOF2" + b"\0" * 64, "font/woff2")})
|
|
guest = new_client(app)
|
|
css = guest.get("/branding/theme.css").text
|
|
assert "@font-face" in css and "color-scheme:dark" in css and "letter-spacing" in css
|
|
assert guest.get("/branding/font").headers["content-type"] == "font/woff2"
|
|
csrf = token_for(root, "/admin/settings")
|
|
root.post("/admin/settings/logo", data={"csrf_token": csrf},
|
|
files={"logo": ("logo.png", png(), "image/png")})
|
|
csrf = token_for(root, "/admin/appearance")
|
|
root.post("/admin/settings/logo", data={"csrf_token": csrf, "variant": "dark"},
|
|
files={"logo": ("dark.png", png((255, 255, 255, 255)), "image/png")})
|
|
assert "/branding/logo-dark.png?v=" in guest.get("/login").text
|
|
exported = root.get("/admin/appearance/export")
|
|
assert exported.status_code == 200 and "surveybox-appearance" in exported.text
|
|
# Zurücksetzen, dann Import stellt alles wieder her
|
|
csrf = token_for(root, "/admin/appearance")
|
|
root.post("/admin/appearance/reset", data={"csrf_token": csrf})
|
|
assert "color-scheme:dark" not in guest.get("/branding/theme.css").text
|
|
csrf = token_for(root, "/admin/appearance")
|
|
resp = root.post("/admin/appearance/import", data={"csrf_token": csrf},
|
|
files={"design": ("d.json", exported.content, "application/json")})
|
|
assert resp.status_code == 303 and "settings_saved" in resp.headers["location"]
|
|
assert "color-scheme:dark" in guest.get("/branding/theme.css").text
|
|
csrf = token_for(root, "/admin/appearance")
|
|
resp = root.post("/admin/appearance/import", data={"csrf_token": csrf},
|
|
files={"design": ("d.json", b'{"format": "x"}', "application/json")})
|
|
assert "error=import" in resp.headers["location"]
|
|
|
|
|
|
def test_only_admins(app: Any) -> None:
|
|
paul = participant(app)
|
|
assert paul.get("/admin/appearance").status_code in (303, 403)
|
|
assert new_client(app).get("/admin/appearance/export").status_code in (303, 401, 403)
|