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