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