test_privatethread_start_api.py 13 KB

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