test_privatethread_start_api.py 13 KB

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