test_user_feeds_api.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from django.urls import reverse
  2. from misago.threads import testutils
  3. from misago.threads.tests.test_threads_api import ThreadsApiTestCase
  4. class UserThreadsApiTests(ThreadsApiTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.api_link = reverse("misago:api:user-threads", kwargs={"pk": self.user.pk})
  8. def test_invalid_user_id(self):
  9. """api validates user id"""
  10. link = reverse("misago:api:user-threads", kwargs={"pk": "abcd"})
  11. response = self.client.get(link)
  12. self.assertEqual(response.status_code, 404)
  13. def test_nonexistant_user_id(self):
  14. """api validates that user for id exists"""
  15. link = reverse("misago:api:user-threads", kwargs={"pk": self.user.pk + 1})
  16. response = self.client.get(link)
  17. self.assertEqual(response.status_code, 404)
  18. def test_empty_response(self):
  19. """api has no showstopers on empty response"""
  20. response = self.client.get(self.api_link)
  21. self.assertEqual(response.status_code, 200)
  22. self.assertEqual(response.json()["count"], 0)
  23. def test_user_post(self):
  24. """user post doesn't show in feed because its not first post in thread"""
  25. testutils.reply_thread(self.thread, poster=self.user)
  26. response = self.client.get(self.api_link)
  27. self.assertEqual(response.status_code, 200)
  28. self.assertEqual(response.json()["count"], 0)
  29. def test_user_event(self):
  30. """events don't show in feeds at all"""
  31. testutils.reply_thread(self.thread, poster=self.user, is_event=True)
  32. response = self.client.get(self.api_link)
  33. self.assertEqual(response.status_code, 200)
  34. self.assertEqual(response.json()["count"], 0)
  35. def test_user_thread(self):
  36. """user thread shows in feed"""
  37. thread = testutils.post_thread(category=self.category, poster=self.user)
  38. # this post will not show in feed
  39. testutils.reply_thread(thread, poster=self.user)
  40. response = self.client.get(self.api_link)
  41. self.assertEqual(response.status_code, 200)
  42. self.assertEqual(response.json()["count"], 1)
  43. self.assertEqual(response.json()["results"][0]["id"], thread.first_post_id)
  44. def test_user_thread_anonymous(self):
  45. """user thread shows in feed requested by unauthenticated user"""
  46. thread = testutils.post_thread(category=self.category, poster=self.user)
  47. self.logout_user()
  48. response = self.client.get(self.api_link)
  49. self.assertEqual(response.status_code, 200)
  50. self.assertEqual(response.json()["count"], 1)
  51. self.assertEqual(response.json()["results"][0]["id"], thread.first_post_id)
  52. class UserPostsApiTests(ThreadsApiTestCase):
  53. def setUp(self):
  54. super().setUp()
  55. self.api_link = reverse("misago:api:user-posts", kwargs={"pk": self.user.pk})
  56. def test_invalid_user_id(self):
  57. """api validates user id"""
  58. link = reverse("misago:api:user-posts", kwargs={"pk": "abcd"})
  59. response = self.client.get(link)
  60. self.assertEqual(response.status_code, 404)
  61. def test_nonexistant_user_id(self):
  62. """api validates that user for id exists"""
  63. link = reverse("misago:api:user-posts", kwargs={"pk": self.user.pk + 1})
  64. response = self.client.get(link)
  65. self.assertEqual(response.status_code, 404)
  66. def test_empty_response(self):
  67. """api has no showstopers on empty response"""
  68. response = self.client.get(self.api_link)
  69. self.assertEqual(response.status_code, 200)
  70. self.assertEqual(response.json()["count"], 0)
  71. def test_user_event(self):
  72. """events don't show in feeds at all"""
  73. testutils.reply_thread(self.thread, poster=self.user, is_event=True)
  74. response = self.client.get(self.api_link)
  75. self.assertEqual(response.status_code, 200)
  76. self.assertEqual(response.json()["count"], 0)
  77. def test_user_hidden_post(self):
  78. """hidden posts don't show in feeds at all"""
  79. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True)
  80. response = self.client.get(self.api_link)
  81. self.assertEqual(response.status_code, 200)
  82. self.assertEqual(response.json()["count"], 0)
  83. def test_user_unapproved_post(self):
  84. """unapproved posts don't show in feeds at all"""
  85. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True)
  86. response = self.client.get(self.api_link)
  87. self.assertEqual(response.status_code, 200)
  88. self.assertEqual(response.json()["count"], 0)
  89. def test_user_posts(self):
  90. """user posts show in feed"""
  91. post = testutils.reply_thread(self.thread, poster=self.user)
  92. other_post = testutils.reply_thread(self.thread, poster=self.user)
  93. response = self.client.get(self.api_link)
  94. self.assertEqual(response.status_code, 200)
  95. self.assertEqual(response.json()["count"], 2)
  96. self.assertEqual(response.json()["results"][0]["id"], other_post.pk)
  97. self.assertEqual(response.json()["results"][1]["id"], post.pk)
  98. def test_user_thread(self):
  99. """user thread shows in feed"""
  100. thread = testutils.post_thread(category=self.category, poster=self.user)
  101. post = testutils.reply_thread(thread, poster=self.user)
  102. response = self.client.get(self.api_link)
  103. self.assertEqual(response.status_code, 200)
  104. self.assertEqual(response.json()["count"], 2)
  105. self.assertEqual(response.json()["results"][0]["id"], post.pk)
  106. self.assertEqual(response.json()["results"][1]["id"], thread.first_post_id)
  107. def test_user_post_anonymous(self):
  108. """user post shows in feed requested by unauthenticated user"""
  109. post = testutils.reply_thread(self.thread, poster=self.user)
  110. other_post = testutils.reply_thread(self.thread, poster=self.user)
  111. self.logout_user()
  112. response = self.client.get(self.api_link)
  113. self.assertEqual(response.status_code, 200)
  114. self.assertEqual(response.json()["count"], 2)
  115. self.assertEqual(response.json()["results"][0]["id"], other_post.pk)
  116. self.assertEqual(response.json()["results"][1]["id"], post.pk)
  117. def test_user_thread_anonymous(self):
  118. """user thread shows in feed requested by unauthenticated user"""
  119. thread = testutils.post_thread(category=self.category, poster=self.user)
  120. post = testutils.reply_thread(thread, poster=self.user)
  121. self.logout_user()
  122. response = self.client.get(self.api_link)
  123. self.assertEqual(response.status_code, 200)
  124. self.assertEqual(response.json()["count"], 2)
  125. self.assertEqual(response.json()["results"][0]["id"], post.pk)
  126. self.assertEqual(response.json()["results"][1]["id"], thread.first_post_id)