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

Return 404 for inactive versions and allow redirects on them #4599

Merged
merged 1 commit into from Sep 5, 2018
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
5 changes: 3 additions & 2 deletions readthedocs/core/views/serve.py
Expand Up @@ -155,8 +155,9 @@ def serve_docs(
try:
version = project.versions.public(request.user).get(slug=version_slug)
except Version.DoesNotExist:
# Properly raise a 404 if the version doesn't exist & a 401 if it does
if project.versions.filter(slug=version_slug).exists():
# Properly raise a 404 if the version doesn't exist (or is inactive) and
# a 401 if it does
if project.versions.filter(slug=version_slug, active=True).exists():
return _serve_401(request, project)
raise Http404('Version does not exist.')
filename = resolve_path(
Expand Down
27 changes: 26 additions & 1 deletion readthedocs/rtd_tests/tests/test_redirects.py
@@ -1,5 +1,4 @@
from __future__ import absolute_import
from django.core.urlresolvers import reverse
from django.http import Http404
from django.test import TestCase
from django.test.utils import override_settings
Expand All @@ -9,6 +8,7 @@
from mock import patch

from readthedocs.builds.constants import LATEST
from readthedocs.builds.models import Version
from readthedocs.projects.models import Project
from readthedocs.redirects.models import Redirect

Expand Down Expand Up @@ -185,6 +185,31 @@ def test_redirect_exact_with_rest(self):
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/master/guides/install.html')

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_inactive_version(self):
"""
Inactive Version (``active=False``) should redirect properly.

The function that servers the page should return 404 when serving a page
of an inactive version and the redirect system should work.
"""
version = get(
Version,
slug='oldversion',
project=self.pip,
active=False,
)
Redirect.objects.create(
project=self.pip,
redirect_type='exact',
from_url='/en/oldversion/',
to_url='/en/newversion/',
)
r = self.client.get('/en/oldversion/', HTTP_HOST='pip.readthedocs.org')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/newversion/')

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_keeps_version_number(self):
Redirect.objects.create(
Expand Down