PageRenderTime 5593ms CodeModel.GetById 36ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/django-1.4/tests/regressiontests/comment_tests/tests/comment_view_tests.py

https://github.com/theosp/google_appengine
Python | 294 lines | 266 code | 22 blank | 6 comment | 3 complexity | 2745502aad237ee1843147e742d668c5 MD5 | raw file
  1. from __future__ import absolute_import
  2. import re
  3. from django.conf import settings
  4. from django.contrib.auth.models import User
  5. from django.contrib.comments import signals
  6. from django.contrib.comments.models import Comment
  7. from . import CommentTestCase
  8. from ..models import Article, Book
  9. post_redirect_re = re.compile(r'^http://testserver/posted/\?c=(?P<pk>\d+$)')
  10. class CommentViewTests(CommentTestCase):
  11. def testPostCommentHTTPMethods(self):
  12. a = Article.objects.get(pk=1)
  13. data = self.getValidData(a)
  14. response = self.client.get("/post/", data)
  15. self.assertEqual(response.status_code, 405)
  16. 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)
  23. def testPostCommentBadCtype(self):
  24. a = Article.objects.get(pk=1)
  25. data = self.getValidData(a)
  26. data["content_type"] = "Nobody expects the Spanish Inquisition!"
  27. response = self.client.post("/post/", data)
  28. self.assertEqual(response.status_code, 400)
  29. def testPostCommentMissingObjectPK(self):
  30. a = Article.objects.get(pk=1)
  31. data = self.getValidData(a)
  32. del data["object_pk"]
  33. response = self.client.post("/post/", data)
  34. self.assertEqual(response.status_code, 400)
  35. def testPostCommentBadObjectPK(self):
  36. a = Article.objects.get(pk=1)
  37. data = self.getValidData(a)
  38. data["object_pk"] = "14"
  39. response = self.client.post("/post/", data)
  40. self.assertEqual(response.status_code, 400)
  41. def testPostInvalidIntegerPK(self):
  42. a = Article.objects.get(pk=1)
  43. data = self.getValidData(a)
  44. data["comment"] = "This is another comment"
  45. data["object_pk"] = u'\ufffd'
  46. response = self.client.post("/post/", data)
  47. self.assertEqual(response.status_code, 400)
  48. def testPostInvalidDecimalPK(self):
  49. b = Book.objects.get(pk='12.34')
  50. data = self.getValidData(b)
  51. data["comment"] = "This is another comment"
  52. data["object_pk"] = 'cookies'
  53. response = self.client.post("/post/", data)
  54. self.assertEqual(response.status_code, 400)
  55. def testCommentPreview(self):
  56. a = Article.objects.get(pk=1)
  57. data = self.getValidData(a)
  58. data["preview"] = "Preview"
  59. response = self.client.post("/post/", data)
  60. self.assertEqual(response.status_code, 200)
  61. self.assertTemplateUsed(response, "comments/preview.html")
  62. def testHashTampering(self):
  63. a = Article.objects.get(pk=1)
  64. data = self.getValidData(a)
  65. data["security_hash"] = "Nobody expects the Spanish Inquisition!"
  66. response = self.client.post("/post/", data)
  67. self.assertEqual(response.status_code, 400)
  68. def testDebugCommentErrors(self):
  69. """The debug error template should be shown only if DEBUG is True"""
  70. olddebug = settings.DEBUG
  71. settings.DEBUG = True
  72. a = Article.objects.get(pk=1)
  73. data = self.getValidData(a)
  74. data["security_hash"] = "Nobody expects the Spanish Inquisition!"
  75. response = self.client.post("/post/", data)
  76. self.assertEqual(response.status_code, 400)
  77. self.assertTemplateUsed(response, "comments/400-debug.html")
  78. settings.DEBUG = False
  79. response = self.client.post("/post/", data)
  80. self.assertEqual(response.status_code, 400)
  81. self.assertTemplateNotUsed(response, "comments/400-debug.html")
  82. settings.DEBUG = olddebug
  83. def testCreateValidComment(self):
  84. a = Article.objects.get(pk=1)
  85. data = self.getValidData(a)
  86. self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
  87. self.assertEqual(self.response.status_code, 302)
  88. self.assertEqual(Comment.objects.count(), 1)
  89. c = Comment.objects.all()[0]
  90. self.assertEqual(c.ip_address, "1.2.3.4")
  91. self.assertEqual(c.comment, "This is my comment")
  92. def testPostAsAuthenticatedUser(self):
  93. a = Article.objects.get(pk=1)
  94. data = self.getValidData(a)
  95. data['name'] = data['email'] = ''
  96. self.client.login(username="normaluser", password="normaluser")
  97. self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
  98. self.assertEqual(self.response.status_code, 302)
  99. self.assertEqual(Comment.objects.count(), 1)
  100. c = Comment.objects.all()[0]
  101. self.assertEqual(c.ip_address, "1.2.3.4")
  102. u = User.objects.get(username='normaluser')
  103. self.assertEqual(c.user, u)
  104. self.assertEqual(c.user_name, u.get_full_name())
  105. self.assertEqual(c.user_email, u.email)
  106. def testPostAsAuthenticatedUserWithoutFullname(self):
  107. """
  108. Check that the user's name in the comment is populated for
  109. authenticated users without first_name and last_name.
  110. """
  111. user = User.objects.create_user(username='jane_other',
  112. email='jane@example.com', password='jane_other')
  113. a = Article.objects.get(pk=1)
  114. data = self.getValidData(a)
  115. data['name'] = data['email'] = ''
  116. self.client.login(username="jane_other", password="jane_other")
  117. self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
  118. c = Comment.objects.get(user=user)
  119. self.assertEqual(c.ip_address, "1.2.3.4")
  120. self.assertEqual(c.user_name, 'jane_other')
  121. user.delete()
  122. def testPreventDuplicateComments(self):
  123. """Prevent posting the exact same comment twice"""
  124. a = Article.objects.get(pk=1)
  125. data = self.getValidData(a)
  126. self.client.post("/post/", data)
  127. self.client.post("/post/", data)
  128. self.assertEqual(Comment.objects.count(), 1)
  129. # This should not trigger the duplicate prevention
  130. self.client.post("/post/", dict(data, comment="My second comment."))
  131. self.assertEqual(Comment.objects.count(), 2)
  132. def testCommentSignals(self):
  133. """Test signals emitted by the comment posting view"""
  134. # callback
  135. def receive(sender, **kwargs):
  136. self.assertEqual(kwargs['comment'].comment, "This is my comment")
  137. self.assertTrue('request' in kwargs)
  138. received_signals.append(kwargs.get('signal'))
  139. # Connect signals and keep track of handled ones
  140. received_signals = []
  141. expected_signals = [
  142. signals.comment_will_be_posted, signals.comment_was_posted
  143. ]
  144. for signal in expected_signals:
  145. signal.connect(receive)
  146. # Post a comment and check the signals
  147. self.testCreateValidComment()
  148. self.assertEqual(received_signals, expected_signals)
  149. for signal in expected_signals:
  150. signal.disconnect(receive)
  151. def testWillBePostedSignal(self):
  152. """
  153. Test that the comment_will_be_posted signal can prevent the comment from
  154. actually getting saved
  155. """
  156. def receive(sender, **kwargs): return False
  157. signals.comment_will_be_posted.connect(receive, dispatch_uid="comment-test")
  158. a = Article.objects.get(pk=1)
  159. data = self.getValidData(a)
  160. response = self.client.post("/post/", data)
  161. self.assertEqual(response.status_code, 400)
  162. self.assertEqual(Comment.objects.count(), 0)
  163. signals.comment_will_be_posted.disconnect(dispatch_uid="comment-test")
  164. def testWillBePostedSignalModifyComment(self):
  165. """
  166. Test that the comment_will_be_posted signal can modify a comment before
  167. it gets posted
  168. """
  169. def receive(sender, **kwargs):
  170. # a bad but effective spam filter :)...
  171. kwargs['comment'].is_public = False
  172. signals.comment_will_be_posted.connect(receive)
  173. self.testCreateValidComment()
  174. c = Comment.objects.all()[0]
  175. self.assertFalse(c.is_public)
  176. def testCommentNext(self):
  177. """Test the different "next" actions the comment view can take"""
  178. a = Article.objects.get(pk=1)
  179. data = self.getValidData(a)
  180. response = self.client.post("/post/", data)
  181. location = response["Location"]
  182. match = post_redirect_re.match(location)
  183. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)
  184. data["next"] = "/somewhere/else/"
  185. data["comment"] = "This is another comment"
  186. response = self.client.post("/post/", data)
  187. location = response["Location"]
  188. match = re.search(r"^http://testserver/somewhere/else/\?c=\d+$", location)
  189. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)
  190. data["next"] = "http://badserver/somewhere/else/"
  191. data["comment"] = "This is another comment with an unsafe next url"
  192. response = self.client.post("/post/", data)
  193. location = response["Location"]
  194. match = post_redirect_re.match(location)
  195. self.assertTrue(match != None, "Unsafe redirection to: %s" % location)
  196. def testCommentDoneView(self):
  197. a = Article.objects.get(pk=1)
  198. data = self.getValidData(a)
  199. response = self.client.post("/post/", data)
  200. location = response["Location"]
  201. match = post_redirect_re.match(location)
  202. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)
  203. pk = int(match.group('pk'))
  204. response = self.client.get(location)
  205. self.assertTemplateUsed(response, "comments/posted.html")
  206. self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=pk))
  207. def testCommentNextWithQueryString(self):
  208. """
  209. The `next` key needs to handle already having a query string (#10585)
  210. """
  211. a = Article.objects.get(pk=1)
  212. data = self.getValidData(a)
  213. data["next"] = "/somewhere/else/?foo=bar"
  214. data["comment"] = "This is another comment"
  215. response = self.client.post("/post/", data)
  216. location = response["Location"]
  217. match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+$", location)
  218. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)
  219. def testCommentPostRedirectWithInvalidIntegerPK(self):
  220. """
  221. Tests that attempting to retrieve the location specified in the
  222. post redirect, after adding some invalid data to the expected
  223. querystring it ends with, doesn't cause a server error.
  224. """
  225. a = Article.objects.get(pk=1)
  226. data = self.getValidData(a)
  227. data["comment"] = "This is another comment"
  228. response = self.client.post("/post/", data)
  229. location = response["Location"]
  230. broken_location = location + u"\ufffd"
  231. response = self.client.get(broken_location)
  232. self.assertEqual(response.status_code, 200)
  233. def testCommentNextWithQueryStringAndAnchor(self):
  234. """
  235. The `next` key needs to handle already having an anchor. Refs #13411.
  236. """
  237. # With a query string also.
  238. a = Article.objects.get(pk=1)
  239. data = self.getValidData(a)
  240. data["next"] = "/somewhere/else/?foo=bar#baz"
  241. data["comment"] = "This is another comment"
  242. response = self.client.post("/post/", data)
  243. location = response["Location"]
  244. match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+#baz$", location)
  245. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)
  246. # Without a query string
  247. a = Article.objects.get(pk=1)
  248. data = self.getValidData(a)
  249. data["next"] = "/somewhere/else/#baz"
  250. data["comment"] = "This is another comment"
  251. response = self.client.post("/post/", data)
  252. location = response["Location"]
  253. match = re.search(r"^http://testserver/somewhere/else/\?c=\d+#baz$", location)
  254. self.assertTrue(match != None, "Unexpected redirect location: %s" % location)