Skip to content

Commit

Permalink
Merge pull request #6630 from stsewd/proxi-api-on-docs-domain
Browse files Browse the repository at this point in the history
Proxy footer api on docs' domains
  • Loading branch information
ericholscher authored Mar 9, 2020
2 parents 1d726ac + ada230b commit 23f9d19
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 8 deletions.
18 changes: 18 additions & 0 deletions readthedocs/api/v2/proxied_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Proxied API URLs.
Served from the same domain docs are served,
so they can make use of features that require to have access to their cookies.
"""

from django.conf.urls import include, url

from .views.proxied import ProxiedFooterHTML
from readthedocs.search.proxied_api import ProxiedPageSearchAPIView

api_footer_urls = [
url(r'footer_html/', ProxiedFooterHTML.as_view(), name='footer_html'),
url(r'docsearch/$', ProxiedPageSearchAPIView.as_view(), name='doc_search'),
]

urlpatterns = api_footer_urls
15 changes: 15 additions & 0 deletions readthedocs/api/v2/views/proxied.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from readthedocs.core.utils.extend import SettingsOverrideObject

from .footer_views import BaseFooterHTML


class BaseProxiedFooterHTML(BaseFooterHTML):

# DRF has BasicAuthentication and SessionAuthentication as default classes.
# We don't support neither in the community site.
authentication_classes = []


class ProxiedFooterHTML(SettingsOverrideObject):

_default_class = BaseProxiedFooterHTML
18 changes: 18 additions & 0 deletions readthedocs/proxito/tests/test_proxied_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase, override_settings

from readthedocs.rtd_tests.tests.test_footer import BaseTestFooterHTML


@override_settings(
PUBLIC_DOMAIN='readthedocs.io',
ROOT_URLCONF='readthedocs.proxito.urls',
)
class TestProxiedFooterHTML(BaseTestFooterHTML, TestCase):

def setUp(self):
super().setUp()
self.host = 'pip.readthedocs.io'

def render(self):
r = self.client.get(self.url, HTTP_HOST=self.host)
return r
16 changes: 14 additions & 2 deletions readthedocs/proxito/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@
* Can't have subprojects (pip.rtfd.io/en/projects/foo/en/latestindex.html)
* This would stop us from detaching translations from Project modeling
* Can't be translated (pip.rtfd.io/cz/en/latest/index.html)
## Proxied API
pip.rtd.io/_/api/*
"""

from django.conf import settings
from django.conf.urls import url
from django.conf.urls import include, url
from django.views import defaults

from readthedocs.constants import pattern_opts
from readthedocs.projects.views.public import ProjectDownloadMedia
from readthedocs.proxito.views.serve import (
ServePageRedirect,
ServeDocs,
ServeError404,
ServePageRedirect,
ServeRobotsTXT,
ServeSitemapXML,
)
Expand Down Expand Up @@ -77,6 +81,14 @@
name='project_download_media',
),

# Serve proxied API
url(
r'^{DOC_PATH_PREFIX}api/v2/'.format(
DOC_PATH_PREFIX=DOC_PATH_PREFIX,
),
include('readthedocs.api.v2.proxied_urls'),
),

# Serve custom 404 pages
url(
r'^_proxito_404_(?P<proxito_path>.*)$',
Expand Down
9 changes: 8 additions & 1 deletion readthedocs/rtd_tests/tests/test_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from readthedocs.projects.models import Project


class TestFooterHTML(TestCase):
class BaseTestFooterHTML:

def setUp(self):
self.pip = get(
Expand All @@ -27,6 +27,8 @@ def setUp(self):
privacy_level=PUBLIC,
main_language_project=None,
)
self.pip.versions.update(privacy_level=PUBLIC)

self.latest = self.pip.versions.get(slug=LATEST)
self.url = (
reverse('footer_html') +
Expand Down Expand Up @@ -142,6 +144,11 @@ def test_not_show_edit_on_github(self):
self.assertNotIn('Edit', response.data['html'])


class TestFooterHTML(BaseTestFooterHTML, TestCase):

pass


@override_settings(
USE_SUBDOMAIN=True,
PUBLIC_DOMAIN='readthedocs.io',
Expand Down
12 changes: 12 additions & 0 deletions readthedocs/search/proxied_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from readthedocs.core.utils.extend import SettingsOverrideObject
from .api import PageSearchAPIView


class BaseProxiedPageSearchAPIView(PageSearchAPIView):

pass


class ProxiedPageSearchAPIView(SettingsOverrideObject):

_default_class = BaseProxiedPageSearchAPIView
14 changes: 9 additions & 5 deletions readthedocs/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

@pytest.mark.django_db
@pytest.mark.search
class TestDocumentSearch:
class BaseTestDocumentSearch:

@classmethod
def setup_class(cls):
# This reverse needs to be inside the ``setup_class`` method because from
def setup_method(self, method):
# This reverse needs to be inside the ``setup_method`` method because from
# the Corporate site we don't define this URL if ``-ext`` module is not
# installed
cls.url = reverse('doc_search')
self.url = reverse('doc_search')

def get_search(self, api_client, search_params):
return api_client.get(self.url, search_params)
Expand Down Expand Up @@ -260,3 +259,8 @@ def test_doc_search_unexisting_version(self, api_client, project):

data = resp.data['results']
assert len(data) == 0


class TestDocumentSearch(BaseTestDocumentSearch):

pass
13 changes: 13 additions & 0 deletions readthedocs/search/tests/test_proxied_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from readthedocs.search.tests.test_api import BaseTestDocumentSearch


@pytest.mark.urls('readthedocs.proxito.urls')
@pytest.mark.search
class TestProxiedSearchAPI(BaseTestDocumentSearch):

host = 'pip.readthedocs.io'

def get_search(self, api_client, search_params):
return api_client.get(self.url, search_params, HTTP_HOST=self.host)

0 comments on commit 23f9d19

Please sign in to comment.