CI / validate-monitoring (push) Successful in 10s
CI / lint (push) Successful in 16s
CI / federation-interop (push) Successful in 1m2s
CI / test (push) Successful in 2m40s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
protocol/schedule.py (pure): target_week(now_utc, tz) resolves to exactly one Monday-Sunday week — the most recent one whose Monday 09:00-local posting gate has passed — computed via wall-clock arithmetic in the user's own zone so it's correct across a DST change (a week can be 6h23/7h01 of real time, never silently 7 flat days). Because it's a pure function of "now", a worker that was down for weeks naturally catches up with one post, not a backlog. Also resolve_timezone()/is_valid_timezone() for a settings field that can otherwise crash a task or silently do nothing forever. protocol/content.py (pure): the small HTML subset shared by every post kind (p, strong, em, ol, li), with escaping split from formatting so a hostile artist/track name can't inject markup. publishing/weekly.py: build_content() (the template) and check_and_post()/check_all() (the federation.weekly periodic task, every 15 minutes). An empty week is simply never posted, and — since the target week only ever advances — never reconsidered either, with no separate marker needed. has_post() lets the common "already posted this week" pass skip the stats queries entirely. Core: accounts.get_user(id) (federation needs it and can't import the User model directly); sharing.EDITABLE and update() gain timezone/post_weekly_summary/post_milestones, with the same now-not-later validation visibility already had. Tested: the DST week and gate boundaries against real computed reference dates (not guessed ones — one guess was wrong and the test caught it before it shipped), hostile/emoji/RTL names in the template, and the full task against a real database (empty vs non-empty weeks, posting twice, disabled settings, a worker down for weeks, a bad stored timezone not stopping other users). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
102 lines
3.9 KiB
Python
102 lines
3.9 KiB
Python
"""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>") == "<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") == "<p>hi</p>"
|
|
assert content.numbered_list(["a", "b"]) == "<ol><li>a</li><li>b</li></ol>"
|
|
|
|
|
|
@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, "<script>alert(1)</script>", 5)]
|
|
tracks = [TopItem(1, 'Earth, Wind & Fire\'s "Anthem"', 3, artist="A & B", duration=200)]
|
|
text, html = build_content(8, artists, tracks)
|
|
|
|
assert "<script>" not in html
|
|
assert "<script>alert(1)</script>" in html
|
|
assert "& B" in html
|
|
# The plain-text version is never escaped: it's not HTML, so escaping it would show
|
|
# literal "&" to a reader.
|
|
assert "Earth, Wind & Fire" in text
|
|
assert "<" not in text and "&" not in text
|
|
|
|
|
|
def test_build_content_handles_emoji_and_non_latin_names():
|
|
artists = [TopItem(1, "坂本龍一", 10), TopItem(2, "Björk 🎵", 5)]
|
|
text, html = build_content(15, artists, [])
|
|
assert "坂本龍一" in text and "坂本龍一" in html
|
|
assert "Björk 🎵" in text and "Björk 🎵" in html
|
|
|
|
|
|
def test_build_content_shape():
|
|
artists = [TopItem(1, "Radiohead", 48), TopItem(2, "Björk", 31), TopItem(3, "Portishead", 22)]
|
|
tracks = [TopItem(1, "Reckoner", 12, artist="Radiohead", duration=290)]
|
|
text, html = build_content(312, artists, tracks)
|
|
|
|
assert text.splitlines() == [
|
|
"My week in music: 312 plays",
|
|
"Top artists: 1. Radiohead (48) 2. Björk (31) 3. Portishead (22)",
|
|
"Most played: Reckoner by Radiohead (12 plays)",
|
|
"#Scrobbler",
|
|
]
|
|
assert html.startswith("<p><strong>My week in music: 312 plays</strong></p>")
|
|
assert "<ol><li>Radiohead" in html
|
|
assert "<em>Reckoner</em> by Radiohead" in html
|
|
assert html.endswith("<p>#Scrobbler</p>")
|
|
assert "<script" not in html # sanity: nothing unescaped ever reaches the markup
|
|
|
|
|
|
def test_build_content_without_any_plays_this_week():
|
|
text, html = build_content(0, [], [])
|
|
assert text == "My week in music: 0 plays\n#Scrobbler"
|
|
assert "<ol>" not in html # no top-artists list when there isn't one
|
|
assert "Most played" not in html
|
|
|
|
|
|
def test_build_content_singular_play():
|
|
text, _ = build_content(1, [TopItem(1, "Radiohead", 1)], [])
|
|
assert "My week in music: 1 play" in text
|
|
assert "Radiohead (1)" in text
|