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