test_post_mentions.py 6.1 KB

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