test_privatethread_start_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 use private threads"""
  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_no_permission(self):
  117. """api validates invited user permission to private thread"""
  118. override_acl(self.other_user, {
  119. 'can_use_private_threads': 0
  120. })
  121. response = self.client.post(self.api_link, data={
  122. 'to': [self.other_user.username],
  123. 'title': "Lorem ipsum dolor met",
  124. 'post': "Lorem ipsum dolor.",
  125. })
  126. self.assertEqual(response.status_code, 400)
  127. self.assertEqual(response.json(), {
  128. 'to': [
  129. "BobBoberson can't participate in private threads."
  130. ]
  131. })
  132. def test_cant_invite_blocking(self):
  133. """api validates that you cant invite blocking user to thread"""
  134. self.other_user.blocks.add(self.user)
  135. response = self.client.post(self.api_link, data={
  136. 'to': [self.other_user.username],
  137. 'title': "Lorem ipsum dolor met",
  138. 'post': "Lorem ipsum dolor.",
  139. })
  140. self.assertEqual(response.status_code, 400)
  141. self.assertEqual(response.json(), {
  142. 'to': [
  143. "BobBoberson is blocking you."
  144. ]
  145. })
  146. # allow us to bypass blocked check
  147. override_acl(self.user, {'can_add_everyone_to_private_threads': 1})
  148. response = self.client.post(self.api_link, data={
  149. 'to': [self.other_user.username],
  150. 'title': "-----",
  151. 'post': "Lorem ipsum dolor.",
  152. })
  153. self.assertEqual(response.status_code, 400)
  154. self.assertEqual(response.json(), {
  155. 'title': [
  156. "Thread title should contain alpha-numeric characters."
  157. ]
  158. })
  159. def test_cant_invite_followers_only(self):
  160. """api validates that you cant invite followers-only user to thread"""
  161. self.other_user.limits_private_thread_invites_to = LIMITS_PRIVATE_THREAD_INVITES_TO_FOLLOWED
  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. self.other_user.limits_private_thread_invites_to = LIMITS_PRIVATE_THREAD_INVITES_TO_NOBODY
  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. self.reload_user()
  245. self.assertEqual(self.user.threads, 1)
  246. self.assertEqual(self.user.posts, 1)
  247. self.assertEqual(thread.category_id, self.category.pk)
  248. self.assertEqual(thread.title, "Hello, I am test thread!")
  249. self.assertEqual(thread.starter_id, self.user.id)
  250. self.assertEqual(thread.starter_name, self.user.username)
  251. self.assertEqual(thread.starter_slug, self.user.slug)
  252. self.assertEqual(thread.last_poster_id, self.user.id)
  253. self.assertEqual(thread.last_poster_name, self.user.username)
  254. self.assertEqual(thread.last_poster_slug, self.user.slug)
  255. post = self.user.post_set.all()[:1][0]
  256. self.assertEqual(post.category_id, self.category.pk)
  257. self.assertEqual(post.original, 'Lorem ipsum dolor met!')
  258. self.assertEqual(post.poster_id, self.user.id)
  259. self.assertEqual(post.poster_name, self.user.username)
  260. # thread has two participants
  261. self.assertEqual(thread.participants.count(), 2)
  262. # we are thread owner
  263. ThreadParticipant.objects.get(
  264. thread=thread,
  265. user=self.user,
  266. is_owner=True
  267. )
  268. # other user was added to thread
  269. ThreadParticipant.objects.get(
  270. thread=thread,
  271. user=self.other_user,
  272. is_owner=False
  273. )
  274. # other user has sync_unread_private_threads flag
  275. User = get_user_model()
  276. user_to_sync = User.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)