~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20081115191533-84v2zyjbmp1074ni
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.conf import settings
2
 
from django.contrib.auth.models import User
3
 
from django.contrib.comments import signals
4
 
from django.contrib.comments.models import Comment
5
 
from regressiontests.comment_tests.models import Article
6
 
from regressiontests.comment_tests.tests import CommentTestCase
7
 
 
8
 
class CommentViewTests(CommentTestCase):
9
 
 
10
 
    def testPostCommentHTTPMethods(self):
11
 
        a = Article.objects.get(pk=1)
12
 
        data = self.getValidData(a)
13
 
        response = self.client.get("/post/", data)
14
 
        self.assertEqual(response.status_code, 405)
15
 
        self.assertEqual(response["Allow"], "POST")
16
 
 
17
 
    def testPostCommentMissingCtype(self):
18
 
        a = Article.objects.get(pk=1)
19
 
        data = self.getValidData(a)
20
 
        del data["content_type"]
21
 
        response = self.client.post("/post/", data)
22
 
        self.assertEqual(response.status_code, 400)
23
 
 
24
 
    def testPostCommentBadCtype(self):
25
 
        a = Article.objects.get(pk=1)
26
 
        data = self.getValidData(a)
27
 
        data["content_type"] = "Nobody expects the Spanish Inquisition!"
28
 
        response = self.client.post("/post/", data)
29
 
        self.assertEqual(response.status_code, 400)
30
 
 
31
 
    def testPostCommentMissingObjectPK(self):
32
 
        a = Article.objects.get(pk=1)
33
 
        data = self.getValidData(a)
34
 
        del data["object_pk"]
35
 
        response = self.client.post("/post/", data)
36
 
        self.assertEqual(response.status_code, 400)
37
 
 
38
 
    def testPostCommentBadObjectPK(self):
39
 
        a = Article.objects.get(pk=1)
40
 
        data = self.getValidData(a)
41
 
        data["object_pk"] = "14"
42
 
        response = self.client.post("/post/", data)
43
 
        self.assertEqual(response.status_code, 400)
44
 
 
45
 
    def testCommentPreview(self):
46
 
        a = Article.objects.get(pk=1)
47
 
        data = self.getValidData(a)
48
 
        data["submit"] = "preview"
49
 
        response = self.client.post("/post/", data)
50
 
        self.assertEqual(response.status_code, 200)
51
 
        self.assertTemplateUsed(response, "comments/preview.html")
52
 
 
53
 
    def testHashTampering(self):
54
 
        a = Article.objects.get(pk=1)
55
 
        data = self.getValidData(a)
56
 
        data["security_hash"] = "Nobody expects the Spanish Inquisition!"
57
 
        response = self.client.post("/post/", data)
58
 
        self.assertEqual(response.status_code, 400)
59
 
 
60
 
    def testDebugCommentErrors(self):
61
 
        """The debug error template should be shown only if DEBUG is True"""
62
 
        olddebug = settings.DEBUG
63
 
 
64
 
        settings.DEBUG = True
65
 
        a = Article.objects.get(pk=1)
66
 
        data = self.getValidData(a)
67
 
        data["security_hash"] = "Nobody expects the Spanish Inquisition!"
68
 
        response = self.client.post("/post/", data)
69
 
        self.assertEqual(response.status_code, 400)
70
 
        self.assertTemplateUsed(response, "comments/400-debug.html")
71
 
 
72
 
        settings.DEBUG = False
73
 
        response = self.client.post("/post/", data)
74
 
        self.assertEqual(response.status_code, 400)
75
 
        self.assertTemplateNotUsed(response, "comments/400-debug.html")
76
 
 
77
 
        settings.DEBUG = olddebug
78
 
 
79
 
    def testCreateValidComment(self):
80
 
        a = Article.objects.get(pk=1)
81
 
        data = self.getValidData(a)
82
 
        self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
83
 
        self.assertEqual(self.response.status_code, 302)
84
 
        self.assertEqual(Comment.objects.count(), 1)
85
 
        c = Comment.objects.all()[0]
86
 
        self.assertEqual(c.ip_address, "1.2.3.4")
87
 
        self.assertEqual(c.comment, "This is my comment")
88
 
        
89
 
    def testPostAsAuthenticatedUser(self):
90
 
        a = Article.objects.get(pk=1)
91
 
        data = self.getValidData(a)
92
 
        data['name'] = data['email'] = ''
93
 
        self.client.login(username="normaluser", password="normaluser")
94
 
        self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
95
 
        self.assertEqual(self.response.status_code, 302)
96
 
        self.assertEqual(Comment.objects.count(), 1)
97
 
        c = Comment.objects.all()[0]
98
 
        self.assertEqual(c.ip_address, "1.2.3.4")
99
 
        u = User.objects.get(username='normaluser')
100
 
        self.assertEqual(c.user, u)
101
 
        self.assertEqual(c.user_name, u.get_full_name())
102
 
        self.assertEqual(c.user_email, u.email)
103
 
 
104
 
    def testPreventDuplicateComments(self):
105
 
        """Prevent posting the exact same comment twice"""
106
 
        a = Article.objects.get(pk=1)
107
 
        data = self.getValidData(a)
108
 
        self.client.post("/post/", data)
109
 
        self.client.post("/post/", data)
110
 
        self.assertEqual(Comment.objects.count(), 1)
111
 
 
112
 
        # This should not trigger the duplicate prevention
113
 
        self.client.post("/post/", dict(data, comment="My second comment."))
114
 
        self.assertEqual(Comment.objects.count(), 2)
115
 
 
116
 
    def testCommentSignals(self):
117
 
        """Test signals emitted by the comment posting view"""
118
 
 
119
 
        # callback
120
 
        def receive(sender, **kwargs):
121
 
            self.assertEqual(kwargs['comment'].comment, "This is my comment")
122
 
            self.assert_('request' in kwargs)
123
 
            received_signals.append(kwargs.get('signal'))
124
 
 
125
 
        # Connect signals and keep track of handled ones
126
 
        received_signals = []
127
 
        excepted_signals = [signals.comment_will_be_posted, signals.comment_was_posted]
128
 
        for signal in excepted_signals:
129
 
            signal.connect(receive)
130
 
 
131
 
        # Post a comment and check the signals
132
 
        self.testCreateValidComment()
133
 
        self.assertEqual(received_signals, excepted_signals)
134
 
        
135
 
    def testWillBePostedSignal(self):
136
 
        """
137
 
        Test that the comment_will_be_posted signal can prevent the comment from
138
 
        actually getting saved
139
 
        """
140
 
        def receive(sender, **kwargs): return False
141
 
        signals.comment_will_be_posted.connect(receive)
142
 
        a = Article.objects.get(pk=1)
143
 
        data = self.getValidData(a)
144
 
        response = self.client.post("/post/", data)
145
 
        self.assertEqual(response.status_code, 400)
146
 
        self.assertEqual(Comment.objects.count(), 0)
147
 
 
148
 
    def testWillBePostedSignalModifyComment(self):
149
 
        """
150
 
        Test that the comment_will_be_posted signal can modify a comment before
151
 
        it gets posted
152
 
        """
153
 
        def receive(sender, **kwargs):
154
 
             # a bad but effective spam filter :)...
155
 
            kwargs['comment'].is_public = False
156
 
 
157
 
        signals.comment_will_be_posted.connect(receive)
158
 
        self.testCreateValidComment()
159
 
        c = Comment.objects.all()[0]
160
 
        self.failIf(c.is_public)
161
 
 
162
 
    def testCommentNext(self):
163
 
        """Test the different "next" actions the comment view can take"""
164
 
        a = Article.objects.get(pk=1)
165
 
        data = self.getValidData(a)
166
 
        response = self.client.post("/post/", data)
167
 
        self.assertEqual(response["Location"], "http://testserver/posted/?c=1")
168
 
 
169
 
        data["next"] = "/somewhere/else/"
170
 
        data["comment"] = "This is another comment"
171
 
        response = self.client.post("/post/", data)
172
 
        self.assertEqual(response["Location"], "http://testserver/somewhere/else/?c=2")
173
 
 
174
 
    def testCommentDoneView(self):
175
 
        a = Article.objects.get(pk=1)
176
 
        data = self.getValidData(a)
177
 
        response = self.client.post("/post/", data)
178
 
        response = self.client.get("/posted/", {'c':1})
179
 
        self.assertTemplateUsed(response, "comments/posted.html")
180
 
        self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=1))
181