Files
ad/tests/api/index.py
T

62 lines
3.1 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
def test_home_is_molodetz(anon):
html = anon.get("/").text
assert "Molodetz" in html
for label in ("Roll", "Standard", "Flyers", "Memes", "People", "Join"):
assert f">{label}</a>" in html
assert "wa.me" not in html and "whatsapp" not in html.lower()
def test_unknown_page_is_404(anon):
response = anon.get("/does-not-exist-xyz")
assert response.status_code == 404
assert anon.get("/does-not-exist-xyz", headers={"Accept": "application/json"}).json()["error"]
def _intro_section(html):
from bs4 import BeautifulSoup
return BeautifulSoup(html, "html.parser").select_one("section.hero [data-intro]")
def test_intro_renders_on_home(anon):
from molodetz.constants import SITE_INTRO_SEED
intro = _intro_section(anon.get("/").text)
assert intro is not None
paragraphs = intro.find_all("p", recursive=False)
assert len(paragraphs) == len([block for block in SITE_INTRO_SEED.split("\n\n") if block.strip()])
assert paragraphs[0].get_text(" ", strip=True).startswith("I wrote more code than you. I wrote more code than the best programmer you know.")
assert paragraphs[-1].get_text(strip=True) == "Will you be the one that will be replaced? Or will you be the one that will replace. Look at the guy next to you."
assert len(paragraphs[1].find_all("br")) == 5
text = intro.get_text(" ", strip=True)
for fixed in ("especially since it was", "Well, for starters", "development environment", "more divided in skills", "i triple down", "No professional texts", "it must have been worth", "measure its usage", "Buy now.", 'Vibe "Coders"'):
assert fixed in text, fixed
for typo in ("sicne", "deelopment", "devided", "tripple", "teksts", "have be worth", "it s usage", "\u1e82"):
assert typo not in text, typo
assert "<!--" not in str(intro)
def test_intro_json_face(anon):
data = anon.get("/", headers={"Accept": "application/json"}).json()
assert data["intro"].startswith("I wrote more code than you.")
def test_admin_can_edit_intro(admin, anon):
original = admin.get("/admin/settings", headers={"Accept": "application/json"}).json()["settings"]["site_intro"]
try:
response = admin.post("/admin/settings", json={"site_intro": "First line.\n\nSecond paragraph\nwith an extra line."}, headers={"Accept": "application/json"})
assert response.status_code == 200
intro = _intro_section(anon.get("/").text)
assert intro.find("p").get_text() == "First line."
assert intro.find_all("p")[1].find("br") is not None
form = admin.post("/admin/settings", data={"site_intro": "Via the form.\r\n\r\nLine one\r\nline two"})
assert form.status_code in (302, 303)
stored = admin.get("/admin/settings", headers={"Accept": "application/json"}).json()["settings"]["site_intro"]
assert stored == "Via the form.\n\nLine one\nline two"
finally:
admin.post("/admin/settings", json={"site_intro": original}, headers={"Accept": "application/json"})
assert _intro_section(anon.get("/").text).find("p").get_text(" ", strip=True).startswith("I wrote more code than you.")