-test_threadparticipants_views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. from django.contrib.auth import get_user_model
  2. from django.core.urlresolvers import reverse
  3. from django.utils import timezone
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.users.testutils import AuthenticatedUserTestCase
  7. from misago.threads import testutils
  8. from misago.threads.models import Thread, ThreadParticipant
  9. class ThreadParticipantsTests(AuthenticatedUserTestCase):
  10. ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
  11. def setUp(self):
  12. super(ThreadParticipantsTests, self).setUp()
  13. self.category = Category.objects.private_threads()
  14. self.thread = testutils.post_thread(self.category)
  15. def test_participants_list(self):
  16. """participants list displays thread participants"""
  17. User = get_user_model()
  18. users = (
  19. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  20. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  21. )
  22. ThreadParticipant.objects.set_owner(self.thread, self.user)
  23. ThreadParticipant.objects.add_participant(self.thread, users[0])
  24. ThreadParticipant.objects.add_participant(self.thread, users[1])
  25. override_acl(self.user, {
  26. 'can_use_private_threads': True,
  27. 'can_moderate_private_threads': True
  28. })
  29. link = reverse('misago:private_thread_participants', kwargs={
  30. 'thread_id': self.thread.id,
  31. 'thread_slug': self.thread.slug
  32. })
  33. response = self.client.get(link, **self.ajax_header)
  34. self.assertEqual(response.status_code, 200)
  35. owner_pos = response.content.find(self.user.get_absolute_url())
  36. for user in users:
  37. participant_pos = response.content.find(user.get_absolute_url())
  38. self.assertTrue(owner_pos < participant_pos)
  39. def test_edit_participants(self):
  40. """edit participants view displays thread participants"""
  41. User = get_user_model()
  42. users = (
  43. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  44. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  45. )
  46. ThreadParticipant.objects.set_owner(self.thread, self.user)
  47. ThreadParticipant.objects.add_participant(self.thread, users[0])
  48. ThreadParticipant.objects.add_participant(self.thread, users[1])
  49. override_acl(self.user, {
  50. 'can_use_private_threads': True,
  51. 'can_moderate_private_threads': True
  52. })
  53. link = reverse('misago:private_thread_edit_participants', kwargs={
  54. 'thread_id': self.thread.id,
  55. 'thread_slug': self.thread.slug
  56. })
  57. response = self.client.get(link, **self.ajax_header)
  58. self.assertEqual(response.status_code, 200)
  59. owner_pos = response.content.find(self.user.get_absolute_url())
  60. for user in users:
  61. participant_pos = response.content.find(user.get_absolute_url())
  62. self.assertTrue(owner_pos < participant_pos)
  63. def test_owner_remove_participant(self):
  64. """remove participant allows owner to remove participant"""
  65. User = get_user_model()
  66. other_user = User.objects.create_user("Bob", "bob@bob.com", "pass123")
  67. ThreadParticipant.objects.set_owner(self.thread, self.user)
  68. ThreadParticipant.objects.add_participant(self.thread, other_user)
  69. override_acl(self.user, {
  70. 'can_use_private_threads': True,
  71. 'can_moderate_private_threads': True
  72. })
  73. link = reverse('misago:private_thread_remove_participant', kwargs={
  74. 'thread_id': self.thread.id,
  75. 'thread_slug': self.thread.slug,
  76. 'user_id': other_user.id,
  77. })
  78. response = self.client.post(link, **self.ajax_header)
  79. self.assertEqual(response.status_code, 200)
  80. self.assertEqual(self.thread.threadparticipant_set.count(), 1)
  81. owner = self.thread.threadparticipant_set.get(is_owner=True)
  82. self.assertEqual(owner.user, self.user)
  83. Thread.objects.get(pk=self.thread.pk)
  84. self.thread.threadparticipant_set.get(user=self.user)
  85. def test_owner_remove_non_participant(self):
  86. """remove participant handles attempt to remove invalid participant"""
  87. User = get_user_model()
  88. other_user = User.objects.create_user("Bob", "bob@bob.com", "pass123")
  89. ThreadParticipant.objects.set_owner(self.thread, self.user)
  90. ThreadParticipant.objects.add_participant(self.thread, other_user)
  91. override_acl(self.user, {
  92. 'can_use_private_threads': True,
  93. 'can_moderate_private_threads': True
  94. })
  95. link = reverse('misago:private_thread_remove_participant', kwargs={
  96. 'thread_id': self.thread.id,
  97. 'thread_slug': self.thread.slug,
  98. 'user_id': 123456,
  99. })
  100. response = self.client.post(link, **self.ajax_header)
  101. self.assertEqual(response.status_code, 200)
  102. self.assertEqual(self.thread.threadparticipant_set.count(), 2)
  103. owner = self.thread.threadparticipant_set.get(is_owner=True)
  104. self.assertEqual(owner.user, self.user)
  105. Thread.objects.get(pk=self.thread.pk)
  106. self.thread.threadparticipant_set.get(user=self.user)
  107. def test_non_owner_remove_participant(self):
  108. """non-owner cant remove participant"""
  109. User = get_user_model()
  110. other_user = User.objects.create_user("Bob", "bob@bob.com", "pass123")
  111. ThreadParticipant.objects.set_owner(self.thread, other_user)
  112. ThreadParticipant.objects.add_participant(self.thread, self.user)
  113. override_acl(self.user, {
  114. 'can_use_private_threads': True,
  115. 'can_moderate_private_threads': True
  116. })
  117. link = reverse('misago:private_thread_remove_participant', kwargs={
  118. 'thread_id': self.thread.id,
  119. 'thread_slug': self.thread.slug,
  120. 'user_id': other_user.pk,
  121. })
  122. response = self.client.post(link, **self.ajax_header)
  123. self.assertEqual(response.status_code, 406)
  124. self.assertEqual(self.thread.threadparticipant_set.count(), 2)
  125. owner = self.thread.threadparticipant_set.get(is_owner=True)
  126. self.assertEqual(owner.user, other_user)
  127. Thread.objects.get(pk=self.thread.pk)
  128. self.thread.threadparticipant_set.get(user=self.user)
  129. def test_owner_add_participant(self):
  130. """owner can add participants"""
  131. User = get_user_model()
  132. users = (
  133. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  134. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  135. )
  136. ThreadParticipant.objects.set_owner(self.thread, self.user)
  137. ThreadParticipant.objects.add_participant(self.thread, users[0])
  138. override_acl(self.user, {
  139. 'can_use_private_threads': True,
  140. 'can_moderate_private_threads': True
  141. })
  142. link = reverse('misago:private_thread_add_participants', kwargs={
  143. 'thread_id': self.thread.id,
  144. 'thread_slug': self.thread.slug,
  145. })
  146. response = self.client.post(link, data={
  147. 'users': 'Bob, Dam'
  148. }, **self.ajax_header)
  149. self.assertEqual(response.status_code, 200)
  150. self.assertEqual(self.thread.threadparticipant_set.count(), 3)
  151. for participant in self.thread.threadparticipant_set.all():
  152. if participant.is_owner:
  153. self.assertEqual(participant.user, self.user)
  154. else:
  155. self.assertIn(participant.user, users)
  156. Thread.objects.get(pk=self.thread.pk)
  157. self.thread.threadparticipant_set.get(user=self.user)
  158. def test_non_owner_add_participant(self):
  159. """non-owner cant add participants"""
  160. User = get_user_model()
  161. users = (
  162. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  163. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  164. )
  165. ThreadParticipant.objects.set_owner(self.thread, users[0])
  166. ThreadParticipant.objects.add_participant(self.thread, self.user)
  167. override_acl(self.user, {
  168. 'can_use_private_threads': True,
  169. 'can_moderate_private_threads': True
  170. })
  171. link = reverse('misago:private_thread_add_participants', kwargs={
  172. 'thread_id': self.thread.id,
  173. 'thread_slug': self.thread.slug,
  174. })
  175. response = self.client.post(link, data={
  176. 'users': 'Bob, Dam'
  177. }, **self.ajax_header)
  178. self.assertEqual(response.status_code, 406)
  179. def test_owner_leave_thread_new_owner(self):
  180. """
  181. leave thread view makes owner leave thread and makes new user owner
  182. """
  183. User = get_user_model()
  184. users = (
  185. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  186. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  187. )
  188. ThreadParticipant.objects.set_owner(self.thread, self.user)
  189. ThreadParticipant.objects.add_participant(self.thread, users[0])
  190. ThreadParticipant.objects.add_participant(self.thread, users[1])
  191. override_acl(self.user, {
  192. 'can_use_private_threads': True,
  193. 'can_moderate_private_threads': True
  194. })
  195. link = reverse('misago:private_thread_leave', kwargs={
  196. 'thread_id': self.thread.id,
  197. 'thread_slug': self.thread.slug
  198. })
  199. response = self.client.post(link, **self.ajax_header)
  200. self.assertEqual(response.status_code, 302)
  201. self.assertEqual(self.thread.threadparticipant_set.count(), 2)
  202. new_owner = self.thread.threadparticipant_set.get(is_owner=True)
  203. self.assertNotEqual(new_owner.user, self.user)
  204. self.assertIn(new_owner.user, users)
  205. Thread.objects.get(pk=self.thread.pk)
  206. with self.assertRaises(ThreadParticipant.DoesNotExist):
  207. self.thread.threadparticipant_set.get(user=self.user)
  208. def test_owner_leave_thread_delete(self):
  209. """
  210. leave thread view makes owner leave thread and deletes abadoned thread
  211. """
  212. ThreadParticipant.objects.set_owner(self.thread, self.user)
  213. override_acl(self.user, {
  214. 'can_use_private_threads': True,
  215. 'can_moderate_private_threads': True
  216. })
  217. link = reverse('misago:private_thread_leave', kwargs={
  218. 'thread_id': self.thread.id,
  219. 'thread_slug': self.thread.slug
  220. })
  221. response = self.client.post(link, **self.ajax_header)
  222. self.assertEqual(response.status_code, 302)
  223. with self.assertRaises(Thread.DoesNotExist):
  224. Thread.objects.get(pk=self.thread.pk)
  225. with self.assertRaises(ThreadParticipant.DoesNotExist):
  226. self.thread.threadparticipant_set.get(user=self.user)
  227. def test_participant_leave_thread(self):
  228. """
  229. leave thread view makes user leave thread
  230. """
  231. User = get_user_model()
  232. users = (
  233. User.objects.create_user("Bob", "bob@bob.com", "pass123"),
  234. User.objects.create_user("Dam", "dam@bob.com", "pass123")
  235. )
  236. ThreadParticipant.objects.set_owner(self.thread, users[0])
  237. ThreadParticipant.objects.add_participant(self.thread, users[1])
  238. ThreadParticipant.objects.add_participant(self.thread, self.user)
  239. override_acl(self.user, {
  240. 'can_use_private_threads': True,
  241. 'can_moderate_private_threads': True
  242. })
  243. link = reverse('misago:private_thread_leave', kwargs={
  244. 'thread_id': self.thread.id,
  245. 'thread_slug': self.thread.slug
  246. })
  247. response = self.client.post(link, **self.ajax_header)
  248. self.assertEqual(response.status_code, 302)
  249. self.assertEqual(self.thread.threadparticipant_set.count(), 2)
  250. owner = self.thread.threadparticipant_set.get(is_owner=True)
  251. self.assertEqual(owner.user, users[0])
  252. for participants in self.thread.threadparticipant_set.all():
  253. self.assertIn(participants.user, users)
  254. Thread.objects.get(pk=self.thread.pk)
  255. with self.assertRaises(ThreadParticipant.DoesNotExist):
  256. self.thread.threadparticipant_set.get(user=self.user)