2009-03-23 21:07:02 +00:00
|
|
|
from django.contrib.comments.models import Comment
|
2011-07-13 09:35:51 +00:00
|
|
|
from django.contrib.comments.moderation import (moderator, CommentModerator,
|
2011-10-13 18:51:33 +00:00
|
|
|
AlreadyModerated)
|
|
|
|
|
from django.core import mail
|
2013-12-23 15:01:13 +00:00
|
|
|
from django.test import override_settings
|
2011-10-13 18:51:33 +00:00
|
|
|
|
|
|
|
|
from . import CommentTestCase
|
|
|
|
|
from ..models import Entry
|
2011-07-13 09:35:51 +00:00
|
|
|
|
2009-03-23 21:07:02 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2010-11-13 18:42:56 +00:00
|
|
|
class EntryModerator5(CommentModerator):
|
|
|
|
|
auto_moderate_field = 'pub_date'
|
|
|
|
|
moderate_after = 0
|
|
|
|
|
|
|
|
|
|
class EntryModerator6(CommentModerator):
|
|
|
|
|
auto_close_field = 'pub_date'
|
|
|
|
|
close_after = 0
|
|
|
|
|
|
2009-03-23 21:07:02 +00:00
|
|
|
class CommentUtilsModeratorTests(CommentTestCase):
|
|
|
|
|
fixtures = ["comment_utils.xml"]
|
|
|
|
|
|
|
|
|
|
def createSomeComments(self):
|
2009-05-14 15:20:50 +00:00
|
|
|
# Tests for the moderation signals must actually post data
|
|
|
|
|
# through the comment views, because only the comment views
|
|
|
|
|
# emit the custom signals moderation listens for.
|
|
|
|
|
e = Entry.objects.get(pk=1)
|
|
|
|
|
data = self.getValidData(e)
|
2009-05-15 02:54:55 +00:00
|
|
|
|
2009-05-14 15:20:50 +00:00
|
|
|
self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
|
|
|
|
|
|
|
|
|
|
# We explicitly do a try/except to get the comment we've just
|
|
|
|
|
# posted because moderation may have disallowed it, in which
|
|
|
|
|
# case we can just return it as None.
|
|
|
|
|
try:
|
|
|
|
|
c1 = Comment.objects.all()[0]
|
|
|
|
|
except IndexError:
|
|
|
|
|
c1 = None
|
|
|
|
|
|
2009-05-15 02:54:55 +00:00
|
|
|
self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
|
|
|
|
|
|
2009-05-14 15:20:50 +00:00
|
|
|
try:
|
|
|
|
|
c2 = Comment.objects.all()[0]
|
|
|
|
|
except IndexError:
|
|
|
|
|
c2 = None
|
2009-03-23 21:07:02 +00:00
|
|
|
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):
|
2013-03-11 15:04:58 +00:00
|
|
|
with override_settings(MANAGERS=("test@example.com",)):
|
|
|
|
|
moderator.register(Entry, EntryModerator1)
|
|
|
|
|
self.createSomeComments()
|
|
|
|
|
self.assertEqual(len(mail.outbox), 2)
|
2009-03-23 21:07:02 +00:00
|
|
|
|
|
|
|
|
def testCommentsEnabled(self):
|
|
|
|
|
moderator.register(Entry, EntryModerator2)
|
2009-05-14 15:20:50 +00:00
|
|
|
self.createSomeComments()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(Comment.objects.all().count(), 1)
|
2009-03-23 21:07:02 +00:00
|
|
|
|
|
|
|
|
def testAutoCloseField(self):
|
|
|
|
|
moderator.register(Entry, EntryModerator3)
|
2009-05-14 15:20:50 +00:00
|
|
|
self.createSomeComments()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(Comment.objects.all().count(), 0)
|
2009-03-23 21:07:02 +00:00
|
|
|
|
|
|
|
|
def testAutoModerateField(self):
|
|
|
|
|
moderator.register(Entry, EntryModerator4)
|
|
|
|
|
c1, c2 = self.createSomeComments()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(c2.is_public, False)
|
2010-11-13 18:42:56 +00:00
|
|
|
|
|
|
|
|
def testAutoModerateFieldImmediate(self):
|
|
|
|
|
moderator.register(Entry, EntryModerator5)
|
|
|
|
|
c1, c2 = self.createSomeComments()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(c2.is_public, False)
|
2010-11-13 18:42:56 +00:00
|
|
|
|
|
|
|
|
def testAutoCloseFieldImmediate(self):
|
|
|
|
|
moderator.register(Entry, EntryModerator6)
|
|
|
|
|
c1, c2 = self.createSomeComments()
|
2013-03-11 15:04:58 +00:00
|
|
|
self.assertEqual(Comment.objects.all().count(), 0)
|