test_privatethread_start_api.py 12 KB

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