diff --git a/README.rst b/README.rst index 7922c16..0f0769e 100644 --- a/README.rst +++ b/README.rst @@ -342,7 +342,7 @@ Testing the live-updater ------------------------ 1. Clone the repo -2. Set the 'NOTIFICATION_TEST' environemnt variable. E.g. `export NOTIFICATION_TEST=1` +2. Set the 'NOTIFICATION_TEST' environment variable. E.g. `export NOTIFICATION_TEST=1` 3. Run `./manage.py runserver` 4. Browse to `yourserverip/test/` 5. Click 'Make a notification' and a new notification should appear in the list in 5-10 seconds. diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 3570ebe..bf64e58 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -284,3 +284,16 @@ class NotificationTestPages(TestCase): page = render(request, 'notifications/test_tags.html', {'request':request}) #TODO: Add more tests to check what is being output. + + def test_anon_user_gets_nothing(self): + response = self.client.post(reverse('notifications:live_unread_notification_count')) + self.assertEqual(response.status_code, 200) + data = json.loads(response.content.decode('utf-8')) + self.assertEqual(data['unread_count'],0) + + response = self.client.post(reverse('notifications:live_unread_notification_list')) + self.assertEqual(response.status_code, 200) + data = json.loads(response.content.decode('utf-8')) + self.assertEqual(data['unread_count'],0) + self.assertEqual(data['unread_list'],[]) + \ No newline at end of file diff --git a/notifications/tests/views.py b/notifications/tests/views.py index 7af9ea5..08ca584 100644 --- a/notifications/tests/views.py +++ b/notifications/tests/views.py @@ -21,6 +21,7 @@ def make_notification(request): 'cleaning the car', 'jumping the shark', 'testing the app', + 'attaching the plumbus', ]) notify.send(sender=request.user, recipient=request.user, verb='you asked for a notification - you are '+the_notification) diff --git a/notifications/views.py b/notifications/views.py index de4c7ac..ea53947 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -120,13 +120,22 @@ def delete(request, slug=None): def live_unread_notification_count(request): - data = { - 'unread_count': request.user.notifications.unread().count(), - } + if not request.user.is_authenticated(): + data = {'unread_count':0} + else: + data = { + 'unread_count': request.user.notifications.unread().count(), + } return JsonResponse(data) def live_unread_notification_list(request): + if not request.user.is_authenticated(): + data = { + 'unread_count':0, + 'unread_list':[] + } + return JsonResponse(data) try: num_to_fetch = request.GET.get('max', 5) # If they don't specify, make it 5.