test_privatethread_start_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 ...acl.test import patch_user_acl
  6. from ...categories.models import Category
  7. from ...users.test import AuthenticatedUserTestCase, create_test_user
  8. from ..models import ThreadParticipant
  9. from ..test import other_user_cant_use_private_threads
  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(), {"to": ["OtherUser can't participate in private threads."]}
  148. )
  149. def test_cant_invite_blocking(self):
  150. """api validates that you cant invite blocking user to thread"""
  151. self.other_user.blocks.add(self.user)
  152. response = self.client.post(
  153. self.api_link,
  154. data={
  155. "to": [self.other_user.username],
  156. "title": "Lorem ipsum dolor met",
  157. "post": "Lorem ipsum dolor.",
  158. },
  159. )
  160. self.assertEqual(response.status_code, 400)
  161. self.assertEqual(response.json(), {"to": ["OtherUser is blocking you."]})
  162. @patch_user_acl({"can_add_everyone_to_private_threads": 1})
  163. def test_cant_invite_blocking_override(self):
  164. """api validates that you cant invite blocking user to thread"""
  165. self.other_user.blocks.add(self.user)
  166. response = self.client.post(
  167. self.api_link,
  168. data={
  169. "to": [self.other_user.username],
  170. "title": "-----",
  171. "post": "Lorem ipsum dolor.",
  172. },
  173. )
  174. self.assertEqual(response.status_code, 400)
  175. self.assertEqual(
  176. response.json(),
  177. {"title": ["Thread title should contain alpha-numeric characters."]},
  178. )
  179. def test_cant_invite_followers_only(self):
  180. """api validates that you cant invite followers-only user to thread"""
  181. user_constant = User.LIMIT_INVITES_TO_FOLLOWED
  182. self.other_user.limits_private_thread_invites_to = user_constant
  183. self.other_user.save()
  184. response = self.client.post(
  185. self.api_link,
  186. data={
  187. "to": [self.other_user.username],
  188. "title": "Lorem ipsum dolor met",
  189. "post": "Lorem ipsum dolor.",
  190. },
  191. )
  192. self.assertEqual(response.status_code, 400)
  193. self.assertEqual(
  194. response.json(),
  195. {
  196. "to": [
  197. "OtherUser limits invitations to private threads to followed users."
  198. ]
  199. },
  200. )
  201. # allow us to bypass following check
  202. with patch_user_acl({"can_add_everyone_to_private_threads": 1}):
  203. response = self.client.post(
  204. self.api_link,
  205. data={
  206. "to": [self.other_user.username],
  207. "title": "-----",
  208. "post": "Lorem ipsum dolor.",
  209. },
  210. )
  211. self.assertEqual(response.status_code, 400)
  212. self.assertEqual(
  213. response.json(),
  214. {"title": ["Thread title should contain alpha-numeric characters."]},
  215. )
  216. # make user follow us
  217. self.other_user.follows.add(self.user)
  218. response = self.client.post(
  219. self.api_link,
  220. data={
  221. "to": [self.other_user.username],
  222. "title": "-----",
  223. "post": "Lorem ipsum dolor.",
  224. },
  225. )
  226. self.assertEqual(response.status_code, 400)
  227. self.assertEqual(
  228. response.json(),
  229. {"title": ["Thread title should contain alpha-numeric characters."]},
  230. )
  231. def test_cant_invite_anyone(self):
  232. """api validates that you cant invite nobody user to thread"""
  233. user_constant = User.LIMIT_INVITES_TO_NOBODY
  234. self.other_user.limits_private_thread_invites_to = user_constant
  235. self.other_user.save()
  236. response = self.client.post(
  237. self.api_link,
  238. data={
  239. "to": [self.other_user.username],
  240. "title": "Lorem ipsum dolor met",
  241. "post": "Lorem ipsum dolor.",
  242. },
  243. )
  244. self.assertEqual(response.status_code, 400)
  245. self.assertEqual(
  246. response.json(),
  247. {"to": ["OtherUser is not allowing invitations to private threads."]},
  248. )
  249. # allow us to bypass user preference check
  250. with patch_user_acl({"can_add_everyone_to_private_threads": 1}):
  251. response = self.client.post(
  252. self.api_link,
  253. data={
  254. "to": [self.other_user.username],
  255. "title": "-----",
  256. "post": "Lorem ipsum dolor.",
  257. },
  258. )
  259. self.assertEqual(response.status_code, 400)
  260. self.assertEqual(
  261. response.json(),
  262. {"title": ["Thread title should contain alpha-numeric characters."]},
  263. )
  264. def test_can_start_thread(self):
  265. """endpoint creates new thread"""
  266. response = self.client.post(
  267. self.api_link,
  268. data={
  269. "to": [self.other_user.username],
  270. "title": "Hello, I am test thread!",
  271. "post": "Lorem ipsum dolor met!",
  272. },
  273. )
  274. self.assertEqual(response.status_code, 200)
  275. thread = self.user.thread_set.all()[:1][0]
  276. response_json = response.json()
  277. self.assertEqual(response_json["url"], thread.get_absolute_url())
  278. response = self.client.get(thread.get_absolute_url())
  279. self.assertContains(response, self.category.name)
  280. self.assertContains(response, thread.title)
  281. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")
  282. # don't count private threads
  283. self.reload_user()
  284. self.assertEqual(self.user.threads, 0)
  285. self.assertEqual(self.user.posts, 0)
  286. self.assertEqual(thread.category_id, self.category.pk)
  287. self.assertEqual(thread.title, "Hello, I am test thread!")
  288. self.assertEqual(thread.starter_id, self.user.id)
  289. self.assertEqual(thread.starter_name, self.user.username)
  290. self.assertEqual(thread.starter_slug, self.user.slug)
  291. self.assertEqual(thread.last_poster_id, self.user.id)
  292. self.assertEqual(thread.last_poster_name, self.user.username)
  293. self.assertEqual(thread.last_poster_slug, self.user.slug)
  294. post = self.user.post_set.all()[:1][0]
  295. self.assertEqual(post.category_id, self.category.pk)
  296. self.assertEqual(post.original, "Lorem ipsum dolor met!")
  297. self.assertEqual(post.poster_id, self.user.id)
  298. self.assertEqual(post.poster_name, self.user.username)
  299. self.assertEqual(self.user.audittrail_set.count(), 1)
  300. # thread has two participants
  301. self.assertEqual(thread.participants.count(), 2)
  302. # we are thread owner
  303. ThreadParticipant.objects.get(thread=thread, user=self.user, is_owner=True)
  304. # other user was added to thread
  305. ThreadParticipant.objects.get(
  306. thread=thread, user=self.other_user, is_owner=False
  307. )
  308. # other user has sync_unread_private_threads flag
  309. self.other_user.refresh_from_db()
  310. self.assertTrue(self.other_user.sync_unread_private_threads)
  311. # notification about new private thread was sent to other user
  312. self.assertEqual(len(mail.outbox), 1)
  313. email = mail.outbox[-1]
  314. self.assertIn(self.user.username, email.subject)
  315. self.assertIn(thread.title, email.subject)
  316. email_body = smart_str(email.body)
  317. self.assertIn(self.user.username, email_body)
  318. self.assertIn(thread.title, email_body)
  319. self.assertIn(thread.get_absolute_url(), email_body)
  320. def test_post_unicode(self):
  321. """unicode characters can be posted"""
  322. response = self.client.post(
  323. self.api_link,
  324. data={
  325. "to": [self.other_user.username],
  326. "title": "Brzęczyżczykiewicz",
  327. "post": "Chrzążczyżewoszyce, powiat Łękółody.",
  328. },
  329. )
  330. self.assertEqual(response.status_code, 200)