test_thread_postread_api.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.urls import reverse
  2. from django.utils import timezone
  3. from misago.threads import testutils
  4. from .test_threads_api import ThreadsApiTestCase
  5. class PostReadApiTests(ThreadsApiTestCase):
  6. def setUp(self):
  7. super(PostReadApiTests, self).setUp()
  8. self.post = testutils.reply_thread(
  9. self.thread,
  10. poster=self.user,
  11. posted_on=timezone.now(),
  12. )
  13. self.api_link = reverse(
  14. 'misago:api:thread-post-read',
  15. kwargs={
  16. 'thread_pk': self.thread.pk,
  17. 'pk': self.post.pk,
  18. }
  19. )
  20. def test_read_anonymous(self):
  21. """api validates if reading user is authenticated"""
  22. self.logout_user()
  23. response = self.client.post(self.api_link)
  24. self.assertContains(response, "This action is not available to guests.", status_code=403)
  25. def test_read_post(self):
  26. """api marks post as read"""
  27. response = self.client.post(self.api_link)
  28. self.assertEqual(response.status_code, 200)
  29. thread_read = self.user.threadread_set.order_by('id').last()
  30. self.assertEqual(thread_read.thread_id, self.thread.id)
  31. self.assertEqual(thread_read.last_read_on, self.post.posted_on)
  32. category_read = self.user.categoryread_set.order_by('id').last()
  33. self.assertTrue(category_read.last_read_on >= self.post.posted_on)
  34. def test_read_subscribed_thread_post(self):
  35. """api marks post as read and updates subscription"""
  36. self.thread.subscription_set.create(
  37. user=self.user,
  38. thread=self.thread,
  39. category=self.thread.category,
  40. last_read_on=self.thread.post_set.order_by('id').first().posted_on,
  41. )
  42. response = self.client.post(self.api_link)
  43. self.assertEqual(response.status_code, 200)
  44. thread_read = self.user.threadread_set.order_by('id').last()
  45. self.assertEqual(thread_read.thread_id, self.thread.id)
  46. self.assertEqual(thread_read.last_read_on, self.post.posted_on)
  47. category_read = self.user.categoryread_set.order_by('id').last()
  48. self.assertTrue(category_read.last_read_on >= self.post.posted_on)
  49. subscription = self.thread.subscription_set.order_by('id').last()
  50. self.assertEqual(subscription.last_read_on, self.post.posted_on)