annotate wiki/tests.py @ 846:fbfc9b6fac96

Use new updated Halloween NSSR banner from Niels.
author Brian Neal <bgneal@gmail.com>
date Tue, 21 Oct 2014 18:50:16 -0500
parents f4c043cf55ac
children
rev   line source
bgneal@626 1 """
bgneal@626 2 Tests for the wiki integration application.
bgneal@626 3
bgneal@626 4 """
bgneal@626 5 import hashlib
bgneal@626 6 import datetime
bgneal@626 7
bgneal@626 8 from django.contrib.auth.models import User
bgneal@626 9 from django.test import TestCase
bgneal@626 10 from django.test.client import RequestFactory
bgneal@626 11 from django.http import HttpResponse
bgneal@626 12 from django.conf import settings
bgneal@626 13
bgneal@626 14 from core.services import get_redis_connection
bgneal@627 15 from wiki.middleware import WikiMiddleware
bgneal@629 16 from wiki.constants import SESSION_SET_MEMBER
bgneal@626 17
bgneal@626 18
bgneal@626 19 class MiddleWareTestCase(TestCase):
bgneal@626 20
bgneal@626 21 def setUp(self):
bgneal@626 22 self.factory = RequestFactory()
bgneal@626 23 self.user = User.objects.create_user('test_user', 'test@example.com',
bgneal@626 24 'password')
bgneal@626 25 self.conn = get_redis_connection()
bgneal@626 26 self.mw = WikiMiddleware()
bgneal@626 27
bgneal@626 28 def tearDown(self):
bgneal@626 29 self.conn.delete(settings.WIKI_REDIS_SET)
bgneal@626 30
bgneal@629 31 def create_request(self):
bgneal@626 32 request = self.factory.get('/contact/')
bgneal@626 33 request.session = {}
bgneal@626 34 request.user = self.user
bgneal@629 35 return request
bgneal@629 36
bgneal@629 37 def test_middleware(self):
bgneal@629 38
bgneal@629 39 request = self.create_request()
bgneal@626 40 response = HttpResponse()
bgneal@626 41
bgneal@629 42 request.wiki_set_cookie = True
bgneal@626 43 response = self.mw.process_response(request, response)
bgneal@626 44
bgneal@626 45 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
bgneal@626 46 cookie_val = ''
bgneal@626 47 self.assertIsNotNone(cookie)
bgneal@626 48 if cookie:
bgneal@626 49 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
bgneal@626 50 self.assertEqual(cookie['path'], '/')
bgneal@626 51 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
bgneal@626 52
bgneal@626 53 cookie_val = cookie.value
bgneal@626 54 try:
bgneal@626 55 user, email, key = cookie_val.split('#')
bgneal@627 56 except ValueError:
bgneal@626 57 self.fail('invalid cookie value')
bgneal@626 58 else:
bgneal@626 59 self.assertEqual(user, self.user.username)
bgneal@626 60 self.assertEqual(email, self.user.email)
bgneal@626 61 self.assertEqual(len(key), 64)
bgneal@626 62
bgneal@626 63 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
bgneal@626 64
bgneal@626 65 h = hashlib.sha256()
bgneal@626 66 h.update(cookie_val)
bgneal@626 67 member = h.hexdigest()
bgneal@626 68
bgneal@626 69 score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
bgneal@626 70 now = datetime.datetime.utcnow()
bgneal@626 71 session_start = datetime.datetime.fromtimestamp(score)
bgneal@626 72 self.assertLess(now - session_start, datetime.timedelta(seconds=2))
bgneal@626 73
bgneal@627 74 session_member = request.session.get(SESSION_SET_MEMBER)
bgneal@627 75 self.assertTrue(session_member and session_member == member)
bgneal@626 76
bgneal@626 77 # test the destroy session logic
bgneal@626 78
bgneal@629 79 request = self.create_request()
bgneal@627 80 request.wiki_delete_cookie = member
bgneal@626 81 response = self.mw.process_response(request, response)
bgneal@626 82
bgneal@626 83 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
bgneal@626 84 self.assertIsNotNone(cookie)
bgneal@626 85 if cookie:
bgneal@626 86 self.assertEqual(cookie.value, '')
bgneal@626 87 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
bgneal@626 88 self.assertEqual(cookie['path'], '/')
bgneal@626 89 self.assertEqual(cookie['max-age'], 0)
bgneal@626 90 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
bgneal@626 91
bgneal@626 92 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)