comparison wiki/tests.py @ 627:a4300639c6e7

Wiki integration. Create task to delete old cookie records. Rework logic upon logout, as session will not be available. Set an attribute on the request instead.
author Brian Neal <bgneal@gmail.com>
date Mon, 12 Nov 2012 15:10:52 -0600
parents a6bc1e2efa63
children f4c043cf55ac
comparison
equal deleted inserted replaced
626:a6bc1e2efa63 627:a4300639c6e7
10 from django.test.client import RequestFactory 10 from django.test.client import RequestFactory
11 from django.http import HttpResponse 11 from django.http import HttpResponse
12 from django.conf import settings 12 from django.conf import settings
13 13
14 from core.services import get_redis_connection 14 from core.services import get_redis_connection
15 from wiki.middleware import WikiMiddleware, SESSION_KEY 15 from wiki.middleware import WikiMiddleware
16 from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER
16 17
17 18
18 class MiddleWareTestCase(TestCase): 19 class MiddleWareTestCase(TestCase):
19 20
20 def setUp(self): 21 def setUp(self):
32 request = self.factory.get('/contact/') 33 request = self.factory.get('/contact/')
33 request.session = {} 34 request.session = {}
34 request.user = self.user 35 request.user = self.user
35 response = HttpResponse() 36 response = HttpResponse()
36 37
37 request.session['wiki_set_cookie'] = True 38 request.session[SESSION_SET_FLAG] = True
38 response = self.mw.process_response(request, response) 39 response = self.mw.process_response(request, response)
39 40
40 self.assertIsNone(request.session.get('wiki_set_cookie')) 41 self.assertIsNone(request.session.get('wiki_set_cookie'))
41 42
42 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) 43 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
48 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE) 49 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
49 50
50 cookie_val = cookie.value 51 cookie_val = cookie.value
51 try: 52 try:
52 user, email, key = cookie_val.split('#') 53 user, email, key = cookie_val.split('#')
53 except KeyError: 54 except ValueError:
54 self.fail('invalid cookie value') 55 self.fail('invalid cookie value')
55 else: 56 else:
56 self.assertEqual(user, self.user.username) 57 self.assertEqual(user, self.user.username)
57 self.assertEqual(email, self.user.email) 58 self.assertEqual(email, self.user.email)
58 self.assertEqual(len(key), 64) 59 self.assertEqual(len(key), 64)
66 score = self.conn.zscore(settings.WIKI_REDIS_SET, member) 67 score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
67 now = datetime.datetime.utcnow() 68 now = datetime.datetime.utcnow()
68 session_start = datetime.datetime.fromtimestamp(score) 69 session_start = datetime.datetime.fromtimestamp(score)
69 self.assertLess(now - session_start, datetime.timedelta(seconds=2)) 70 self.assertLess(now - session_start, datetime.timedelta(seconds=2))
70 71
71 session_key = request.session.get(SESSION_KEY) 72 session_member = request.session.get(SESSION_SET_MEMBER)
72 self.assertTrue(session_key and session_key == member) 73 self.assertTrue(session_member and session_member == member)
73 74
74 # test the destroy session logic 75 # test the destroy session logic
75 76
76 request.session['wiki_delete_cookie'] = True 77 request.wiki_delete_cookie = member
77 response = self.mw.process_response(request, response) 78 response = self.mw.process_response(request, response)
78
79 self.assertIsNone(request.session.get('wiki_delete_cookie'))
80 79
81 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) 80 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
82 self.assertIsNotNone(cookie) 81 self.assertIsNotNone(cookie)
83 if cookie: 82 if cookie:
84 self.assertEqual(cookie.value, '') 83 self.assertEqual(cookie.value, '')
86 self.assertEqual(cookie['path'], '/') 85 self.assertEqual(cookie['path'], '/')
87 self.assertEqual(cookie['max-age'], 0) 86 self.assertEqual(cookie['max-age'], 0)
88 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT') 87 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
89 88
90 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0) 89 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)
91 self.assertIsNone(request.session.get(SESSION_KEY))