Ticket #5938: testclient_login_without_password.diff

File testclient_login_without_password.diff, 2.5 KB (added by Thomas Güttler <hv@…>, 8 years ago)
  • tests/regressiontests/test_client_regress/models.py

     
    261261        # Check that assertRedirects uses the original client, not the
    262262        # default client.
    263263        self.assertRedirects(response, "http://testserver/test_client_regress/get_view/")
     264
     265    def test_login_with_user_instance(self):
     266        "Check login_user(): Log in without a password."
     267        from django.contrib.auth.models import User
     268        c = Client()
     269        login = c.login_user(User.objects.get(username='testclient'))
     270        self.failUnless(login, 'Could not log in')
     271
  • django/test/client.py

     
    33from cStringIO import StringIO
    44from urlparse import urlparse
    55from django.conf import settings
    6 from django.contrib.auth import authenticate, login
     6from django.contrib.auth import authenticate, login, get_backends
    77from django.core.handlers.base import BaseHandler
    88from django.core.handlers.wsgi import WSGIRequest
    99from django.core.signals import got_request_exception
     
    245245        not available.
    246246        """
    247247        user = authenticate(**credentials)
     248        return self.login_user(user)
     249
     250    def login_user(self, user):
     251        if user and not hasattr(user, "backend"):
     252            backend=get_backends()[0]
     253            user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
    248254        if user and user.is_active and 'django.contrib.sessions' in settings.INSTALLED_APPS:
    249255            engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
    250256
  • docs/testing.txt

     
    552552    conditions. You'll need to create users as part of the test suite -- either
    553553    manually (using the Django model API) or with a test fixture.
    554554
     555``login_user(user)``
     556    **New in Django development version**
     557
     558    Like ``login()`` but you can use a user instance (without using a password)::
     559
     560        >>> c = Client()
     561        >>> if c.login_user(User.objects.get(username='admin')):
     562        >>>     ...
     563
    555564``logout()``
    556565    **New in Django development version**
    557566
Back to Top