-test_privatethread_view.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from django.contrib.auth import get_user_model
  2. from django.utils import timezone
  3. from misago.acl.testutils import override_acl
  4. from misago.forums.models import Forum
  5. from misago.users.testutils import AuthenticatedUserTestCase
  6. from misago.threads import testutils
  7. from misago.threads.models import ThreadParticipant
  8. class PrivateThreadTests(AuthenticatedUserTestCase):
  9. def setUp(self):
  10. super(PrivateThreadTests, self).setUp()
  11. self.forum = Forum.objects.private_threads()
  12. self.thread = testutils.post_thread(self.forum)
  13. def test_anon_access_to_view(self):
  14. """anonymous user has no access to private thread"""
  15. self.logout_user()
  16. response = self.client.get(self.thread.get_absolute_url())
  17. self.assertEqual(response.status_code, 403)
  18. def test_non_participant_access_to_thread(self):
  19. """non-participant user has no access to private thread"""
  20. override_acl(self.user, {'can_use_private_threads': True})
  21. response = self.client.get(self.thread.get_absolute_url())
  22. self.assertEqual(response.status_code, 404)
  23. def test_owner_can_access_thread(self):
  24. """owner has access to private thread"""
  25. override_acl(self.user, {'can_use_private_threads': True})
  26. ThreadParticipant.objects.set_owner(self.thread, self.user)
  27. response = self.client.get(self.thread.get_absolute_url())
  28. self.assertEqual(response.status_code, 200)
  29. self.assertIn(self.thread.title, response.content)
  30. def test_participant_can_access_thread(self):
  31. """participant has access to private thread"""
  32. override_acl(self.user, {'can_use_private_threads': True})
  33. ThreadParticipant.objects.add_participant(self.thread, self.user)
  34. response = self.client.get(self.thread.get_absolute_url())
  35. self.assertEqual(response.status_code, 200)
  36. self.assertIn(self.thread.title, response.content)
  37. def test_removed_user_cant_access_thread(self):
  38. """removed user can't access thread"""
  39. override_acl(self.user, {'can_use_private_threads': True})
  40. ThreadParticipant.objects.add_participant(self.thread, self.user)
  41. ThreadParticipant.objects.remove_participant(self.thread, self.user)
  42. response = self.client.get(self.thread.get_absolute_url())
  43. self.assertEqual(response.status_code, 404)
  44. def test_moderator_cant_access_unreported_thread(self):
  45. """moderator cant see private thread without reports"""
  46. override_acl(self.user, {
  47. 'can_use_private_threads': True,
  48. 'can_moderate_private_threads': True
  49. })
  50. response = self.client.get(self.thread.get_absolute_url())
  51. self.assertEqual(response.status_code, 404)
  52. def test_moderator_can_access_reported_thread(self):
  53. """moderator can see private thread with reports"""
  54. override_acl(self.user, {
  55. 'can_use_private_threads': True,
  56. 'can_moderate_private_threads': True
  57. })
  58. self.thread.has_reported_posts = True
  59. self.thread.save()
  60. response = self.client.get(self.thread.get_absolute_url())
  61. self.assertEqual(response.status_code, 200)
  62. self.assertIn(self.thread.title, response.content)
  63. def test_moderator_can_takeover_reported_thread(self):
  64. """moderator can take over private thread"""
  65. override_acl(self.user, {
  66. 'can_use_private_threads': True,
  67. 'can_moderate_private_threads': True
  68. })
  69. self.thread.has_reported_posts = True
  70. self.thread.save()
  71. response = self.client.post(self.thread.get_absolute_url(), data={
  72. 'thread_action': 'takeover'
  73. })
  74. self.assertEqual(response.status_code, 302)
  75. user = self.thread.threadparticipant_set.get(user=self.user)
  76. self.assertTrue(user.is_owner)
  77. def test_owner_can_pass_thread_to_participant(self):
  78. """thread owner can pass thread to other participant"""
  79. User = get_user_model()
  80. new_owner = User.objects.create_user("Bob", "bob@bob.com", "pass123")
  81. ThreadParticipant.objects.set_owner(self.thread, self.user)
  82. ThreadParticipant.objects.add_participant(self.thread, new_owner)
  83. override_acl(self.user, {'can_use_private_threads': True})
  84. response = self.client.post(self.thread.get_absolute_url(), data={
  85. 'thread_action': 'make_owner:%s' % new_owner.id
  86. })
  87. self.assertEqual(response.status_code, 302)
  88. user = self.thread.threadparticipant_set.get(user=self.user)
  89. self.assertFalse(user.is_owner)
  90. user = self.thread.threadparticipant_set.get(user=new_owner)
  91. self.assertTrue(user.is_owner)