Skip to content

Proxito middleware: reset to original urlconf after request #7137

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 5 commits into from
May 28, 2020
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
5 changes: 5 additions & 0 deletions readthedocs/proxito/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from django.conf import settings
from django.shortcuts import render
from django.urls.base import set_urlconf
from django.utils.deprecation import MiddlewareMixin

from readthedocs.projects.models import Domain, Project
Expand Down Expand Up @@ -199,6 +200,10 @@ def process_response(self, request, response): # noqa
* For custom domains, check the HSTS values on the Domain object.
The domain object should be saved already in request.domain.
"""
# Reset URLconf for this thread
# to the original one.
Comment on lines +203 to +204
Copy link
Member

Choose a reason for hiding this comment

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

why not put the comment in just one line? 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

I just like short lines to have more space in my monitor 🤷‍♂️

set_urlconf(None)

host = request.get_host().lower().split(':')[0]
public_domain = settings.PUBLIC_DOMAIN.lower().split(':')[0]

Expand Down
36 changes: 27 additions & 9 deletions readthedocs/proxito/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import sys

import pytest
from django.urls.base import set_urlconf, get_urlconf
from django.test import TestCase
from django.test.utils import override_settings
from django.urls.base import get_urlconf, set_urlconf
from django_dynamic_fixture import get

from readthedocs.builds.models import Version
from readthedocs.projects.constants import PUBLIC
from readthedocs.projects.models import Domain, Project, ProjectRelationship
from readthedocs.proxito.middleware import ProxitoMiddleware
from readthedocs.rtd_tests.base import RequestFactoryTestMixin
Expand Down Expand Up @@ -159,16 +161,23 @@ def setUp(self):
Project,
slug='pip',
users=[self.owner],
privacy_level='public',
privacy_level=PUBLIC,
urlconf='subpath/to/$version/$language/$filename' # Flipped
)
self.testing_version = get(
Version,
slug='testing',
project=self.pip,
built=True,
active=True,
)
self.pip.versions.update(privacy_level=PUBLIC)

self.old_urlconf = get_urlconf()
sys.modules['fake_urlconf'] = self.pip.proxito_urlconf
set_urlconf('fake_urlconf')

def tearDown(self):
set_urlconf(self.old_urlconf)
set_urlconf(None)

def test_proxied_api_methods(self):
# This is mostly a unit test, but useful to make sure the below tests work
Expand Down Expand Up @@ -221,29 +230,38 @@ def setUp(self):
name='pip',
slug='pip',
users=[self.owner],
privacy_level='public',
privacy_level=PUBLIC,
urlconf='subpath/$subproject/$version/$language/$filename' # Flipped
)
self.pip.versions.update(privacy_level=PUBLIC)
self.subproject = get(
Project,
name='subproject',
slug='subproject',
users=[self.owner],
privacy_level='public',
privacy_level=PUBLIC,
main_language_project=None,
)
self.testing_version = get(
Version,
slug='testing',
project=self.subproject,
built=True,
active=True,
)
self.subproject.versions.update(privacy_level=PUBLIC)
self.relationship = get(
ProjectRelationship,
parent=self.pip,
child=self.subproject,
)

self.old_urlconf = get_urlconf()
sys.modules['fake_urlconf'] = self.pip.proxito_urlconf
set_urlconf('fake_urlconf')

# TODO: Figure out why this is failing in travis
@pytest.mark.xfail(strict=True)
def tearDown(self):
set_urlconf(None)

def test_middleware_urlconf_subproject(self):
resp = self.client.get('/subpath/subproject/testing/en/foodex.html', HTTP_HOST=self.domain)
self.assertEqual(resp.status_code, 200)
Expand Down