test_privatethread_start_api.py 12 KB

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