From f0560dfdb2adaa44fc739941a2a784c558ae6427 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 23 Mar 2009 21:07:02 +0000 Subject: Fixed #9282: added a generic comment moderation toolkit. See the documentation for details. This began life as (part of) James Bennett's comment-utils app, and was adapted to be part of Django by Thejaswi Puthraya and Jannis Leidel. Thanks, all! git-svn-id: http://code.djangoproject.com/svn/django/trunk@10122 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../comment_tests/fixtures/comment_utils.xml | 15 +++++ tests/regressiontests/comment_tests/models.py | 8 +++ .../comment_tests/tests/__init__.py | 1 + .../tests/comment_utils_moderators_tests.py | 70 ++++++++++++++++++++++ tests/runtests.py | 4 ++ .../comments/comment_notification_email.txt | 3 + 6 files changed, 101 insertions(+) create mode 100644 tests/regressiontests/comment_tests/fixtures/comment_utils.xml create mode 100644 tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py create mode 100644 tests/templates/comments/comment_notification_email.txt (limited to 'tests') diff --git a/tests/regressiontests/comment_tests/fixtures/comment_utils.xml b/tests/regressiontests/comment_tests/fixtures/comment_utils.xml new file mode 100644 index 0000000000..a39bbf63e1 --- /dev/null +++ b/tests/regressiontests/comment_tests/fixtures/comment_utils.xml @@ -0,0 +1,15 @@ + + + + ABC + This is the body + 2008-01-01 + True + + + XYZ + Text here + 2008-01-02 + False + + diff --git a/tests/regressiontests/comment_tests/models.py b/tests/regressiontests/comment_tests/models.py index 28022e2848..62f416882c 100644 --- a/tests/regressiontests/comment_tests/models.py +++ b/tests/regressiontests/comment_tests/models.py @@ -20,3 +20,11 @@ class Article(models.Model): def __str__(self): return self.headline +class Entry(models.Model): + title = models.CharField(max_length=250) + body = models.TextField() + pub_date = models.DateField() + enable_comments = models.BooleanField() + + def __str__(self): + return self.title diff --git a/tests/regressiontests/comment_tests/tests/__init__.py b/tests/regressiontests/comment_tests/tests/__init__.py index 09026aa010..449fea471d 100644 --- a/tests/regressiontests/comment_tests/tests/__init__.py +++ b/tests/regressiontests/comment_tests/tests/__init__.py @@ -86,3 +86,4 @@ from regressiontests.comment_tests.tests.comment_form_tests import * from regressiontests.comment_tests.tests.templatetag_tests import * from regressiontests.comment_tests.tests.comment_view_tests import * from regressiontests.comment_tests.tests.moderation_view_tests import * +from regressiontests.comment_tests.tests.comment_utils_moderators_tests import * diff --git a/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py b/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py new file mode 100644 index 0000000000..4fe8b8ae12 --- /dev/null +++ b/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py @@ -0,0 +1,70 @@ +from regressiontests.comment_tests.tests import CommentTestCase, CT, Site +from django.contrib.comments.models import Comment +from django.contrib.comments.moderation import moderator, CommentModerator, AlreadyModerated +from regressiontests.comment_tests.models import Entry +from django.core import mail + +class EntryModerator1(CommentModerator): + email_notification = True + +class EntryModerator2(CommentModerator): + enable_field = 'enable_comments' + +class EntryModerator3(CommentModerator): + auto_close_field = 'pub_date' + close_after = 7 + +class EntryModerator4(CommentModerator): + auto_moderate_field = 'pub_date' + moderate_after = 7 + +class CommentUtilsModeratorTests(CommentTestCase): + fixtures = ["comment_utils.xml"] + + def createSomeComments(self): + c1 = Comment.objects.create( + content_type = CT(Entry), + object_pk = "1", + user_name = "Joe Somebody", + user_email = "jsomebody@example.com", + user_url = "http://example.com/~joe/", + comment = "First!", + site = Site.objects.get_current(), + ) + c2 = Comment.objects.create( + content_type = CT(Entry), + object_pk = "2", + user_name = "Joe the Plumber", + user_email = "joetheplumber@whitehouse.gov", + user_url = "http://example.com/~joe/", + comment = "Second!", + site = Site.objects.get_current(), + ) + return c1, c2 + + def tearDown(self): + moderator.unregister(Entry) + + def testRegisterExistingModel(self): + moderator.register(Entry, EntryModerator1) + self.assertRaises(AlreadyModerated, moderator.register, Entry, EntryModerator1) + + def testEmailNotification(self): + moderator.register(Entry, EntryModerator1) + c1, c2 = self.createSomeComments() + self.assertEquals(len(mail.outbox), 2) + + def testCommentsEnabled(self): + moderator.register(Entry, EntryModerator2) + c1, c2 = self.createSomeComments() + self.assertEquals(Comment.objects.all().count(), 1) + + def testAutoCloseField(self): + moderator.register(Entry, EntryModerator3) + c1, c2 = self.createSomeComments() + self.assertEquals(Comment.objects.all().count(), 0) + + def testAutoModerateField(self): + moderator.register(Entry, EntryModerator4) + c1, c2 = self.createSomeComments() + self.assertEquals(c2.is_public, False) diff --git a/tests/runtests.py b/tests/runtests.py index bd7f59bdf0..f556246c90 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -110,6 +110,10 @@ def django_tests(verbosity, interactive, test_labels): 'django.middleware.common.CommonMiddleware', ) settings.SITE_ID = 1 + # For testing comment-utils, we require the MANAGERS attribute + # to be set, so that a test email is sent out which we catch + # in our tests. + settings.MANAGERS = ("admin@djangoproject.com",) # Load all the ALWAYS_INSTALLED_APPS. # (This import statement is intentionally delayed until after we diff --git a/tests/templates/comments/comment_notification_email.txt b/tests/templates/comments/comment_notification_email.txt new file mode 100644 index 0000000000..63f149392e --- /dev/null +++ b/tests/templates/comments/comment_notification_email.txt @@ -0,0 +1,3 @@ +A comment has been posted on {{ content_object }}. +The comment reads as follows: +{{ comment }} -- cgit v1.3