81 lines
4.3 KiB
Python
81 lines
4.3 KiB
Python
"""v1.2.1 : page Vidéos centrée sur les relevés, nom du site, choix du thème."""
|
||
|
||
import re
|
||
|
||
import psycopg
|
||
|
||
from shortstats.web.app import _fmt_age, _fmt_num
|
||
|
||
from .test_evolution import evo, seed_snapshots # noqa: F401 (fixture)
|
||
from .test_web import ORIGIN, app_client, login, seed_comments, seed_videos, users_file # noqa: F401
|
||
|
||
|
||
def test_formatting():
|
||
assert _fmt_num(1234567) == "1 234 567" and _fmt_num(None) == "" and _fmt_num(2.5) == "2,50"
|
||
assert [_fmt_age(h) for h in (0.2, 5, 47, 50, 400, 1000, 3000, 9000, 20000)] == \
|
||
["12 min", "5 h", "47 h", "2 j", "2 sem.", "5 sem.", "4 mois", "1 an", "2 ans"]
|
||
|
||
|
||
def test_videos_page_shows_tracking(evo, clean_db):
|
||
seed_videos(clean_db["owner"], n=3) # 3 vidéos relevées à 7 j, sans relevé successif
|
||
page = evo.get("/videos").text
|
||
for text in ("Shorts suivis", "Relevés successifs", "Prochain relevé", "Dernier relevé", "Suivi en cours",
|
||
"7 j à venir", "7 j relevé", "relevé(s)", 'data-video="evo00000011"', "Commentaires relevés"):
|
||
assert text in page, text
|
||
# dernier relevé d'une vidéo avec relevés successifs, 7 j pour une vidéo sans
|
||
assert '<span class="badge badge-step">7 j</span>' in page
|
||
assert "Vidéos · Shorts Stats" in page and "J+7" not in page and "épinglé" not in page
|
||
# filtres : état et chaîne (valeurs inconnues ignorées)
|
||
tracking = evo.get("/videos?state=j7_done").text
|
||
assert "evo00000011" not in tracking and "vid00000000" in tracking
|
||
assert "evo00000011" in evo.get("/videos?state=tracking").text
|
||
assert "Aucune vidéo pour ces critères" in evo.get("/videos?state=excluded").text
|
||
assert evo.get("/videos?state=inconnu&channel=pas-un-id").status_code == 200
|
||
with psycopg.connect(clean_db["owner"]) as conn:
|
||
other = conn.execute("SELECT channel_id FROM shortstats.channel WHERE youtube_title = 'Autre chaîne'").fetchone()[0]
|
||
only = evo.get(f"/videos?channel={other}").text
|
||
assert "evo00000000" in only and "evo00000001" not in only
|
||
|
||
|
||
def test_videos_page_shows_comment_themes(app_client, clean_db):
|
||
seed_videos(clean_db["owner"], n=4)
|
||
seed_comments(clean_db["owner"], n=4)
|
||
login(app_client)
|
||
page = app_client.get("/videos").text
|
||
# vid00000003 : 5 commentaires relevés, dont 3 « Apothecary Diaries » et 3 « applepie » (un commentaire
|
||
# relève des deux)
|
||
row = page[page.index("vid00000003"):page.index("vid00000002")]
|
||
assert "5 relevé(s)" in row
|
||
assert '<span class="badge badge-theme">applepie 3 (60 %)</span>' in row
|
||
assert '<span class="badge badge-theme">Apothecary Diaries 3 (60 %)</span>' in row
|
||
# vid00000000 : 2 relevés, « Apothecary Diaries » 1 et « applepie » 1
|
||
|
||
|
||
def test_video_snapshots_api(evo):
|
||
rows = evo.get("/api/videos/evo00000003/snapshots").json()
|
||
assert [r["step"] for r in rows][:3] == ["1 h", "2 h", "3 h"] and len(rows) == 12
|
||
assert set(rows[0]) == {"step", "taken_at", "age_hours", "views", "likes", "comments", "comments_read", "themes", "estimated"}
|
||
assert rows[0]["comments_read"] > 0 and {t[0] for t in rows[0]["themes"]} <= {"applepie", "Apothecary Diaries"}
|
||
assert evo.get("/api/videos/inconnue000/snapshots").status_code == 404
|
||
assert evo.get("/api/videos/pas%20valide/snapshots").status_code == 404
|
||
evo.post("/logout", headers={"Origin": ORIGIN})
|
||
assert evo.get("/api/videos/evo00000003/snapshots").status_code == 401
|
||
|
||
|
||
def test_theme_cookie(app_client):
|
||
page = app_client.get("/login").text
|
||
assert '<html lang="fr">' in page and "Shorts <span>Stats</span>" in page
|
||
login(app_client)
|
||
page = app_client.get("/").text
|
||
assert '<html lang="fr">' in page # système par défaut
|
||
assert re.search(r'data-theme-choice="system"\s+aria-checked="true"', page)
|
||
assert "Shorts suivis" in page and "Relevés sur 24 h" in page
|
||
app_client.cookies.set("theme", "dark")
|
||
page = app_client.get("/").text
|
||
assert '<html lang="fr" data-theme="dark">' in page
|
||
assert re.search(r'data-theme-choice="dark"\s+aria-checked="true"', page)
|
||
app_client.cookies.set("theme", "light")
|
||
assert '<html lang="fr" data-theme="light">' in app_client.get("/videos").text
|
||
app_client.cookies.set("theme", '"><script>x</script>')
|
||
page = app_client.get("/").text
|
||
assert '<html lang="fr">' in page and "<script>x</script>" not in page
|