﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
15736	test.client.RequestFactory doesn't support sessions	Paul Winkler	nobody	"I have some helper functions called by various views which take a request as an argument.
I would like to be able to unit-test these.
They are not views exposed by a URL, so I can't use self.client.get(...).

`RequestFactory().get('')` would be an ideal way to do this, except that it blows up any code that expects request.session or request.cookies to exist:

{{{
$ django-admin.py shell
>>> r = RequestFactory().get('/foo')
>>> request.session
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
AttributeError: 'WSGIRequest' object has no attribute 'session'
}}}

By contrast, test.client.Client supports sessions just fine.

I was able to get my tests working with this monkeypatch, which just copies the _request implementation
into RequestFactory:
{{{
def _session(self):
    """"""
    Obtains the current session variables.
    """"""
    if 'django.contrib.sessions' in settings.INSTALLED_APPS:
        engine = import_module(settings.SESSION_ENGINE)
        cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
        if cookie:
            return engine.SessionStore(cookie.value)
    return {}

def request(self, **request):
    ""Construct a generic request object.""
    req = WSGIRequest(self._base_environ(**request))
    req.session = self._session()
    return req

RequestFactory._session = _session
RequestFactory.request = request
}}}"	New feature	closed	Testing framework	1.3	Normal	wontfix		Preston Timmons	Unreviewed	0	0	0	0	0	0
