test_privatethread_start_api.py 13 KB

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