~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/comment_tests/tests/comment_form_tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import time
 
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
 
7
 
 
8
class CommentFormTests(CommentTestCase):
 
9
 
 
10
    def testInit(self):
 
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)
 
16
 
 
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)
 
21
        return f
 
22
 
 
23
    def tamperWithForm(self, **kwargs):
 
24
        a = Article.objects.get(pk=1)
 
25
        d = self.getValidData(a)
 
26
        d.update(kwargs)
 
27
        f = CommentForm(Article.objects.get(pk=1), data=d)
 
28
        self.failIf(f.is_valid())
 
29
        return f
 
30
 
 
31
    def testHoneypotTampering(self):
 
32
        self.tamperWithForm(honeypot="I am a robot")
 
33
 
 
34
    def testTimestampTampering(self):
 
35
        self.tamperWithForm(timestamp=str(time.time() - 28800))
 
36
 
 
37
    def testSecurityHashTampering(self):
 
38
        self.tamperWithForm(security_hash="Nobody expects the Spanish Inquisition!")
 
39
 
 
40
    def testContentTypeTampering(self):
 
41
        self.tamperWithForm(content_type="auth.user")
 
42
 
 
43
    def testObjectPKTampering(self):
 
44
        self.tamperWithForm(object_pk="3")
 
45
 
 
46
    def testSecurityErrors(self):
 
47
        f = self.tamperWithForm(honeypot="I am a robot")
 
48
        self.assert_("honeypot" in f.security_errors())
 
49
 
 
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")
 
56
        c.save()
 
57
        self.assertEqual(Comment.objects.count(), 1)
 
58
 
 
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)
 
63
 
 
64
        # Save settings in case other tests need 'em
 
65
        saved = settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES
 
66
 
 
67
        # Don't wanna swear in the unit tests if we don't have to...
 
68
        settings.PROFANITIES_LIST = ["rooster"]
 
69
 
 
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())
 
74
 
 
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())
 
79
 
 
80
        # Restore settings
 
81
        settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved