test_privatethread_start_api.py 13 KB

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