test_thread_postread_api.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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().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.assertEqual(response.status_code, 403)
  25. self.assertEqual(response.json(), {
  26. "detail": "This action is not available to guests."
  27. })
  28. def test_read_post(self):
  29. """api marks post as read"""
  30. response = self.client.post(self.api_link)
  31. self.assertEqual(response.status_code, 200)
  32. self.assertEqual(self.user.postread_set.count(), 1)
  33. self.user.postread_set.get(post=self.post)
  34. # one post read, first post is still unread
  35. self.assertFalse(response.json()['thread_is_read'])
  36. # read second post
  37. response = self.client.post(reverse(
  38. 'misago:api:thread-post-read',
  39. kwargs={
  40. 'thread_pk': self.thread.pk,
  41. 'pk': self.thread.first_post.pk,
  42. }
  43. ))
  44. self.assertEqual(response.status_code, 200)
  45. self.assertEqual(self.user.postread_set.count(), 2)
  46. self.user.postread_set.get(post=self.thread.first_post)
  47. # both posts are read
  48. self.assertTrue(response.json()['thread_is_read'])
  49. def test_read_subscribed_thread_post(self):
  50. """api marks post as read and updates subscription"""
  51. self.thread.subscription_set.create(
  52. user=self.user,
  53. thread=self.thread,
  54. category=self.thread.category,
  55. last_read_on=self.thread.post_set.order_by('id').first().posted_on,
  56. )
  57. response = self.client.post(self.api_link)
  58. self.assertEqual(response.status_code, 200)
  59. subscription = self.thread.subscription_set.order_by('id').last()
  60. self.assertEqual(subscription.last_read_on, self.post.posted_on)