PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/comment_tests/tests/comment_form_tests.py

https://code.google.com/p/mango-py/
Python | 101 lines | 85 code | 14 blank | 2 comment | 0 complexity | aed9e635dc6a189642b6e9793aa1b8d2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import time
  2. from django.conf import settings
  3. from django.contrib.comments.forms import CommentForm
  4. from django.contrib.comments.models import Comment
  5. from django.utils.hashcompat import sha_constructor
  6. from regressiontests.comment_tests.models import Article
  7. from regressiontests.comment_tests.tests import CommentTestCase
  8. class CommentFormTests(CommentTestCase):
  9. def testInit(self):
  10. f = CommentForm(Article.objects.get(pk=1))
  11. self.assertEqual(f.initial['content_type'], str(Article._meta))
  12. self.assertEqual(f.initial['object_pk'], "1")
  13. self.assertNotEqual(f.initial['security_hash'], None)
  14. self.assertNotEqual(f.initial['timestamp'], None)
  15. def testValidPost(self):
  16. a = Article.objects.get(pk=1)
  17. f = CommentForm(a, data=self.getValidData(a))
  18. self.assertTrue(f.is_valid(), f.errors)
  19. return f
  20. def tamperWithForm(self, **kwargs):
  21. a = Article.objects.get(pk=1)
  22. d = self.getValidData(a)
  23. d.update(kwargs)
  24. f = CommentForm(Article.objects.get(pk=1), data=d)
  25. self.assertFalse(f.is_valid())
  26. return f
  27. def testHoneypotTampering(self):
  28. self.tamperWithForm(honeypot="I am a robot")
  29. def testTimestampTampering(self):
  30. self.tamperWithForm(timestamp=str(time.time() - 28800))
  31. def testSecurityHashTampering(self):
  32. self.tamperWithForm(security_hash="Nobody expects the Spanish Inquisition!")
  33. def testContentTypeTampering(self):
  34. self.tamperWithForm(content_type="auth.user")
  35. def testObjectPKTampering(self):
  36. self.tamperWithForm(object_pk="3")
  37. def testDjango12Hash(self):
  38. # Ensure we can use the hashes generated by Django 1.2
  39. a = Article.objects.get(pk=1)
  40. d = self.getValidData(a)
  41. content_type = d['content_type']
  42. object_pk = d['object_pk']
  43. timestamp = d['timestamp']
  44. # The Django 1.2 method hard-coded here:
  45. info = (content_type, object_pk, timestamp, settings.SECRET_KEY)
  46. security_hash = sha_constructor("".join(info)).hexdigest()
  47. d['security_hash'] = security_hash
  48. f = CommentForm(a, data=d)
  49. self.assertTrue(f.is_valid(), f.errors)
  50. def testSecurityErrors(self):
  51. f = self.tamperWithForm(honeypot="I am a robot")
  52. self.assertTrue("honeypot" in f.security_errors())
  53. def testGetCommentObject(self):
  54. f = self.testValidPost()
  55. c = f.get_comment_object()
  56. self.assertTrue(isinstance(c, Comment))
  57. self.assertEqual(c.content_object, Article.objects.get(pk=1))
  58. self.assertEqual(c.comment, "This is my comment")
  59. c.save()
  60. self.assertEqual(Comment.objects.count(), 1)
  61. def testProfanities(self):
  62. """Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
  63. a = Article.objects.get(pk=1)
  64. d = self.getValidData(a)
  65. # Save settings in case other tests need 'em
  66. saved = settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES
  67. # Don't wanna swear in the unit tests if we don't have to...
  68. settings.PROFANITIES_LIST = ["rooster"]
  69. # Try with COMMENTS_ALLOW_PROFANITIES off
  70. settings.COMMENTS_ALLOW_PROFANITIES = False
  71. f = CommentForm(a, data=dict(d, comment="What a rooster!"))
  72. self.assertFalse(f.is_valid())
  73. # Now with COMMENTS_ALLOW_PROFANITIES on
  74. settings.COMMENTS_ALLOW_PROFANITIES = True
  75. f = CommentForm(a, data=dict(d, comment="What a rooster!"))
  76. self.assertTrue(f.is_valid())
  77. # Restore settings
  78. settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved