Browse Source

#414: notifications

Rafał Pitoń 10 years ago
parent
commit
e440f21e4f

+ 1 - 1
misago/core/errorpages.py

@@ -9,7 +9,7 @@ from misago.admin.views.errorpages import admin_error_page, admin_csrf_failure
 def _ajax_error(code=406, message=None):
 def _ajax_error(code=406, message=None):
     response_dict = {'is_error': True}
     response_dict = {'is_error': True}
     if message:
     if message:
-        response_dict['message'] = message
+        response_dict['message'] = unicode(message)
     return JsonResponse(response_dict, status=code)
     return JsonResponse(response_dict, status=code)
 
 
 
 

+ 1 - 1
misago/core/exceptionhandler.py

@@ -15,7 +15,7 @@ def is_misago_exception(exception):
 
 
 
 
 def handle_ajax_error(request, exception):
 def handle_ajax_error(request, exception):
-    json = {'is_error': 1, 'message': exception.message}
+    json = {'is_error': 1, 'message': unicode(exception.message)}
     return JsonResponse(json, status=exception.code)
     return JsonResponse(json, status=exception.code)
 
 
 
 

+ 40 - 15
misago/notifications/tests/test_views.py

@@ -1,28 +1,25 @@
 from django.contrib.auth import get_user_model
 from django.contrib.auth import get_user_model
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
 
 
-from misago.admin.testutils import AdminTestCase
+from misago.users.testutils import UserTestCase, AuthenticatedUserTestCase
 
 
 from misago.notifications.api import notify_user
 from misago.notifications.api import notify_user
 
 
 
 
-class NotificationViewsTestCase(AdminTestCase):
+class NotificationViewsTests(AuthenticatedUserTestCase):
     def setUp(self):
     def setUp(self):
         self.view_link = reverse('misago:notifications')
         self.view_link = reverse('misago:notifications')
         self.ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
         self.ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
-        super(NotificationViewsTestCase, self).setUp()
-
-    def reload_test_admin(self):
-        self.test_admin = get_user_model().objects.get(id=self.test_admin.id)
+        super(NotificationViewsTests, self).setUp()
 
 
     def notify_user(self):
     def notify_user(self):
-        notify_user(self.test_admin,
+        notify_user(self.user,
                     "Test notify %(token)s",
                     "Test notify %(token)s",
                     "/users/",
                     "/users/",
                     "test",
                     "test",
                     {'token': 'Bob'},
                     {'token': 'Bob'},
-                    self.test_admin)
-        self.test_admin = get_user_model().objects.get(id=self.test_admin.id)
+                    self.user)
+        self.user = get_user_model().objects.get(id=self.user.id)
 
 
     def test_get(self):
     def test_get(self):
         """get request to list renders list"""
         """get request to list renders list"""
@@ -46,11 +43,11 @@ class NotificationViewsTestCase(AdminTestCase):
         response = self.client.get(self.view_link)
         response = self.client.get(self.view_link)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-        self.reload_test_admin()
-        self.assertEqual(self.test_admin.new_notifications, 0)
+        self.reload_user()
+        self.assertEqual(self.user.new_notifications, 0)
 
 
     def test_get_ajax(self):
     def test_get_ajax(self):
-        """get request to list renders list"""
+        """get ajax to list renders list"""
         response = self.client.get(self.view_link, **self.ajax_header)
         response = self.client.get(self.view_link, **self.ajax_header)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
         self.assertIn("have any new notifications", response.content)
         self.assertIn("have any new notifications", response.content)
@@ -62,7 +59,7 @@ class NotificationViewsTestCase(AdminTestCase):
         self.assertIn("Test notify <strong>Bob</strong>", response.content)
         self.assertIn("Test notify <strong>Bob</strong>", response.content)
 
 
     def test_post_ajax(self):
     def test_post_ajax(self):
-        """post request to list sets all notifications as read"""
+        """post ajax to list sets all notifications as read"""
         self.notify_user()
         self.notify_user()
 
 
         response = self.client.post(self.view_link, **self.ajax_header)
         response = self.client.post(self.view_link, **self.ajax_header)
@@ -71,5 +68,33 @@ class NotificationViewsTestCase(AdminTestCase):
         response = self.client.get(self.view_link)
         response = self.client.get(self.view_link)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
-        self.reload_test_admin()
-        self.assertEqual(self.test_admin.new_notifications, 0)
+        self.reload_user()
+        self.assertEqual(self.user.new_notifications, 0)
+
+
+class AnonymousNotificationsViewsTests(UserTestCase):
+    def setUp(self):
+        self.view_link = reverse('misago:notifications')
+        self.ajax_header = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
+        super(AnonymousNotificationsViewsTests, self).setUp()
+
+    def test_get(self):
+        """get request to list returns 403 for guests"""
+        response = self.client.get(self.view_link)
+        self.assertEqual(response.status_code, 403)
+
+    def test_post(self):
+        """post request to list returns 403 for guests"""
+        response = self.client.post(self.view_link)
+        self.assertEqual(response.status_code, 403)
+
+    def test_get_ajax(self):
+        """get ajax request to list returns 403 for guests"""
+        response = self.client.get(self.view_link, **self.ajax_header)
+        self.assertEqual(response.status_code, 403)
+
+    def test_post_ajax(self):
+        """post ajax request to list returns 403 for guests"""
+        response = self.client.post(self.view_link, **self.ajax_header)
+        self.assertEqual(response.status_code, 403)
+