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@627
|
16 from wiki.constants import SESSION_SET_FLAG, 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@626
|
31 def test_middleware(self):
|
bgneal@626
|
32
|
bgneal@626
|
33 request = self.factory.get('/contact/')
|
bgneal@626
|
34 request.session = {}
|
bgneal@626
|
35 request.user = self.user
|
bgneal@626
|
36 response = HttpResponse()
|
bgneal@626
|
37
|
bgneal@627
|
38 request.session[SESSION_SET_FLAG] = True
|
bgneal@626
|
39 response = self.mw.process_response(request, response)
|
bgneal@626
|
40
|
bgneal@626
|
41 self.assertIsNone(request.session.get('wiki_set_cookie'))
|
bgneal@626
|
42
|
bgneal@626
|
43 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
|
bgneal@626
|
44 cookie_val = ''
|
bgneal@626
|
45 self.assertIsNotNone(cookie)
|
bgneal@626
|
46 if cookie:
|
bgneal@626
|
47 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
|
bgneal@626
|
48 self.assertEqual(cookie['path'], '/')
|
bgneal@626
|
49 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
|
bgneal@626
|
50
|
bgneal@626
|
51 cookie_val = cookie.value
|
bgneal@626
|
52 try:
|
bgneal@626
|
53 user, email, key = cookie_val.split('#')
|
bgneal@627
|
54 except ValueError:
|
bgneal@626
|
55 self.fail('invalid cookie value')
|
bgneal@626
|
56 else:
|
bgneal@626
|
57 self.assertEqual(user, self.user.username)
|
bgneal@626
|
58 self.assertEqual(email, self.user.email)
|
bgneal@626
|
59 self.assertEqual(len(key), 64)
|
bgneal@626
|
60
|
bgneal@626
|
61 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
|
bgneal@626
|
62
|
bgneal@626
|
63 h = hashlib.sha256()
|
bgneal@626
|
64 h.update(cookie_val)
|
bgneal@626
|
65 member = h.hexdigest()
|
bgneal@626
|
66
|
bgneal@626
|
67 score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
|
bgneal@626
|
68 now = datetime.datetime.utcnow()
|
bgneal@626
|
69 session_start = datetime.datetime.fromtimestamp(score)
|
bgneal@626
|
70 self.assertLess(now - session_start, datetime.timedelta(seconds=2))
|
bgneal@626
|
71
|
bgneal@627
|
72 session_member = request.session.get(SESSION_SET_MEMBER)
|
bgneal@627
|
73 self.assertTrue(session_member and session_member == member)
|
bgneal@626
|
74
|
bgneal@626
|
75 # test the destroy session logic
|
bgneal@626
|
76
|
bgneal@627
|
77 request.wiki_delete_cookie = member
|
bgneal@626
|
78 response = self.mw.process_response(request, response)
|
bgneal@626
|
79
|
bgneal@626
|
80 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
|
bgneal@626
|
81 self.assertIsNotNone(cookie)
|
bgneal@626
|
82 if cookie:
|
bgneal@626
|
83 self.assertEqual(cookie.value, '')
|
bgneal@626
|
84 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
|
bgneal@626
|
85 self.assertEqual(cookie['path'], '/')
|
bgneal@626
|
86 self.assertEqual(cookie['max-age'], 0)
|
bgneal@626
|
87 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
|
bgneal@626
|
88
|
bgneal@626
|
89 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)
|