"""Safe post content (protocol/content.py): escaping, and the weekly summary template built on top of it (publishing/weekly.py's build_content).""" import pytest from scrobbler.federation.protocol import content from scrobbler.federation.publishing.weekly import build_content from scrobbler.services.stats import TopItem def test_escape_neutralises_markup(): assert content.escape("") == "<script>alert(1)</script>" def test_escape_handles_ampersands_without_double_escaping(): assert content.escape("Earth, Wind & Fire") == "Earth, Wind & Fire" def test_escape_passes_through_emoji_and_non_latin_text(): # Emoji and non-Latin scripts aren't HTML-significant, so they pass through untouched. assert content.escape("Björk 🎵 坂本龍一") == "Björk 🎵 坂本龍一" def test_escape_does_not_break_right_to_left_text(): name = "محمد <عبده>" # Arabic text with embedded angle brackets escaped = content.escape(name) assert "<" not in escaped and ">" not in escaped assert "محمد" in escaped # the Arabic letters are untouched def test_paragraph_and_numbered_list_only_use_tags_mastodon_keeps(): assert content.paragraph("hi") == "

hi

" assert content.numbered_list(["a", "b"]) == "
  1. a
  2. b
" @pytest.mark.parametrize( ("count", "word"), [(0, "plays"), (1, "play"), (2, "plays"), (312, "plays")] ) def test_plural(count, word): assert content.plural(count, "play") == word def test_hashtag_line_matches_the_vocab_hashtag_name(): from scrobbler.federation.protocol import vocab assert content.hashtag_line() == vocab.hashtag(content.HASHTAG)["name"] # --- The weekly summary template (uses the primitives above) -------------------------- def test_build_content_escapes_hostile_names_in_both_text_and_html(): artists = [TopItem(1, "", 5)] tracks = [TopItem(1, 'Earth, Wind & Fire\'s "Anthem"', 3, artist="A & B", duration=200)] text, html = build_content(8, artists, tracks) assert "