From ce0f1fc0ec7cac4d871ce1778ac70365969f3566 Mon Sep 17 00:00:00 2001 From: "theodore.therone@gmail.com" Date: Thu, 9 Apr 2015 04:36:15 +0000 Subject: [PATCH] extra tests and coverage Small tweaks to views - to to prevent collision with python keyword. --- manage.py | 2 +- notifications/tests/__init__.py | 1 + .../{test_settings.py => tests/settings.py} | 9 ++- notifications/{ => tests}/tests.py | 65 ++++++++++++++++++- notifications/tests/urls.py | 11 ++++ notifications/views.py | 18 ++--- 6 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 notifications/tests/__init__.py rename notifications/{test_settings.py => tests/settings.py} (71%) rename notifications/{ => tests}/tests.py (54%) create mode 100644 notifications/tests/urls.py diff --git a/manage.py b/manage.py index 0158c31..367aa92 100755 --- a/manage.py +++ b/manage.py @@ -4,7 +4,7 @@ import sys if __name__ == "__main__": if 'test' in sys.argv: - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notifications.test_settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notifications.tests.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) \ No newline at end of file diff --git a/notifications/tests/__init__.py b/notifications/tests/__init__.py new file mode 100644 index 0000000..b8f079e --- /dev/null +++ b/notifications/tests/__init__.py @@ -0,0 +1 @@ +from .tests import * \ No newline at end of file diff --git a/notifications/test_settings.py b/notifications/tests/settings.py similarity index 71% rename from notifications/test_settings.py rename to notifications/tests/settings.py index 0cbb5c8..c1609b9 100644 --- a/notifications/test_settings.py +++ b/notifications/tests/settings.py @@ -11,15 +11,20 @@ DATABASES = { } -MIDDLEWARE_CLASSES = () +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', +) INSTALLED_APPS = ( + 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', + 'django.contrib.sessions', 'notifications', ) -ROOT_URLCONF = 'notifications.urls' +ROOT_URLCONF = 'notifications.tests.urls' # Need to skip migrations for now as migrations created with python2 break with python3 diff --git a/notifications/tests.py b/notifications/tests/tests.py similarity index 54% rename from notifications/tests.py rename to notifications/tests/tests.py index 47127e2..0cee47f 100644 --- a/notifications/tests.py +++ b/notifications/tests/tests.py @@ -13,13 +13,14 @@ except ImportError: from django.conf import settings from django.contrib.auth.models import User +from django.core.urlresolvers import reverse from django.utils.timezone import utc, localtime from django.utils import timezone import pytz from notifications import notify from notifications.models import Notification - +from notifications.utils import id2slug class NotificationTest(TestCase): @@ -80,3 +81,65 @@ class NotificationManagersTest(TestCase): self.assertEqual(Notification.objects.unread().count(),0) Notification.objects.filter(recipient=self.to_user).mark_all_as_unread() self.assertEqual(Notification.objects.unread().count(),10) + + +class NotificationTestPages(TestCase): + def setUp(self): + self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com") + self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com") + self.to_user.is_staff = True + self.to_user.save() + for i in range(10): + notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user) + + def logout(self): + self.client.post(reverse('admin:logout')+'?next=/', {}) + + def login(self,username,password): + self.logout() + response = self.client.post(reverse('admin:login'), {'username': username, 'password': password}) + self.assertEqual(response.status_code,302) + return response + + def test_all_messages_page(self): + self.login('to','pwd') + response = self.client.get(reverse('notifications:all')) + + self.assertEqual(response.status_code,200) + self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.all())) + + def test_unread_messages_pages(self): + self.login('to','pwd') + response = self.client.get(reverse('notifications:unread')) + self.assertEqual(response.status_code,200) + self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread())) + self.assertEqual(len(response.context['notifications']),10) + + for i,n in enumerate(self.to_user.notifications.all()): + if i%3 == 0: + response = self.client.get(reverse('notifications:mark_as_read',args=[id2slug(n.id)])) + self.assertEqual(response.status_code,302) + + response = self.client.get(reverse('notifications:unread')) + self.assertEqual(response.status_code,200) + self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread())) + self.assertTrue(len(response.context['notifications'])<10) + + response = self.client.get(reverse('notifications:mark_all_as_read')) + self.assertRedirects(response,reverse('notifications:all')) + response = self.client.get(reverse('notifications:unread')) + self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread())) + self.assertEqual(len(response.context['notifications']),0) + + def test_next_pages(self): + self.login('to','pwd') + response = self.client.get(reverse('notifications:mark_all_as_read')+"?next="+reverse('notifications:unread')) + self.assertRedirects(response,reverse('notifications:unread')) + + slug = id2slug(self.to_user.notifications.first().id) + response = self.client.get(reverse('notifications:mark_as_read',args=[slug])+"?next="+reverse('notifications:unread')) + self.assertRedirects(response,reverse('notifications:unread')) + + slug = id2slug(self.to_user.notifications.first().id) + response = self.client.get(reverse('notifications:mark_as_unread',args=[slug])+"?next="+reverse('notifications:unread')) + self.assertRedirects(response,reverse('notifications:unread')) diff --git a/notifications/tests/urls.py b/notifications/tests/urls.py new file mode 100644 index 0000000..bf36e77 --- /dev/null +++ b/notifications/tests/urls.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from django.conf.urls import include, patterns, url +from django.contrib import admin + +import notifications + +urlpatterns = patterns('', + url(r'^admin/', include(admin.site.urls)), + url(r'^', include(notifications.urls)), +) \ No newline at end of file diff --git a/notifications/views.py b/notifications/views.py index 1fa97eb..dc422df 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -43,10 +43,10 @@ def unread(request): def mark_all_as_read(request): request.user.notifications.mark_all_as_read() - next = request.REQUEST.get('next') + _next = request.REQUEST.get('next') - if next: - return redirect(next) + if _next: + return redirect(_next) return redirect('notifications:all') @login_required @@ -56,10 +56,10 @@ def mark_as_read(request, slug=None): notification = get_object_or_404(Notification, recipient=request.user, id=id) notification.mark_as_read() - next = request.REQUEST.get('next') + _next = request.REQUEST.get('next') - if next: - return redirect(next) + if _next: + return redirect(_next) return redirect('notifications:all') @@ -70,10 +70,10 @@ def mark_as_unread(request, slug=None): notification = get_object_or_404(Notification, recipient=request.user, id=id) notification.mark_as_unread() - next = request.REQUEST.get('next') + _next = request.REQUEST.get('next') - if next: - return redirect(next) + if _next: + return redirect(_next) return redirect('notifications:all')