test_thread_postread_api.py 2.3 KB

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