#14350: Please add a login method to the test Client that doesn't depend on passwords ---------------------------------------------------------------+------------ Reporter: James Westby <jw+deb...@jameswestby.net> | Owner: nobody Status: new | Milestone: Component: Uncategorized | Version: 1.2 Resolution: | Keywords: Stage: Unreviewed | Has_patch: 0 Needs_docs: 0 | Needs_tests: 0 Needs_better_patch: 0 | ---------------------------------------------------------------+------------ Old description:
> Hi, > > Requiring passwords in tests that don't test the login is unnecessary and > binds them too tightly to implementation that they don't care about. > > I suggest something like the following: > > def login_user(self, user): > if not 'django.contrib.sessions' in settings.INSTALLED_APPS: > raise AssertionError("Unable to login without > django.contrib.sessions in INSTALLED_APPS") > user.backend = "%s.%s" % ("django.contrib.auth.backends", > "ModelBackend") > engine = import_module(settings.SESSION_ENGINE) > > # Create a fake request to store login details. > request = HttpRequest() > if self.session: > request.session = self.session > else: > request.session = engine.SessionStore() > login(request, user) > > # Set the cookie to represent the session. > session_cookie = settings.SESSION_COOKIE_NAME > self.cookies[session_cookie] = request.session.session_key > cookie_data = { > 'max-age': None, > 'path': '/', > 'domain': settings.SESSION_COOKIE_DOMAIN, > 'secure': settings.SESSION_COOKIE_SECURE or None, > 'expires': None, > } > self.cookies[session_cookie].update(cookie_data) > > # Save the session values. > request.session.save() > > refactoring with the common code in other methods. > > That would allow you to just login with a User object. > > Thanks, > > James New description: Hi, Requiring passwords in tests that don't test the login is unnecessary and binds them too tightly to implementation that they don't care about. I suggest something like the following: {{{ def login_user(self, user): if not 'django.contrib.sessions' in settings.INSTALLED_APPS: raise AssertionError("Unable to login without django.contrib.sessions in INSTALLED_APPS") user.backend = "%s.%s" % ("django.contrib.auth.backends", "ModelBackend") engine = import_module(settings.SESSION_ENGINE) # Create a fake request to store login details. request = HttpRequest() if self.session: request.session = self.session else: request.session = engine.SessionStore() login(request, user) # Set the cookie to represent the session. session_cookie = settings.SESSION_COOKIE_NAME self.cookies[session_cookie] = request.session.session_key cookie_data = { 'max-age': None, 'path': '/', 'domain': settings.SESSION_COOKIE_DOMAIN, 'secure': settings.SESSION_COOKIE_SECURE or None, 'expires': None, } self.cookies[session_cookie].update(cookie_data) # Save the session values. request.session.save() }}} refactoring with the common code in other methods. That would allow you to just login with a User object. Thanks, James Comment (by ramiro): Re-formatted description. -- Ticket URL: <http://code.djangoproject.com/ticket/14350#comment:2> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.