/apps/forums/tests/test_views.py

https://github.com/erikrose/kitsune · Python · 277 lines · 193 code · 50 blank · 34 comment · 2 complexity · eb35a366d648896093581018734c9d91 MD5 · raw file

  1. from mock import patch, Mock
  2. from nose.tools import eq_
  3. from django.contrib.auth.models import User
  4. from forums.models import Forum, Thread
  5. from forums.tests import ForumTestCase
  6. from forums.events import NewThreadEvent, NewPostEvent
  7. from sumo.tests import get, post
  8. from sumo.urlresolvers import reverse
  9. class PostPermissionsTests(ForumTestCase):
  10. """Test post views permissions."""
  11. def test_read_without_permission(self):
  12. """Listing posts without the view_in_forum permission should 404."""
  13. response = get(self.client, 'forums.posts',
  14. args=['restricted-forum', 6])
  15. eq_(404, response.status_code)
  16. def test_reply_without_view_permission(self):
  17. """Posting without view_in_forum permission should 404."""
  18. self.client.login(username='jsocol', password='testpass')
  19. response = post(self.client, 'forums.reply', {'content': 'Blahs'},
  20. args=['restricted-forum', 6])
  21. eq_(404, response.status_code)
  22. def test_reply_without_post_permission(self):
  23. """Posting without post_in_forum permission should 403."""
  24. self.client.login(username='jsocol', password='testpass')
  25. with patch.object(Forum, 'allows_viewing_by', Mock(return_value=True)):
  26. response = post(self.client, 'forums.reply', {'content': 'Blahs'},
  27. args=['restricted-forum', 6])
  28. eq_(403, response.status_code)
  29. def test_reply_thread_405(self):
  30. """Replying to a thread via a GET instead of a POST request."""
  31. f = Forum.objects.all()[0]
  32. t = f.thread_set.all()[0]
  33. self.client.login(username='jsocol', password='testpass')
  34. response = get(self.client, 'forums.lock_thread',
  35. args=[f.slug, t.id])
  36. eq_(405, response.status_code)
  37. class ThreadAuthorityPermissionsTests(ForumTestCase):
  38. """Test thread views authority permissions."""
  39. def test_new_thread_without_view_permission(self):
  40. """Making a new thread without view permission should 404."""
  41. self.client.login(username='jsocol', password='testpass')
  42. response = post(self.client, 'forums.new_thread',
  43. {'title': 'Blahs', 'content': 'Blahs'},
  44. args=['restricted-forum'])
  45. eq_(404, response.status_code)
  46. def test_new_thread_without_post_permission(self):
  47. """Making a new thread without post permission should 403."""
  48. self.client.login(username='jsocol', password='testpass')
  49. with patch.object(Forum, 'allows_viewing_by', Mock(return_value=True)):
  50. response = post(self.client, 'forums.new_thread',
  51. {'title': 'Blahs', 'content': 'Blahs'},
  52. args=['restricted-forum'])
  53. eq_(403, response.status_code)
  54. def test_watch_GET_405(self):
  55. """Watch forum with HTTP GET results in 405."""
  56. self.client.login(username='rrosario', password='testpass')
  57. f = Forum.objects.filter()[0]
  58. response = get(self.client, 'forums.watch_forum', args=[f.id])
  59. eq_(405, response.status_code)
  60. def test_watch_forum_without_permission(self):
  61. """Watching forums without the view_in_forum permission should 404."""
  62. self.client.login(username='jsocol', password='testpass')
  63. response = self.client.post(reverse('forums.watch_forum',
  64. args=['restricted-forum']),
  65. {'watch': 'yes'}, follow=False)
  66. eq_(404, response.status_code)
  67. def test_watch_thread_without_permission(self):
  68. """Watching threads without the view_in_forum permission should 404."""
  69. self.client.login(username='jsocol', password='testpass')
  70. response = self.client.post(reverse('forums.watch_thread',
  71. args=['restricted-forum', 6]),
  72. {'watch': 'yes'}, follow=False)
  73. eq_(404, response.status_code)
  74. def test_read_without_permission(self):
  75. """Listing threads without the view_in_forum permission should 404."""
  76. response = get(self.client, 'forums.threads',
  77. args=['restricted-forum'])
  78. eq_(404, response.status_code)
  79. class ThreadTests(ForumTestCase):
  80. """Test thread views."""
  81. def test_watch_forum(self):
  82. """Watch then unwatch a forum."""
  83. self.client.login(username='rrosario', password='testpass')
  84. user = User.objects.get(username='rrosario')
  85. f = Forum.objects.filter()[0]
  86. post(self.client, 'forums.watch_forum', {'watch': 'yes'},
  87. args=[f.slug])
  88. assert NewThreadEvent.is_notifying(user, f)
  89. # NewPostEvent is not notifying.
  90. assert not NewPostEvent.is_notifying(user, f.last_post)
  91. post(self.client, 'forums.watch_forum', {'watch': 'no'},
  92. args=[f.slug])
  93. assert not NewThreadEvent.is_notifying(user, f)
  94. def test_watch_thread(self):
  95. """Watch then unwatch a thread."""
  96. self.client.login(username='rrosario', password='testpass')
  97. user = User.objects.get(username='rrosario')
  98. t = Thread.objects.filter()[1]
  99. post(self.client, 'forums.watch_thread', {'watch': 'yes'},
  100. args=[t.forum.slug, t.id])
  101. assert NewPostEvent.is_notifying(user, t)
  102. # NewThreadEvent is not notifying.
  103. assert not NewThreadEvent.is_notifying(user, t.forum)
  104. post(self.client, 'forums.watch_thread', {'watch': 'no'},
  105. args=[t.forum.slug, t.id])
  106. assert not NewPostEvent.is_notifying(user, t)
  107. def test_edit_thread(self):
  108. """Changing thread title works."""
  109. self.client.login(username='jsocol', password='testpass')
  110. f = Forum.objects.filter()[0]
  111. t_creator = User.objects.get(username='jsocol')
  112. t = f.thread_set.filter(creator=t_creator)[0]
  113. post(self.client, 'forums.edit_thread', {'title': 'A new title'},
  114. args=[f.slug, t.id])
  115. edited_t = f.thread_set.get(pk=t.id)
  116. eq_('Sticky Thread', t.title)
  117. eq_('A new title', edited_t.title)
  118. def test_edit_thread_moderator(self):
  119. """Editing post as a moderator works."""
  120. self.client.login(username='pcraciunoiu', password='testpass')
  121. t = Thread.objects.get(pk=2)
  122. f = t.forum
  123. eq_('Sticky Thread', t.title)
  124. r = post(self.client, 'forums.edit_thread',
  125. {'title': 'new title'}, args=[f.slug, t.id])
  126. eq_(200, r.status_code)
  127. edited_t = Thread.uncached.get(pk=2)
  128. eq_('new title', edited_t.title)
  129. def test_new_thread_redirect(self):
  130. """Posting a new thread should redirect."""
  131. self.client.login(username='pcraciunoiu', password='testpass')
  132. f = Forum.objects.get(pk=1)
  133. url = reverse('forums.new_thread', args=[f.slug])
  134. data = {'title': 'some title', 'content': 'some content'}
  135. r = self.client.post(url, data, follow=False)
  136. eq_(302, r.status_code)
  137. assert f.slug in r['location']
  138. assert 'last=' in r['location']
  139. def test_reply_redirect(self):
  140. """Posting a reply should redirect."""
  141. self.client.login(username='pcraciunoiu', password='testpass')
  142. t = Thread.objects.get(pk=2)
  143. url = reverse('forums.reply', args=[t.forum.slug, t.id])
  144. data = {'content': 'some content'}
  145. r = self.client.post(url, data, follow=False)
  146. eq_(302, r.status_code)
  147. assert t.forum.slug in r['location']
  148. assert str(t.id) in r['location']
  149. assert 'last=' in r['location']
  150. class ThreadPermissionsTests(ForumTestCase):
  151. def setUp(self):
  152. super(ThreadPermissionsTests, self).setUp()
  153. self.forum = Forum.objects.all()[0]
  154. admin = User.objects.get(pk=1)
  155. self.thread = self.forum.thread_set.filter(creator=admin)[0]
  156. self.post = self.thread.post_set.all()[0]
  157. # Login for testing 403s
  158. self.client.login(username='jsocol', password='testpass')
  159. def tearDown(self):
  160. self.client.logout()
  161. super(ThreadPermissionsTests, self).tearDown()
  162. def test_edit_thread_403(self):
  163. """Editing a thread without permissions returns 403."""
  164. response = get(self.client, 'forums.edit_thread',
  165. args=[self.forum.slug, self.thread.id])
  166. eq_(403, response.status_code)
  167. def test_edit_locked_thread_403(self):
  168. """Editing a locked thread returns 403."""
  169. jsocol = User.objects.get(username='jsocol')
  170. t = self.forum.thread_set.filter(creator=jsocol, is_locked=True)[0]
  171. response = get(self.client, 'forums.edit_thread',
  172. args=[self.forum.slug, t.id])
  173. eq_(403, response.status_code)
  174. def test_delete_thread_403(self):
  175. """Deleting a thread without permissions returns 403."""
  176. response = get(self.client, 'forums.delete_thread',
  177. args=[self.forum.slug, self.thread.id])
  178. eq_(403, response.status_code)
  179. def test_sticky_thread_405(self):
  180. """Marking a thread sticky with a HTTP GET returns 405."""
  181. response = get(self.client, 'forums.sticky_thread',
  182. args=[self.forum.slug, self.thread.id])
  183. eq_(405, response.status_code)
  184. def test_sticky_thread_403(self):
  185. """Marking a thread sticky without permissions returns 403."""
  186. response = post(self.client, 'forums.sticky_thread',
  187. args=[self.forum.slug, self.thread.id])
  188. eq_(403, response.status_code)
  189. def test_locked_thread_403(self):
  190. """Marking a thread locked without permissions returns 403."""
  191. response = post(self.client, 'forums.lock_thread',
  192. args=[self.forum.slug, self.thread.id])
  193. eq_(403, response.status_code)
  194. def test_locked_thread_405(self):
  195. """Marking a thread locked via a GET instead of a POST request."""
  196. response = get(self.client, 'forums.lock_thread',
  197. args=[self.forum.slug, self.thread.id])
  198. eq_(405, response.status_code)
  199. def test_move_thread_403(self):
  200. """Moving a thread without permissions returns 403."""
  201. response = post(self.client, 'forums.move_thread', {'forum': 2},
  202. args=[self.forum.slug, self.thread.id])
  203. eq_(403, response.status_code)
  204. def test_move_thread_405(self):
  205. """Moving a thread via a GET instead of a POST request."""
  206. response = get(self.client, 'forums.move_thread',
  207. args=[self.forum.slug, self.thread.id])
  208. eq_(405, response.status_code)
  209. def test_move_thread(self):
  210. """Move a thread."""
  211. self.client.login(username='rrosario', password='testpass')
  212. response = post(self.client, 'forums.move_thread',
  213. {'forum': 2},
  214. args=[self.forum.slug, self.thread.id])
  215. eq_(200, response.status_code)
  216. thread = Thread.uncached.get(pk=self.thread.pk)
  217. eq_(2, thread.forum.id)
  218. def test_post_edit_403(self):
  219. """Editing a post without permissions returns 403."""
  220. response = get(self.client, 'forums.edit_post',
  221. args=[self.forum.slug, self.thread.id, self.post.id])
  222. eq_(403, response.status_code)
  223. def test_post_delete_403(self):
  224. """Deleting a post without permissions returns 403."""
  225. response = get(self.client, 'forums.delete_post',
  226. args=[self.forum.slug, self.thread.id, self.post.id])
  227. eq_(403, response.status_code)