test_privatethread_start_api.py 13 KB

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