test_post_mentions.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(self, url, data=None):
  34. content = encode_multipart(BOUNDARY, data or {})
  35. return self.client.put(url, content, content_type=MULTIPART_CONTENT)
  36. def test_mention_noone(self):
  37. """endpoint handles no mentions in post"""
  38. response = self.client.post(
  39. self.post_link, data={
  40. 'post': "This is test response!",
  41. }
  42. )
  43. self.assertEqual(response.status_code, 200)
  44. post = self.user.post_set.order_by('id').last()
  45. self.assertEqual(post.mentions.count(), 0)
  46. def test_mention_nonexistant(self):
  47. """endpoint handles nonexistant mention"""
  48. response = self.client.post(
  49. self.post_link, data={
  50. 'post': "This is test response, @InvalidUser!",
  51. }
  52. )
  53. self.assertEqual(response.status_code, 200)
  54. post = self.user.post_set.order_by('id').last()
  55. self.assertEqual(post.mentions.count(), 0)
  56. def test_mention_self(self):
  57. """endpoint mentions author"""
  58. response = self.client.post(
  59. self.post_link, data={
  60. 'post': "This is test response, @{}!".format(self.user),
  61. }
  62. )
  63. self.assertEqual(response.status_code, 200)
  64. post = self.user.post_set.order_by('id').last()
  65. self.assertEqual(post.mentions.count(), 1)
  66. self.assertEqual(post.mentions.all()[0], self.user)
  67. def test_mention_limit(self):
  68. """endpoint mentions limits mentions to 24 users"""
  69. users = []
  70. for i in range(MENTIONS_LIMIT + 5):
  71. users.append(
  72. UserModel.objects.
  73. create_user('Mention{}'.format(i), 'mention{}@bob.com'.format(i), 'pass123')
  74. )
  75. mentions = ['@{}'.format(u) for u in users]
  76. response = self.client.post(
  77. self.post_link,
  78. data={
  79. 'post': "This is test response, {}!".format(', '.join(mentions)),
  80. }
  81. )
  82. self.assertEqual(response.status_code, 200)
  83. post = self.user.post_set.order_by('id').last()
  84. self.assertEqual(post.mentions.count(), 24)
  85. self.assertEqual(list(post.mentions.order_by('id')), users[:24])
  86. def test_mention_update(self):
  87. """edit post endpoint updates mentions"""
  88. user_a = UserModel.objects.create_user('Mention', 'mention@test.com', 'pass123')
  89. user_b = UserModel.objects.create_user('MentionB', 'mentionb@test.com', 'pass123')
  90. response = self.client.post(
  91. self.post_link, data={
  92. 'post': "This is test response, @{}!".format(user_a),
  93. }
  94. )
  95. self.assertEqual(response.status_code, 200)
  96. post = self.user.post_set.order_by('id').last()
  97. self.assertEqual(post.mentions.count(), 1)
  98. self.assertEqual(post.mentions.order_by('id')[0], user_a)
  99. # add mention to post
  100. edit_link = reverse(
  101. 'misago:api:thread-post-detail', kwargs={
  102. 'thread_pk': self.thread.pk,
  103. 'pk': post.pk,
  104. }
  105. )
  106. self.override_acl()
  107. response = self.put(
  108. edit_link,
  109. data={
  110. 'post': "This is test response, @{} and @{}!".format(user_a, user_b),
  111. }
  112. )
  113. self.assertEqual(response.status_code, 200)
  114. self.assertEqual(post.mentions.count(), 2)
  115. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  116. # remove first mention from post - should preserve mentions
  117. self.override_acl()
  118. response = self.put(
  119. edit_link, data={
  120. 'post': "This is test response, @{}!".format(user_b),
  121. }
  122. )
  123. self.assertEqual(response.status_code, 200)
  124. self.assertEqual(post.mentions.count(), 2)
  125. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  126. # remove mentions from post - should preserve mentions
  127. self.override_acl()
  128. response = self.put(
  129. edit_link, data={
  130. 'post': "This is test response!",
  131. }
  132. )
  133. self.assertEqual(response.status_code, 200)
  134. self.assertEqual(post.mentions.count(), 2)
  135. self.assertEqual(list(post.mentions.order_by('id')), [user_a, user_b])
  136. def test_mentions_merge(self):
  137. """posts merge sums mentions"""
  138. user_a = UserModel.objects.create_user('Mention', 'mention@test.com', 'pass123')
  139. user_b = UserModel.objects.create_user('MentionB', 'mentionb@test.com', 'pass123')
  140. response = self.client.post(
  141. self.post_link, data={
  142. 'post': "This is test response, @{}!".format(user_a),
  143. }
  144. )
  145. self.assertEqual(response.status_code, 200)
  146. post_a = self.user.post_set.order_by('id').last()
  147. self.assertEqual(post_a.mentions.count(), 1)
  148. self.assertEqual(list(post_a.mentions.all()), [user_a])
  149. # post second reply
  150. self.user.last_post_on = None
  151. self.user.save()
  152. response = self.client.post(
  153. self.post_link,
  154. data={
  155. 'post': "This is test response, @{} and @{}!".format(user_a, user_b),
  156. }
  157. )
  158. self.assertEqual(response.status_code, 200)
  159. post_b = self.user.post_set.order_by('id').last()
  160. # merge posts and validate that post A has all mentions
  161. post_b.merge(post_a)
  162. self.assertEqual(post_a.mentions.count(), 2)
  163. self.assertEqual(list(post_a.mentions.order_by('id')), [user_a, user_b])