test_post_mentions.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. from __future__ import unicode_literals
  2. from django.contrib.auth import get_user_model
  3. from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
  4. from django.urls import reverse
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  7. from misago.markup.mentions import MENTIONS_LIMIT
  8. from misago.threads import testutils
  9. from misago.users.testutils import AuthenticatedUserTestCase
  10. UserModel = get_user_model()
  11. class PostMentionsTests(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(PostMentionsTests, self).setUp()
  14. self.category = Category.objects.get(slug='first-category')
  15. self.thread = testutils.post_thread(category=self.category)
  16. self.override_acl()
  17. self.post_link = reverse(
  18. 'misago:api:thread-post-list', kwargs={
  19. 'thread_pk': self.thread.pk,
  20. }
  21. )
  22. def override_acl(self):
  23. new_acl = self.user.acl_cache
  24. new_acl['categories'][self.category.pk].update({
  25. 'can_see': 1,
  26. 'can_browse': 1,
  27. 'can_start_threads': 1,
  28. 'can_reply_threads': 1,
  29. 'can_edit_posts': 1,
  30. })
  31. override_acl(self.user, new_acl)
  32. def put(self, url, data=None):
  33. content = encode_multipart(BOUNDARY, data or {})
  34. return self.client.put(url, content, content_type=MULTIPART_CONTENT)
  35. def test_mention_noone(self):
  36. """endpoint handles no mentions in post"""
  37. response = self.client.post(
  38. self.post_link, data={
  39. 'post': "This is test response!",
  40. }
  41. )
  42. self.assertEqual(response.status_code, 200)
  43. post = self.user.post_set.order_by('id').last()
  44. self.assertEqual(post.mentions.count(), 0)
  45. def test_mention_nonexistant(self):
  46. """endpoint handles nonexistant mention"""
  47. response = self.client.post(
  48. self.post_link, data={
  49. 'post': "This is test response, @InvalidUser!",
  50. }
  51. )
  52. self.assertEqual(response.status_code, 200)
  53. post = self.user.post_set.order_by('id').last()
  54. self.assertEqual(post.mentions.count(), 0)
  55. def test_mention_self(self):
  56. """endpoint mentions author"""
  57. response = self.client.post(
  58. self.post_link, data={
  59. 'post': "This is test response, @{}!".format(self.user),
  60. }
  61. )
  62. self.assertEqual(response.status_code, 200)
  63. post = self.user.post_set.order_by('id').last()
  64. self.assertEqual(post.mentions.count(), 1)
  65. self.assertEqual(post.mentions.all()[0], self.user)
  66. def test_mention_limit(self):
  67. """endpoint mentions limits mentions to 24 users"""
  68. users = []
  69. for i in range(MENTIONS_LIMIT + 5):
  70. users.append(
  71. UserModel.objects.
  72. create_user('Mention{}'.format(i), 'mention{}@bob.com'.format(i), 'pass123')
  73. )
  74. mentions = ['@{}'.format(u) for u in users]
  75. response = self.client.post(
  76. self.post_link,
  77. data={
  78. 'post': "This is test response, {}!".format(', '.join(mentions)),
  79. }
  80. )
  81. self.assertEqual(response.status_code, 200)
  82. post = self.user.post_set.order_by('id').last()
  83. self.assertEqual(post.mentions.count(), 24)
  84. self.assertEqual(list(post.mentions.order_by('id')), users[:24])
  85. def test_mention_update(self):
  86. """edit post endpoint updates mentions"""
  87. user_a = UserModel.objects.create_user('Mention', 'mention@test.com', 'pass123')
  88. user_b = UserModel.objects.create_user('MentionB', 'mentionb@test.com', 'pass123')
  89. response = self.client.post(
  90. self.post_link, data={
  91. 'post': "This is test response, @{}!".format(user_a),
  92. }
  93. )
  94. self.assertEqual(response.status_code, 200)
  95. post = self.user.post_set.order_by('id').last()
  96. self.assertEqual(post.mentions.count(), 1)
  97. self.assertEqual(post.mentions.order_by('id')[0], user_a)
  98. # add mention to post
  99. edit_link = reverse(
  100. 'misago:api:thread-post-detail', kwargs={
  101. 'thread_pk': self.thread.pk,
  102. 'pk': post.pk,
  103. }
  104. )
  105. self.override_acl()
  106. response = self.put(
  107. edit_link,
  108. data={
  109. 'post': "This is test response, @{} and @{}!".format(user_a, user_b),
  110. }
  111. )
  112. self.assertEqual(response.status_code, 200)
  113. self.assertEqual(post.mentions.count(), 2)
  114. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  115. # remove first mention from post - should preserve mentions
  116. self.override_acl()
  117. response = self.put(
  118. edit_link, data={
  119. 'post': "This is test response, @{}!".format(user_b),
  120. }
  121. )
  122. self.assertEqual(response.status_code, 200)
  123. self.assertEqual(post.mentions.count(), 2)
  124. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  125. # remove mentions from post - should preserve mentions
  126. self.override_acl()
  127. response = self.put(
  128. edit_link, data={
  129. 'post': "This is test response!",
  130. }
  131. )
  132. self.assertEqual(response.status_code, 200)
  133. self.assertEqual(post.mentions.count(), 2)
  134. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  135. def test_mentions_merge(self):
  136. """posts merge sums mentions"""
  137. user_a = UserModel.objects.create_user('Mention', 'mention@test.com', 'pass123')
  138. user_b = UserModel.objects.create_user('MentionB', 'mentionb@test.com', 'pass123')
  139. response = self.client.post(
  140. self.post_link, data={
  141. 'post': "This is test response, @{}!".format(user_a),
  142. }
  143. )
  144. self.assertEqual(response.status_code, 200)
  145. post_a = self.user.post_set.order_by('id').last()
  146. self.assertEqual(post_a.mentions.count(), 1)
  147. self.assertEqual(list(post_a.mentions.all()), [user_a])
  148. # post second reply
  149. self.user.last_post_on = None
  150. self.user.save()
  151. response = self.client.post(
  152. self.post_link,
  153. data={
  154. 'post': "This is test response, @{} and @{}!".format(user_a, user_b),
  155. }
  156. )
  157. self.assertEqual(response.status_code, 200)
  158. post_b = self.user.post_set.order_by('id').last()
  159. # merge posts and validate that post A has all mentions
  160. post_b.merge(post_a)
  161. self.assertEqual(post_a.mentions.count(), 2)
  162. self.assertEqual(list(post_a.mentions.order_by('id')), [user_a, user_b])