test_privatethread_start_api.py 12 KB

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