PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/regressiontests/comment_tests/tests/comment_view_tests.py

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