Skip to content
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

Check for /index on pages' slug #6789

Merged
merged 5 commits into from Mar 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions readthedocs/api/v2/views/footer_views.py
@@ -1,5 +1,7 @@
"""Endpoint to generate footer HTML."""

import re

from django.conf import settings
from django.shortcuts import get_object_or_404
from django.template import loader as template_loader
Expand Down Expand Up @@ -142,13 +144,12 @@ def _get_context(self):
version = self._get_version()

page_slug = self.request.GET.get('page', '')
path = ''
if page_slug and page_slug != 'index':
if main_project.documentation_type == 'sphinx_htmldir':
path = page_slug + '/'
if version.documentation_type == 'sphinx_htmldir':
path = re.sub('/index$', '', page_slug) + '/'
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we use the regex module that has timeout here?

Copy link
Member Author

Choose a reason for hiding this comment

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

regex are dangerous when the user provides the pattern, this pattern is simple enough.

else:
path = page_slug + '.html'
else:
path = ''

context = {
'project': project,
Expand Down
25 changes: 0 additions & 25 deletions readthedocs/config/config.py
Expand Up @@ -685,8 +685,6 @@ def validate(self):
self.validate_doc_types()
self._config['mkdocs'] = self.validate_mkdocs()
self._config['sphinx'] = self.validate_sphinx()
# TODO: remove later
self.validate_final_doc_type()
self._config['submodules'] = self.validate_submodules()
self.validate_keys()

Expand Down Expand Up @@ -965,29 +963,6 @@ def validate_sphinx(self):

return sphinx

def validate_final_doc_type(self):
"""
Validates that the doctype is the same as the admin panel.

This a temporal validation, as the configuration file should support per
version doctype, but we need to adapt the rtd code for that.
"""
dashboard_doctype = self.defaults.get('doctype', 'sphinx')
if self.doctype != dashboard_doctype:
error_msg = (
'Your project is configured as "{}" in your admin dashboard,'
).format(self.builders_display[dashboard_doctype])

if dashboard_doctype == 'mkdocs' or not self.sphinx:
error_msg += ' but there is no "{}" key specified.'.format(
'mkdocs' if dashboard_doctype == 'mkdocs' else 'sphinx',
)
else:
error_msg += ' but your "sphinx.builder" key does not match.'

key = 'mkdocs' if self.doctype == 'mkdocs' else 'sphinx.builder'
self.error(key, error_msg, code=INVALID_KEYS_COMBINATION)

def validate_submodules(self):
"""
Validates the submodules key.
Expand Down
25 changes: 0 additions & 25 deletions readthedocs/config/tests/test_config.py
Expand Up @@ -1511,9 +1511,6 @@ def test_sphinx_builder_default(self):
build.validate()
build.sphinx.builder == 'sphinx'

@pytest.mark.skip(
'This test is not compatible with the new validation around doctype.',
)
def test_sphinx_builder_ignores_default(self):
build = self.get_build_config(
{},
Expand Down Expand Up @@ -1686,28 +1683,6 @@ def test_mkdocs_fail_on_warning_check_default(self):
build.validate()
assert build.mkdocs.fail_on_warning is False

def test_validates_different_filetype_mkdocs(self):
build = self.get_build_config(
{'mkdocs': {}},
{'defaults': {'doctype': 'sphinx'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'mkdocs'
assert 'configured as "Sphinx Html"' in str(excinfo.value)
assert 'there is no "sphinx" key' in str(excinfo.value)

def test_validates_different_filetype_sphinx(self):
build = self.get_build_config(
{'sphinx': {}},
{'defaults': {'doctype': 'sphinx_htmldir'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'sphinx.builder'
assert 'configured as "Sphinx HtmlDir"' in str(excinfo.value)
assert 'your "sphinx.builder" key does not match' in str(excinfo.value)

def test_submodule_defaults(self):
build = self.get_build_config({})
build.validate()
Expand Down
3 changes: 0 additions & 3 deletions readthedocs/rtd_tests/tests/test_config_integration.py
Expand Up @@ -781,9 +781,6 @@ def test_sphinx_builder(

get_builder_class.assert_called_with(result)

@pytest.mark.skip(
'This test is not compatible with the new validation around doctype.',
)
@patch('readthedocs.projects.tasks.get_builder_class')
def test_sphinx_builder_default(
self, get_builder_class, checkout_path, tmpdir,
Expand Down
54 changes: 54 additions & 0 deletions readthedocs/rtd_tests/tests/test_footer.py
Expand Up @@ -143,6 +143,60 @@ def test_not_show_edit_on_github(self):
self.assertIn('View', response.data['html'])
self.assertNotIn('Edit', response.data['html'])

def test_index_pages_sphinx_htmldir(self):
version = self.pip.versions.get(slug=LATEST)
version.documentation_type = 'sphinx_htmldir'
version.save()

# A page with slug 'index' should render like /en/latest/
self.url = (
reverse('footer_html') +
f'?project={self.pip.slug}&version={self.latest.slug}&page=index&docroot=/'
)
response = self.render()
self.assertIn('/en/latest/', response.data['html'])
self.assertNotIn('/en/latest/index.html', response.data['html'])

# A page with slug 'foo/index' should render like /en/latest/foo/
self.url = (
reverse('footer_html') +
f'?project={self.pip.slug}&version={self.latest.slug}&page=foo/index&docroot=/'
)
response = self.render()
self.assertIn('/en/latest/foo/', response.data['html'])
self.assertNotIn('/en/latest/foo.html', response.data['html'])
self.assertNotIn('/en/latest/foo/index.html', response.data['html'])

# A page with slug 'foo/bar' should render like /en/latest/foo/bar/
self.url = (
reverse('footer_html') +
f'?project={self.pip.slug}&version={self.latest.slug}&page=foo/bar&docroot=/'
)
response = self.render()
self.assertIn('/en/latest/foo/bar/', response.data['html'])
self.assertNotIn('/en/latest/foo/bar.html', response.data['html'])
self.assertNotIn('/en/latest/foo/bar/index.html', response.data['html'])

# A page with slug 'foo/bar/index' should render like /en/latest/foo/bar/
self.url = (
reverse('footer_html') +
f'?project={self.pip.slug}&version={self.latest.slug}&page=foo/bar/index&docroot=/'
)
response = self.render()
self.assertIn('/en/latest/foo/bar/', response.data['html'])
self.assertNotIn('/en/latest/foo/bar.html', response.data['html'])
self.assertNotIn('/en/latest/foo/bar/index.html', response.data['html'])

# A page with slug 'foo/index/bar' should render like /en/latest/foo/index/bar/
self.url = (
reverse('footer_html') +
f'?project={self.pip.slug}&version={self.latest.slug}&page=foo/index/bar&docroot=/'
)
response = self.render()
self.assertIn('/en/latest/foo/index/bar/', response.data['html'])
self.assertNotIn('/en/latest/foo/index/bar.html', response.data['html'])
self.assertNotIn('/en/latest/foo/index/bar/index.html', response.data['html'])


class TestFooterHTML(BaseTestFooterHTML, TestCase):

Expand Down