Skip to content

Commit 3764e00

Browse files
committed
Return 404 for inactive versions and allow redirects on them
When a version exists and is inactive, we return 404 instead of 401 so our redirect system can be used for these versions. We only return 401 when the reader has no permission to read the version (active and private).
1 parent a29fea4 commit 3764e00

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

readthedocs/core/views/serve.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ def serve_docs(
155155
try:
156156
version = project.versions.public(request.user).get(slug=version_slug)
157157
except Version.DoesNotExist:
158-
# Properly raise a 404 if the version doesn't exist & a 401 if it does
159-
if project.versions.filter(slug=version_slug).exists():
158+
# Properly raise a 404 if the version doesn't exist (or is inactive) and
159+
# a 401 if it does
160+
if project.versions.filter(slug=version_slug, active=True).exists():
160161
return _serve_401(request, project)
161162
raise Http404('Version does not exist.')
162163
filename = resolve_path(

readthedocs/rtd_tests/tests/test_redirects.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import absolute_import
2-
from django.core.urlresolvers import reverse
32
from django.http import Http404
43
from django.test import TestCase
54
from django.test.utils import override_settings
@@ -9,6 +8,7 @@
98
from mock import patch
109

1110
from readthedocs.builds.constants import LATEST
11+
from readthedocs.builds.models import Version
1212
from readthedocs.projects.models import Project
1313
from readthedocs.redirects.models import Redirect
1414

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

188+
@override_settings(USE_SUBDOMAIN=True)
189+
def test_redirect_inactive_version(self):
190+
"""
191+
Inactive Version (``active=False``) should redirect properly.
192+
193+
The function that servers the page should return 404 when serving a page
194+
of an inactive version and the redirect system should work.
195+
"""
196+
version = get(
197+
Version,
198+
slug='oldversion',
199+
project=self.pip,
200+
active=False,
201+
)
202+
Redirect.objects.create(
203+
project=self.pip,
204+
redirect_type='exact',
205+
from_url='/en/oldversion/',
206+
to_url='/en/newversion/',
207+
)
208+
r = self.client.get('/en/oldversion/', HTTP_HOST='pip.readthedocs.org')
209+
self.assertEqual(r.status_code, 302)
210+
self.assertEqual(
211+
r['Location'], 'http://pip.readthedocs.org/en/newversion/')
212+
188213
@override_settings(USE_SUBDOMAIN=True)
189214
def test_redirect_keeps_version_number(self):
190215
Redirect.objects.create(

0 commit comments

Comments
 (0)