Skip to content

Use a real SessionBase object on FooterNoSessionMiddleware #5797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.http import Http404, HttpResponseBadRequest
Expand Down Expand Up @@ -205,7 +206,7 @@ def process_request(self, request):
settings.SESSION_COOKIE_NAME not in request.COOKIES
):
# Hack request.session otherwise the Authentication middleware complains.
request.session = {}
request.session = SessionBase() # create an empty session
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could instantiate an empty instance of the same class that we are using in the backend.

from importlib import import_module
from django.conf import settings
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
request.session = SessionStore()

I suppose that won't be any difference since the session_key is None, though.

return
super().process_request(request)

Expand Down
4 changes: 3 additions & 1 deletion readthedocs/rtd_tests/tests/test_footer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from django.contrib.sessions.backends.base import SessionBase
from django.test import TestCase
from rest_framework.test import APIRequestFactory, APITestCase

Expand Down Expand Up @@ -80,7 +81,8 @@ def test_no_session_logged_out(self):
# Null session here
request = self.factory.get('/api/v2/footer_html/')
mid.process_request(request)
self.assertEqual(request.session, {})
self.assertIsInstance(request.session, SessionBase)
self.assertEqual(list(request.session.keys()), [])

# Proper session here
home_request = self.factory.get('/')
Expand Down