test_threadview.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. from unittest.mock import Mock
  2. from .. import test
  3. from ...acl import useracl
  4. from ...acl.test import patch_user_acl
  5. from ...categories.models import Category
  6. from ...conf import settings
  7. from ...conftest import get_cache_versions
  8. from ...users.test import AuthenticatedUserTestCase
  9. from ..checksums import update_post_checksum
  10. from ..events import record_event
  11. from ..moderation import hide_post
  12. from ..moderation import threads as threads_moderation
  13. cache_versions = get_cache_versions()
  14. def patch_category_acl(new_acl=None):
  15. def patch_acl(_, user_acl):
  16. category = Category.objects.get(slug="first-category")
  17. category_acl = user_acl["categories"][category.id]
  18. # reset category ACL to single predictable state
  19. category_acl.update(
  20. {
  21. "can_see": 1,
  22. "can_browse": 1,
  23. "can_see_all_threads": 1,
  24. "can_see_own_threads": 0,
  25. "can_hide_threads": 0,
  26. "can_approve_content": 0,
  27. "can_edit_posts": 0,
  28. "can_hide_posts": 0,
  29. "can_hide_own_posts": 0,
  30. "can_close_threads": 0,
  31. "post_edit_time": 0,
  32. "can_hide_events": 0,
  33. }
  34. )
  35. if new_acl:
  36. category_acl.update(new_acl)
  37. return patch_user_acl(patch_acl)
  38. class ThreadViewTestCase(AuthenticatedUserTestCase):
  39. def setUp(self):
  40. super().setUp()
  41. self.category = Category.objects.get(slug="first-category")
  42. self.thread = test.post_thread(category=self.category)
  43. class ThreadVisibilityTests(ThreadViewTestCase):
  44. def test_thread_displays(self):
  45. """thread view has no showstoppers"""
  46. response = self.client.get(self.thread.get_absolute_url())
  47. self.assertContains(response, self.thread.title)
  48. def test_view_shows_owner_thread(self):
  49. """view handles "owned threads" only"""
  50. with patch_category_acl({"can_see_all_threads": 0}):
  51. response = self.client.get(self.thread.get_absolute_url())
  52. self.assertEqual(response.status_code, 404)
  53. self.thread.starter = self.user
  54. self.thread.save()
  55. response = self.client.get(self.thread.get_absolute_url())
  56. self.assertContains(response, self.thread.title)
  57. def test_view_validates_category_permissions(self):
  58. """view validates category visiblity"""
  59. with patch_category_acl({"can_see": 0}):
  60. response = self.client.get(self.thread.get_absolute_url())
  61. self.assertEqual(response.status_code, 404)
  62. with patch_category_acl({"can_browse": 0}):
  63. response = self.client.get(self.thread.get_absolute_url())
  64. self.assertEqual(response.status_code, 404)
  65. def test_view_shows_unapproved_thread(self):
  66. """view handles unapproved thread"""
  67. with patch_category_acl({"can_approve_content": 0}):
  68. self.thread.is_unapproved = True
  69. self.thread.save()
  70. response = self.client.get(self.thread.get_absolute_url())
  71. self.assertEqual(response.status_code, 404)
  72. # grant permission to see unapproved content
  73. with patch_category_acl({"can_approve_content": 1}):
  74. response = self.client.get(self.thread.get_absolute_url())
  75. self.assertContains(response, self.thread.title)
  76. # make test user thread's owner and remove permission to see unapproved
  77. # user should be able to see thread as its author anyway
  78. self.thread.starter = self.user
  79. self.thread.save()
  80. with patch_category_acl({"can_approve_content": 0}):
  81. response = self.client.get(self.thread.get_absolute_url())
  82. self.assertContains(response, self.thread.title)
  83. def test_view_shows_hidden_thread(self):
  84. """view handles hidden thread"""
  85. with patch_category_acl({"can_hide_threads": 0}):
  86. self.thread.is_hidden = True
  87. self.thread.save()
  88. response = self.client.get(self.thread.get_absolute_url())
  89. self.assertEqual(response.status_code, 404)
  90. # threads owners are not extempt from hidden threads check
  91. self.thread.starter = self.user
  92. self.thread.save()
  93. response = self.client.get(self.thread.get_absolute_url())
  94. self.assertEqual(response.status_code, 404)
  95. # grant permission to see hidden content
  96. with patch_category_acl({"can_hide_threads": 1}):
  97. response = self.client.get(self.thread.get_absolute_url())
  98. self.assertContains(response, self.thread.title)
  99. class ThreadPostsVisibilityTests(ThreadViewTestCase):
  100. def test_post_renders(self):
  101. """post renders"""
  102. post = test.reply_thread(self.thread, poster=self.user)
  103. response = self.client.get(self.thread.get_absolute_url())
  104. self.assertContains(response, post.get_absolute_url())
  105. def test_invalid_post_renders(self):
  106. """invalid post renders"""
  107. post = test.reply_thread(self.thread, poster=self.user)
  108. post.parsed = "fiddled post content"
  109. post.save()
  110. response = self.client.get(self.thread.get_absolute_url())
  111. self.assertContains(response, post.get_absolute_url())
  112. self.assertContains(response, "This post's contents cannot be displayed.")
  113. self.assertNotContains(response, post.parsed)
  114. def test_hidden_post_visibility(self):
  115. """hidden post renders correctly"""
  116. post = test.reply_thread(self.thread, message="Hello, I'm hidden post!")
  117. hide_post(self.user, post)
  118. response = self.client.get(self.thread.get_absolute_url())
  119. self.assertContains(response, post.get_absolute_url())
  120. self.assertContains(
  121. response, "This post is hidden. You cannot not see its contents."
  122. )
  123. self.assertNotContains(response, post.parsed)
  124. # posts authors are not extempt from seeing hidden posts content
  125. post.posted_by = self.user
  126. post.save()
  127. response = self.client.get(self.thread.get_absolute_url())
  128. self.assertContains(response, post.get_absolute_url())
  129. self.assertContains(
  130. response, "This post is hidden. You cannot not see its contents."
  131. )
  132. self.assertNotContains(response, post.parsed)
  133. # permission to hide own posts isn't enought to see post content
  134. with patch_category_acl({"can_hide_own_posts": 1}):
  135. response = self.client.get(self.thread.get_absolute_url())
  136. self.assertContains(response, post.get_absolute_url())
  137. self.assertContains(
  138. response, "This post is hidden. You cannot not see its contents."
  139. )
  140. self.assertNotContains(response, post.parsed)
  141. # post's content is displayed after permission to see posts is granted
  142. with patch_category_acl({"can_hide_posts": 1}):
  143. response = self.client.get(self.thread.get_absolute_url())
  144. self.assertContains(response, post.get_absolute_url())
  145. self.assertContains(
  146. response,
  147. "This post is hidden. Only users with permission may see its contents.",
  148. )
  149. self.assertNotContains(
  150. response, "This post is hidden. You cannot not see its contents."
  151. )
  152. self.assertContains(response, post.parsed)
  153. def test_unapproved_post_visibility(self):
  154. """
  155. unapproved post renders for its author and users with perm to approve content
  156. """
  157. post = test.reply_thread(self.thread, is_unapproved=True)
  158. # post is hdden because we aren't its author nor user with permission to approve
  159. response = self.client.get(self.thread.get_absolute_url())
  160. self.assertNotContains(response, post.get_absolute_url())
  161. # post displays because we have permission to approve unapproved content
  162. with patch_category_acl({"can_approve_content": 1}):
  163. response = self.client.get(self.thread.get_absolute_url())
  164. self.assertContains(response, post.get_absolute_url())
  165. self.assertContains(response, "This post is unapproved.")
  166. self.assertContains(response, post.parsed)
  167. # post displays because we are its author
  168. with patch_category_acl({"can_approve_content": 0}):
  169. post.poster = self.user
  170. post.save()
  171. response = self.client.get(self.thread.get_absolute_url())
  172. self.assertContains(response, post.get_absolute_url())
  173. self.assertContains(response, "This post is unapproved.")
  174. self.assertContains(response, post.parsed)
  175. class ThreadEventVisibilityTests(ThreadViewTestCase):
  176. def test_thread_events_render(self):
  177. """different thread events render"""
  178. TEST_ACTIONS = [
  179. (
  180. threads_moderation.pin_thread_globally,
  181. "Thread has been pinned globally.",
  182. ),
  183. (threads_moderation.pin_thread_locally, "Thread has been pinned locally."),
  184. (threads_moderation.unpin_thread, "Thread has been unpinned."),
  185. (threads_moderation.approve_thread, "Thread has been approved."),
  186. (threads_moderation.close_thread, "Thread has been closed."),
  187. (threads_moderation.open_thread, "Thread has been opened."),
  188. (threads_moderation.hide_thread, "Thread has been made hidden."),
  189. (threads_moderation.unhide_thread, "Thread has been revealed."),
  190. ]
  191. self.thread.is_unapproved = True
  192. self.thread.save()
  193. for action, message in TEST_ACTIONS:
  194. self.thread.post_set.filter(is_event=True).delete()
  195. with patch_category_acl({"can_approve_content": 1, "can_hide_threads": 1}):
  196. user_acl = useracl.get_user_acl(self.user, cache_versions)
  197. request = Mock(user=self.user, user_acl=user_acl, user_ip="127.0.0.1")
  198. action(request, self.thread)
  199. event = self.thread.post_set.filter(is_event=True)[0]
  200. # event renders
  201. response = self.client.get(self.thread.get_absolute_url())
  202. self.assertContains(response, event.get_absolute_url())
  203. self.assertContains(response, message)
  204. # hidden events don't render without permission
  205. with patch_category_acl({"can_approve_content": 1, "can_hide_threads": 1}):
  206. hide_post(self.user, event)
  207. response = self.client.get(self.thread.get_absolute_url())
  208. self.assertNotContains(response, event.get_absolute_url())
  209. self.assertNotContains(response, message)
  210. # hidden event renders with permission
  211. with patch_category_acl(
  212. {"can_approve_content": 1, "can_hide_threads": 1, "can_hide_events": 1}
  213. ):
  214. hide_post(self.user, event)
  215. response = self.client.get(self.thread.get_absolute_url())
  216. self.assertContains(response, event.get_absolute_url())
  217. self.assertContains(response, message)
  218. self.assertContains(response, "Hidden by")
  219. # Event is only loaded if thread has events flag
  220. with patch_category_acl(
  221. {"can_approve_content": 1, "can_hide_threads": 1, "can_hide_events": 1}
  222. ):
  223. self.thread.has_events = False
  224. self.thread.save()
  225. response = self.client.get(self.thread.get_absolute_url())
  226. self.assertNotContains(response, event.get_absolute_url())
  227. def test_events_limit(self):
  228. """forum will trim oldest events if theres more than allowed by config"""
  229. events_limit = settings.MISAGO_EVENTS_PER_PAGE
  230. events = []
  231. for _ in range(events_limit + 5):
  232. request = Mock(user=self.user, user_ip="127.0.0.1")
  233. event = record_event(request, self.thread, "closed")
  234. events.append(event)
  235. # test that only events within limits were rendered
  236. response = self.client.get(self.thread.get_absolute_url())
  237. for event in events[5:]:
  238. self.assertContains(response, event.get_absolute_url())
  239. for event in events[:5]:
  240. self.assertNotContains(response, event.get_absolute_url())
  241. def test_events_dont_take_space(self):
  242. """events dont take space away from posts"""
  243. posts_limit = settings.MISAGO_POSTS_PER_PAGE
  244. events_limit = settings.MISAGO_EVENTS_PER_PAGE
  245. events = []
  246. for _ in range(events_limit + 5):
  247. request = Mock(user=self.user, user_ip="127.0.0.1")
  248. event = record_event(request, self.thread, "closed")
  249. events.append(event)
  250. posts = []
  251. for _ in range(posts_limit - 1):
  252. post = test.reply_thread(self.thread)
  253. posts.append(post)
  254. # test that all events and posts within limits were rendered
  255. response = self.client.get(self.thread.get_absolute_url())
  256. for event in events[5:]:
  257. self.assertContains(response, event.get_absolute_url())
  258. for post in posts:
  259. self.assertContains(response, post.get_absolute_url())
  260. # add second page to thread with more events
  261. for _ in range(posts_limit):
  262. post = test.reply_thread(self.thread)
  263. for _ in range(events_limit):
  264. request = Mock(user=self.user, user_ip="127.0.0.1")
  265. event = record_event(request, self.thread, "closed")
  266. events.append(event)
  267. # see first page
  268. response = self.client.get(self.thread.get_absolute_url())
  269. for event in events[5:events_limit]:
  270. self.assertContains(response, event.get_absolute_url())
  271. for post in posts[: posts_limit - 1]:
  272. self.assertContains(response, post.get_absolute_url())
  273. # see second page
  274. response = self.client.get("%s2/" % self.thread.get_absolute_url())
  275. for event in events[5 + events_limit :]:
  276. self.assertContains(response, event.get_absolute_url())
  277. for post in posts[posts_limit - 1 :]:
  278. self.assertContains(response, post.get_absolute_url())
  279. def test_changed_thread_title_event_renders(self):
  280. """changed thread title event renders"""
  281. request = Mock(user=self.user, user_ip="127.0.0.1")
  282. threads_moderation.change_thread_title(
  283. request, self.thread, "Lorem renamed ipsum!"
  284. )
  285. event = self.thread.post_set.filter(is_event=True)[0]
  286. self.assertEqual(event.event_type, "changed_title")
  287. # event renders
  288. response = self.client.get(self.thread.get_absolute_url())
  289. self.assertContains(response, event.get_absolute_url())
  290. self.assertContains(response, "title has been changed from")
  291. self.assertContains(response, self.thread.title)
  292. def test_thread_move_event_renders(self):
  293. """moved thread event renders"""
  294. self.thread.category = self.thread.category.parent
  295. self.thread.save()
  296. request = Mock(user=self.user, user_ip="127.0.0.1")
  297. threads_moderation.move_thread(request, self.thread, self.category)
  298. event = self.thread.post_set.filter(is_event=True)[0]
  299. self.assertEqual(event.event_type, "moved")
  300. # event renders
  301. response = self.client.get(self.thread.get_absolute_url())
  302. self.assertContains(response, event.get_absolute_url())
  303. self.assertContains(response, "Thread has been moved from")
  304. def test_thread_merged_event_renders(self):
  305. """merged thread event renders"""
  306. request = Mock(user=self.user, user_ip="127.0.0.1")
  307. other_thread = test.post_thread(category=self.category)
  308. threads_moderation.merge_thread(request, self.thread, other_thread)
  309. event = self.thread.post_set.filter(is_event=True)[0]
  310. self.assertEqual(event.event_type, "merged")
  311. # event renders
  312. response = self.client.get(self.thread.get_absolute_url())
  313. self.assertContains(response, event.get_absolute_url())
  314. self.assertContains(response, "thread has been merged into this thread")
  315. class ThreadAttachmentsViewTests(ThreadViewTestCase):
  316. def mock_attachment_cache(self, data):
  317. json = {
  318. "url": {},
  319. "size": 16914,
  320. "filename": "Archiwum.zip",
  321. "filetype": "ZIP",
  322. "is_image": False,
  323. "uploaded_on": "2016-10-22T21:17:40.408710Z",
  324. "uploader_name": "User",
  325. }
  326. json.update(data)
  327. return json
  328. def test_attachments_display(self):
  329. """thread posts show list of attachments below them"""
  330. post = self.thread.first_post
  331. post.attachments_cache = [
  332. self.mock_attachment_cache(
  333. {
  334. "url": {
  335. "index": "/attachment/loremipsum-123/",
  336. "thumb": None,
  337. "uploader": "/user/user-123/",
  338. },
  339. "filename": "Archiwum-1.zip",
  340. }
  341. ),
  342. self.mock_attachment_cache(
  343. {
  344. "url": {
  345. "index": "/attachment/loremipsum-223/",
  346. "thumb": "/attachment/thumb/loremipsum-223/",
  347. "uploader": "/user/user-223/",
  348. },
  349. "is_image": True,
  350. "filename": "Archiwum-2.zip",
  351. }
  352. ),
  353. self.mock_attachment_cache(
  354. {
  355. "url": {
  356. "index": "/attachment/loremipsum-323/",
  357. "thumb": None,
  358. "uploader": "/user/user-323/",
  359. },
  360. "filename": "Archiwum-3.zip",
  361. }
  362. ),
  363. ]
  364. post.save()
  365. # attachments render
  366. response = self.client.get(self.thread.get_absolute_url())
  367. for attachment in post.attachments_cache:
  368. self.assertContains(response, attachment["filename"])
  369. self.assertContains(response, attachment["uploader_name"])
  370. self.assertContains(response, attachment["url"]["index"])
  371. self.assertContains(response, attachment["url"]["uploader"])
  372. if attachment["url"]["thumb"]:
  373. self.assertContains(response, attachment["url"]["thumb"])
  374. class ThreadPollViewTests(ThreadViewTestCase):
  375. def test_poll_voted_display(self):
  376. """view has no showstoppers when displaying voted poll"""
  377. poll = test.post_poll(self.thread, self.user)
  378. response = self.client.get(self.thread.get_absolute_url())
  379. self.assertContains(response, poll.question)
  380. self.assertContains(response, "4 votes")
  381. self.assertNotContains(response, "Save your vote")
  382. def test_poll_unvoted_display(self):
  383. """view has no showstoppers when displaying poll vote form"""
  384. poll = test.post_poll(self.thread, self.user)
  385. poll.pollvote_set.all().delete()
  386. response = self.client.get(self.thread.get_absolute_url())
  387. self.assertContains(response, poll.question)
  388. self.assertContains(response, "Save your vote")
  389. def test_poll_anonymous_view(self):
  390. """view has no showstoppers when displaying poll to anon user"""
  391. poll = test.post_poll(self.thread, self.user)
  392. self.logout_user()
  393. response = self.client.get(self.thread.get_absolute_url())
  394. self.assertContains(response, poll.question)
  395. self.assertContains(response, "4 votes")
  396. self.assertNotContains(response, "Save your vote")
  397. class ThreadLikedPostsViewTests(ThreadViewTestCase):
  398. def test_liked_posts_display(self):
  399. """view has no showstoppers on displaying posts with likes"""
  400. test.like_post(self.thread.first_post, self.user)
  401. response = self.client.get(self.thread.get_absolute_url())
  402. self.assertContains(response, '"is_liked": true')
  403. def test_liked_posts_no_permission(self):
  404. """
  405. view has no showstoppers on displaying posts with likes without perm
  406. """
  407. test.like_post(self.thread.first_post, self.user)
  408. with patch_category_acl({"can_see_posts_likes": 0}):
  409. response = self.client.get(self.thread.get_absolute_url())
  410. self.assertNotContains(response, '"is_liked": true')
  411. self.assertNotContains(response, '"is_liked": false')
  412. self.assertContains(response, '"is_liked": null')
  413. class ThreadAnonViewTests(ThreadViewTestCase):
  414. def test_anonymous_user_view_no_showstoppers_display(self):
  415. """kitchensink thread view has no showstoppers for anons"""
  416. request = Mock(user=self.user, user_ip="127.0.0.1")
  417. poll = test.post_poll(self.thread, self.user)
  418. event = record_event(request, self.thread, "closed")
  419. hidden_event = record_event(request, self.thread, "opened")
  420. hide_post(self.user, hidden_event)
  421. unapproved_post = test.reply_thread(self.thread, is_unapproved=True)
  422. post = test.reply_thread(self.thread)
  423. self.logout_user()
  424. response = self.client.get(self.thread.get_absolute_url())
  425. self.assertContains(response, poll.question)
  426. self.assertContains(response, event.get_absolute_url())
  427. self.assertContains(response, post.get_absolute_url())
  428. self.assertNotContains(response, hidden_event.get_absolute_url())
  429. self.assertNotContains(response, unapproved_post.get_absolute_url())
  430. class ThreadUnicodeSupportTests(ThreadViewTestCase):
  431. def test_category_name(self):
  432. """unicode in category name causes no showstopper"""
  433. self.category.name = "Łódź"
  434. self.category.save()
  435. with patch_category_acl():
  436. response = self.client.get(self.thread.get_absolute_url())
  437. self.assertEqual(response.status_code, 200)
  438. def test_thread_title(self):
  439. """unicode in thread title causes no showstopper"""
  440. self.thread.title = "Łódź"
  441. self.thread.slug = "Lodz"
  442. self.thread.save()
  443. with patch_category_acl():
  444. response = self.client.get(self.thread.get_absolute_url())
  445. self.assertEqual(response.status_code, 200)
  446. def test_post_content(self):
  447. """unicode in thread title causes no showstopper"""
  448. self.thread.first_post.original = "Łódź"
  449. self.thread.first_post.parsed = "<p>Łódź</p>"
  450. update_post_checksum(self.thread.first_post)
  451. self.thread.first_post.save()
  452. with patch_category_acl():
  453. response = self.client.get(self.thread.get_absolute_url())
  454. self.assertEqual(response.status_code, 200)
  455. def test_user_rank(self):
  456. """unicode in user rank causes no showstopper"""
  457. self.user.title = "Łódź"
  458. self.user.rank.name = "Łódź"
  459. self.user.rank.title = "Łódź"
  460. self.user.rank.save()
  461. self.user.save()
  462. with patch_category_acl():
  463. response = self.client.get(self.thread.get_absolute_url())
  464. self.assertEqual(response.status_code, 200)