Implements Issue #35 with category snippets, article category routing, category-aware templates, and category RSS feeds with tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
236 lines
7.2 KiB
Python
236 lines
7.2 KiB
Python
import pytest
|
|
from taggit.models import Tag
|
|
|
|
from apps.blog.models import ArticleIndexPage, ArticlePage, Category
|
|
from apps.blog.tests.factories import AuthorFactory
|
|
from apps.comments.models import Comment
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_homepage_render(client, home_page):
|
|
resp = client.get("/")
|
|
assert resp.status_code == 200
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_index_pagination_and_tag_filter(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
for n in range(14):
|
|
article = ArticlePage(
|
|
title=f"A{n}",
|
|
slug=f"a{n}",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save_revision().publish()
|
|
|
|
resp = client.get("/articles/?page=2")
|
|
assert resp.status_code == 200
|
|
assert resp.context["articles"].number == 2
|
|
assert "Pagination" in resp.content.decode()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_page_related_context(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
main = ArticlePage(
|
|
title="Main",
|
|
slug="main",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=main)
|
|
main.save_revision().publish()
|
|
|
|
related = ArticlePage(
|
|
title="Related",
|
|
slug="related",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=related)
|
|
related.save_revision().publish()
|
|
|
|
resp = client.get("/articles/main/")
|
|
assert resp.status_code == 200
|
|
assert "related_articles" in resp.context
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_newsletter_forms_render_in_nav_and_footer(client, home_page):
|
|
resp = client.get("/")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
# Nav has a Subscribe CTA link (no inline form — wireframe spec)
|
|
assert 'href="#newsletter"' in html
|
|
# Footer has Connect section with social/RSS links (no newsletter form)
|
|
assert "Connect" in html
|
|
assert 'name="source" value="nav"' not in html
|
|
assert 'name="source" value="footer"' not in html
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_page_renders_share_links_and_newsletter_form(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="Main",
|
|
slug="main",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save_revision().publish()
|
|
|
|
resp = client.get("/articles/main/")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
assert "Share on X" in html
|
|
assert "Share on LinkedIn" in html
|
|
assert 'data-copy-link' in html
|
|
assert 'name="source" value="article"' in html
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_page_renders_approved_comments_and_reply_form(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="Main",
|
|
slug="main",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save_revision().publish()
|
|
comment = Comment.objects.create(
|
|
article=article,
|
|
author_name="A",
|
|
author_email="a@example.com",
|
|
body="Top level",
|
|
is_approved=True,
|
|
)
|
|
Comment.objects.create(
|
|
article=article,
|
|
parent=comment,
|
|
author_name="B",
|
|
author_email="b@example.com",
|
|
body="Reply",
|
|
is_approved=True,
|
|
)
|
|
|
|
resp = client.get("/articles/main/")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
assert "Top level" in html
|
|
assert "Reply" in html
|
|
assert f'name="parent_id" value="{comment.id}"' in html
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_index_renders_tag_filter_controls(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="Main",
|
|
slug="main",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save_revision().publish()
|
|
tag = Tag.objects.create(name="TagOne", slug="tag-one")
|
|
article.tags.add(tag)
|
|
article.save_revision().publish()
|
|
|
|
resp = client.get("/articles/")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
assert "/articles/?tag=tag-one" in html
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_index_category_route_filters_articles(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
reviews = Category.objects.create(name="Reviews", slug="reviews")
|
|
tutorials = Category.objects.create(name="Tutorials", slug="tutorials")
|
|
review_article = ArticlePage(
|
|
title="Review A",
|
|
slug="review-a",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
category=reviews,
|
|
)
|
|
tutorial_article = ArticlePage(
|
|
title="Tutorial A",
|
|
slug="tutorial-a",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
category=tutorials,
|
|
)
|
|
index.add_child(instance=review_article)
|
|
review_article.save_revision().publish()
|
|
index.add_child(instance=tutorial_article)
|
|
tutorial_article.save_revision().publish()
|
|
|
|
resp = client.get("/articles/category/reviews/")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
assert "Review A" in html
|
|
assert "Tutorial A" not in html
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_index_category_route_supports_tag_filter(client, home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
reviews = Category.objects.create(name="Reviews", slug="reviews")
|
|
keep = ArticlePage(
|
|
title="Keep Me",
|
|
slug="keep-me",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
category=reviews,
|
|
)
|
|
drop = ArticlePage(
|
|
title="Drop Me",
|
|
slug="drop-me",
|
|
author=author,
|
|
summary="summary",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
category=reviews,
|
|
)
|
|
index.add_child(instance=keep)
|
|
keep.save_revision().publish()
|
|
index.add_child(instance=drop)
|
|
drop.save_revision().publish()
|
|
target_tag = Tag.objects.create(name="Python", slug="python")
|
|
keep.tags.add(target_tag)
|
|
keep.save_revision().publish()
|
|
|
|
resp = client.get("/articles/category/reviews/?tag=python")
|
|
html = resp.content.decode()
|
|
assert resp.status_code == 200
|
|
assert "Keep Me" in html
|
|
assert "Drop Me" not in html
|