import pytest from apps.blog.models import ArticleIndexPage, ArticlePage from apps.blog.tests.factories import AuthorFactory from apps.comments.models import Comment from apps.comments.wagtail_hooks import ApproveCommentBulkAction, CommentViewSet @pytest.mark.django_db def test_comment_viewset_annotates_pending_in_article(rf, home_page): index = ArticleIndexPage(title="Articles", slug="articles") home_page.add_child(instance=index) author = AuthorFactory() article = ArticlePage(title="A", slug="a", author=author, summary="s", body=[("rich_text", "

body

")]) index.add_child(instance=article) article.save_revision().publish() pending = Comment.objects.create( article=article, author_name="Pending", author_email="pending@example.com", body="Awaiting moderation", is_approved=False, ) Comment.objects.create( article=article, author_name="Pending2", author_email="pending2@example.com", body="Awaiting moderation too", is_approved=False, ) Comment.objects.create( article=article, author_name="Approved", author_email="approved@example.com", body="Already approved", is_approved=True, ) viewset = CommentViewSet() qs = viewset.get_queryset(rf.get("/cms/snippets/comments/comment/")) annotated = qs.get(pk=pending.pk) assert annotated.pending_in_article == 2 @pytest.mark.django_db def test_bulk_approve_action_marks_selected_pending_comments_as_approved(home_page): index = ArticleIndexPage(title="Articles", slug="articles") home_page.add_child(instance=index) author = AuthorFactory() article = ArticlePage(title="A", slug="a", author=author, summary="s", body=[("rich_text", "

body

")]) index.add_child(instance=article) article.save_revision().publish() pending = Comment.objects.create( article=article, author_name="Pending", author_email="pending@example.com", body="Awaiting moderation", is_approved=False, ) approved = Comment.objects.create( article=article, author_name="Approved", author_email="approved@example.com", body="Already approved", is_approved=True, ) class _Context: model = Comment updated, child_updates = ApproveCommentBulkAction.execute_action([pending, approved], self=_Context()) pending.refresh_from_db() approved.refresh_from_db() assert updated == 1 assert child_updates == 0 assert pending.is_approved is True assert approved.is_approved is True