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

Redirects: improvements from design doc #10881

Merged
merged 27 commits into from
Jan 2, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Tests
stsewd committed Dec 5, 2023

Verified

This commit was signed with the committer’s verified signature.
stsewd Santos Gallegos
commit 9973e1c7aeeff30e68d93548e5cc156a4431ad3b
4 changes: 2 additions & 2 deletions readthedocs/api/v3/serializers.py
Original file line number Diff line number Diff line change
@@ -885,13 +885,13 @@ def validate_type(self, value):
if value == "sphinx_html":
raise serializers.ValidationError(
_(
f"sphinx_html redirect has been renamed to clean_url_to_html. See {blog_link}"
f"sphinx_html redirect has been renamed to clean_url_to_html. See {blog_link}."
)
)
if value == "sphinx_htmldir":
raise serializers.ValidationError(
_(
f"sphinx_htmldir redirect has been renamed to html_to_clean_url. See {blog_link}"
f"sphinx_htmldir redirect has been renamed to html_to_clean_url. See {blog_link}."
)
)
return value
45 changes: 45 additions & 0 deletions readthedocs/proxito/tests/test_old_redirects.py
Original file line number Diff line number Diff line change
@@ -198,6 +198,51 @@ def test_disabled_redirect(self):
with self.assertRaises(Http404):
self.client.get(url, headers={"host": "project.dev.readthedocs.io"})

def test_redirect_order(self):
redirect_a = fixture.get(
Redirect,
project=self.project,
redirect_type=EXACT_REDIRECT,
from_url="/en/latest/*",
to_url="/en/latest/tutorial/:splat",
enabled=True,
)
redirect_b = fixture.get(
Redirect,
project=self.project,
redirect_type=EXACT_REDIRECT,
from_url="/en/latest/install.html",
to_url="/en/latest/installation.html",
enabled=True,
)

redirect_a.refresh_from_db()
self.assertEqual(redirect_b.position, 0)
self.assertEqual(redirect_a.position, 1)

url = "/en/latest/install.html"
r = self.client.get(url, headers={"host": "project.dev.readthedocs.io"})
self.assertEqual(r.status_code, 302)
self.assertEqual(
r["Location"],
"http://project.dev.readthedocs.io/en/latest/installation.html",
)

redirect_a.position = 0
redirect_a.save()
redirect_b.refresh_from_db()

self.assertEqual(redirect_a.position, 0)
self.assertEqual(redirect_b.position, 1)

url = "/en/latest/install.html"
r = self.client.get(url, headers={"host": "project.dev.readthedocs.io"})
self.assertEqual(r.status_code, 302)
self.assertEqual(
r["Location"],
"http://project.dev.readthedocs.io/en/latest/tutorial/install.html",
)

def test_redirect_ignored_on_external_domain(self):
fixture.get(
Redirect,
69 changes: 69 additions & 0 deletions readthedocs/rtd_tests/tests/test_redirects.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
from readthedocs.builds.constants import LATEST
from readthedocs.projects.constants import SINGLE_VERSION_WITHOUT_TRANSLATIONS
from readthedocs.projects.models import Project
from readthedocs.redirects.constants import EXACT_REDIRECT
from readthedocs.redirects.models import Redirect


@@ -37,6 +38,74 @@ def test_redirect_fragment(self):
expected_path = "/en/latest/install.html#custom-fragment"
self.assertEqual(path, expected_path)

def test_redirects_order(self):
self.pip.redirects.all().delete()
redirect_a = get(
Redirect,
project=self.pip,
from_url="/a/",
to_url="/z/",
redirect_type=EXACT_REDIRECT,
)
redirect_b = get(
Redirect,
project=self.pip,
from_url="/b/",
to_url="/z/",
redirect_type=EXACT_REDIRECT,
)
redirect_c = get(
Redirect,
project=self.pip,
from_url="/c/",
to_url="/z/",
redirect_type=EXACT_REDIRECT,
)

def _refresh():
redirect_a.refresh_from_db()
redirect_b.refresh_from_db()
redirect_c.refresh_from_db()

_refresh()
self.assertEqual(self.pip.redirects.count(), 3)
self.assertEqual(redirect_c.position, 0)
self.assertEqual(redirect_b.position, 1)
self.assertEqual(redirect_a.position, 2)

# Move redirect to the top
redirect_a.position = 0
redirect_a.save()

_refresh()

self.assertEqual(redirect_a.position, 0)
self.assertEqual(redirect_c.position, 1)
self.assertEqual(redirect_b.position, 2)

# Move redirect to the bottom
redirect_c.position = 5
redirect_c.save()

_refresh()
self.assertEqual(redirect_a.position, 0)
self.assertEqual(redirect_b.position, 1)
self.assertEqual(redirect_c.position, 2)

# Delete redirect
redirect_a.delete()

redirect_b.refresh_from_db()
redirect_c.refresh_from_db()
self.assertEqual(redirect_b.position, 0)
self.assertEqual(redirect_c.position, 1)

redirect_c.delete()
redirect_b.refresh_from_db()
self.assertEqual(redirect_b.position, 0)

redirect_b.delete()


@override_settings(PUBLIC_DOMAIN="readthedocs.org")
class RedirectBuildTests(TestCase):