"""Page « Publier » : Markdown sûr, tableaux, brouillons privés, publication (vrai HTTP local, SQLite jetable).""" import json import re import unittest from urllib.parse import urlencode import test_forum as base import markdown_sur from forum import Invalide class TestMarkdown(unittest.TestCase): def test_html_echappe_et_liens_surs(self): h = markdown_sur.rendu("\n") self.assertNotIn("]*\s(onclick|onmouseover)=") self.assertNotIn("href='data", h) self.assertNotIn("href='/", h) h = markdown_sur.rendu("[Météo](https://vercors.me/?a=1&b=2)") self.assertIn("Météo", h) def test_mise_en_forme(self): h = markdown_sur.rendu("# Titre\n\n**gras** et *ita* et _ita2_ et `code`\nligne 2\n\n- un\n- deux\n\n1. a\n2. b") self.assertIn("

Titre

", h) self.assertIn("gras", h) self.assertIn("ita", h) self.assertIn("ita2", h) self.assertIn("<b>code</b>", h) self.assertIn("
ligne 2

", h) self.assertIn("", h) self.assertIn("
  1. a
  2. b
", h) self.assertIn("**pas gras**", markdown_sur.rendu("`**pas gras**`")) def test_caracteres_de_controle(self): h = markdown_sur.rendu("a \x005\x00 `c` \x000\x00 b\x07") self.assertNotIn("\x00", h) self.assertIn("c", h) self.assertEqual(markdown_sur._en_ligne("\x009\x00"), "\x009\x00") # marqueur inconnu : pas d'IndexError f = base.Forum() u = f.fixer_proprietaire("theo", "Théo") for titre, corps in (("t\x00", "c"), ("t", "a\x005\x00")): with self.assertRaises(Invalide): f.creer_sujet(u, titre, corps) f.creer_sujet(u, "t", "ligne\r\nsuite\ttab") # retours à la ligne et tabulations restent permis def test_entrees_pieges_rapides(self): import time for piege in ("# " + " " * 20000 + "x", "#" * 3 + " " + "#" * 20000 + " x", "**" + "a " * 20000, "*a" * 20000, "_a " * 20000, "| " * 20000 + "\n|" + "-|" * 20000, "[" * 20000 + "](" * 20000): debut = time.perf_counter() markdown_sur.rendu(piege) self.assertLess(time.perf_counter() - debut, 1.0, piege[:20]) self.assertIn("

Titre

", markdown_sur.rendu("# Titre ## ")) self.assertNotIn("

", markdown_sur.rendu("# ###")) def test_tableaux(self): h = markdown_sur.rendu("| Station | Tmin | Note |\n| --- | ---: | :-: |\n| Gêve | -4,2 | **gel** |\n| x | 1 |") self.assertIn("", h) self.assertIn("", h) self.assertIn("", h) self.assertIn("", h) # cellule manquante complétée self.assertNotIn("
StationTminNoteGêve-4,2gel<i>x</i>1
", markdown_sur.rendu("a | b\nc | d")) # sans ligne de séparation : texte class TestPublier(base.BaseHTTP): def post(self, chemin, qui, champs, **kw): return self.req(chemin, qui, urlencode(champs).encode(), **kw) def test_page_et_bouton_entete(self): page = self.req("/publier", "a1")[2].decode() self.assertNotIn("class='bouton principal publier'", page) # plus de bouton Publier dans l'en-tête (Théo, 06/11) self.assertIn("href='/brouillons'>Mes brouillons", page) for libelle in ("for='titre'", "for='station'", "for='corps'", "for='photos'", "Me prévenir des réponses", "Catégorie bientôt", "Enregistrer le brouillon", "formnovalidate"): self.assertIn(libelle, page) self.assertNotRegex(page, r"name='prevenir' value='1' checked") # décochée par défaut self.assertIn("Gêve", page) # liste des stations self.assertEqual(self.req("/publier", "m1")[0], 403) self.assertNotIn("href='/publier'", self.req("/", "m1")[2].decode()) self.assertNotIn("Mes brouillons", self.req("/", "m1")[2].decode()) def test_publication_markdown_et_brouillon_supprime(self): st, _, b = self.post("/brouillon", "a1", {"titre": "Gel", "corps": "brouillon", "station": "geve"}) bid = json.loads(b)["id"] self.assertRegex(json.loads(b)["heure"], r"^\d{1,2} h \d{2}$") corps = "## Relevés\n\n| Station | Tmin |\n| --- | --- |\n| Gêve | -4 |\n\n [x](javascript:alert(1))" st, h, _ = self.post("/publier", "a1", {"brouillon": bid, "titre": "Gel matinal", "corps": corps, "station": "geve", "prevenir": "1", "action": "publier"}) self.assertEqual(st, 303) tid = int(re.search(r"/t/(\d+)", h["Location"]).group(1)) page = self.req(h["Location"])[2].decode() self.assertIn("

Relevés

", page) self.assertIn("
", page) self.assertNotIn("| a | b |\n|---|---|\n| 1 | 2 |"}) self.assertEqual(st, 200) self.assertNotIn("
Gêve-4