From d55762edd38a23ff2106ea730827bce0a34b612d Mon Sep 17 00:00:00 2001 From: Samuel Spencer Date: Mon, 8 Feb 2016 03:54:35 +0000 Subject: [PATCH] Added sentinals for unauthenticated users, preventing a 500 error also fix spelling mistake in README --- README.rst | 2 +- notifications/tests/tests.py | 13 +++++++++++++ notifications/tests/views.py | 1 + notifications/views.py | 15 ++++++++++++--- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 7922c16d..0f0769e8 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 3570ebe8..bf64e580 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -284,3 +284,16 @@ def test_live_update_tags(self): 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 7af9ea54..08ca584c 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 de4c7ac1..ea539471 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.