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
8
class CommentViewTests(CommentTestCase):
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")
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)
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)
31
def testPostCommentMissingObjectPK(self):
32
a = Article.objects.get(pk=1)
33
data = self.getValidData(a)
35
response = self.client.post("/post/", data)
36
self.assertEqual(response.status_code, 400)
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)
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")
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)
60
def testDebugCommentErrors(self):
61
"""The debug error template should be shown only if DEBUG is True"""
62
olddebug = settings.DEBUG
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")
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")
77
settings.DEBUG = olddebug
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")
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)
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)
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)
116
def testCommentSignals(self):
117
"""Test signals emitted by the comment posting view"""
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'))
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)
131
# Post a comment and check the signals
132
self.testCreateValidComment()
133
self.assertEqual(received_signals, excepted_signals)
135
def testWillBePostedSignal(self):
137
Test that the comment_will_be_posted signal can prevent the comment from
138
actually getting saved
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)
148
def testWillBePostedSignalModifyComment(self):
150
Test that the comment_will_be_posted signal can modify a comment before
153
def receive(sender, **kwargs):
154
# a bad but effective spam filter :)...
155
kwargs['comment'].is_public = False
157
signals.comment_will_be_posted.connect(receive)
158
self.testCreateValidComment()
159
c = Comment.objects.all()[0]
160
self.failIf(c.is_public)
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")
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")
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))