Ticket #12233: patch.diff

File patch.diff, 3.0 KB (added by dmathieu, 15 years ago)

Patch

  • django/contrib/auth/views.py

     
    1818@csrf_protect
    1919@never_cache
    2020def login(request, template_name='registration/login.html',
     21          redirect_if_logged_in=None,
    2122          redirect_field_name=REDIRECT_FIELD_NAME,
    2223          authentication_form=AuthenticationForm):
    2324    "Displays the login form and handles the login action."
     25    if request.user.is_authenticated() and redirect_if_logged_in != None:
     26      return HttpResponseRedirect(redirect_if_logged_in)
    2427    redirect_to = request.REQUEST.get(redirect_field_name, '')
    2528    if request.method == "POST":
    2629        form = authentication_form(data=request.POST)
  • django/contrib/auth/tests/views.py

     
    184184        self.login(password='password1')
    185185
    186186class LoginTest(AuthViewsTestCase):
    187 
     187    def login(self, password='password'):
     188        response = self.client.post('/login/', {
     189            'username': 'testclient',
     190            'password': password
     191            }
     192        )
     193        self.assertEquals(response.status_code, 302)
     194        self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
     195        self.assert_(SESSION_KEY in self.client.session)
     196 
     197 
    188198    def test_current_site_in_context_after_login(self):
    189199        response = self.client.get(reverse('django.contrib.auth.views.login'))
    190200        self.assertEquals(response.status_code, 200)
     
    193203        self.assertEquals(response.context['site_name'], site.name)
    194204        self.assert_(isinstance(response.context['form'], AuthenticationForm),
    195205                     'Login form is not an AuthenticationForm')
    196        
     206   
     207    def test_logged_in_user_should_be_redirected(self):
     208      "If the user is logged in, we should redirect to the specified page"
     209      self.login()
     210      response = self.client.get(reverse('django.contrib.auth.views.login', {'redirect_if_logged_in': '/next_page/'}))
     211      self.assertEquals(302, response.status_code)
     212      self.assertEquals('/next_page/', response['Location'])
     213     
     214    def test_logged_in_user_should_not_be_redirected(self):
     215      "If the user is logged in but we don't have any redirect url, we display the login form"
     216      self.login()
     217      response = self.client.get(reverse('django.contrib.auth.views.login'))
     218      self.assertEquals(200, response.status_code)
     219   
     220    def test_guest_should_not_be_redirected(self):
     221      "If the user is not logged, we display the login form"
     222      response = self.client.get(reverse('django.contrib.auth.views.login', {'redirect_if_logged_in': '/next_page/'}))
     223      self.assertEquals(200, response.status_code)
     224     
    197225class LogoutTest(AuthViewsTestCase):
    198226    urls = 'django.contrib.auth.tests.urls'
    199227
Back to Top