2
from django.conf import settings
3
from django.contrib.comments.models import Comment
4
from django.contrib.comments.forms import CommentForm
5
from regressiontests.comment_tests.models import Article
6
from regressiontests.comment_tests.tests import CommentTestCase
8
class CommentFormTests(CommentTestCase):
11
f = CommentForm(Article.objects.get(pk=1))
12
self.assertEqual(f.initial['content_type'], str(Article._meta))
13
self.assertEqual(f.initial['object_pk'], "1")
14
self.failIfEqual(f.initial['security_hash'], None)
15
self.failIfEqual(f.initial['timestamp'], None)
17
def testValidPost(self):
18
a = Article.objects.get(pk=1)
19
f = CommentForm(a, data=self.getValidData(a))
20
self.assert_(f.is_valid(), f.errors)
23
def tamperWithForm(self, **kwargs):
24
a = Article.objects.get(pk=1)
25
d = self.getValidData(a)
27
f = CommentForm(Article.objects.get(pk=1), data=d)
28
self.failIf(f.is_valid())
31
def testHoneypotTampering(self):
32
self.tamperWithForm(honeypot="I am a robot")
34
def testTimestampTampering(self):
35
self.tamperWithForm(timestamp=str(time.time() - 28800))
37
def testSecurityHashTampering(self):
38
self.tamperWithForm(security_hash="Nobody expects the Spanish Inquisition!")
40
def testContentTypeTampering(self):
41
self.tamperWithForm(content_type="auth.user")
43
def testObjectPKTampering(self):
44
self.tamperWithForm(object_pk="3")
46
def testSecurityErrors(self):
47
f = self.tamperWithForm(honeypot="I am a robot")
48
self.assert_("honeypot" in f.security_errors())
50
def testGetCommentObject(self):
51
f = self.testValidPost()
52
c = f.get_comment_object()
53
self.assert_(isinstance(c, Comment))
54
self.assertEqual(c.content_object, Article.objects.get(pk=1))
55
self.assertEqual(c.comment, "This is my comment")
57
self.assertEqual(Comment.objects.count(), 1)
59
def testProfanities(self):
60
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
61
a = Article.objects.get(pk=1)
62
d = self.getValidData(a)
64
# Save settings in case other tests need 'em
65
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"]
70
# Try with COMMENTS_ALLOW_PROFANITIES off
71
settings.COMMENTS_ALLOW_PROFANITIES = False
72
f = CommentForm(a, data=dict(d, comment="What a rooster!"))
73
self.failIf(f.is_valid())
75
# Now with COMMENTS_ALLOW_PROFANITIES on
76
settings.COMMENTS_ALLOW_PROFANITIES = True
77
f = CommentForm(a, data=dict(d, comment="What a rooster!"))
78
self.failUnless(f.is_valid())
81
settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved