bgneal@626: """ bgneal@626: Tests for the wiki integration application. bgneal@626: bgneal@626: """ bgneal@626: import hashlib bgneal@626: import datetime bgneal@626: bgneal@626: from django.contrib.auth.models import User bgneal@626: from django.test import TestCase bgneal@626: from django.test.client import RequestFactory bgneal@626: from django.http import HttpResponse bgneal@626: from django.conf import settings bgneal@626: bgneal@626: from core.services import get_redis_connection bgneal@627: from wiki.middleware import WikiMiddleware bgneal@627: from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER bgneal@626: bgneal@626: bgneal@626: class MiddleWareTestCase(TestCase): bgneal@626: bgneal@626: def setUp(self): bgneal@626: self.factory = RequestFactory() bgneal@626: self.user = User.objects.create_user('test_user', 'test@example.com', bgneal@626: 'password') bgneal@626: self.conn = get_redis_connection() bgneal@626: self.mw = WikiMiddleware() bgneal@626: bgneal@626: def tearDown(self): bgneal@626: self.conn.delete(settings.WIKI_REDIS_SET) bgneal@626: bgneal@626: def test_middleware(self): bgneal@626: bgneal@626: request = self.factory.get('/contact/') bgneal@626: request.session = {} bgneal@626: request.user = self.user bgneal@626: response = HttpResponse() bgneal@626: bgneal@627: request.session[SESSION_SET_FLAG] = True bgneal@626: response = self.mw.process_response(request, response) bgneal@626: bgneal@626: self.assertIsNone(request.session.get('wiki_set_cookie')) bgneal@626: bgneal@626: cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) bgneal@626: cookie_val = '' bgneal@626: self.assertIsNotNone(cookie) bgneal@626: if cookie: bgneal@626: self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN) bgneal@626: self.assertEqual(cookie['path'], '/') bgneal@626: self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE) bgneal@626: bgneal@626: cookie_val = cookie.value bgneal@626: try: bgneal@626: user, email, key = cookie_val.split('#') bgneal@627: except ValueError: bgneal@626: self.fail('invalid cookie value') bgneal@626: else: bgneal@626: self.assertEqual(user, self.user.username) bgneal@626: self.assertEqual(email, self.user.email) bgneal@626: self.assertEqual(len(key), 64) bgneal@626: bgneal@626: self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1) bgneal@626: bgneal@626: h = hashlib.sha256() bgneal@626: h.update(cookie_val) bgneal@626: member = h.hexdigest() bgneal@626: bgneal@626: score = self.conn.zscore(settings.WIKI_REDIS_SET, member) bgneal@626: now = datetime.datetime.utcnow() bgneal@626: session_start = datetime.datetime.fromtimestamp(score) bgneal@626: self.assertLess(now - session_start, datetime.timedelta(seconds=2)) bgneal@626: bgneal@627: session_member = request.session.get(SESSION_SET_MEMBER) bgneal@627: self.assertTrue(session_member and session_member == member) bgneal@626: bgneal@626: # test the destroy session logic bgneal@626: bgneal@627: request.wiki_delete_cookie = member bgneal@626: response = self.mw.process_response(request, response) bgneal@626: bgneal@626: cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) bgneal@626: self.assertIsNotNone(cookie) bgneal@626: if cookie: bgneal@626: self.assertEqual(cookie.value, '') bgneal@626: self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN) bgneal@626: self.assertEqual(cookie['path'], '/') bgneal@626: self.assertEqual(cookie['max-age'], 0) bgneal@626: self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT') bgneal@626: bgneal@626: self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)