test_thread_postread_api.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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('misago:api:thread-post-read', kwargs={
  14. 'thread_pk': self.thread.pk,
  15. 'pk': self.post.pk
  16. })
  17. def test_read_anonymous(self):
  18. """api validates if reading user is authenticated"""
  19. self.logout_user()
  20. response = self.client.post(self.api_link)
  21. self.assertContains(response, "This action is not available to guests.", status_code=403)
  22. def test_read_post(self):
  23. """api marks post as read"""
  24. response = self.client.post(self.api_link)
  25. self.assertEqual(response.status_code, 200)
  26. thread_read = self.user.threadread_set.order_by('id').last()
  27. self.assertEqual(thread_read.thread_id, self.thread.id)
  28. self.assertEqual(thread_read.last_read_on, self.post.posted_on)
  29. category_read = self.user.categoryread_set.order_by('id').last()
  30. self.assertTrue(category_read.last_read_on >= self.post.posted_on)
  31. def test_read_subscribed_thread_post(self):
  32. """api marks post as read and updates subscription"""
  33. self.thread.subscription_set.create(
  34. user=self.user,
  35. thread=self.thread,
  36. category=self.thread.category,
  37. last_read_on=self.thread.post_set.order_by('id').first().posted_on
  38. )
  39. response = self.client.post(self.api_link)
  40. self.assertEqual(response.status_code, 200)
  41. thread_read = self.user.threadread_set.order_by('id').last()
  42. self.assertEqual(thread_read.thread_id, self.thread.id)
  43. self.assertEqual(thread_read.last_read_on, self.post.posted_on)
  44. category_read = self.user.categoryread_set.order_by('id').last()
  45. self.assertTrue(category_read.last_read_on >= self.post.posted_on)
  46. subscription = self.thread.subscription_set.order_by('id').last()
  47. self.assertEqual(subscription.last_read_on, self.post.posted_on)