Opened 5 years ago

Closed 5 years ago

#15736 closed New feature (wontfix)

test.client.RequestFactory doesn't support sessions

Reported by: slinkp Owned by: nobody
Component: Testing framework Version: 1.3
Severity: Normal Keywords:
Cc: prestontimmons Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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:

$ 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

Change History (4)

comment:1 Changed 5 years ago by slinkp

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from test.client.RequestFactory doesn't support sessions or cookies to test.client.RequestFactory doesn't support sessions
  • deleted wrong comment -
Last edited 5 years ago by slinkp (previous) (diff)

comment:2 Changed 5 years ago by slinkp

ignore the part about request.cookies, obviously that's supposed to be request.COOKIES which works fine.

comment:3 Changed 5 years ago by prestontimmons

  • Cc prestontimmons added

comment:4 Changed 5 years ago by carljm

  • Component changed from Uncategorized to Testing framework
  • Resolution set to wontfix
  • Status changed from new to closed
  • Type changed from Uncategorized to New feature

I think this would be the wrong direction for RequestFactory. In real code, a bare request has no session attribute - that's added only by SessionMiddleware, which is part of a contrib app, not even part of the core framework. Your test failure is correctly telling you that your helper function requires a request object that's been annotated with a session attribute, so your test should explicitly do that annotation in the test setup, if it's a unittest and not an end-to-end test that runs the full request-response cycle (including middleware).

Note: See TracTickets for help on using tickets.
Back to Top