test_forumthreads_view.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. from django.contrib.auth import get_user_model
  2. from django.core.urlresolvers import reverse
  3. from django.utils.translation import ugettext as _
  4. from misago.acl import add_acl
  5. from misago.acl.testutils import override_acl
  6. from misago.forums.models import Forum
  7. from misago.users.testutils import AuthenticatedUserTestCase
  8. from misago.threads import testutils
  9. from misago.threads.models import Thread, Label
  10. from misago.threads.moderation import ModerationError
  11. from misago.threads.views.generic.forum import (ForumActions, ForumFiltering,
  12. ForumThreads)
  13. class ForumViewHelperTestCase(AuthenticatedUserTestCase):
  14. def setUp(self):
  15. super(ForumViewHelperTestCase, self).setUp()
  16. self.forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  17. self.forum.labels = []
  18. def override_acl(self, new_acl):
  19. new_acl.update({'can_browse': True})
  20. forums_acl = self.user.acl
  21. forums_acl['visible_forums'].append(self.forum.pk)
  22. forums_acl['forums'][self.forum.pk] = new_acl
  23. override_acl(self.user, forums_acl)
  24. self.forum.acl = {}
  25. add_acl(self.user, self.forum)
  26. class MockRequest(object):
  27. def __init__(self, user, method='GET', POST=None):
  28. self.POST = POST or {}
  29. self.user = user
  30. self.session = {}
  31. self.path = '/forum/fake-forum-1/'
  32. class ActionsTests(ForumViewHelperTestCase):
  33. def setUp(self):
  34. super(ActionsTests, self).setUp()
  35. self.user._misago_real_ip = '127.0.0.1'
  36. Label.objects.clear_cache()
  37. def tearDown(self):
  38. super(ActionsTests, self).tearDown()
  39. Label.objects.clear_cache()
  40. def test_label_actions(self):
  41. """ForumActions initializes list with label actions"""
  42. self.override_acl({
  43. 'can_change_threads_labels': 0,
  44. })
  45. actions = ForumActions(user=self.user, forum=self.forum)
  46. self.assertEqual(actions.available_actions, [])
  47. self.override_acl({
  48. 'can_change_threads_labels': 1,
  49. })
  50. actions = ForumActions(user=self.user, forum=self.forum)
  51. self.assertEqual(actions.available_actions, [])
  52. self.override_acl({
  53. 'can_change_threads_labels': 2,
  54. })
  55. actions = ForumActions(user=self.user, forum=self.forum)
  56. self.assertEqual(actions.available_actions, [])
  57. label = Label.objects.create(name="Mock Label", slug="mock-label")
  58. self.forum.labels = [label]
  59. actions = ForumActions(user=self.user, forum=self.forum)
  60. self.assertEqual(actions.available_actions, [
  61. {
  62. 'action': 'label:%s' % label.slug,
  63. 'icon': 'tag',
  64. 'name': _('Label as "%(label)s"') % {'label': label.name}
  65. },
  66. {
  67. 'action': 'unlabel',
  68. 'icon': 'times-circle',
  69. 'name': _("Remove labels")
  70. },
  71. ])
  72. def test_pin_unpin_actions(self):
  73. """ForumActions initializes list with pin and unpin actions"""
  74. self.override_acl({
  75. 'can_pin_threads': 0,
  76. })
  77. actions = ForumActions(user=self.user, forum=self.forum)
  78. self.assertEqual(actions.available_actions, [])
  79. self.override_acl({
  80. 'can_pin_threads': 1,
  81. })
  82. actions = ForumActions(user=self.user, forum=self.forum)
  83. self.assertEqual(actions.available_actions, [
  84. {
  85. 'action': 'pin',
  86. 'icon': 'star',
  87. 'name': _("Pin threads")
  88. },
  89. {
  90. 'action': 'unpin',
  91. 'icon': 'circle',
  92. 'name': _("Unpin threads")
  93. },
  94. ])
  95. def test_approve_action(self):
  96. """ForumActions initializes list with approve threads action"""
  97. self.override_acl({
  98. 'can_review_moderated_content': 0,
  99. })
  100. actions = ForumActions(user=self.user, forum=self.forum)
  101. self.assertEqual(actions.available_actions, [])
  102. self.override_acl({
  103. 'can_review_moderated_content': 1,
  104. })
  105. actions = ForumActions(user=self.user, forum=self.forum)
  106. self.assertEqual(actions.available_actions, [
  107. {
  108. 'action': 'approve',
  109. 'icon': 'check',
  110. 'name': _("Approve threads")
  111. },
  112. ])
  113. def test_move_action(self):
  114. """ForumActions initializes list with move threads action"""
  115. self.override_acl({
  116. 'can_move_threads': 0,
  117. })
  118. actions = ForumActions(user=self.user, forum=self.forum)
  119. self.assertEqual(actions.available_actions, [])
  120. self.override_acl({
  121. 'can_move_threads': 1,
  122. })
  123. actions = ForumActions(user=self.user, forum=self.forum)
  124. self.assertEqual(actions.available_actions, [
  125. {
  126. 'action': 'move',
  127. 'icon': 'arrow-right',
  128. 'name': _("Move threads")
  129. },
  130. ])
  131. def test_merge_action(self):
  132. """ForumActions initializes list with merge threads action"""
  133. self.override_acl({
  134. 'can_merge_threads': 0,
  135. })
  136. actions = ForumActions(user=self.user, forum=self.forum)
  137. self.assertEqual(actions.available_actions, [])
  138. self.override_acl({
  139. 'can_merge_threads': 1,
  140. })
  141. actions = ForumActions(user=self.user, forum=self.forum)
  142. self.assertEqual(actions.available_actions, [
  143. {
  144. 'action': 'merge',
  145. 'icon': 'reply-all',
  146. 'name': _("Merge threads")
  147. },
  148. ])
  149. def test_close_open_actions(self):
  150. """ForumActions initializes list with close and open actions"""
  151. self.override_acl({
  152. 'can_close_threads': 0,
  153. })
  154. actions = ForumActions(user=self.user, forum=self.forum)
  155. self.assertEqual(actions.available_actions, [])
  156. self.override_acl({
  157. 'can_close_threads': 1,
  158. })
  159. actions = ForumActions(user=self.user, forum=self.forum)
  160. self.assertEqual(actions.available_actions, [
  161. {
  162. 'action': 'open',
  163. 'icon': 'unlock-alt',
  164. 'name': _("Open threads")
  165. },
  166. {
  167. 'action': 'close',
  168. 'icon': 'lock',
  169. 'name': _("Close threads")
  170. },
  171. ])
  172. def test_hide_delete_actions(self):
  173. """ForumActions initializes list with hide/delete actions"""
  174. self.override_acl({
  175. 'can_hide_threads': 0,
  176. })
  177. actions = ForumActions(user=self.user, forum=self.forum)
  178. self.assertEqual(actions.available_actions, [])
  179. self.override_acl({
  180. 'can_hide_threads': 1,
  181. })
  182. actions = ForumActions(user=self.user, forum=self.forum)
  183. self.assertEqual(actions.available_actions, [
  184. {
  185. 'action': 'unhide',
  186. 'icon': 'eye',
  187. 'name': _("Unhide threads")
  188. },
  189. {
  190. 'action': 'hide',
  191. 'icon': 'eye-slash',
  192. 'name': _("Hide threads")
  193. },
  194. ])
  195. self.override_acl({
  196. 'can_hide_threads': 2,
  197. })
  198. actions = ForumActions(user=self.user, forum=self.forum)
  199. self.assertEqual(actions.available_actions, [
  200. {
  201. 'action': 'unhide',
  202. 'icon': 'eye',
  203. 'name': _("Unhide threads")
  204. },
  205. {
  206. 'action': 'hide',
  207. 'icon': 'eye-slash',
  208. 'name': _("Hide threads")
  209. },
  210. {
  211. 'action': 'delete',
  212. 'icon': 'times',
  213. 'name': _("Delete threads"),
  214. 'confirmation': _("Are you sure you want to delete selected "
  215. "threads? This action can't be undone.")
  216. },
  217. ])
  218. class ForumFilteringTests(ForumViewHelperTestCase):
  219. def setUp(self):
  220. super(ForumFilteringTests, self).setUp()
  221. Label.objects.clear_cache()
  222. def tearDown(self):
  223. super(ForumFilteringTests, self).tearDown()
  224. Label.objects.clear_cache()
  225. def test_get_available_filters(self):
  226. """get_available_filters returns filters varying on forum acl"""
  227. default_acl = {
  228. 'can_see_all_threads': False,
  229. 'can_see_reports': False,
  230. 'can_review_moderated_content': False,
  231. }
  232. cases = (
  233. ('can_see_all_threads', 'my-threads'),
  234. ('can_see_reports', 'reported'),
  235. ('can_review_moderated_content', 'moderated-threads'),
  236. )
  237. for permission, filter_type in cases:
  238. self.override_acl(default_acl)
  239. filtering = ForumFiltering(self.forum, 'misago:forum', {
  240. 'forum_id': self.forum.id,
  241. 'forum_slug': self.forum.slug,
  242. })
  243. available_filters = filtering.get_available_filters()
  244. available_filters = [f['type'] for f in available_filters]
  245. self.assertNotIn(filter_type, available_filters)
  246. acl = default_acl.copy()
  247. acl[permission] = True
  248. self.override_acl(acl)
  249. filtering = ForumFiltering(self.forum, 'misago:forum', {
  250. 'forum_id': self.forum.id,
  251. 'forum_slug': self.forum.slug,
  252. })
  253. available_filters = filtering.get_available_filters()
  254. available_filters = [f['type'] for f in available_filters]
  255. self.assertIn(filter_type, available_filters)
  256. self.forum.labels = [
  257. Label(name='Label A', slug='label-a'),
  258. Label(name='Label B', slug='label-b'),
  259. Label(name='Label C', slug='label-c'),
  260. Label(name='Label D', slug='label-d'),
  261. ]
  262. self.override_acl(default_acl)
  263. ForumFiltering(self.forum, 'misago:forum', {
  264. 'forum_id': self.forum.id,
  265. 'forum_slug': self.forum.slug,
  266. })
  267. available_filters = filtering.get_available_filters()
  268. available_filters = [f['type'] for f in available_filters]
  269. self.assertEqual(len(available_filters), len(self.forum.labels))
  270. for label in self.forum.labels:
  271. self.assertIn(label.slug, available_filters)
  272. def test_clean_kwargs(self):
  273. """clean_kwargs cleans kwargs"""
  274. self.override_acl({
  275. 'can_see_all_threads': True,
  276. 'can_see_reports': True,
  277. 'can_review_moderated_content': True,
  278. })
  279. filtering = ForumFiltering(self.forum, 'misago:forum', {
  280. 'forum_id': self.forum.id,
  281. 'forum_slug': self.forum.slug,
  282. })
  283. available_filters = filtering.get_available_filters()
  284. available_filters = [f['type'] for f in available_filters]
  285. clean_kwargs = filtering.clean_kwargs({'test': 'kwarg'})
  286. self.assertEqual(clean_kwargs, {'test': 'kwarg'})
  287. clean_kwargs = filtering.clean_kwargs({
  288. 'test': 'kwarg',
  289. 'show': 'everything-hue-hue',
  290. })
  291. self.assertEqual(clean_kwargs, {'test': 'kwarg'})
  292. self.assertFalse(filtering.is_active)
  293. self.assertIsNone(filtering.show)
  294. for filter_type in available_filters:
  295. clean_kwargs = filtering.clean_kwargs({
  296. 'test': 'kwarg',
  297. 'show': filter_type,
  298. })
  299. self.assertEqual(clean_kwargs, {
  300. 'test': 'kwarg',
  301. 'show': filter_type,
  302. })
  303. self.assertTrue(filtering.is_active)
  304. self.assertEqual(filtering.show, filter_type)
  305. def test_current(self):
  306. """current returns dict with current filter"""
  307. self.override_acl({
  308. 'can_see_all_threads': True,
  309. 'can_see_reports': True,
  310. 'can_review_moderated_content': True,
  311. })
  312. test_cases = (
  313. ('my-threads', _("My threads")),
  314. ('reported', _("With reported posts")),
  315. ('moderated-threads', _("Moderated threads")),
  316. ('moderated-posts', _("With moderated posts")),
  317. )
  318. for filter_type, name in test_cases:
  319. filtering = ForumFiltering(self.forum, 'misago:forum', {
  320. 'forum_id': self.forum.id,
  321. 'forum_slug': self.forum.slug,
  322. })
  323. filtering.clean_kwargs({'show': filter_type})
  324. self.assertEqual(filtering.current['name'], name)
  325. def test_choices(self):
  326. """choices returns list of dicts with available filters"""
  327. self.override_acl({
  328. 'can_see_all_threads': True,
  329. 'can_see_reports': True,
  330. 'can_review_moderated_content': True,
  331. })
  332. test_cases = (
  333. 'my-threads',
  334. 'reported',
  335. 'moderated-threads',
  336. 'moderated-posts',
  337. )
  338. for filter_type in test_cases:
  339. filtering = ForumFiltering(self.forum, 'misago:forum', {
  340. 'forum_id': self.forum.id,
  341. 'forum_slug': self.forum.slug,
  342. })
  343. filtering.clean_kwargs({'show': filter_type})
  344. choices = [choice['type'] for choice in filtering.choices()]
  345. self.assertNotIn(filter_type, choices)
  346. class ForumThreadsTests(ForumViewHelperTestCase):
  347. def test_empty_list(self):
  348. """list returns empty list of items"""
  349. self.override_acl({
  350. 'can_see_all_threads': True,
  351. 'can_review_moderated_content': False
  352. })
  353. threads = ForumThreads(self.user, self.forum)
  354. threads_list = threads.list()
  355. self.assertEqual(threads_list, [])
  356. def test_list_exception(self):
  357. """
  358. uninitialized list raises exceptions when
  359. page and paginator attributes are accessed
  360. """
  361. self.override_acl({
  362. 'can_see_all_threads': False,
  363. 'can_review_moderated_content': False
  364. })
  365. threads = ForumThreads(self.user, self.forum)
  366. with self.assertRaises(AttributeError):
  367. threads.page
  368. with self.assertRaises(AttributeError):
  369. threads.paginator
  370. def test_list_with_threads(self):
  371. """list returns list of visible threads"""
  372. test_threads = [
  373. testutils.post_thread(
  374. forum=self.forum,
  375. title="Hello, I am thread",
  376. is_moderated=False,
  377. poster=self.user),
  378. testutils.post_thread(
  379. forum=self.forum,
  380. title="Hello, I am moderated thread",
  381. is_moderated=True,
  382. poster=self.user),
  383. testutils.post_thread(
  384. forum=self.forum,
  385. title="Hello, I am other user thread",
  386. is_moderated=False,
  387. poster="Bob"),
  388. testutils.post_thread(
  389. forum=self.forum,
  390. title="Hello, I am other user moderated thread",
  391. is_moderated=True,
  392. poster="Bob"),
  393. ]
  394. self.override_acl({
  395. 'can_see_all_threads': False,
  396. 'can_review_moderated_content': False
  397. })
  398. threads = ForumThreads(self.user, self.forum)
  399. self.assertEqual(threads.list(), [test_threads[1], test_threads[0]])
  400. self.override_acl({
  401. 'can_see_all_threads': True,
  402. 'can_review_moderated_content': False
  403. })
  404. threads = ForumThreads(self.user, self.forum)
  405. self.assertEqual(threads.list(),
  406. [test_threads[2], test_threads[1], test_threads[0]])
  407. self.override_acl({
  408. 'can_see_all_threads': True,
  409. 'can_review_moderated_content': True
  410. })
  411. threads = ForumThreads(self.user, self.forum)
  412. test_threads.reverse()
  413. self.assertEqual(threads.list(), test_threads)
  414. self.assertTrue(threads.page)
  415. self.assertTrue(threads.paginator)
  416. class ForumThreadsViewTests(AuthenticatedUserTestCase):
  417. def setUp(self):
  418. super(ForumThreadsViewTests, self).setUp()
  419. self.forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  420. self.link = self.forum.get_absolute_url()
  421. self.forum.delete_content()
  422. Label.objects.clear_cache()
  423. def tearDown(self):
  424. super(ForumThreadsViewTests, self).tearDown()
  425. Label.objects.clear_cache()
  426. def override_acl(self, new_acl, forum=None):
  427. forum = forum or self.forum
  428. forums_acl = self.user.acl
  429. if new_acl['can_see']:
  430. forums_acl['visible_forums'].append(forum.pk)
  431. else:
  432. forums_acl['visible_forums'].remove(forum.pk)
  433. forums_acl['forums'][forum.pk] = new_acl
  434. override_acl(self.user, forums_acl)
  435. def test_cant_see(self):
  436. """has no permission to see forum"""
  437. self.override_acl({
  438. 'can_see': 0,
  439. 'can_browse': 0,
  440. })
  441. response = self.client.get(self.link)
  442. self.assertEqual(response.status_code, 404)
  443. def test_cant_browse(self):
  444. """has no permission to browse forum"""
  445. self.override_acl({
  446. 'can_see': 1,
  447. 'can_browse': 0,
  448. })
  449. response = self.client.get(self.link)
  450. self.assertEqual(response.status_code, 403)
  451. def test_can_browse_empty(self):
  452. """has permission to browse forum, sees empty list"""
  453. self.override_acl({
  454. 'can_see': 1,
  455. 'can_browse': 1,
  456. })
  457. response = self.client.get(self.link)
  458. self.assertEqual(response.status_code, 200)
  459. self.assertIn("No threads", response.content)
  460. def test_owned_threads_visibility(self):
  461. """
  462. can_see_all_threads=0 displays only owned threads to authenticated user
  463. """
  464. test_acl = {
  465. 'can_see': 1,
  466. 'can_browse': 1,
  467. 'can_see_all_threads': 0,
  468. 'can_review_moderated_content': 0,
  469. }
  470. other_moderated_title = "Test other user moderated thread"
  471. testutils.post_thread(
  472. forum=self.forum, title=other_moderated_title, is_moderated=True)
  473. other_title = "Test other user thread"
  474. testutils.post_thread(forum=self.forum, title=other_title)
  475. owned_title = "Test authenticated user thread"
  476. testutils.post_thread(
  477. forum=self.forum,
  478. title=owned_title,
  479. poster=self.user)
  480. owned_moderated_title = "Test authenticated user moderated thread"
  481. testutils.post_thread(
  482. forum=self.forum,
  483. title=owned_moderated_title,
  484. poster=self.user,
  485. is_moderated=True)
  486. self.override_acl(test_acl)
  487. response = self.client.get(self.link)
  488. self.assertEqual(response.status_code, 200)
  489. self.assertNotIn(other_title, response.content)
  490. self.assertNotIn(other_moderated_title, response.content)
  491. self.assertIn(owned_title, response.content)
  492. self.assertIn(owned_moderated_title, response.content)
  493. self.assertNotIn('show-my-threads', response.content)
  494. def test_moderated_threads_visibility(self):
  495. """moderated threads are not rendered to non-moderator, except owned"""
  496. test_acl = {
  497. 'can_see': 1,
  498. 'can_browse': 1,
  499. 'can_see_all_threads': 1,
  500. 'can_review_moderated_content': 0,
  501. }
  502. test_title = "Test moderated thread"
  503. thread = testutils.post_thread(
  504. forum=self.forum, title=test_title, is_moderated=True)
  505. self.override_acl(test_acl)
  506. response = self.client.get(self.link)
  507. self.assertEqual(response.status_code, 200)
  508. self.assertNotIn(test_title, response.content)
  509. test_title = "Test owned moderated thread"
  510. thread = testutils.post_thread(
  511. forum=self.forum,
  512. title=test_title,
  513. is_moderated=True,
  514. poster=self.user)
  515. self.override_acl(test_acl)
  516. response = self.client.get(self.link)
  517. self.assertEqual(response.status_code, 200)
  518. self.assertIn(test_title, response.content)
  519. def test_owned_threads_filter(self):
  520. """owned threads filter is available to authenticated user"""
  521. test_acl = {
  522. 'can_see': 1,
  523. 'can_browse': 1,
  524. 'can_see_all_threads': 1,
  525. 'can_review_moderated_content': 0,
  526. }
  527. other_moderated_title = "Test other user moderated thread"
  528. testutils.post_thread(
  529. forum=self.forum, title=other_moderated_title, is_moderated=True)
  530. other_title = "Test other user thread"
  531. testutils.post_thread(forum=self.forum, title=other_title)
  532. owned_title = "Test authenticated user thread"
  533. testutils.post_thread(
  534. forum=self.forum,
  535. title=owned_title,
  536. poster=self.user)
  537. owned_moderated_title = "Test authenticated user moderated thread"
  538. testutils.post_thread(
  539. forum=self.forum,
  540. title=owned_moderated_title,
  541. poster=self.user,
  542. is_moderated=True)
  543. self.override_acl(test_acl)
  544. response = self.client.get(self.link)
  545. self.assertEqual(response.status_code, 200)
  546. self.assertIn(other_title, response.content)
  547. self.assertNotIn(other_moderated_title, response.content)
  548. self.assertIn(owned_title, response.content)
  549. self.assertIn(owned_moderated_title, response.content)
  550. self.assertIn('show-my-threads', response.content)
  551. self.override_acl(test_acl)
  552. response = self.client.get(reverse('misago:forum', kwargs={
  553. 'forum_id': self.forum.id,
  554. 'forum_slug': self.forum.slug,
  555. 'show': 'my-threads',
  556. }))
  557. self.assertEqual(response.status_code, 200)
  558. self.assertNotIn(other_title, response.content)
  559. self.assertNotIn(other_moderated_title, response.content)
  560. self.assertIn(owned_title, response.content)
  561. self.assertIn(owned_moderated_title, response.content)
  562. def test_moderated_threads_filter(self):
  563. """moderated threads filter is available to moderator"""
  564. test_acl = {
  565. 'can_see': 1,
  566. 'can_browse': 1,
  567. 'can_see_all_threads': 1,
  568. 'can_review_moderated_content': 0,
  569. }
  570. not_moderated_title = "Not moderated thread"
  571. testutils.post_thread(forum=self.forum, title=not_moderated_title)
  572. hidden_title = "Test moderated thread"
  573. testutils.post_thread(
  574. forum=self.forum, title=hidden_title, is_moderated=True)
  575. visible_title = "Test owned moderated thread"
  576. testutils.post_thread(
  577. forum=self.forum,
  578. title=visible_title,
  579. is_moderated=True,
  580. poster=self.user)
  581. self.override_acl(test_acl)
  582. response = self.client.get(self.link)
  583. self.assertEqual(response.status_code, 200)
  584. self.assertIn(not_moderated_title, response.content)
  585. self.assertNotIn(hidden_title, response.content)
  586. self.assertIn(visible_title, response.content)
  587. self.assertNotIn('show-moderated-threads', response.content)
  588. self.assertNotIn('show-moderated-posts', response.content)
  589. self.override_acl(test_acl)
  590. response = self.client.get(reverse('misago:forum', kwargs={
  591. 'forum_id': self.forum.id,
  592. 'forum_slug': self.forum.slug,
  593. 'show': 'moderated-threads',
  594. }))
  595. self.assertEqual(response.status_code, 302)
  596. self.override_acl(test_acl)
  597. response = self.client.get(reverse('misago:forum', kwargs={
  598. 'forum_id': self.forum.id,
  599. 'forum_slug': self.forum.slug,
  600. 'show': 'moderated-posts',
  601. }))
  602. self.assertEqual(response.status_code, 302)
  603. test_acl = {
  604. 'can_see': 1,
  605. 'can_browse': 1,
  606. 'can_see_all_threads': 1,
  607. 'can_review_moderated_content': 1,
  608. }
  609. self.override_acl(test_acl)
  610. response = self.client.get(self.link)
  611. self.assertEqual(response.status_code, 200)
  612. self.assertIn(not_moderated_title, response.content)
  613. self.assertIn(hidden_title, response.content)
  614. self.assertIn(visible_title, response.content)
  615. self.assertIn('show-moderated-threads', response.content)
  616. self.assertIn('show-moderated-posts', response.content)
  617. self.override_acl(test_acl)
  618. response = self.client.get(reverse('misago:forum', kwargs={
  619. 'forum_id': self.forum.id,
  620. 'forum_slug': self.forum.slug,
  621. 'show': 'moderated-threads',
  622. }))
  623. self.assertEqual(response.status_code, 200)
  624. self.assertNotIn(not_moderated_title, response.content)
  625. self.assertIn(hidden_title, response.content)
  626. self.assertIn(visible_title, response.content)
  627. self.assertIn('show-moderated-threads', response.content)
  628. self.assertIn('show-moderated-posts', response.content)
  629. def test_anonymous_request(self):
  630. """view renders to anonymous users"""
  631. anon_title = "Hello Anon!"
  632. testutils.post_thread(forum=self.forum, title=anon_title)
  633. response = self.client.get(self.link)
  634. self.assertEqual(response.status_code, 200)
  635. self.assertIn(anon_title, response.content)
  636. def test_change_threads_labels(self):
  637. """moderation allows for changing threads labels"""
  638. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  639. test_acl = {
  640. 'can_see': 1,
  641. 'can_browse': 1,
  642. 'can_see_all_threads': 1,
  643. 'can_change_threads_labels': 2
  644. }
  645. labels = [
  646. Label(name='Label A', slug='label-a'),
  647. Label(name='Label B', slug='label-b'),
  648. ]
  649. for label in labels:
  650. label.save()
  651. label.forums.add(self.forum)
  652. self.override_acl(test_acl)
  653. response = self.client.get(self.link)
  654. self.assertEqual(response.status_code, 200)
  655. self.assertIn("Remove labels", response.content)
  656. # label threads with invalid label
  657. self.override_acl(test_acl)
  658. response = self.client.post(self.link, data={
  659. 'action': 'label:mehssiah', 'thread': [t.pk for t in threads]
  660. })
  661. self.assertEqual(response.status_code, 200)
  662. self.assertIn("Requested action is invalid.", response.content)
  663. # label threads
  664. self.override_acl(test_acl)
  665. response = self.client.post(self.link, data={
  666. 'action': 'label:%s' % labels[0].slug,
  667. 'thread': [t.pk for t in threads]
  668. })
  669. self.assertEqual(response.status_code, 302)
  670. self.override_acl(test_acl)
  671. response = self.client.get(self.link)
  672. self.assertEqual(response.status_code, 200)
  673. self.assertIn("10 threads were labeled", response.content)
  674. # label labeled threads
  675. self.override_acl(test_acl)
  676. response = self.client.post(self.link, data={
  677. 'action': 'label:%s' % labels[0].slug,
  678. 'thread': [t.pk for t in threads]
  679. })
  680. self.assertEqual(response.status_code, 302)
  681. self.override_acl(test_acl)
  682. response = self.client.get(self.link)
  683. self.assertEqual(response.status_code, 200)
  684. self.assertIn("No threads were labeled.", response.content)
  685. # relabel labeled threads
  686. self.override_acl(test_acl)
  687. response = self.client.post(self.link, data={
  688. 'action': 'label:%s' % labels[1].slug,
  689. 'thread': [t.pk for t in threads]
  690. })
  691. self.assertEqual(response.status_code, 302)
  692. self.override_acl(test_acl)
  693. response = self.client.get(self.link)
  694. self.assertEqual(response.status_code, 200)
  695. self.assertIn("10 threads were labeled", response.content)
  696. # remove labels from threads
  697. self.override_acl(test_acl)
  698. response = self.client.post(self.link, data={
  699. 'action': 'unlabel', 'thread': [t.pk for t in threads]
  700. })
  701. self.assertEqual(response.status_code, 302)
  702. self.override_acl(test_acl)
  703. response = self.client.get(self.link)
  704. self.assertEqual(response.status_code, 200)
  705. self.assertIn("10 threads labels were removed", response.content)
  706. # remove labels from unlabeled threads
  707. self.override_acl(test_acl)
  708. response = self.client.post(self.link, data={
  709. 'action': 'unlabel', 'thread': [t.pk for t in threads]
  710. })
  711. self.assertEqual(response.status_code, 302)
  712. self.override_acl(test_acl)
  713. response = self.client.get(self.link)
  714. self.assertEqual(response.status_code, 200)
  715. self.assertIn("No threads were unlabeled.", response.content)
  716. def test_pin_unpin_threads(self):
  717. """moderation allows for pinning and unpinning threads"""
  718. test_acl = {
  719. 'can_see': 1,
  720. 'can_browse': 1,
  721. 'can_see_all_threads': 1,
  722. 'can_pin_threads': 1
  723. }
  724. self.override_acl(test_acl)
  725. response = self.client.get(self.link)
  726. self.assertEqual(response.status_code, 200)
  727. self.assertIn("Pin threads", response.content)
  728. pinned = testutils.post_thread(self.forum, is_pinned=True)
  729. thread = testutils.post_thread(self.forum, is_pinned=False)
  730. # pin nothing
  731. self.override_acl(test_acl)
  732. response = self.client.post(self.link, data={'action': 'pin'})
  733. self.assertEqual(response.status_code, 200)
  734. self.assertIn("You have to select at least one thread.",
  735. response.content)
  736. # pin pinned
  737. self.override_acl(test_acl)
  738. response = self.client.post(self.link, data={
  739. 'action': 'pin', 'thread': [pinned.pk]
  740. })
  741. self.assertEqual(response.status_code, 302)
  742. self.override_acl(test_acl)
  743. response = self.client.get(self.link)
  744. self.assertEqual(response.status_code, 200)
  745. self.assertIn("No threads were pinned.",
  746. response.content)
  747. # pin unpinned
  748. self.override_acl(test_acl)
  749. response = self.client.post(self.link, data={
  750. 'action': 'pin', 'thread': [pinned.pk, thread.pk]
  751. })
  752. self.assertEqual(response.status_code, 302)
  753. self.override_acl(test_acl)
  754. response = self.client.get(self.link)
  755. self.assertEqual(response.status_code, 200)
  756. self.assertIn("1 thread was pinned.",
  757. response.content)
  758. pinned = Thread.objects.get(pk=pinned.pk)
  759. thread = Thread.objects.get(pk=thread.pk)
  760. self.assertTrue(pinned.is_pinned)
  761. self.assertTrue(thread.is_pinned)
  762. # unpin thread
  763. self.override_acl(test_acl)
  764. response = self.client.post(self.link, data={
  765. 'action': 'unpin', 'thread': [thread.pk]
  766. })
  767. self.assertEqual(response.status_code, 302)
  768. self.override_acl(test_acl)
  769. response = self.client.get(self.link)
  770. self.assertEqual(response.status_code, 200)
  771. self.assertIn("1 thread was unpinned.", response.content)
  772. pinned = Thread.objects.get(pk=pinned.pk)
  773. thread = Thread.objects.get(pk=thread.pk)
  774. self.assertTrue(pinned.is_pinned)
  775. self.assertFalse(thread.is_pinned)
  776. def test_approve_moderated_threads(self):
  777. """moderation allows for aproving moderated threads"""
  778. test_acl = {
  779. 'can_see': 1,
  780. 'can_browse': 1,
  781. 'can_see_all_threads': 1,
  782. 'can_review_moderated_content': 1
  783. }
  784. self.override_acl(test_acl)
  785. response = self.client.get(self.link)
  786. self.assertEqual(response.status_code, 200)
  787. self.assertIn("Approve threads", response.content)
  788. thread = testutils.post_thread(self.forum, is_moderated=False)
  789. moderated_thread = testutils.post_thread(self.forum, is_moderated=True)
  790. # approve approved thread
  791. self.override_acl(test_acl)
  792. response = self.client.post(self.link, data={
  793. 'action': 'approve', 'thread': [thread.pk]
  794. })
  795. self.assertEqual(response.status_code, 302)
  796. self.override_acl(test_acl)
  797. response = self.client.get(self.link)
  798. self.assertEqual(response.status_code, 200)
  799. self.assertIn("No threads were approved.", response.content)
  800. # approve moderated thread
  801. self.override_acl(test_acl)
  802. response = self.client.post(self.link, data={
  803. 'action': 'approve', 'thread': [moderated_thread.pk]
  804. })
  805. self.assertEqual(response.status_code, 302)
  806. self.override_acl(test_acl)
  807. response = self.client.get(self.link)
  808. self.assertEqual(response.status_code, 200)
  809. self.assertIn("1 thread was approved.", response.content)
  810. def test_move_threads(self):
  811. """moderation allows for moving threads"""
  812. new_forum = Forum(name="New Forum",
  813. slug="new-forum",
  814. role="forum")
  815. new_forum.insert_at(self.forum, save=True)
  816. test_acl = {
  817. 'can_see': 1,
  818. 'can_browse': 1,
  819. 'can_see_all_threads': 1,
  820. 'can_move_threads': 1
  821. }
  822. self.override_acl(test_acl)
  823. response = self.client.get(self.link)
  824. self.assertEqual(response.status_code, 200)
  825. self.assertIn("Move threads", response.content)
  826. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  827. # see move threads form
  828. self.override_acl(test_acl)
  829. response = self.client.post(self.link, data={
  830. 'action': 'move', 'thread': [t.pk for t in threads[:5]]
  831. })
  832. self.assertEqual(response.status_code, 200)
  833. for thread in threads[:5]:
  834. self.assertIn(thread.title, response.content)
  835. # submit form with non-existing forum
  836. self.override_acl(test_acl)
  837. response = self.client.post(self.link, data={
  838. 'action': 'move',
  839. 'thread': [t.pk for t in threads[:5]],
  840. 'submit': '',
  841. 'new_forum': new_forum.pk + 1234
  842. })
  843. self.assertEqual(response.status_code, 200)
  844. self.assertIn("Select valid forum.", response.content)
  845. # attempt move to category
  846. self.override_acl(test_acl)
  847. category = Forum.objects.all_forums().filter(role="category")[:1][0]
  848. response = self.client.post(self.link, data={
  849. 'action': 'move',
  850. 'thread': [t.pk for t in threads[:5]],
  851. 'submit': '',
  852. 'new_forum': category.pk
  853. })
  854. self.assertEqual(response.status_code, 200)
  855. self.assertIn("You can't move threads to category.",
  856. response.content)
  857. # attempt move to redirect
  858. self.override_acl(test_acl)
  859. redirect = Forum.objects.all_forums().filter(role="redirect")[:1][0]
  860. response = self.client.post(self.link, data={
  861. 'action': 'move',
  862. 'thread': [t.pk for t in threads[:5]],
  863. 'submit': '',
  864. 'new_forum': redirect.pk
  865. })
  866. self.assertEqual(response.status_code, 200)
  867. self.assertIn("You can't move threads to redirect.",
  868. response.content)
  869. # move to new_forum
  870. self.override_acl(test_acl)
  871. self.override_acl(test_acl, new_forum)
  872. response = self.client.post(self.link, data={
  873. 'action': 'move',
  874. 'thread': [t.pk for t in threads[:5]],
  875. 'submit': '',
  876. 'new_forum': new_forum.pk
  877. })
  878. self.assertEqual(response.status_code, 302)
  879. self.override_acl(test_acl)
  880. response = self.client.get(self.link)
  881. self.assertEqual(response.status_code, 200)
  882. self.assertIn("5 threads were moved to "New Forum".",
  883. response.content)
  884. for thread in new_forum.thread_set.all():
  885. self.assertIn(thread, threads[:5])
  886. for thread in self.forum.thread_set.all():
  887. self.assertIn(thread, threads[5:])
  888. def test_merge_threads(self):
  889. """moderation allows for merging threads"""
  890. test_acl = {
  891. 'can_see': 1,
  892. 'can_browse': 1,
  893. 'can_see_all_threads': 1,
  894. 'can_merge_threads': 1
  895. }
  896. self.override_acl(test_acl)
  897. response = self.client.get(self.link)
  898. self.assertEqual(response.status_code, 200)
  899. self.assertIn("Merge threads", response.content)
  900. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  901. # see merge threads form
  902. self.override_acl(test_acl)
  903. response = self.client.post(self.link, data={
  904. 'action': 'merge', 'thread': [t.pk for t in threads[:5]]
  905. })
  906. self.assertEqual(response.status_code, 200)
  907. # submit form with empty title
  908. self.override_acl(test_acl)
  909. response = self.client.post(self.link, data={
  910. 'action': 'merge',
  911. 'thread': [t.pk for t in threads[:5]],
  912. 'submit': ''
  913. })
  914. self.assertEqual(response.status_code, 200)
  915. self.assertIn("You have to enter merged thread title.",
  916. response.content)
  917. # submit form with one thread selected
  918. self.override_acl(test_acl)
  919. response = self.client.post(self.link, data={
  920. 'action': 'merge',
  921. 'thread': [threads[0].pk],
  922. 'submit': ''
  923. })
  924. self.assertEqual(response.status_code, 200)
  925. self.assertIn("You have to select at least two threads to merge.",
  926. response.content)
  927. # submit form with valid title
  928. self.override_acl(test_acl)
  929. response = self.client.post(self.link, data={
  930. 'action': 'merge',
  931. 'thread': [t.pk for t in threads[:5]],
  932. 'merged_thread_title': 'Merged thread',
  933. 'submit': ''
  934. })
  935. self.assertEqual(response.status_code, 302)
  936. # see if merged thread is there
  937. self.override_acl(test_acl)
  938. response = self.client.get(self.link)
  939. self.assertEqual(response.status_code, 200)
  940. self.assertIn("Merged thread", response.content)
  941. # assert that merged threads are gone
  942. for thread in threads[:5]:
  943. self.assertNotIn(thread.get_absolute_url(), response.content)
  944. # assert that non-merged threads were untouched
  945. for thread in threads[5:]:
  946. self.assertIn(thread.get_absolute_url(), response.content)
  947. def test_close_open_threads(self):
  948. """moderation allows for closing and opening threads"""
  949. test_acl = {
  950. 'can_see': 1,
  951. 'can_browse': 1,
  952. 'can_see_all_threads': 1,
  953. 'can_close_threads': 1
  954. }
  955. self.override_acl(test_acl)
  956. response = self.client.get(self.link)
  957. self.assertEqual(response.status_code, 200)
  958. self.assertIn("Close threads", response.content)
  959. self.assertIn("Open threads", response.content)
  960. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  961. # close threads
  962. self.override_acl(test_acl)
  963. response = self.client.post(self.link, data={
  964. 'action': 'close', 'thread': [t.pk for t in threads]
  965. })
  966. self.assertEqual(response.status_code, 302)
  967. self.override_acl(test_acl)
  968. response = self.client.get(self.link)
  969. self.assertEqual(response.status_code, 200)
  970. self.assertIn("10 threads were closed.", response.content)
  971. # close closed threads
  972. self.override_acl(test_acl)
  973. response = self.client.post(self.link, data={
  974. 'action': 'close', 'thread': [t.pk for t in threads]
  975. })
  976. self.assertEqual(response.status_code, 302)
  977. self.override_acl(test_acl)
  978. response = self.client.get(self.link)
  979. self.assertEqual(response.status_code, 200)
  980. self.assertIn("No threads were closed.", response.content)
  981. # open closed threads
  982. self.override_acl(test_acl)
  983. response = self.client.post(self.link, data={
  984. 'action': 'open', 'thread': [t.pk for t in threads]
  985. })
  986. self.assertEqual(response.status_code, 302)
  987. self.override_acl(test_acl)
  988. response = self.client.get(self.link)
  989. self.assertEqual(response.status_code, 200)
  990. self.assertIn("10 threads were opened.", response.content)
  991. # open opened threads
  992. self.override_acl(test_acl)
  993. response = self.client.post(self.link, data={
  994. 'action': 'open', 'thread': [t.pk for t in threads]
  995. })
  996. self.assertEqual(response.status_code, 302)
  997. self.override_acl(test_acl)
  998. response = self.client.get(self.link)
  999. self.assertEqual(response.status_code, 200)
  1000. self.assertIn("No threads were opened.", response.content)
  1001. def test_hide_unhide_threads(self):
  1002. """moderation allows for hiding and unhiding threads"""
  1003. test_acl = {
  1004. 'can_see': 1,
  1005. 'can_browse': 1,
  1006. 'can_see_all_threads': 1,
  1007. 'can_hide_threads': 1
  1008. }
  1009. self.override_acl(test_acl)
  1010. response = self.client.get(self.link)
  1011. self.assertEqual(response.status_code, 200)
  1012. self.assertIn("Unhide threads", response.content)
  1013. self.assertIn("Hide threads", response.content)
  1014. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  1015. # hide threads
  1016. self.override_acl(test_acl)
  1017. response = self.client.post(self.link, data={
  1018. 'action': 'hide', 'thread': [t.pk for t in threads]
  1019. })
  1020. self.assertEqual(response.status_code, 302)
  1021. self.override_acl(test_acl)
  1022. response = self.client.get(self.link)
  1023. self.assertEqual(response.status_code, 200)
  1024. self.assertIn("10 threads were hidden.", response.content)
  1025. # hide hidden threads
  1026. self.override_acl(test_acl)
  1027. response = self.client.post(self.link, data={
  1028. 'action': 'hide', 'thread': [t.pk for t in threads]
  1029. })
  1030. self.assertEqual(response.status_code, 302)
  1031. self.override_acl(test_acl)
  1032. response = self.client.get(self.link)
  1033. self.assertEqual(response.status_code, 200)
  1034. self.assertIn("No threads were hidden.", response.content)
  1035. # unhide hidden threads
  1036. self.override_acl(test_acl)
  1037. response = self.client.post(self.link, data={
  1038. 'action': 'unhide', 'thread': [t.pk for t in threads]
  1039. })
  1040. self.assertEqual(response.status_code, 302)
  1041. self.override_acl(test_acl)
  1042. response = self.client.get(self.link)
  1043. self.assertEqual(response.status_code, 200)
  1044. self.assertIn("10 threads were made visible.", response.content)
  1045. # unhide visible threads
  1046. self.override_acl(test_acl)
  1047. response = self.client.post(self.link, data={
  1048. 'action': 'unhide', 'thread': [t.pk for t in threads]
  1049. })
  1050. self.assertEqual(response.status_code, 302)
  1051. self.override_acl(test_acl)
  1052. response = self.client.get(self.link)
  1053. self.assertEqual(response.status_code, 200)
  1054. self.assertIn("No threads were made visible.", response.content)
  1055. def test_delete_threads(self):
  1056. """moderation allows for deleting threads"""
  1057. threads = [testutils.post_thread(self.forum) for t in xrange(10)]
  1058. self.forum.synchronize()
  1059. self.assertEqual(self.forum.threads, 10)
  1060. test_acl = {
  1061. 'can_see': 1,
  1062. 'can_browse': 1,
  1063. 'can_see_all_threads': 1,
  1064. 'can_hide_threads': 2
  1065. }
  1066. self.override_acl(test_acl)
  1067. response = self.client.get(self.link)
  1068. self.assertEqual(response.status_code, 200)
  1069. self.assertIn("Delete threads", response.content)
  1070. self.override_acl(test_acl)
  1071. response = self.client.post(self.link, data={
  1072. 'action': 'delete', 'thread': [t.pk for t in threads]
  1073. })
  1074. forum = Forum.objects.get(pk=self.forum.pk)
  1075. self.assertEqual(forum.threads, 0)