mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-04-20 05:01:00 +00:00
Merge pull request #51 from LegoStormtroopr/master
extra tests and coverage
This commit is contained in:
commit
dbd04dd0f4
8 changed files with 101 additions and 13 deletions
|
|
@ -7,6 +7,7 @@ python:
|
|||
env:
|
||||
- DJANGO=1.6
|
||||
- DJANGO=1.7
|
||||
- DJANGO=1.8
|
||||
install:
|
||||
# command to install dependencies
|
||||
- "pip install coveralls"
|
||||
|
|
@ -20,5 +21,7 @@ matrix:
|
|||
exclude:
|
||||
- python: "2.6"
|
||||
env: DJANGO=1.7
|
||||
- python: "2.6"
|
||||
env: DJANGO=1.8
|
||||
after_success:
|
||||
- coveralls
|
||||
|
|
|
|||
|
|
@ -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('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'))
|
||||
12
notifications/tests/urls.py
Normal file
12
notifications/tests/urls.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.conf.urls import include, patterns, url
|
||||
from django.contrib import admin
|
||||
|
||||
import notifications
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^login/$', 'django.contrib.auth.views.login', name='login'), # needed for Django 1.6 tests
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'^', include(notifications.urls)),
|
||||
)
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
if sys.version > '3':
|
||||
long = int
|
||||
|
||||
def slug2id(slug):
|
||||
return long(slug) - 110909
|
||||
|
||||
|
|
|
|||
|
|
@ -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