vienalatina/apps/board/tests/test_templates.py
Claude 5cc1630d6a
The calendar is a month again, at every width
A phone showed a column of thirty-one dates. That was my answer to a
seven-column grid being unreadable at 390px, and it stopped being a
calendar: you could not see that the 24th was a Saturday without
counting, and a month you cannot see the shape of is a list.

It is seven columns now at 320, 390 and 1280, measured rather than
reasoned about. Below 640px the cells are about 50px, the titles come
out of them, and a day with something in it shows a dot per event and
links to that day's block in a list under the grid. An anchor, so it
needs no script and cannot fail under the CSP.

It is also an <ol> on a grid rather than a <table>, and that is the
actual fix. A table brings tbody/tr/td with it, so every phone rule had
to name an element to undo them — and `.cal td` at (0,1,1) outranks
`.cal__pad` at (0,1,0), which is how an empty month came to draw four
blank boxes and nothing else. A grid has nothing to undo, which is why
the public agenda has never had the bug. The three CSS guards are
retargeted accordingly, and the padding-cell one is inverted: a pad is
no longer hidden, it is what puts the 1st under its own weekday.

Two more from the same screenshots:

The month arrows. `← Anterior / noviembre 2026 / Siguiente →` was three
runs of text in a flex row that could not wrap, so at 320px the next
month was simply unreachable. Two 44px circles with the label between
them, wrapping, both inside the viewport at every width.

Links leaving their card. `profiles.clean_links` accepts a 500-character
label and a URL is one word, so a bio ran to 519px on a 390px screen.
`.rich` — the box the text is typed into — has broken long words since
the editor shipped; the places that show what was typed did not. Both
the before and the after are measured in the browser, not asserted from
the stylesheet.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-10-13 18:05:31 +00:00

496 lines
24 KiB
Python

"""Guards on the templates themselves.
These check one thing that no request-level test can: the Content-Security-
Policy makes a whole category of markup silently inert rather than broken, so
nothing at runtime will ever fail to tell you about it.
"""
from __future__ import annotations
import re
from pathlib import Path
import pytest
TEMPLATES = sorted((Path(__file__).resolve().parents[1] / "templates").glob("*.html"))
INLINE_HANDLER = re.compile(r"\son[a-z]+\s*=", re.IGNORECASE)
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_no_inline_event_handlers(template):
"""`default-src 'self'` with no 'unsafe-inline' means the browser ignores
an onsubmit="" attribute without complaining. A delete button written that
way loses its confirmation dialog and nobody finds out until something is
deleted by accident. Use data-confirm, handled in static/board.js.
Comments stripped first, like the two guards below: base.html explains in a
Jinja comment why its account menu is a <details> rather than a button with
an onclick, and a guard that fails on its own reasoning gets deleted rather
than obeyed."""
found = INLINE_HANDLER.findall(
JINJA_COMMENT.sub("", template.read_text(encoding="utf-8")))
assert not found, f"{template.name} has inline handler(s): {found}"
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_no_inline_style_blocks(template):
"""The same policy, the same silence, a different symptom.
`default-src 'self'` covers style-src too, and 'self' does not permit an
inline <style>. The browser drops the whole block without a word, and the
page arrives in Times New Roman with blue links — which is exactly how the
public profile page shipped. Put it in static/ and link it, where the
policy allows it and the browser caches it.
"""
# Comments stripped first: profile.html explains in a Jinja comment why it
# has no <style> block, and a guard that trips on its own documentation is
# a guard people delete.
html = JINJA_COMMENT.sub("", template.read_text(encoding="utf-8"))
assert "<style" not in html, (
f"{template.name} has an inline <style> block, which the CSP ignores. "
"Move it to apps/board/static/ and link it with url_for('static', …)."
)
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_no_table_cell_is_told_to_be_a_flex_container(template):
"""`.actions` is display:flex, and a <td> given it stops being a table cell.
It leaves the row's layout, so its bottom border is drawn at its own content
height rather than the row's and the columns' rules no longer meet. Nothing
errors, nothing logs, the page just looks subtly broken — which is how it
survived two rounds of somebody looking straight at it. The flex row belongs
in a <div> inside the cell.
"""
html = JINJA_COMMENT.sub("", template.read_text(encoding="utf-8"))
offenders = re.findall(r"<td[^>]*class=[\"'][^\"']*\bactions\b[^\"']*[\"']", html)
assert not offenders, (
f"{template.name} puts a flex class on a <td>: {offenders}. "
"Wrap the buttons in <div class=\"actions\"> inside the cell instead."
)
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_every_post_form_carries_a_csrf_token(template):
"""The hook in app.py rejects a POST without one, so a form that forgets it
is a button that always fails — and fails with a 400 that reads like the
page is broken rather than like a missing field."""
html = template.read_text(encoding="utf-8")
forms = re.findall(r"<form[^>]*method=[\"']post[\"'][^>]*>(.*?)</form>", html,
re.IGNORECASE | re.DOTALL)
for form in forms:
assert "csrf_token" in form, f"{template.name} has a POST form without a CSRF token"
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_nothing_links_to_the_account_server_logout(template):
"""A link there is a GET, and that route is POST-only, so the browser gets
a 404 and the session it was meant to end carries on. We shipped exactly
that and it went unnoticed for a week, because a dead link on a page nobody
reaches twice looks like nothing at all.
It cannot be fixed by turning the link into a form either: the POST needs a
CSRF token belonging to that other domain, which is unreadable from here by
design. The page has to tell the member what to do instead."""
html = JINJA_COMMENT.sub("", template.read_text(encoding="utf-8"))
assert "/user/logout" not in html, (
f"{template.name} links to /user/logout, which answers 404 to a GET"
)
# --- the name of the software behind the login ---------------------------
#
# Members sign in through an OAuth provider that happens to be Gitea. They are
# never told so: as far as anyone using vienalatina.com is concerned there is
# one site, and a stray brand name in an error message is the seam showing.
# Comments and docstrings are exempt — the code has to stay honest about what
# it talks to, and neither reaches a browser.
JINJA_COMMENT = re.compile(r"\{#.*?#\}", re.DOTALL)
@pytest.mark.parametrize("template", TEMPLATES, ids=lambda p: p.name)
def test_no_template_shows_the_name_of_the_account_server(template):
body = JINJA_COMMENT.sub("", template.read_text(encoding="utf-8"))
assert "Gitea" not in body, (
f"{template.name} shows 'Gitea' to the member; call it el servidor de "
"cuentas, or put the remark in a {# Jinja comment #}"
)
def test_no_message_in_the_code_shows_it_either():
"""String literals only, docstrings excluded, and case-sensitive on
purpose: `gitea_login` and `gitea_tokens` are column names nobody sees, so
only the capitalised prose form is worth failing on."""
import ast
offenders = []
for path in sorted(Path(__file__).resolve().parents[1].glob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"))
docstrings = {
doc for node in ast.walk(tree)
if isinstance(node, (ast.Module, ast.FunctionDef,
ast.AsyncFunctionDef, ast.ClassDef))
and (doc := ast.get_docstring(node, clean=False))
}
offenders += [
f"{path.name}:{node.lineno}: {node.value!r}"
for node in ast.walk(tree)
if isinstance(node, ast.Constant) and isinstance(node.value, str)
and "Gitea" in node.value and node.value not in docstrings
]
assert not offenders, "\n".join(offenders)
# --- the writing boxes opt in ---------------------------------------------
PROSE_BOXES = {
"thread_form.html": "body",
"comment_form.html": "body",
"thread.html": "body",
"member.html": "body",
"submission_form.html": "body",
"content_form.html": "body",
"profile_edit.html": "bio",
}
@pytest.mark.parametrize("name,box", sorted(PROSE_BOXES.items()))
def test_every_prose_box_has_the_markdown_toolbar(name, box):
"""`data-markdown` is what board.js attaches the toolbar and the preview to.
A box that forgets it loses both silently — the page still works, it is just
the plain textarea it always was, which nobody files as a bug."""
html = (Path(__file__).resolve().parents[1] / "templates" / name).read_text(encoding="utf-8")
opening = re.search(rf'<textarea id="{box}"[^>]*>', html)
assert opening, f"{name} has no textarea #{box}"
assert "data-markdown" in opening.group(0), (
f"{name}: #{box} is prose and should carry data-markdown")
def test_the_fields_that_are_not_prose_stay_plain():
"""The links and FAQ boxes are parsed line by line, not rendered. A bold
button there would put asterisks into a value, not into writing."""
for name, box in (("profile_edit.html", "links"), ("content_form.html", "faq")):
html = (Path(__file__).resolve().parents[1] / "templates" / name).read_text(encoding="utf-8")
opening = re.search(rf'<textarea id="{box}"[^>]*>', html)
assert opening and "data-markdown" not in opening.group(0), name
# --- the form controls are named by what they are not ---------------------
UNSTYLED_ON_PURPOSE = {"checkbox", "radio", "file", "submit",
"button", "reset", "image", "hidden"}
def test_the_form_rule_is_an_exclusion_and_not_a_list_of_accepted_types():
"""It used to be `input[type=text], input[type=email], input[type=date],
input:not([type])` — an allowlist, which is wrong the moment anybody adds a
field. It was: the password box on the sign-in page was a browser default
sitting under a full-width username box, and the number box for a page's
order was the same. Nothing errors; the form just looks unfinished, which
only a person looking at it can catch.
So the guard is on the shape of the rule rather than on any one type: it has
to exclude the controls that are not text boxes and style everything else.
"""
css = (Path(__file__).resolve().parents[1] / "static" / "board.css").read_text(encoding="utf-8")
selector = [line for line in css.splitlines() if line.startswith("input:not(")]
assert selector, (
"board.css has no `input:not(...)` form rule. If it is back to listing "
"accepted types, the next field somebody adds will be unstyled."
)
excluded = set(re.findall(r'\[type="([a-z]+)"\]', selector[0]))
assert excluded == UNSTYLED_ON_PURPOSE, (
f"the stylesheet excludes {sorted(excluded)}; expected "
f"{sorted(UNSTYLED_ON_PURPOSE)}. Anything a person types in belongs in "
"the rule, not outside it."
)
for typed_in in ("password", "number", "email", "date", "text", "search", "url"):
assert typed_in not in excluded, f"{typed_in} is a box somebody types in"
# --- the theme's own chrome -----------------------------------------------
#
# These read the Hugo theme rather than the members area, which no other test
# here does. They are here because the bug they guard against is this
# suite's recurring shape — CSS that expects markup nobody wrote — and because
# pytest is the only thing in this repository that runs on every commit.
THEME = Path(__file__).resolve().parents[3] / "themes" / "vienalatina"
def test_the_collapsed_menu_has_something_that_can_open_it():
"""The one that shipped: `main.css` hid `.site-bar__pages` below 768px and
showed it again on a toggle, the hamburger's own styles were ported with
it, and `header.html` never got the button. For three phases the public
site had no navigation at all on a phone — every rule correct, every
selector pointing at an element that did not exist.
So: if the stylesheet hides the menu behind a state, the header has to carry
that state and the control that flips it.
"""
css = (THEME / "assets" / "css" / "main.css").read_text(encoding="utf-8")
header = (THEME / "layouts" / "partials" / "header.html").read_text(encoding="utf-8")
assert ".site-menu__state:checked ~ .site-bar__pages" in css, (
"main.css no longer opens the mobile menu from the checkbox. If the "
"mechanism changed, change this test with it — but check first that the "
"menu still opens on a phone."
)
for needed in ('id="site-menu"', 'class="site-menu__state"', 'for="site-menu"'):
assert needed in header, (
f"header.html has no {needed}. The CSS hides the menu on a phone and "
"only shows it again on that checkbox, so without it the site has no "
"navigation below 768px."
)
def test_the_way_into_the_members_area_is_in_the_bar_itself():
"""Not in the menu. It was a menu entry, which on a phone put it inside a
menu that did not open — and even on a desktop made the one link that is not
content look like one of the pages."""
header = (THEME / "layouts" / "partials" / "header.html").read_text(encoding="utf-8")
bar = header.split('<nav class="site-bar__pages"')[0]
assert "site-bar__cta" in bar and "/comunidad/" in bar
# --- the calendar on a phone ----------------------------------------------
#
# A stylesheet guard, like the form rule above, and for the same reason: the
# bug it is about produced correct HTML and a blank page, so no request-level
# test in this suite could ever have seen it.
CALENDAR_PHONE_BLOCK = re.compile(
r"@media \(max-width: 640px\) \{(?P<body>(?:[^{}]|\{[^{}]*\})*?\.cal(?:[^{}]|\{[^{}]*\})*?)\n\}",
re.DOTALL)
RULE = re.compile(r"(?P<selectors>[^{}]+)\{(?P<body>[^{}]*)\}")
def _specificity(selector):
"""(ids, classes, elements) for one selector, near enough for this guard.
`:not()` and `:has()` contribute their own contents, which is how the
cascade actually counts them.
"""
inner = re.sub(r":(?:not|has|is)\(", " ", selector).replace(")", " ")
ids = len(re.findall(r"#[\w-]+", inner))
classes = (len(re.findall(r"\.[\w-]+", inner))
+ len(re.findall(r"\[[^\]]+\]", inner))
+ len(re.findall(r"(?<!:):[a-z-]+", inner)))
elements = len(re.findall(r"(?:^|[\s>+~])([a-z]+)(?![\w-]*\()", inner))
return ids, classes, elements
def _winning_display(rules, ending_in):
"""What `display` an element whose last compound is `ending_in` ends up with.
Only the rules whose own last compound could match it are considered, which
is all this guard needs: the question is which of two neighbouring rules
wins, not whether the ancestors line up.
"""
winner = None
for order, (selector, body) in enumerate(rules):
# The pseudo-classes come off before comparing but stay in the
# specificity: `.cal__day:not(:has(.cal__event))` is a rule about a day
# cell, and it is the one that hid every day on a phone.
last = re.sub(r":[\w-]+(\([^()]*(?:\([^()]*\))?[^()]*\))?", "",
selector.strip().split()[-1])
if last not in ending_in:
continue
display = re.search(r"display\s*:\s*([\w-]+)", body)
if not display:
continue
key = (_specificity(selector), order)
if winner is None or key > winner[0]:
winner = (key, selector.strip(), display.group(1))
return winner
def _rules_in(body):
body = re.sub(r"/\*.*?\*/", "", body, flags=re.DOTALL)
return [(selector.strip(), rule.group("body"))
for rule in RULE.finditer(body)
for selector in rule.group("selectors").split(",") if selector.strip()]
@pytest.fixture(scope="module")
def board_css():
return (Path(__file__).resolve().parents[1] / "static" / "board.css").read_text(
encoding="utf-8")
@pytest.fixture(scope="module")
def calendar_phone_rules(board_css):
block = CALENDAR_PHONE_BLOCK.search(board_css)
assert block, "board.css has no phone block for the calendar"
return _rules_in(block.group("body"))
@pytest.fixture(scope="module")
def calendar_base_rules(board_css):
"""The calendar's rules outside any media block — everything from the
section heading up to the first `@media` after it."""
start = board_css.index("--- the private calendar")
end = board_css.index("@media", start)
return _rules_in(board_css[start:end])
def test_the_month_is_seven_columns_at_every_width(calendar_base_rules,
calendar_phone_rules):
"""The regression this phase is about, stated as a rule.
The month was a <table>, and the only way to make a seven-column table
legible at 390px was to stop it being one — which turned a calendar into a
list of dates, and on the way there produced the specificity bug that drew
an empty month as four blank boxes. It is a grid now, and a grid stays a
grid: the phone makes the cells smaller and moves the titles out of them,
and that is all it is allowed to do.
"""
columns = [re.search(r"grid-template-columns\s*:\s*([^;]+)", body).group(1)
for selector, body in calendar_base_rules
if selector.endswith(".cal__grid") and "grid-template-columns" in body]
assert columns, "the base stylesheet never gives .cal__grid its columns"
assert "repeat(7," in columns[0].replace(" ", ""), (
f"the month is laid out as `{columns[0].strip()}` rather than seven columns")
for selector, body in calendar_phone_rules:
if not selector.endswith(".cal__grid"):
continue
display = re.search(r"display\s*:\s*([\w-]+)", body)
assert not display or display.group(1) == "grid", (
f"`{selector}` takes the month off the grid on a phone "
f"(`display: {display.group(1)}`), which is how it became a list "
"of dates the first time.")
assert "grid-template-columns" not in body, (
f"`{selector}` changes the number of columns on a phone; a month "
"has seven of them at every width.")
def test_no_cell_of_the_month_is_hidden_on_a_phone(calendar_phone_rules):
"""Neither a day nor a padding cell.
The day cells are obvious. The padding cells are the ones worth a test:
they were `display: none` while the month was a list, and in a grid they
are what puts the 1st under its own weekday — hide them and every date in
the month lands on the wrong column, which still *looks* like a calendar.
"""
for compounds, what in ((({"li.cal__day", ".cal__day"}), "a day"),
(({"li.cal__pad", ".cal__pad"}), "a padding cell")):
winner = _winning_display(calendar_phone_rules, compounds)
assert winner is None or winner[2] != "none", (
f"{what} ends up hidden on a phone, from `{winner[1]}`.")
def test_the_weekday_is_hidden_from_the_eye_and_not_from_a_screen_reader(
calendar_base_rules):
"""A fifty-pixel cell has room for a number and nothing else, so the
weekday inside each cell is clipped rather than drawn — the header row says
it for anyone who can see it. `display: none` would be the obvious way to
do that and the wrong one: it takes the element out of the accessibility
tree too, and then «22» is announced as the number 22 with no day attached.
"""
rules = [(selector, body) for selector, body in calendar_base_rules
if selector.strip().endswith(".cal__weekday")]
assert rules, "nothing in board.css hides .cal__weekday from the eye"
for selector, body in rules:
display = re.search(r"display\s*:\s*([\w-]+)", body)
assert not display or display.group(1) != "none", (
f"`{selector}` uses display:none, which hides the weekday from a "
"screen reader as well. Clip it instead.")
assert any("clip-path" in body for _, body in rules), (
"the weekday is not clipped, so it is drawn inside every cell")
def test_everywhere_a_member_writes_breaks_a_word_too_long_for_its_box(board_css):
"""A pasted URL is one word, and `profiles.clean_links` lets it be five
hundred characters long.
`.rich` — the box the text is typed into — has broken long words since the
editor shipped; the places that *show* what was typed did not, so a bio
looked right while being written and came back out through the side of the
card. The two lists are the same list, which is the only reason this is a
test rather than a line in the stylesheet nobody reads.
"""
for selector in (".rich", ".prose", ".person-head__links", ".cal__event"):
assert re.search(
r"(^|,)\s*" + re.escape(selector) + r"\s*(,[^{]*)?\{[^}]*overflow-wrap",
board_css, re.MULTILINE), (
f"`{selector}` has no overflow-wrap, so a pasted link leaves the box")
AGENDA_PHONE_BLOCK = re.compile(
r"@media \(max-width: 640px\) \{(?P<body>(?:[^{}]|\{[^{}]*\})*?\.agenda__(?:[^{}]|\{[^{}]*\})*?)\n\}",
re.DOTALL)
def test_the_public_agenda_draws_every_day_on_a_phone_too():
"""The same rule as the members area's calendar, in the other stylesheet.
The two are one feature seen from two sides — the private calendar lists
the public events — so a member who moves between them should not meet two
designs. The agenda used to show only the days with something in them,
which made an empty month a heading and one sentence.
"""
css = (THEME / "assets" / "css" / "main.css").read_text(encoding="utf-8")
block = AGENDA_PHONE_BLOCK.search(css)
assert block, "main.css has no phone block for the agenda"
body = re.sub(r"/\*.*?\*/", "", block.group("body"), flags=re.DOTALL)
rules = [(selector.strip(), rule.group("body"))
for rule in RULE.finditer(body)
for selector in rule.group("selectors").split(",") if selector.strip()]
day = _winning_display(rules, {".agenda__day", "li.agenda__day"})
assert day and day[2] != "none", (
f"a day of the month is hidden on a phone (`{day[1] if day else '—'}`)")
pad = _winning_display(rules, {".agenda__pad", "li.agenda__pad"})
assert pad and pad[2] == "none", "the neighbouring months' padding cells are drawn"
weekday = _winning_display(rules, {".agenda__weekday", "span.agenda__weekday"})
assert weekday and weekday[2] != "none", (
"the phone block never shows .agenda__weekday, so every row of the "
"month is a bare number — there is no column header at that width")
# --- the share row, which is theme and not members area --------------------
def test_the_share_buttons_that_need_a_script_say_so_in_the_markup():
"""Three of the seven cannot be links: Signal has no web intent, Instagram
accepts no URL at all, and copying needs the clipboard. They carry `hidden`
and share.js takes it off — so a reader without scripting sees four buttons
that work rather than seven of which three do nothing.
"""
share = (THEME / "layouts" / "partials" / "share.html").read_text(encoding="utf-8")
for hook in ("data-share-native", "data-share-copy"):
for element in re.findall(r"<(?:a|button)[^>]*" + hook + r"[^>]*>", share):
assert "hidden" in element, f"{hook} without `hidden`: {element[:80]}"
def test_a_hidden_share_button_is_actually_hidden():
"""`[hidden] { display: none }` lives in the browser's own stylesheet, and
an author rule beats a user-agent rule whatever the specificity — so
`.share__btn { display: inline-flex }` quietly un-hid all three. With
scripting off, seven buttons appeared and three of them did nothing, which
is the exact failure the attribute was there to prevent.
"""
css = (THEME / "assets" / "css" / "main.css").read_text(encoding="utf-8")
assert re.search(r"\.share__btn\[hidden\]\s*\{[^}]*display:\s*none", css), (
"main.css gives .share__btn a display without taking it back for "
"[hidden], so the buttons share.js is meant to reveal are visible "
"before it runs — and useless until it does")
def test_every_share_link_is_marked_safe_so_it_is_not_encoded_twice():
"""Go's template escaper treats an href as a URL and escapes it on the way
out. A value already encoded with `urlquery` therefore went out as %253A
and %2b — a WhatsApp link with the percent signs spelled out, which opens
the composer with gibberish in it. `safeURL` is what stops the second pass.
"""
share = (THEME / "layouts" / "partials" / "share.html").read_text(encoding="utf-8")
# By line rather than by matching the attribute: the value contains its own
# quotes — `href="{{ printf "https://…" $enc | safeURL }}"` — and a regex
# for href="..." stops at the first of them and reads nothing.
lines = [line.strip() for line in share.splitlines()
if "href=" in line and "$enc" in line]
assert lines, "share.html builds no encoded links at all any more"
for line in lines:
assert "safeURL" in line, f"encoded twice: {line}"