changeset 939:3782fa705821

Add test for wiki signal handlers.
author Brian Neal <bgneal@gmail.com>
date Wed, 29 Apr 2015 20:29:47 -0500
parents 9f9e50df5b83
children 0cff6acf7d61
files wiki/tests.py wiki/tests/__init__.py wiki/tests/test_middleware.py wiki/tests/test_receivers.py
diffstat 3 files changed, 124 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/wiki/tests.py	Tue Apr 28 21:22:42 2015 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-"""
-Tests for the wiki integration application.
-
-"""
-import hashlib
-import datetime
-
-from django.contrib.auth.models import User
-from django.test import TestCase
-from django.test.client import RequestFactory
-from django.http import HttpResponse
-from django.conf import settings
-
-from core.services import get_redis_connection
-from wiki.middleware import WikiMiddleware
-from wiki.constants import SESSION_SET_MEMBER
-
-
-class MiddleWareTestCase(TestCase):
-
-    def setUp(self):
-        self.factory = RequestFactory()
-        self.user = User.objects.create_user('test_user', 'test@example.com',
-                'password')
-        self.conn = get_redis_connection()
-        self.mw = WikiMiddleware()
-
-    def tearDown(self):
-        self.conn.delete(settings.WIKI_REDIS_SET)
-
-    def create_request(self):
-        request = self.factory.get('/contact/')
-        request.session = {}
-        request.user = self.user
-        return request
-
-    def test_middleware(self):
-
-        request = self.create_request()
-        response = HttpResponse()
-
-        request.wiki_set_cookie = True
-        response = self.mw.process_response(request, response)
-
-        cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
-        cookie_val = ''
-        self.assertIsNotNone(cookie)
-        if cookie:
-            self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
-            self.assertEqual(cookie['path'], '/')
-            self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
-
-            cookie_val = cookie.value
-            try:
-                user, email, key = cookie_val.split('#')
-            except ValueError:
-                self.fail('invalid cookie value')
-            else:
-                self.assertEqual(user, self.user.username)
-                self.assertEqual(email, self.user.email)
-                self.assertEqual(len(key), 64)
-
-        self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
-
-        h = hashlib.sha256()
-        h.update(cookie_val)
-        member = h.hexdigest()
-
-        score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
-        now = datetime.datetime.utcnow()
-        session_start = datetime.datetime.fromtimestamp(score)
-        self.assertLess(now - session_start, datetime.timedelta(seconds=2))
-
-        session_member = request.session.get(SESSION_SET_MEMBER)
-        self.assertTrue(session_member and session_member == member)
-
-        # test the destroy session logic
-
-        request = self.create_request()
-        request.wiki_delete_cookie = member
-        response = self.mw.process_response(request, response)
-
-        cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
-        self.assertIsNotNone(cookie)
-        if cookie:
-            self.assertEqual(cookie.value, '')
-            self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
-            self.assertEqual(cookie['path'], '/')
-            self.assertEqual(cookie['max-age'], 0)
-            self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
-
-        self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wiki/tests/test_middleware.py	Wed Apr 29 20:29:47 2015 -0500
@@ -0,0 +1,92 @@
+"""
+Tests for the wiki integration application.
+
+"""
+import hashlib
+import datetime
+
+from django.contrib.auth.models import User
+from django.test import TestCase
+from django.test.client import RequestFactory
+from django.http import HttpResponse
+from django.conf import settings
+
+from core.services import get_redis_connection
+from wiki.middleware import WikiMiddleware
+from wiki.constants import SESSION_SET_MEMBER
+
+
+class MiddleWareTestCase(TestCase):
+
+    def setUp(self):
+        self.factory = RequestFactory()
+        self.user = User.objects.create_user('test_user', 'test@example.com',
+                'password')
+        self.conn = get_redis_connection()
+        self.mw = WikiMiddleware()
+
+    def tearDown(self):
+        self.conn.delete(settings.WIKI_REDIS_SET)
+
+    def create_request(self):
+        request = self.factory.get('/contact/')
+        request.session = {}
+        request.user = self.user
+        return request
+
+    def test_middleware(self):
+
+        request = self.create_request()
+        response = HttpResponse()
+
+        request.wiki_set_cookie = True
+        response = self.mw.process_response(request, response)
+
+        cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
+        cookie_val = ''
+        self.assertIsNotNone(cookie)
+        if cookie:
+            self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
+            self.assertEqual(cookie['path'], '/')
+            self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
+
+            cookie_val = cookie.value
+            try:
+                user, email, key = cookie_val.split('#')
+            except ValueError:
+                self.fail('invalid cookie value')
+            else:
+                self.assertEqual(user, self.user.username)
+                self.assertEqual(email, self.user.email)
+                self.assertEqual(len(key), 64)
+
+        self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
+
+        h = hashlib.sha256()
+        h.update(cookie_val)
+        member = h.hexdigest()
+
+        score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
+        now = datetime.datetime.utcnow()
+        session_start = datetime.datetime.fromtimestamp(score)
+        self.assertLess(now - session_start, datetime.timedelta(seconds=2))
+
+        session_member = request.session.get(SESSION_SET_MEMBER)
+        self.assertTrue(session_member and session_member == member)
+
+        # test the destroy session logic
+
+        request = self.create_request()
+        request.wiki_delete_cookie = member
+        response = self.mw.process_response(request, response)
+
+        cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
+        self.assertIsNotNone(cookie)
+        if cookie:
+            self.assertEqual(cookie.value, '')
+            self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
+            self.assertEqual(cookie['path'], '/')
+            self.assertEqual(cookie['max-age'], 0)
+            self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
+
+        self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wiki/tests/test_receivers.py	Wed Apr 29 20:29:47 2015 -0500
@@ -0,0 +1,32 @@
+"""Tests for the wiki app's signal handlers."""
+import logging
+
+from django.contrib.auth.models import User
+from django.test import TestCase
+
+from testfixtures import log_capture
+
+
+class ReceiverTestCase(TestCase):
+
+    def setUp(self):
+        self.user = User.objects.create_user('user', 'user@example.com', 'pw')
+
+        # Temporarily enable logging
+        self.old_disable = logging.getLogger().manager.disable
+        logging.disable(logging.NOTSET)
+
+    def tearDown(self):
+        logging.disable(self.old_disable)
+
+    @log_capture('wiki.receivers')
+    def test_signal_handlers(self, lc):
+        # We don't have access to the dummy request that the test client creates
+        # when logging in, so we can't really check to see if we added
+        # attributes to the request object. But that code is pretty simple, so
+        # lets just test that we logged something so we know our signal handlers
+        # are hooked up and running.
+        self.client.login(username='user', password='pw')
+        self.client.logout()
+        lc.check(('wiki.receivers', 'INFO', 'User login: user'),
+                 ('wiki.receivers', 'INFO', 'User logout: user'))