From a14c1d0c484c72fa0e09bea43677b9b210f01bce Mon Sep 17 00:00:00 2001 From: xiez Date: Sun, 19 Aug 2012 17:32:44 +0800 Subject: [PATCH] Add notification unit tests --- notifications/tests.py | 106 ++++++++++++++++++++++++++++++++++------ test-seahub.sh.template | 2 +- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/notifications/tests.py b/notifications/tests.py index ca85bc39a1..1d38bc7889 100644 --- a/notifications/tests.py +++ b/notifications/tests.py @@ -1,23 +1,97 @@ -""" -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". +from django.core.urlresolvers import reverse +from django.test import TestCase, Client -Replace this with more appropriate tests for your application. -""" +from models import Notification +from base.accounts import User -# from django.test import TestCase +class NotificationTestCase(TestCase): + """ + Helper base class for all the follow test cases. + """ + def setUp(self): + self.new_notification = 'This is a new notification!' + self.user = User.objects.create_superuser('admin@admin.com', 'testpassword') + def tearDown(self): + self.user.delete() -# class SimpleTest(TestCase): -# def test_basic_addition(self): -# """ -# Tests that 1 + 1 always equals 2. -# """ -# self.assertEqual(1 + 1, 2) +class NotificationTest(NotificationTestCase): + def login(self): + response = self.client.post('/accounts/login/', { + 'username': 'admin@admin.com', + 'password': 'testpassword', + }) + self.assertEqual(response.status_code, 302) -from django.utils import unittest + def test_add_new_notification(self): + self.login() + r = self.client.get('/sys/notificationadmin/') + # Check that response is 200 OK + self.assertEqual(r.status_code, 200) + + # now there are no notifications + self.assertEqual(len(r.context['notes']), 0) + + # try add one notification + r = self.client.post('/notification/add/', { + 'message': self.new_notification, + }) + # Redirect only if it worked + self.assertEqual(r.status_code, 302) + + # look up in database + notifications = Notification.objects.all() + self.assertEqual(notifications.count(), 1) + + # check this notification is not set primary, thus will not showed + # in top bar + n = Notification.objects.all()[0] + self.assertFalse(n.primary) + + def test_set_primary(self): + n = Notification() + n.message = self.new_notification + n.primary = False + n.save() + + # set primary + self.login() + r = self.client.get(reverse('set_primary', args=[n.id]), {}) + # Redirect only if it worked + self.assertEqual(r.status_code, 302) + + # now check it's primary + n = Notification.objects.all()[0] + self.assertTrue(n.primary) + + # check it's showed in top bar + r = self.client.get('/sys/notificationadmin/') + self.assert_('This is a new notification!' in str(r)) + + # and it's still there when reach other pages + r = self.client.get('/home/my/') + self.assert_('This is a new notification!' in str(r)) + + def test_close_notification(self): + n = Notification() + n.message = self.new_notification + n.primary = True + n.save() + + self.login() + r = self.client.get('/home/my/') + self.assert_('This is a new notification!' in str(r)) + + # now close notification + r = self.client.get(reverse('notification_close', args=[1]), {}) + # Redirect only if it worked + self.assertEqual(r.status_code, 302) + + # it's gone + self.assert_('This is a new notification!' not in str(r)) + + # and it's gone when reach other pages + r = self.client.get('/home/my/') + self.assert_('This is a new notification!' not in str(r)) -class SimpleTestCase(unittest.TestCase): - def test_base_addition(self): - self.assertEqual(1 + 1, 2) diff --git a/test-seahub.sh.template b/test-seahub.sh.template index 27f13c70b1..653bd0bea0 100755 --- a/test-seahub.sh.template +++ b/test-seahub.sh.template @@ -2,7 +2,7 @@ . setenv.sh -./manage.py test base avatar group -v 2 +./manage.py test base avatar group notifications echo $?