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