test_privatethread_start_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib.auth import get_user_model
  4. from django.core import mail
  5. from django.urls import reverse
  6. from django.utils.encoding import smart_str
  7. from misago.acl.testutils import override_acl
  8. from misago.categories.models import Category
  9. from misago.threads.models import ThreadParticipant
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. UserModel = get_user_model()
  12. class StartPrivateThreadTests(AuthenticatedUserTestCase):
  13. def setUp(self):
  14. super(StartPrivateThreadTests, self).setUp()
  15. self.category = Category.objects.private_threads()
  16. self.api_link = reverse('misago:api:private-thread-list')
  17. self.other_user = UserModel.objects.create_user(
  18. 'BobBoberson', 'bob@boberson.com', 'pass123'
  19. )
  20. def test_cant_start_thread_as_guest(self):
  21. """user has to be authenticated to be able to post private thread"""
  22. self.logout_user()
  23. response = self.client.post(self.api_link)
  24. self.assertEqual(response.status_code, 403)
  25. def test_cant_use_private_threads(self):
  26. """has no permission to use private threads"""
  27. override_acl(self.user, {'can_use_private_threads': 0})
  28. response = self.client.post(self.api_link)
  29. self.assertContains(response, "You can't use private threads.", status_code=403)
  30. def test_cant_start_private_thread(self):
  31. """permission to start private thread is validated"""
  32. override_acl(self.user, {'can_start_private_threads': 0})
  33. response = self.client.post(self.api_link)
  34. self.assertContains(response, "You can't start private threads.", status_code=403)
  35. def test_empty_data(self):
  36. """no data sent handling has no showstoppers"""
  37. response = self.client.post(self.api_link, data={})
  38. self.assertEqual(response.status_code, 400)
  39. self.assertEqual(
  40. response.json(), {
  41. 'to': ["You have to enter user names."],
  42. 'title': ["You have to enter thread title."],
  43. 'post': ["You have to enter a message."]
  44. }
  45. )
  46. def test_title_is_validated(self):
  47. """title is validated"""
  48. response = self.client.post(
  49. self.api_link,
  50. data={
  51. 'to': [self.other_user.username],
  52. 'title': "------",
  53. 'post': "Lorem ipsum dolor met, sit amet elit!",
  54. }
  55. )
  56. self.assertEqual(response.status_code, 400)
  57. self.assertEqual(
  58. response.json(), {'title': ["Thread title should contain alpha-numeric characters."]}
  59. )
  60. def test_post_is_validated(self):
  61. """post is validated"""
  62. response = self.client.post(
  63. self.api_link,
  64. data={
  65. 'to': [self.other_user.username],
  66. 'title': "Lorem ipsum dolor met",
  67. 'post': "a",
  68. }
  69. )
  70. self.assertEqual(response.status_code, 400)
  71. self.assertEqual(
  72. response.json(),
  73. {'post': ["Posted message should be at least 5 characters long (it has 1)."]}
  74. )
  75. def test_cant_invite_self(self):
  76. """api validates that you cant invite yourself to private thread"""
  77. response = self.client.post(
  78. self.api_link,
  79. data={
  80. 'to': [self.user.username],
  81. 'title': "Lorem ipsum dolor met",
  82. 'post': "Lorem ipsum dolor.",
  83. }
  84. )
  85. self.assertEqual(response.status_code, 400)
  86. self.assertEqual(
  87. response.json(),
  88. {'to': ["You can't include yourself on the list of users to invite to new thread."]}
  89. )
  90. def test_cant_invite_nonexisting(self):
  91. """api validates that you cant invite nonexisting user to thread"""
  92. response = self.client.post(
  93. self.api_link,
  94. data={
  95. 'to': ['Ab', 'Cd'],
  96. 'title': "Lorem ipsum dolor met",
  97. 'post': "Lorem ipsum dolor.",
  98. }
  99. )
  100. self.assertEqual(response.status_code, 400)
  101. self.assertEqual(response.json(), {'to': ["One or more users could not be found: ab, cd"]})
  102. def test_cant_invite_too_many(self):
  103. """api validates that you cant invite too many users to thread"""
  104. response = self.client.post(
  105. self.api_link,
  106. data={
  107. 'to': ['Username{}'.format(i) for i in range(50)],
  108. 'title': "Lorem ipsum dolor met",
  109. 'post': "Lorem ipsum dolor.",
  110. }
  111. )
  112. self.assertEqual(response.status_code, 400)
  113. self.assertEqual(
  114. response.json(),
  115. {'to': ["You can't add more than 3 users to private thread (you've added 50)."]}
  116. )
  117. def test_cant_invite_no_permission(self):
  118. """api validates invited user permission to private thread"""
  119. override_acl(self.other_user, {'can_use_private_threads': 0})
  120. response = self.client.post(
  121. self.api_link,
  122. data={
  123. 'to': [self.other_user.username],
  124. 'title': "Lorem ipsum dolor met",
  125. 'post': "Lorem ipsum dolor.",
  126. }
  127. )
  128. self.assertEqual(response.status_code, 400)
  129. self.assertEqual(
  130. response.json(), {'to': ["BobBoberson can't participate in private threads."]}
  131. )
  132. def test_cant_invite_blocking(self):
  133. """api validates that you cant invite blocking user to thread"""
  134. self.other_user.blocks.add(self.user)
  135. response = self.client.post(
  136. self.api_link,
  137. data={
  138. 'to': [self.other_user.username],
  139. 'title': "Lorem ipsum dolor met",
  140. 'post': "Lorem ipsum dolor.",
  141. }
  142. )
  143. self.assertEqual(response.status_code, 400)
  144. self.assertEqual(response.json(), {'to': ["BobBoberson is blocking you."]})
  145. # allow us to bypass blocked check
  146. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  147. response = self.client.post(
  148. self.api_link,
  149. data={
  150. 'to': [self.other_user.username],
  151. 'title': "-----",
  152. 'post': "Lorem ipsum dolor.",
  153. }
  154. )
  155. self.assertEqual(response.status_code, 400)
  156. self.assertEqual(
  157. response.json(), {'title': ["Thread title should contain alpha-numeric characters."]}
  158. )
  159. def test_cant_invite_followers_only(self):
  160. """api validates that you cant invite followers-only user to thread"""
  161. user_constant = UserModel.LIMIT_INVITES_TO_FOLLOWED
  162. self.other_user.limits_private_thread_invites_to = user_constant
  163. self.other_user.save()
  164. response = self.client.post(
  165. self.api_link,
  166. data={
  167. 'to': [self.other_user.username],
  168. 'title': "Lorem ipsum dolor met",
  169. 'post': "Lorem ipsum dolor.",
  170. }
  171. )
  172. self.assertEqual(response.status_code, 400)
  173. self.assertEqual(
  174. response.json(),
  175. {'to': ["BobBoberson limits invitations to private threads to followed users."]}
  176. )
  177. # allow us to bypass following check
  178. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  179. response = self.client.post(
  180. self.api_link,
  181. data={
  182. 'to': [self.other_user.username],
  183. 'title': "-----",
  184. 'post': "Lorem ipsum dolor.",
  185. }
  186. )
  187. self.assertEqual(response.status_code, 400)
  188. self.assertEqual(
  189. response.json(), {'title': ["Thread title should contain alpha-numeric characters."]}
  190. )
  191. # make user follow us
  192. override_acl(self.user, {'can_add_everyone_to_private_threads': 0})
  193. self.other_user.follows.add(self.user)
  194. response = self.client.post(
  195. self.api_link,
  196. data={
  197. 'to': [self.other_user.username],
  198. 'title': "-----",
  199. 'post': "Lorem ipsum dolor.",
  200. }
  201. )
  202. self.assertEqual(response.status_code, 400)
  203. self.assertEqual(
  204. response.json(), {'title': ["Thread title should contain alpha-numeric characters."]}
  205. )
  206. def test_cant_invite_anyone(self):
  207. """api validates that you cant invite nobody user to thread"""
  208. user_constant = UserModel.LIMIT_INVITES_TO_NOBODY
  209. self.other_user.limits_private_thread_invites_to = user_constant
  210. self.other_user.save()
  211. response = self.client.post(
  212. self.api_link,
  213. data={
  214. 'to': [self.other_user.username],
  215. 'title': "Lorem ipsum dolor met",
  216. 'post': "Lorem ipsum dolor.",
  217. }
  218. )
  219. self.assertEqual(response.status_code, 400)
  220. self.assertEqual(
  221. response.json(),
  222. {'to': ["BobBoberson is not allowing invitations to private threads."]}
  223. )
  224. # allow us to bypass user preference check
  225. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  226. response = self.client.post(
  227. self.api_link,
  228. data={
  229. 'to': [self.other_user.username],
  230. 'title': "-----",
  231. 'post': "Lorem ipsum dolor.",
  232. }
  233. )
  234. self.assertEqual(response.status_code, 400)
  235. self.assertEqual(
  236. response.json(), {'title': ["Thread title should contain alpha-numeric characters."]}
  237. )
  238. def test_can_start_thread(self):
  239. """endpoint creates new thread"""
  240. response = self.client.post(
  241. self.api_link,
  242. data={
  243. 'to': [self.other_user.username],
  244. 'title': "Hello, I am test thread!",
  245. 'post': "Lorem ipsum dolor met!"
  246. }
  247. )
  248. self.assertEqual(response.status_code, 200)
  249. thread = self.user.thread_set.all()[:1][0]
  250. response_json = response.json()
  251. self.assertEqual(response_json['url'], thread.get_absolute_url())
  252. response = self.client.get(thread.get_absolute_url())
  253. self.assertContains(response, self.category.name)
  254. self.assertContains(response, thread.title)
  255. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")
  256. # don't count private threads
  257. self.reload_user()
  258. self.assertEqual(self.user.threads, 0)
  259. self.assertEqual(self.user.posts, 0)
  260. self.assertEqual(thread.category_id, self.category.pk)
  261. self.assertEqual(thread.title, "Hello, I am test thread!")
  262. self.assertEqual(thread.starter_id, self.user.id)
  263. self.assertEqual(thread.starter_name, self.user.username)
  264. self.assertEqual(thread.starter_slug, self.user.slug)
  265. self.assertEqual(thread.last_poster_id, self.user.id)
  266. self.assertEqual(thread.last_poster_name, self.user.username)
  267. self.assertEqual(thread.last_poster_slug, self.user.slug)
  268. post = self.user.post_set.all()[:1][0]
  269. self.assertEqual(post.category_id, self.category.pk)
  270. self.assertEqual(post.original, 'Lorem ipsum dolor met!')
  271. self.assertEqual(post.poster_id, self.user.id)
  272. self.assertEqual(post.poster_name, self.user.username)
  273. # thread has two participants
  274. self.assertEqual(thread.participants.count(), 2)
  275. # we are thread owner
  276. ThreadParticipant.objects.get(thread=thread, user=self.user, is_owner=True)
  277. # other user was added to thread
  278. ThreadParticipant.objects.get(thread=thread, user=self.other_user, is_owner=False)
  279. # other user has sync_unread_private_threads flag
  280. user_to_sync = UserModel.objects.get(sync_unread_private_threads=True)
  281. self.assertEqual(user_to_sync, self.other_user)
  282. # notification about new private thread was sent to other user
  283. self.assertEqual(len(mail.outbox), 1)
  284. email = mail.outbox[-1]
  285. self.assertIn(self.user.username, email.subject)
  286. self.assertIn(thread.title, email.subject)
  287. email_body = smart_str(email.body)
  288. self.assertIn(self.user.username, email_body)
  289. self.assertIn(thread.title, email_body)
  290. self.assertIn(thread.get_absolute_url(), email_body)
  291. def test_post_unicode(self):
  292. """unicode characters can be posted"""
  293. response = self.client.post(
  294. self.api_link,
  295. data={
  296. 'to': [self.other_user.username],
  297. 'title': "Brzęczyżczykiewicz",
  298. 'post': "Chrzążczyżewoszyce, powiat Łękółody."
  299. }
  300. )
  301. self.assertEqual(response.status_code, 200)