From 5c32164f8fa17f46b38ed29bb8920394a31e8dc6 Mon Sep 17 00:00:00 2001 From: Steve Jalim Date: Wed, 27 Nov 2024 09:42:01 +0000 Subject: [PATCH] Add support for redirecting all of builders/* paths to a separate builders.m.o domain --- birdbox/birdbox/urls.py | 5 +++-- birdbox/common/tests/test_views.py | 29 +++++++++++++++++++++++++++++ birdbox/common/views.py | 5 +++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/birdbox/birdbox/urls.py b/birdbox/birdbox/urls.py index 5cc77871..e2903384 100644 --- a/birdbox/birdbox/urls.py +++ b/birdbox/birdbox/urls.py @@ -5,7 +5,7 @@ from django.conf import settings from django.contrib import admin from django.http import HttpResponse, HttpResponseForbidden -from django.urls import include, path +from django.urls import include, path, re_path from django.utils.module_loading import import_string from django.views.defaults import permission_denied @@ -18,7 +18,7 @@ from wagtail.documents import urls as wagtaildocs_urls from watchman import views as watchman_views -from common.views import csrf_failure, rate_limited +from common.views import csrf_failure, rate_limited, redirect_view from microsite import urls as microsite_urls handler500 = "common.views.server_error_view" @@ -39,6 +39,7 @@ def handler403(request, exception=None): path("documents/", include(wagtaildocs_urls)), path("healthz/", watchman_views.ping, name="watchman.ping"), path("readiness/", watchman_views.status, name="watchman.status"), + re_path("^builders/", redirect_view, {"dest": "https://builders.mozilla.org"}), path("", include(microsite_urls)), path( "robots.txt", diff --git a/birdbox/common/tests/test_views.py b/birdbox/common/tests/test_views.py index f0205778..c6e06220 100644 --- a/birdbox/common/tests/test_views.py +++ b/birdbox/common/tests/test_views.py @@ -47,3 +47,32 @@ def test_robots_txt(client, engage_robots, expected_content): @pytest.mark.django_db def test_csrf_view_is_custom_one(): assert settings.CSRF_FAILURE_VIEW == "common.views.csrf_failure" + + +@pytest.mark.django_db +@pytest.mark.parametrize( + "path", + ( + "/builders/", + "/builders/some/deeper/path/", + ), +) +def test_builders_redirect(path, client): + resp = client.get(path, follow=False) + assert resp.headers["location"] == "https://builders.mozilla.org" + + +@pytest.mark.django_db +def test_builders_redirect_does_not_affect_anyting_else(client, minimal_site_with_blog): + from microsite.models import BlogPage + + bp1, bp2_featured, bp3 = BlogPage.objects.live().all() + + resp = client.get(bp1.url, follow=False) + assert resp.status_code == 200 + + resp = client.get(bp2_featured.url, follow=False) + assert resp.status_code == 200 + + resp = client.get(bp3.url, follow=False) + assert resp.status_code == 200 diff --git a/birdbox/common/views.py b/birdbox/common/views.py index 0dd41984..e15b1aa6 100644 --- a/birdbox/common/views.py +++ b/birdbox/common/views.py @@ -2,6 +2,7 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. +from django.http import HttpResponsePermanentRedirect from django.shortcuts import render from django.views.decorators.cache import never_cache @@ -26,3 +27,7 @@ def rate_limited(request, exception): response = render(request, "429.html", status=429) response["Retry-After"] = "60" return response + + +def redirect_view(request, **kwargs): + return HttpResponsePermanentRedirect(kwargs["dest"])