APIRequestFactory Request object has no attribute session #8091
-
Hi, I encountered this exception: Upun running an APITestCase on an APIView which logs in a user given the correct credentials and also returns the auth token key in a json response. The simplified logic of the APIView looks this:
And the APITestCase looks like this:
Test invocation with coverage: Output:
When I debug the exception traceback, with my custom function, I get this:
=============================================================================== But when I comment out the login(request, user) line, as in:
The test succeeds...When checking the https://docs.djangoproject.com/en/2.2/_modules/django/contrib/auth/#login login() function it does access the .session attribute of the requestBut according to the exception the Request object generated from APIRequestFactory does not have have it!Why does APIRequestFactory request object does not have a session attribute??How can I fix this? I don't want to remove the login() part, of course.Any help, pointers is highly appreciated. Thank you!=============================================================================== Package versions Platform |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I fixed it by manually adding SessionMiddleware on the request, following this answer on a similar issue from stack overflow.I just created a mixin and made my test inherit it and then just call the add_session() method.
|
Beta Was this translation helpful? Give feedback.
I fixed it by manually adding SessionMiddleware on the request, following this answer on a similar issue from stack overflow.
https://stackoverflow.com/questions/35659676/django-test-case-error-wsgirequest-object-has-no-attribute-session
I just created a mixin and made my test inherit it and then just call the add_session() method.