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
  10. UserModel = 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 = UserModel.objects.create_user(
  17. 'BobBoberson', 'bob@boberson.com', 'pass123'
  18. )
  19. def test_cant_start_thread_as_guest(self):
  20. """user has to be authenticated to be able to post private thread"""
  21. self.logout_user()
  22. response = self.client.post(self.api_link)
  23. self.assertEqual(response.status_code, 403)
  24. @patch_user_acl({'can_use_private_threads': False})
  25. def test_cant_use_private_threads(self):
  26. """has no permission to use private threads"""
  27. response = self.client.post(self.api_link)
  28. self.assertEqual(response.status_code, 403)
  29. self.assertEqual(response.json(), {
  30. "detail": "You can't use private threads.",
  31. })
  32. @patch_user_acl({'can_start_private_threads': False})
  33. def test_cant_start_private_thread(self):
  34. """permission to start private thread is validated"""
  35. response = self.client.post(self.api_link)
  36. self.assertEqual(response.status_code, 403)
  37. self.assertEqual(response.json(), {
  38. "detail": "You can't start private threads.",
  39. })
  40. def test_empty_data(self):
  41. """no data sent handling has no showstoppers"""
  42. response = self.client.post(self.api_link, data={})
  43. self.assertEqual(response.status_code, 400)
  44. self.assertEqual(
  45. response.json(), {
  46. 'to': ["You have to enter user names."],
  47. 'title': ["You have to enter thread title."],
  48. 'post': ["You have to enter a message."],
  49. }
  50. )
  51. def test_title_is_validated(self):
  52. """title is validated"""
  53. response = self.client.post(
  54. self.api_link,
  55. data={
  56. 'to': [self.other_user.username],
  57. 'title': "------",
  58. 'post': "Lorem ipsum dolor met, sit amet elit!",
  59. }
  60. )
  61. self.assertEqual(response.status_code, 400)
  62. self.assertEqual(
  63. response.json(), {
  64. 'title': ["Thread title should contain alpha-numeric characters."],
  65. }
  66. )
  67. def test_post_is_validated(self):
  68. """post is validated"""
  69. response = self.client.post(
  70. self.api_link,
  71. data={
  72. 'to': [self.other_user.username],
  73. 'title': "Lorem ipsum dolor met",
  74. 'post': "a",
  75. }
  76. )
  77. self.assertEqual(response.status_code, 400)
  78. self.assertEqual(
  79. response.json(), {
  80. 'post': ["Posted message should be at least 5 characters long (it has 1)."],
  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. 'to': ["You can't include yourself on the list of users to invite to new thread."],
  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(), {
  112. 'to': ["One or more users could not be found: ab, cd"],
  113. }
  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. 'to': ["You can't add more than 3 users to private thread (you've added 50)."],
  129. }
  130. )
  131. @patch_user_acl(other_user_cant_use_private_threads)
  132. def test_cant_invite_no_permission(self):
  133. """api validates invited user permission to private thread"""
  134. response = self.client.post(
  135. self.api_link,
  136. data={
  137. 'to': [self.other_user.username],
  138. 'title': "Lorem ipsum dolor met",
  139. 'post': "Lorem ipsum dolor.",
  140. }
  141. )
  142. self.assertEqual(response.status_code, 400)
  143. self.assertEqual(
  144. response.json(), {
  145. 'to': ["BobBoberson can't participate in private threads."],
  146. }
  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(), {
  161. 'to': ["BobBoberson is blocking you."],
  162. })
  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. )
  181. def test_cant_invite_followers_only(self):
  182. """api validates that you cant invite followers-only user to thread"""
  183. user_constant = UserModel.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. 'to': ["BobBoberson limits invitations to private threads to followed users."],
  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. )
  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. )
  232. def test_cant_invite_anyone(self):
  233. """api validates that you cant invite nobody user to thread"""
  234. user_constant = UserModel.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': ["BobBoberson is not allowing invitations to private threads."],
  249. }
  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. )
  267. def test_can_start_thread(self):
  268. """endpoint creates new thread"""
  269. response = self.client.post(
  270. self.api_link,
  271. data={
  272. 'to': [self.other_user.username],
  273. 'title': "Hello, I am test thread!",
  274. 'post': "Lorem ipsum dolor met!",
  275. }
  276. )
  277. self.assertEqual(response.status_code, 200)
  278. thread = self.user.thread_set.all()[:1][0]
  279. response_json = response.json()
  280. self.assertEqual(response_json['url'], thread.get_absolute_url())
  281. response = self.client.get(thread.get_absolute_url())
  282. self.assertContains(response, self.category.name)
  283. self.assertContains(response, thread.title)
  284. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")
  285. # don't count private threads
  286. self.reload_user()
  287. self.assertEqual(self.user.threads, 0)
  288. self.assertEqual(self.user.posts, 0)
  289. self.assertEqual(thread.category_id, self.category.pk)
  290. self.assertEqual(thread.title, "Hello, I am test thread!")
  291. self.assertEqual(thread.starter_id, self.user.id)
  292. self.assertEqual(thread.starter_name, self.user.username)
  293. self.assertEqual(thread.starter_slug, self.user.slug)
  294. self.assertEqual(thread.last_poster_id, self.user.id)
  295. self.assertEqual(thread.last_poster_name, self.user.username)
  296. self.assertEqual(thread.last_poster_slug, self.user.slug)
  297. post = self.user.post_set.all()[:1][0]
  298. self.assertEqual(post.category_id, self.category.pk)
  299. self.assertEqual(post.original, 'Lorem ipsum dolor met!')
  300. self.assertEqual(post.poster_id, self.user.id)
  301. self.assertEqual(post.poster_name, self.user.username)
  302. self.assertEqual(self.user.audittrail_set.count(), 1)
  303. # thread has two participants
  304. self.assertEqual(thread.participants.count(), 2)
  305. # we are thread owner
  306. ThreadParticipant.objects.get(thread=thread, user=self.user, is_owner=True)
  307. # other user was added to thread
  308. ThreadParticipant.objects.get(thread=thread, user=self.other_user, is_owner=False)
  309. # other user has sync_unread_private_threads flag
  310. user_to_sync = UserModel.objects.get(sync_unread_private_threads=True)
  311. self.assertEqual(user_to_sync, self.other_user)
  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)