test_privatethread_start_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib.auth import get_user_model
  4. from django.core import mail
  5. from django.urls import reverse
  6. from django.utils.encoding import smart_str
  7. from misago.acl.testutils import override_acl
  8. from misago.categories.models import Category
  9. from misago.threads.models import ThreadParticipant
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. UserModel = get_user_model()
  12. class StartPrivateThreadTests(AuthenticatedUserTestCase):
  13. def setUp(self):
  14. super(StartPrivateThreadTests, self).setUp()
  15. self.category = Category.objects.private_threads()
  16. self.api_link = reverse('misago:api:private-thread-list')
  17. self.other_user = UserModel.objects.create_user(
  18. 'BobBoberson', 'bob@boberson.com', 'pass123'
  19. )
  20. def test_cant_start_thread_as_guest(self):
  21. """user has to be authenticated to be able to post private thread"""
  22. self.logout_user()
  23. response = self.client.post(self.api_link)
  24. self.assertEqual(response.status_code, 403)
  25. def test_cant_use_private_threads(self):
  26. """has no permission to use private threads"""
  27. override_acl(self.user, {'can_use_private_threads': 0})
  28. response = self.client.post(self.api_link)
  29. self.assertEqual(response.status_code, 403)
  30. self.assertEqual(response.json(), {
  31. 'detail': "You can't use private threads.",
  32. })
  33. def test_cant_start_private_thread(self):
  34. """permission to start private thread is validated"""
  35. override_acl(self.user, {'can_start_private_threads': 0})
  36. response = self.client.post(self.api_link)
  37. self.assertEqual(response.status_code, 403)
  38. self.assertEqual(response.json(), {
  39. 'detail': "You can't start private threads.",
  40. })
  41. def test_empty_data(self):
  42. """no data sent handling has no showstoppers"""
  43. response = self.client.post(self.api_link, data={})
  44. self.assertEqual(response.status_code, 400)
  45. self.assertEqual(
  46. response.json(), {
  47. 'to': ["You have to enter user names."],
  48. 'title': ["You have to enter thread title."],
  49. 'post': ["You have to enter a message."],
  50. }
  51. )
  52. def test_title_is_validated(self):
  53. """title is validated"""
  54. response = self.client.post(
  55. self.api_link,
  56. data={
  57. 'to': [self.other_user.username],
  58. 'title': "------",
  59. 'post': "Lorem ipsum dolor met, sit amet elit!",
  60. }
  61. )
  62. self.assertEqual(response.status_code, 400)
  63. self.assertEqual(
  64. response.json(), {
  65. 'title': ["Thread title should contain alpha-numeric characters."],
  66. }
  67. )
  68. def test_post_is_validated(self):
  69. """post is validated"""
  70. response = self.client.post(
  71. self.api_link,
  72. data={
  73. 'to': [self.other_user.username],
  74. 'title': "Lorem ipsum dolor met",
  75. 'post': "a",
  76. }
  77. )
  78. self.assertEqual(response.status_code, 400)
  79. self.assertEqual(
  80. response.json(), {
  81. 'post': ["Posted message should be at least 5 characters long (it has 1)."],
  82. }
  83. )
  84. def test_cant_invite_self(self):
  85. """api validates that you cant invite yourself to private thread"""
  86. response = self.client.post(
  87. self.api_link,
  88. data={
  89. 'to': [self.user.username],
  90. 'title': "Lorem ipsum dolor met",
  91. 'post': "Lorem ipsum dolor.",
  92. }
  93. )
  94. self.assertEqual(response.status_code, 400)
  95. self.assertEqual(
  96. response.json(), {
  97. 'to': ["You can't include yourself on the list of users to invite to new thread."],
  98. }
  99. )
  100. def test_cant_invite_nonexisting(self):
  101. """api validates that you cant invite nonexisting user to thread"""
  102. response = self.client.post(
  103. self.api_link,
  104. data={
  105. 'to': ['Ab', 'Cd'],
  106. 'title': "Lorem ipsum dolor met",
  107. 'post': "Lorem ipsum dolor.",
  108. }
  109. )
  110. self.assertEqual(response.status_code, 400)
  111. self.assertEqual(
  112. response.json(), {
  113. 'to': ["One or more users could not be found: ab, cd"],
  114. }
  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{}'.format(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. 'to': ["You can't add more than 3 users to private thread (you've added 50)."],
  130. }
  131. )
  132. def test_cant_invite_no_permission(self):
  133. """api validates invited user permission to private thread"""
  134. override_acl(self.other_user, {'can_use_private_threads': 0})
  135. response = self.client.post(
  136. self.api_link,
  137. data={
  138. 'to': [self.other_user.username],
  139. 'title': "Lorem ipsum dolor met",
  140. 'post': "Lorem ipsum dolor.",
  141. }
  142. )
  143. self.assertEqual(response.status_code, 400)
  144. self.assertEqual(
  145. response.json(), {
  146. 'to': ["BobBoberson can't participate in private threads."],
  147. }
  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(), {
  162. 'to': ["BobBoberson is blocking you."],
  163. })
  164. # allow us to bypass blocked check
  165. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  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. )
  180. def test_cant_invite_followers_only(self):
  181. """api validates that you cant invite followers-only user to thread"""
  182. user_constant = UserModel.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. 'to': ["BobBoberson limits invitations to private threads to followed users."],
  197. }
  198. )
  199. # allow us to bypass following check
  200. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  201. response = self.client.post(
  202. self.api_link,
  203. data={
  204. 'to': [self.other_user.username],
  205. 'title': "-----",
  206. 'post': "Lorem ipsum dolor.",
  207. }
  208. )
  209. self.assertEqual(response.status_code, 400)
  210. self.assertEqual(
  211. response.json(), {
  212. 'title': ["Thread title should contain alpha-numeric characters."],
  213. }
  214. )
  215. # make user follow us
  216. override_acl(self.user, {'can_add_everyone_to_private_threads': 0})
  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. override_acl(self.user, {'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. self.assertEqual(response.json(), {
  280. 'id': thread.pk,
  281. 'title': thread.title,
  282. 'url': thread.get_absolute_url(),
  283. })
  284. response = self.client.get(thread.get_absolute_url())
  285. self.assertContains(response, self.category.name)
  286. self.assertContains(response, thread.title)
  287. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")
  288. # don't count private threads
  289. self.reload_user()
  290. self.assertEqual(self.user.threads, 0)
  291. self.assertEqual(self.user.posts, 0)
  292. self.assertEqual(thread.category_id, self.category.pk)
  293. self.assertEqual(thread.title, "Hello, I am test thread!")
  294. self.assertEqual(thread.starter_id, self.user.id)
  295. self.assertEqual(thread.starter_name, self.user.username)
  296. self.assertEqual(thread.starter_slug, self.user.slug)
  297. self.assertEqual(thread.last_poster_id, self.user.id)
  298. self.assertEqual(thread.last_poster_name, self.user.username)
  299. self.assertEqual(thread.last_poster_slug, self.user.slug)
  300. post = self.user.post_set.all()[:1][0]
  301. self.assertEqual(post.category_id, self.category.pk)
  302. self.assertEqual(post.original, 'Lorem ipsum dolor met!')
  303. self.assertEqual(post.poster_id, self.user.id)
  304. self.assertEqual(post.poster_name, self.user.username)
  305. # thread has two participants
  306. self.assertEqual(thread.participants.count(), 2)
  307. # we are thread owner
  308. ThreadParticipant.objects.get(thread=thread, user=self.user, is_owner=True)
  309. # other user was added to thread
  310. ThreadParticipant.objects.get(thread=thread, user=self.other_user, is_owner=False)
  311. # other user has sync_unread_private_threads flag
  312. user_to_sync = UserModel.objects.get(sync_unread_private_threads=True)
  313. self.assertEqual(user_to_sync, self.other_user)
  314. # notification about new private thread was sent to other user
  315. self.assertEqual(len(mail.outbox), 1)
  316. email = mail.outbox[-1]
  317. self.assertIn(self.user.username, email.subject)
  318. self.assertIn(thread.title, email.subject)
  319. email_body = smart_str(email.body)
  320. self.assertIn(self.user.username, email_body)
  321. self.assertIn(thread.title, email_body)
  322. self.assertIn(thread.get_absolute_url(), email_body)
  323. def test_post_unicode(self):
  324. """unicode characters can be posted"""
  325. response = self.client.post(
  326. self.api_link,
  327. data={
  328. 'to': [self.other_user.username],
  329. 'title': "Brzęczyżczykiewicz",
  330. 'post': "Chrzążczyżewoszyce, powiat Łękółody.",
  331. }
  332. )
  333. self.assertEqual(response.status_code, 200)