test_api.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from django.contrib.auth import get_user_model
  2. from django.test import TestCase
  3. from misago.notifications import api
  4. class NotificationsAPITests(TestCase):
  5. def setUp(self):
  6. User = get_user_model()
  7. self.test_user = User.objects.create_user('Bob', 'bob@bob.com',
  8. 'Pass.123')
  9. def reload_test_user(self):
  10. self.test_user = get_user_model().objects.get(id=self.test_user.id)
  11. def test_notify_user(self):
  12. """notify_user sets new notification on user"""
  13. api.notify_user(self.test_user,
  14. "Test notify %(token)s",
  15. "/users/",
  16. "test",
  17. {'token': 'Bob'},
  18. self.test_user)
  19. self.reload_test_user()
  20. self.assertEqual(self.test_user.new_notifications, 1)
  21. def test_read_user_notification(self):
  22. """read_user_notification reads user notification"""
  23. api.notify_user(self.test_user,
  24. "Test notify %(token)s",
  25. "/users/",
  26. "test",
  27. {'token': 'Bob'},
  28. self.test_user)
  29. self.reload_test_user()
  30. api.read_user_notification(self.test_user, "test")
  31. self.assertEqual(self.test_user.new_notifications, 0)
  32. notifications_qs = self.test_user.notifications.filter(is_new=True)
  33. self.assertEqual(notifications_qs.count(), 0)
  34. def test_read_all_user_alerts(self):
  35. """read_all_user_alerts marks user notifications as read"""
  36. api.notify_user(self.test_user,
  37. "Test notify %(token)s",
  38. "/users/",
  39. "test",
  40. {'token': 'Bob'},
  41. self.test_user)
  42. self.reload_test_user()
  43. api.read_all_user_alerts(self.test_user)
  44. self.assertEqual(self.test_user.new_notifications, 0)
  45. notifications_qs = self.test_user.notifications.filter(is_new=True)
  46. self.assertEqual(notifications_qs.count(), 0)
  47. def test_assert_real_new_notifications_count(self):
  48. """assert_real_new_notifications_count syncs user notifications"""
  49. api.notify_user(self.test_user,
  50. "Test notify %(token)s",
  51. "/users/",
  52. "test",
  53. {'token': 'Bob'},
  54. self.test_user)
  55. self.reload_test_user()
  56. api.read_all_user_alerts(self.test_user)
  57. self.test_user.new_notifications = 42
  58. self.test_user.save()
  59. self.reload_test_user()
  60. self.assertEqual(self.test_user.new_notifications, 42)
  61. notifications_qs = self.test_user.notifications.filter(is_new=True)
  62. self.assertEqual(notifications_qs.count(), 0)
  63. api.assert_real_new_notifications_count(self.test_user)
  64. self.reload_test_user()
  65. self.assertEqual(self.test_user.new_notifications, 0)