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@626
|
15 from wiki.middleware import WikiMiddleware, SESSION_KEY
|
bgneal@626
|
16
|
bgneal@626
|
17
|
bgneal@626
|
18 class MiddleWareTestCase(TestCase):
|
bgneal@626
|
19
|
bgneal@626
|
20 def setUp(self):
|
bgneal@626
|
21 self.factory = RequestFactory()
|
bgneal@626
|
22 self.user = User.objects.create_user('test_user', 'test@example.com',
|
bgneal@626
|
23 'password')
|
bgneal@626
|
24 self.conn = get_redis_connection()
|
bgneal@626
|
25 self.mw = WikiMiddleware()
|
bgneal@626
|
26
|
bgneal@626
|
27 def tearDown(self):
|
bgneal@626
|
28 self.conn.delete(settings.WIKI_REDIS_SET)
|
bgneal@626
|
29
|
bgneal@626
|
30 def test_middleware(self):
|
bgneal@626
|
31
|
bgneal@626
|
32 request = self.factory.get('/contact/')
|
bgneal@626
|
33 request.session = {}
|
bgneal@626
|
34 request.user = self.user
|
bgneal@626
|
35 response = HttpResponse()
|
bgneal@626
|
36
|
bgneal@626
|
37 request.session['wiki_set_cookie'] = True
|
bgneal@626
|
38 response = self.mw.process_response(request, response)
|
bgneal@626
|
39
|
bgneal@626
|
40 self.assertIsNone(request.session.get('wiki_set_cookie'))
|
bgneal@626
|
41
|
bgneal@626
|
42 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
|
bgneal@626
|
43 cookie_val = ''
|
bgneal@626
|
44 self.assertIsNotNone(cookie)
|
bgneal@626
|
45 if cookie:
|
bgneal@626
|
46 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
|
bgneal@626
|
47 self.assertEqual(cookie['path'], '/')
|
bgneal@626
|
48 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
|
bgneal@626
|
49
|
bgneal@626
|
50 cookie_val = cookie.value
|
bgneal@626
|
51 try:
|
bgneal@626
|
52 user, email, key = cookie_val.split('#')
|
bgneal@626
|
53 except KeyError:
|
bgneal@626
|
54 self.fail('invalid cookie value')
|
bgneal@626
|
55 else:
|
bgneal@626
|
56 self.assertEqual(user, self.user.username)
|
bgneal@626
|
57 self.assertEqual(email, self.user.email)
|
bgneal@626
|
58 self.assertEqual(len(key), 64)
|
bgneal@626
|
59
|
bgneal@626
|
60 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
|
bgneal@626
|
61
|
bgneal@626
|
62 h = hashlib.sha256()
|
bgneal@626
|
63 h.update(cookie_val)
|
bgneal@626
|
64 member = h.hexdigest()
|
bgneal@626
|
65
|
bgneal@626
|
66 score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
|
bgneal@626
|
67 now = datetime.datetime.utcnow()
|
bgneal@626
|
68 session_start = datetime.datetime.fromtimestamp(score)
|
bgneal@626
|
69 self.assertLess(now - session_start, datetime.timedelta(seconds=2))
|
bgneal@626
|
70
|
bgneal@626
|
71 session_key = request.session.get(SESSION_KEY)
|
bgneal@626
|
72 self.assertTrue(session_key and session_key == member)
|
bgneal@626
|
73
|
bgneal@626
|
74 # test the destroy session logic
|
bgneal@626
|
75
|
bgneal@626
|
76 request.session['wiki_delete_cookie'] = True
|
bgneal@626
|
77 response = self.mw.process_response(request, response)
|
bgneal@626
|
78
|
bgneal@626
|
79 self.assertIsNone(request.session.get('wiki_delete_cookie'))
|
bgneal@626
|
80
|
bgneal@626
|
81 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
|
bgneal@626
|
82 self.assertIsNotNone(cookie)
|
bgneal@626
|
83 if cookie:
|
bgneal@626
|
84 self.assertEqual(cookie.value, '')
|
bgneal@626
|
85 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
|
bgneal@626
|
86 self.assertEqual(cookie['path'], '/')
|
bgneal@626
|
87 self.assertEqual(cookie['max-age'], 0)
|
bgneal@626
|
88 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
|
bgneal@626
|
89
|
bgneal@626
|
90 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)
|
bgneal@626
|
91 self.assertIsNone(request.session.get(SESSION_KEY))
|