test_views.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django.contrib.auth import get_user_model
  2. from django.core.urlresolvers import reverse
  3. from misago.admin.testutils import AdminTestCase
  4. from misago.notifications.api import notify_user
  5. class NotificationViewsTestCase(AdminTestCase):
  6. def setUp(self):
  7. self.view_link = reverse('misago:notifications')
  8. self.ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
  9. super(NotificationViewsTestCase, self).setUp()
  10. def reload_test_admin(self):
  11. self.test_admin = get_user_model().objects.get(id=self.test_admin.id)
  12. def notify_user(self):
  13. notify_user(self.test_admin,
  14. "Test notify %(token)s",
  15. "/users/",
  16. "test",
  17. {'token': 'Bob'},
  18. self.test_admin)
  19. self.test_admin = get_user_model().objects.get(id=self.test_admin.id)
  20. def test_get(self):
  21. """get request to list renders list"""
  22. response = self.client.get(self.view_link)
  23. self.assertEqual(response.status_code, 200)
  24. self.assertIn("have any notifications", response.content)
  25. self.notify_user()
  26. response = self.client.get(self.view_link)
  27. self.assertEqual(response.status_code, 200)
  28. self.assertIn("Test notify <strong>Bob</strong>", response.content)
  29. def test_post(self):
  30. """post request to list sets all notifications as read"""
  31. self.notify_user()
  32. response = self.client.post(self.view_link)
  33. self.assertEqual(response.status_code, 302)
  34. response = self.client.get(self.view_link)
  35. self.assertEqual(response.status_code, 200)
  36. self.reload_test_admin()
  37. self.assertEqual(self.test_admin.new_notifications, 0)
  38. def test_get_ajax(self):
  39. """get request to list renders list"""
  40. response = self.client.get(self.view_link, **self.ajax_header)
  41. self.assertEqual(response.status_code, 200)
  42. self.assertIn("have any new notifications", response.content)
  43. self.notify_user()
  44. response = self.client.get(self.view_link, **self.ajax_header)
  45. self.assertEqual(response.status_code, 200)
  46. self.assertIn("Test notify <strong>Bob</strong>", response.content)
  47. def test_post_ajax(self):
  48. """post request to list sets all notifications as read"""
  49. self.notify_user()
  50. response = self.client.post(self.view_link, **self.ajax_header)
  51. self.assertEqual(response.status_code, 200)
  52. response = self.client.get(self.view_link)
  53. self.assertEqual(response.status_code, 200)
  54. self.reload_test_admin()
  55. self.assertEqual(self.test_admin.new_notifications, 0)