"""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_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.assertIn("class='bouton principal publier' href='/publier'>Publier
", page) 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