test_privatethread_start_api.py 13 KB

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