test_privatethread_start_api.py 12 KB

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