Ticket #3285: tests.py

File tests.py, 1.9 KB (added by Marty Alchin <gulopine@…>, 18 years ago)

complete unit test suite

Line 
1import unittest
2
3from django.http import HttpRequest, HttpResponse
4
5from middleware import SignedCookiesMiddleware
6
7class SignedCookiesTestCase(unittest.TestCase):
8
9 def setUp(self):
10 self.middleware = SignedCookiesMiddleware()
11 (self.key, self.value) = ('key', 'value')
12 self.signature = self.middleware.get_digest(self.key, self.value)
13 self.signed_value = '%s:%s' % (self.signature, self.value)
14
15 def test_unsigned_cookie(self):
16 "Make sure process_request removes unsigned cookies"
17 request = HttpRequest()
18 request.COOKIES[self.key] = self.value
19 self.middleware.process_request(request)
20 self.assert_(self.key not in request.COOKIES)
21
22 def test_invalid_cookie(self):
23 "Make sure process_request removes invalid cookies"
24 request = HttpRequest()
25 request.COOKIES[self.key] = self.signed_value + 'test'
26 self.middleware.process_request(request)
27 self.assert_(self.key not in request.COOKIES)
28
29 def test_valid_cookie(self):
30 "Make sure process_request removes signatures from valid cookies"
31 request = HttpRequest()
32 request.COOKIES[self.key] = self.signed_value
33 self.middleware.process_request(request)
34 self.assertEqual(request.COOKIES[self.key], self.value)
35
36 def test_set_cookie(self):
37 "Make sure process_response signs cookies appropriately"
38 response = HttpResponse()
39 response.set_cookie(self.key, self.value)
40 self.middleware.process_response(HttpRequest(), response)
41 self.assertEqual(response.cookies[self.key].value, self.signed_value)
42
43 def test_delete_cookie(self):
44 "Make sure process_response leaves deleted cookies alone"
45 response = HttpResponse()
46 response.delete_cookie(self.key)
47 self.middleware.process_response(HttpRequest(), response)
48 self.assertEqual(response.cookies[self.key].value, '')
Back to Top