feat: správa poznávaček, optimalizace fotek do WebP a úložiště
This commit is contained in:
1 parent
7a8dccb2f7
commit
c46bb74386
27 files changed
+4367
-5
No files matched your search
@@ -0,0 +1,163 @@
|
||||
"""Testy přístupem řízeného výdeje médií /media/<sada_id>/<soubor>."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import sqlite3
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
from PIL import Image
|
||||
|
||||
from poznavacky import admin_sets
|
||||
from poznavacky.security import hash_password
|
||||
|
||||
_CSRF_RE = re.compile(r'name="csrf_token"\s+value="([^"]+)"')
|
||||
|
||||
|
||||
def _insert_user(
|
||||
db: sqlite3.Connection,
|
||||
*,
|
||||
email: str,
|
||||
password: str,
|
||||
role: str,
|
||||
stav: str = "schvaleny",
|
||||
) -> None:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO uzivatele (
|
||||
email, heslo_hash, jmeno, prijmeni, role, stav
|
||||
) VALUES (?, ?, 'Jana', 'Testova', ?, ?)
|
||||
""",
|
||||
(email, hash_password(password), role, stav),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def _login(client: FlaskClient, email: str, password: str) -> None:
|
||||
resp = client.get("/prihlaseni")
|
||||
match = _CSRF_RE.search(resp.get_data(as_text=True))
|
||||
assert match is not None
|
||||
login_resp = client.post(
|
||||
"/prihlaseni",
|
||||
data={"csrf_token": match.group(1), "email": email, "heslo": password},
|
||||
)
|
||||
assert login_resp.status_code == 302
|
||||
|
||||
|
||||
def _make_png() -> bytes:
|
||||
buf = BytesIO()
|
||||
Image.new("RGB", (200, 150), (10, 90, 180)).save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def test_media_delivery_permissions_headers_and_traversal_protection(
|
||||
app: Flask,
|
||||
db: sqlite3.Connection,
|
||||
) -> None:
|
||||
_insert_user(
|
||||
db,
|
||||
email="ucitelka@example.test",
|
||||
password="heslo-ucitelka",
|
||||
role="ucitelka",
|
||||
)
|
||||
_insert_user(
|
||||
db,
|
||||
email="zak@example.test",
|
||||
password="heslo-zak",
|
||||
role="zak",
|
||||
)
|
||||
|
||||
cur_pub = db.execute(
|
||||
"""
|
||||
INSERT INTO sady (slug, nazev, stav)
|
||||
VALUES ('pub', 'Pub', 'publikovano')
|
||||
"""
|
||||
)
|
||||
pub_set_id = int(cur_pub.lastrowid)
|
||||
cur_pub_item = db.execute(
|
||||
"INSERT INTO polozky (sada_id, nazev) VALUES (?, 'Položka P')",
|
||||
(pub_set_id,),
|
||||
)
|
||||
pub_item_id = int(cur_pub_item.lastrowid)
|
||||
|
||||
cur_draft = db.execute(
|
||||
"""
|
||||
INSERT INTO sady (slug, nazev, stav)
|
||||
VALUES ('draft', 'Draft', 'koncept')
|
||||
"""
|
||||
)
|
||||
draft_set_id = int(cur_draft.lastrowid)
|
||||
cur_draft_item = db.execute(
|
||||
"INSERT INTO polozky (sada_id, nazev) VALUES (?, 'Položka K')",
|
||||
(draft_set_id,),
|
||||
)
|
||||
draft_item_id = int(cur_draft_item.lastrowid)
|
||||
db.commit()
|
||||
|
||||
media_dir = Path(app.config["MEDIA_DIR"])
|
||||
_, pub_photo = admin_sets.save_photo_for_item(
|
||||
db,
|
||||
media_dir,
|
||||
pub_set_id,
|
||||
pub_item_id,
|
||||
_make_png(),
|
||||
)
|
||||
_, draft_photo = admin_sets.save_photo_for_item(
|
||||
db,
|
||||
media_dir,
|
||||
draft_set_id,
|
||||
draft_item_id,
|
||||
_make_png(),
|
||||
)
|
||||
|
||||
anon_client = app.test_client()
|
||||
student_client = app.test_client()
|
||||
teacher_client = app.test_client()
|
||||
_login(student_client, "zak@example.test", "heslo-zak")
|
||||
_login(teacher_client, "ucitelka@example.test", "heslo-ucitelka")
|
||||
|
||||
# Publikovaná sada -> veřejně dostupná (plná i náhled).
|
||||
for rel in (pub_photo.soubor_plny, pub_photo.soubor_nahled):
|
||||
with anon_client.get(f"/media/{rel}") as resp:
|
||||
assert resp.status_code == 200
|
||||
assert resp.mimetype == "image/webp"
|
||||
assert "public" in resp.headers["Cache-Control"]
|
||||
assert "immutable" in resp.headers["Cache-Control"]
|
||||
assert resp.data == (media_dir / rel).read_bytes()
|
||||
|
||||
# Koncept -> anonym i žák dostanou 404, učitelka dostane 200 + private.
|
||||
draft_url = f"/media/{draft_photo.soubor_plny}"
|
||||
assert anon_client.get(draft_url).status_code == 404
|
||||
assert student_client.get(draft_url).status_code == 404
|
||||
|
||||
with teacher_client.get(draft_url) as teacher_resp:
|
||||
assert teacher_resp.status_code == 200
|
||||
assert teacher_resp.mimetype == "image/webp"
|
||||
assert teacher_resp.headers["Cache-Control"] == "private, no-store"
|
||||
|
||||
# Neevidovaný soubor i path traversal -> 404.
|
||||
untracked = media_dir / str(pub_set_id) / "neevidovany.webp"
|
||||
untracked.write_bytes(b"RIFF....WEBP")
|
||||
assert (
|
||||
teacher_client.get(f"/media/{pub_set_id}/neevidovany.webp").status_code
|
||||
== 404
|
||||
)
|
||||
assert (
|
||||
anon_client.get(f"/media/{pub_set_id}/../poznavacky.db").status_code
|
||||
== 404
|
||||
)
|
||||
assert (
|
||||
anon_client.get(
|
||||
f"/media/{pub_set_id}/..%2Fpoznavacky.db"
|
||||
).status_code
|
||||
== 404
|
||||
)
|
||||
# Soubor patří jiné sadě -> 404.
|
||||
pub_filename = pub_photo.soubor_plny.split("/", 1)[1]
|
||||
assert (
|
||||
teacher_client.get(f"/media/{draft_set_id}/{pub_filename}").status_code
|
||||
== 404
|
||||
)
|
||||
Reference in new issue
Block a user