comparison wiki/tests.py @ 626:a6bc1e2efa63

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