import pytest from django.test import override_settings 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 @pytest.mark.django_db @override_settings(ALLOWED_HOSTS=["testserver", "localhost", "127.0.0.1"]) def test_comments_snippet_index_page_loads(client, django_user_model, home_page): admin = django_user_model.objects.create_superuser( username="admin", email="admin@example.com", password="admin-pass", ) client.force_login(admin) response = client.get("/cms/snippets/comments/comment/") assert response.status_code == 200