Django

Code

Changeset 9552

Show
Ignore:
Timestamp:
12/02/08 17:00:06 (1 month ago)
Author:
lukeplant
Message:

More tests for the other half of CsrfMiddleware?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/csrf/tests.py

    r9551 r9552  
    22 
    33from django.test import TestCase 
    4 from django.http import HttpRequest, HttpResponse 
     4from django.http import HttpRequest, HttpResponse, HttpResponseForbidden 
    55from django.contrib.csrf.middleware import CsrfMiddleware, _make_token 
    66from django.conf import settings 
     
    1010    _session_id = "1" 
    1111 
    12     def _get_no_session_request(self): 
     12    def _get_GET_no_session_request(self): 
    1313        return HttpRequest() 
    1414 
    15     def _get_session_request(self): 
    16         req = self._get_no_session_request() 
     15    def _get_GET_session_request(self): 
     16        req = self._get_GET_no_session_request() 
    1717        req.COOKIES[settings.SESSION_COOKIE_NAME] = self._session_id 
     18        return req 
     19 
     20    def _get_POST_session_request(self): 
     21        req = self._get_GET_session_request() 
     22        req.method = "POST" 
     23        return req 
     24 
     25    def _get_POST_no_session_request(self): 
     26        req = self._get_GET_no_session_request() 
     27        req.method = "POST" 
     28        return req 
     29 
     30    def _get_POST_session_request_with_token(self): 
     31        req = self._get_POST_session_request() 
     32        req.POST['csrfmiddlewaretoken'] = _make_token(self._session_id) 
    1833        return req 
    1934 
     
    3247        self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % _make_token(self._session_id)) 
    3348 
     49    # Check the post processing 
    3450    def test_process_response_no_session(self): 
    3551        """ 
    3652        Check the the post-processor does nothing if no session active 
    3753        """ 
    38         req = self._get_no_session_request() 
     54        req = self._get_GET_no_session_request() 
    3955        resp = self._get_post_form_response() 
    4056        resp_content = resp.content 
     
    4662        Check that the token is inserted if there is an existing session 
    4763        """ 
    48         req = self._get_session_request() 
     64        req = self._get_GET_session_request() 
    4965        resp = self._get_post_form_response() 
    5066        resp_content = resp.content 
     
    5773        Check that the token is inserted if there is a new session being started 
    5874        """ 
    59         req = self._get_no_session_request() # no session in request 
     75        req = self._get_GET_no_session_request() # no session in request 
    6076        resp = self._get_new_session_response() # but new session started 
    6177        resp_content = resp.content 
     
    6379        self.assertNotEqual(resp_content, resp2.content) 
    6480        self._check_token_present(resp2) 
     81 
     82    # Check the request processing 
     83    def test_process_request_no_session(self): 
     84        """ 
     85        Check that if no session is present, the middleware does nothing. 
     86        to the incoming request. 
     87        """ 
     88        req = self._get_POST_no_session_request() 
     89        req2 = CsrfMiddleware().process_request(req) 
     90        self.assertEquals(None, req2) 
     91 
     92    def test_process_request_session_no_token(self): 
     93        """ 
     94        Check that if a session is present but no token, we get a 'forbidden' 
     95        """ 
     96        req = self._get_POST_session_request() 
     97        req2 = CsrfMiddleware().process_request(req) 
     98        self.assertEquals(HttpResponseForbidden, req2.__class__) 
     99 
     100    def test_process_request_session_and_token(self): 
     101        """ 
     102        Check that if a session is present and a token, the middleware lets it through 
     103        """ 
     104        req = self._get_POST_session_request_with_token() 
     105        req2 = CsrfMiddleware().process_request(req) 
     106        self.assertEquals(None, req2)