test_privatethread_start_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 ..models import Thread, ThreadParticipant
  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. User = get_user_model()
  18. self.other_user = get_user_model().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 = get_user_model()
  161. user_constant = User.LIMITS_PRIVATE_THREAD_INVITES_TO_FOLLOWED
  162. self.other_user.limits_private_thread_invites_to = user_constant
  163. self.other_user.save()
  164. response = self.client.post(self.api_link, data={
  165. 'to': [self.other_user.username],
  166. 'title': "Lorem ipsum dolor met",
  167. 'post': "Lorem ipsum dolor.",
  168. })
  169. self.assertEqual(response.status_code, 400)
  170. self.assertEqual(response.json(), {
  171. 'to': [
  172. "BobBoberson limits invitations to private threads to followed users."
  173. ]
  174. })
  175. # allow us to bypass following check
  176. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  177. response = self.client.post(self.api_link, data={
  178. 'to': [self.other_user.username],
  179. 'title': "-----",
  180. 'post': "Lorem ipsum dolor.",
  181. })
  182. self.assertEqual(response.status_code, 400)
  183. self.assertEqual(response.json(), {
  184. 'title': [
  185. "Thread title should contain alpha-numeric characters."
  186. ]
  187. })
  188. # make user follow us
  189. override_acl(self.user, {'can_add_everyone_to_private_threads': 0})
  190. self.other_user.follows.add(self.user)
  191. response = self.client.post(self.api_link, data={
  192. 'to': [self.other_user.username],
  193. 'title': "-----",
  194. 'post': "Lorem ipsum dolor.",
  195. })
  196. self.assertEqual(response.status_code, 400)
  197. self.assertEqual(response.json(), {
  198. 'title': [
  199. "Thread title should contain alpha-numeric characters."
  200. ]
  201. })
  202. def test_cant_invite_anyone(self):
  203. """api validates that you cant invite nobody user to thread"""
  204. User = get_user_model()
  205. user_constant = User.LIMITS_PRIVATE_THREAD_INVITES_TO_NOBODY
  206. self.other_user.limits_private_thread_invites_to = user_constant
  207. self.other_user.save()
  208. response = self.client.post(self.api_link, data={
  209. 'to': [self.other_user.username],
  210. 'title': "Lorem ipsum dolor met",
  211. 'post': "Lorem ipsum dolor.",
  212. })
  213. self.assertEqual(response.status_code, 400)
  214. self.assertEqual(response.json(), {
  215. 'to': [
  216. "BobBoberson is not allowing invitations to private threads."
  217. ]
  218. })
  219. # allow us to bypass user preference check
  220. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  221. response = self.client.post(self.api_link, data={
  222. 'to': [self.other_user.username],
  223. 'title': "-----",
  224. 'post': "Lorem ipsum dolor.",
  225. })
  226. self.assertEqual(response.status_code, 400)
  227. self.assertEqual(response.json(), {
  228. 'title': [
  229. "Thread title should contain alpha-numeric characters."
  230. ]
  231. })
  232. def test_can_start_thread(self):
  233. """endpoint creates new thread"""
  234. response = self.client.post(self.api_link, data={
  235. 'to': [self.other_user.username],
  236. 'title': "Hello, I am test thread!",
  237. 'post': "Lorem ipsum dolor met!"
  238. })
  239. self.assertEqual(response.status_code, 200)
  240. thread = self.user.thread_set.all()[:1][0]
  241. response_json = response.json()
  242. self.assertEqual(response_json['url'], thread.get_absolute_url())
  243. response = self.client.get(thread.get_absolute_url())
  244. self.assertContains(response, self.category.name)
  245. self.assertContains(response, thread.title)
  246. self.assertContains(response, "<p>Lorem ipsum dolor met!</p>")
  247. # don't count private threads
  248. self.reload_user()
  249. self.assertEqual(self.user.threads, 0)
  250. self.assertEqual(self.user.posts, 0)
  251. self.assertEqual(thread.category_id, self.category.pk)
  252. self.assertEqual(thread.title, "Hello, I am test thread!")
  253. self.assertEqual(thread.starter_id, self.user.id)
  254. self.assertEqual(thread.starter_name, self.user.username)
  255. self.assertEqual(thread.starter_slug, self.user.slug)
  256. self.assertEqual(thread.last_poster_id, self.user.id)
  257. self.assertEqual(thread.last_poster_name, self.user.username)
  258. self.assertEqual(thread.last_poster_slug, self.user.slug)
  259. post = self.user.post_set.all()[:1][0]
  260. self.assertEqual(post.category_id, self.category.pk)
  261. self.assertEqual(post.original, 'Lorem ipsum dolor met!')
  262. self.assertEqual(post.poster_id, self.user.id)
  263. self.assertEqual(post.poster_name, self.user.username)
  264. # thread has two participants
  265. self.assertEqual(thread.participants.count(), 2)
  266. # we are thread owner
  267. ThreadParticipant.objects.get(
  268. thread=thread,
  269. user=self.user,
  270. is_owner=True
  271. )
  272. # other user was added to thread
  273. ThreadParticipant.objects.get(
  274. thread=thread,
  275. user=self.other_user,
  276. is_owner=False
  277. )
  278. # other user has sync_unread_private_threads flag
  279. User = get_user_model()
  280. user_to_sync = User.objects.get(sync_unread_private_threads=True)
  281. self.assertEqual(user_to_sync, self.other_user)
  282. # notification about new private thread was sent to other user
  283. self.assertEqual(len(mail.outbox), 1)
  284. email = mail.outbox[-1]
  285. self.assertIn(self.user.username, email.subject)
  286. self.assertIn(thread.title, email.subject)
  287. email_body = smart_str(email.body)
  288. self.assertIn(self.user.username, email_body)
  289. self.assertIn(thread.title, email_body)
  290. self.assertIn(thread.get_absolute_url(), email_body)
  291. def test_post_unicode(self):
  292. """unicode characters can be posted"""
  293. response = self.client.post(self.api_link, data={
  294. 'to': [self.other_user.username],
  295. 'title': "Brzęczyżczykiewicz",
  296. 'post': "Chrzążczyżewoszyce, powiat Łękółody."
  297. })
  298. self.assertEqual(response.status_code, 200)