test_privatethread_start_api.py 13 KB

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