mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-05-13 07:33:10 +00:00
extra tests and coverage
Small tweaks to views - to to prevent collision with python keyword.
This commit is contained in:
parent
8a91c008c9
commit
ce0f1fc0ec
6 changed files with 93 additions and 13 deletions
|
|
@ -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)
|
||||
1
notifications/tests/__init__.py
Normal file
1
notifications/tests/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .tests import *
|
||||
|
|
@ -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
|
||||
|
|
@ -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'))
|
||||
11
notifications/tests/urls.py
Normal file
11
notifications/tests/urls.py
Normal file
|
|
@ -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)),
|
||||
)
|
||||
|
|
@ -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')
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue