test_post_mentions.py 6.8 KB

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