PageRenderTime 31ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/comment_tests/tests/moderation_view_tests.py

https://code.google.com/p/mango-py/
Python | 203 lines | 195 code | 6 blank | 2 comment | 1 complexity | f0491b89a152b903bcd952def80e8a26 MD5 | raw file
  1from django.contrib.auth.models import User, Permission
  2from django.contrib.comments import signals
  3from django.contrib.comments.models import Comment, CommentFlag
  4from django.contrib.contenttypes.models import ContentType
  5
  6from regressiontests.comment_tests.tests import CommentTestCase
  7
  8
  9class FlagViewTests(CommentTestCase):
 10
 11    def testFlagGet(self):
 12        """GET the flag view: render a confirmation page."""
 13        comments = self.createSomeComments()
 14        pk = comments[0].pk
 15        self.client.login(username="normaluser", password="normaluser")
 16        response = self.client.get("/flag/%d/" % pk)
 17        self.assertTemplateUsed(response, "comments/flag.html")
 18
 19    def testFlagPost(self):
 20        """POST the flag view: actually flag the view (nice for XHR)"""
 21        comments = self.createSomeComments()
 22        pk = comments[0].pk
 23        self.client.login(username="normaluser", password="normaluser")
 24        response = self.client.post("/flag/%d/" % pk)
 25        self.assertEqual(response["Location"], "http://testserver/flagged/?c=%d" % pk)
 26        c = Comment.objects.get(pk=pk)
 27        self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1)
 28        return c
 29
 30    def testFlagPostTwice(self):
 31        """Users don't get to flag comments more than once."""
 32        c = self.testFlagPost()
 33        self.client.post("/flag/%d/" % c.pk)
 34        self.client.post("/flag/%d/" % c.pk)
 35        self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1)
 36
 37    def testFlagAnon(self):
 38        """GET/POST the flag view while not logged in: redirect to log in."""
 39        comments = self.createSomeComments()
 40        pk = comments[0].pk
 41        response = self.client.get("/flag/%d/" % pk)
 42        self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/%d/" % pk)
 43        response = self.client.post("/flag/%d/" % pk)
 44        self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/%d/" % pk)
 45
 46    def testFlaggedView(self):
 47        comments = self.createSomeComments()
 48        pk = comments[0].pk
 49        response = self.client.get("/flagged/", data={"c":pk})
 50        self.assertTemplateUsed(response, "comments/flagged.html")
 51
 52    def testFlagSignals(self):
 53        """Test signals emitted by the comment flag view"""
 54
 55        # callback
 56        def receive(sender, **kwargs):
 57            self.assertEqual(kwargs['flag'].flag, CommentFlag.SUGGEST_REMOVAL)
 58            self.assertEqual(kwargs['request'].user.username, "normaluser")
 59            received_signals.append(kwargs.get('signal'))
 60
 61        # Connect signals and keep track of handled ones
 62        received_signals = []
 63        signals.comment_was_flagged.connect(receive)
 64
 65        # Post a comment and check the signals
 66        self.testFlagPost()
 67        self.assertEqual(received_signals, [signals.comment_was_flagged])
 68
 69def makeModerator(username):
 70    u = User.objects.get(username=username)
 71    ct = ContentType.objects.get_for_model(Comment)
 72    p = Permission.objects.get(content_type=ct, codename="can_moderate")
 73    u.user_permissions.add(p)
 74
 75class DeleteViewTests(CommentTestCase):
 76
 77    def testDeletePermissions(self):
 78        """The delete view should only be accessible to 'moderators'"""
 79        comments = self.createSomeComments()
 80        pk = comments[0].pk
 81        self.client.login(username="normaluser", password="normaluser")
 82        response = self.client.get("/delete/%d/" % pk)
 83        self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/delete/%d/" % pk)
 84
 85        makeModerator("normaluser")
 86        response = self.client.get("/delete/%d/" % pk)
 87        self.assertEqual(response.status_code, 200)
 88
 89    def testDeletePost(self):
 90        """POSTing the delete view should mark the comment as removed"""
 91        comments = self.createSomeComments()
 92        pk = comments[0].pk
 93        makeModerator("normaluser")
 94        self.client.login(username="normaluser", password="normaluser")
 95        response = self.client.post("/delete/%d/" % pk)
 96        self.assertEqual(response["Location"], "http://testserver/deleted/?c=%d" % pk)
 97        c = Comment.objects.get(pk=pk)
 98        self.assertTrue(c.is_removed)
 99        self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_DELETION, user__username="normaluser").count(), 1)
100
101    def testDeleteSignals(self):
102        def receive(sender, **kwargs):
103            received_signals.append(kwargs.get('signal'))
104
105        # Connect signals and keep track of handled ones
106        received_signals = []
107        signals.comment_was_flagged.connect(receive)
108
109        # Post a comment and check the signals
110        self.testDeletePost()
111        self.assertEqual(received_signals, [signals.comment_was_flagged])
112
113    def testDeletedView(self):
114        comments = self.createSomeComments()
115        pk = comments[0].pk
116        response = self.client.get("/deleted/", data={"c":pk})
117        self.assertTemplateUsed(response, "comments/deleted.html")
118
119class ApproveViewTests(CommentTestCase):
120
121    def testApprovePermissions(self):
122        """The delete view should only be accessible to 'moderators'"""
123        comments = self.createSomeComments()
124        pk = comments[0].pk
125        self.client.login(username="normaluser", password="normaluser")
126        response = self.client.get("/approve/%d/" % pk)
127        self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/approve/%d/" % pk)
128
129        makeModerator("normaluser")
130        response = self.client.get("/approve/%d/" % pk)
131        self.assertEqual(response.status_code, 200)
132
133    def testApprovePost(self):
134        """POSTing the delete view should mark the comment as removed"""
135        c1, c2, c3, c4 = self.createSomeComments()
136        c1.is_public = False; c1.save()
137
138        makeModerator("normaluser")
139        self.client.login(username="normaluser", password="normaluser")
140        response = self.client.post("/approve/%d/" % c1.pk)
141        self.assertEqual(response["Location"], "http://testserver/approved/?c=%d" % c1.pk)
142        c = Comment.objects.get(pk=c1.pk)
143        self.assertTrue(c.is_public)
144        self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_APPROVAL, user__username="normaluser").count(), 1)
145
146    def testApproveSignals(self):
147        def receive(sender, **kwargs):
148            received_signals.append(kwargs.get('signal'))
149
150        # Connect signals and keep track of handled ones
151        received_signals = []
152        signals.comment_was_flagged.connect(receive)
153
154        # Post a comment and check the signals
155        self.testApprovePost()
156        self.assertEqual(received_signals, [signals.comment_was_flagged])
157
158    def testApprovedView(self):
159        comments = self.createSomeComments()
160        pk = comments[0].pk
161        response = self.client.get("/approved/", data={"c":pk})
162        self.assertTemplateUsed(response, "comments/approved.html")
163
164class AdminActionsTests(CommentTestCase):
165    urls = "regressiontests.comment_tests.urls_admin"
166
167    def setUp(self):
168        super(AdminActionsTests, self).setUp()
169
170        # Make "normaluser" a moderator
171        u = User.objects.get(username="normaluser")
172        u.is_staff = True
173        perms = Permission.objects.filter(
174            content_type__app_label = 'comments',
175            codename__endswith = 'comment'
176        )
177        for perm in perms:
178            u.user_permissions.add(perm)
179        u.save()
180
181    def testActionsNonModerator(self):
182        comments = self.createSomeComments()
183        self.client.login(username="normaluser", password="normaluser")
184        response = self.client.get("/admin/comments/comment/")
185        self.assertEqual("approve_comments" in response.content, False)
186
187    def testActionsModerator(self):
188        comments = self.createSomeComments()
189        makeModerator("normaluser")
190        self.client.login(username="normaluser", password="normaluser")
191        response = self.client.get("/admin/comments/comment/")
192        self.assertEqual("approve_comments" in response.content, True)
193
194    def testActionsDisabledDelete(self):
195        "Tests a CommentAdmin where 'delete_selected' has been disabled."
196        comments = self.createSomeComments()
197        self.client.login(username="normaluser", password="normaluser")
198        response = self.client.get('/admin2/comments/comment/')
199        self.assertEqual(response.status_code, 200)
200        self.assertTrue(
201            '<option value="delete_selected">' not in response.content,
202            "Found an unexpected delete_selected in response"
203        )