comparison wiki/tests.py @ 629:f4c043cf55ac

Wiki integration. Requests don't always have sessions. In particular this occurs when a request is made without a trailing slash. The Common middleware redirects when this happens, and the middleware process_request() processing stops before a session can get added. So just set an attribute on the request object for each operation. This seemed weird to me at first, but there are plenty of examples of this in the Django code base already.
author Brian Neal <bgneal@gmail.com>
date Tue, 13 Nov 2012 13:50:06 -0600
parents a4300639c6e7
children
comparison
equal deleted inserted replaced
628:c6292e46e617 629:f4c043cf55ac
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 15 from wiki.middleware import WikiMiddleware
16 from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER 16 from wiki.constants import SESSION_SET_MEMBER
17 17
18 18
19 class MiddleWareTestCase(TestCase): 19 class MiddleWareTestCase(TestCase):
20 20
21 def setUp(self): 21 def setUp(self):
26 self.mw = WikiMiddleware() 26 self.mw = WikiMiddleware()
27 27
28 def tearDown(self): 28 def tearDown(self):
29 self.conn.delete(settings.WIKI_REDIS_SET) 29 self.conn.delete(settings.WIKI_REDIS_SET)
30 30
31 def test_middleware(self): 31 def create_request(self):
32
33 request = self.factory.get('/contact/') 32 request = self.factory.get('/contact/')
34 request.session = {} 33 request.session = {}
35 request.user = self.user 34 request.user = self.user
35 return request
36
37 def test_middleware(self):
38
39 request = self.create_request()
36 response = HttpResponse() 40 response = HttpResponse()
37 41
38 request.session[SESSION_SET_FLAG] = True 42 request.wiki_set_cookie = True
39 response = self.mw.process_response(request, response) 43 response = self.mw.process_response(request, response)
40
41 self.assertIsNone(request.session.get('wiki_set_cookie'))
42 44
43 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) 45 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
44 cookie_val = '' 46 cookie_val = ''
45 self.assertIsNotNone(cookie) 47 self.assertIsNotNone(cookie)
46 if cookie: 48 if cookie:
72 session_member = request.session.get(SESSION_SET_MEMBER) 74 session_member = request.session.get(SESSION_SET_MEMBER)
73 self.assertTrue(session_member and session_member == member) 75 self.assertTrue(session_member and session_member == member)
74 76
75 # test the destroy session logic 77 # test the destroy session logic
76 78
79 request = self.create_request()
77 request.wiki_delete_cookie = member 80 request.wiki_delete_cookie = member
78 response = self.mw.process_response(request, response) 81 response = self.mw.process_response(request, response)
79 82
80 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) 83 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
81 self.assertIsNotNone(cookie) 84 self.assertIsNotNone(cookie)