PageRenderTime 224ms CodeModel.GetById 100ms app.highlight 9ms RepoModel.GetById 112ms app.codeStats 0ms

/tests/regressiontests/comment_tests/tests/comment_utils_moderators_tests.py

https://code.google.com/p/mango-py/
Python | 93 lines | 67 code | 20 blank | 6 comment | 0 complexity | 47a371324b98589fb8ab9515782e4bef MD5 | raw file
 1from regressiontests.comment_tests.tests import CommentTestCase, CT, Site
 2from django.contrib.comments.forms import CommentForm
 3from django.contrib.comments.models import Comment
 4from django.contrib.comments.moderation import moderator, CommentModerator, AlreadyModerated
 5from regressiontests.comment_tests.models import Entry
 6from django.core import mail
 7
 8class EntryModerator1(CommentModerator):
 9    email_notification = True
10
11class EntryModerator2(CommentModerator):
12    enable_field = 'enable_comments'
13
14class EntryModerator3(CommentModerator):
15    auto_close_field = 'pub_date'
16    close_after = 7
17
18class EntryModerator4(CommentModerator):
19    auto_moderate_field = 'pub_date'
20    moderate_after = 7
21
22class EntryModerator5(CommentModerator):
23    auto_moderate_field = 'pub_date'
24    moderate_after = 0
25
26class EntryModerator6(CommentModerator):
27    auto_close_field = 'pub_date'
28    close_after = 0
29
30class CommentUtilsModeratorTests(CommentTestCase):
31    fixtures = ["comment_utils.xml"]
32
33    def createSomeComments(self):
34        # Tests for the moderation signals must actually post data
35        # through the comment views, because only the comment views
36        # emit the custom signals moderation listens for.
37        e = Entry.objects.get(pk=1)
38        data = self.getValidData(e)
39
40        self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
41
42        # We explicitly do a try/except to get the comment we've just
43        # posted because moderation may have disallowed it, in which
44        # case we can just return it as None.
45        try:
46            c1 = Comment.objects.all()[0]
47        except IndexError:
48            c1 = None
49
50        self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
51
52        try:
53            c2 = Comment.objects.all()[0]
54        except IndexError:
55            c2 = None
56        return c1, c2
57
58    def tearDown(self):
59        moderator.unregister(Entry)
60
61    def testRegisterExistingModel(self):
62        moderator.register(Entry, EntryModerator1)
63        self.assertRaises(AlreadyModerated, moderator.register, Entry, EntryModerator1)
64
65    def testEmailNotification(self):
66        moderator.register(Entry, EntryModerator1)
67        self.createSomeComments()
68        self.assertEqual(len(mail.outbox), 2)
69
70    def testCommentsEnabled(self):
71        moderator.register(Entry, EntryModerator2)
72        self.createSomeComments()
73        self.assertEqual(Comment.objects.all().count(), 1)
74
75    def testAutoCloseField(self):
76        moderator.register(Entry, EntryModerator3)
77        self.createSomeComments()
78        self.assertEqual(Comment.objects.all().count(), 0)
79
80    def testAutoModerateField(self):
81        moderator.register(Entry, EntryModerator4)
82        c1, c2 = self.createSomeComments()
83        self.assertEqual(c2.is_public, False)
84
85    def testAutoModerateFieldImmediate(self):
86        moderator.register(Entry, EntryModerator5)
87        c1, c2 = self.createSomeComments()
88        self.assertEqual(c2.is_public, False)
89
90    def testAutoCloseFieldImmediate(self):
91        moderator.register(Entry, EntryModerator6)
92        c1, c2 = self.createSomeComments()
93        self.assertEqual(Comment.objects.all().count(), 0)