diff --git a/app/__init__.py b/app/__init__.py index 806d63be1..7c5879a30 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,6 +2,7 @@ import os import pathlib from functools import partial from time import monotonic +from urllib.parse import urlparse, urlunparse import jinja2 from flask import ( @@ -250,6 +251,7 @@ def create_app(application): def init_app(application): + application.before_request(redirect_notify_to_beta) application.before_request(load_service_before_request) application.before_request(load_organization_before_request) application.before_request(request_helper.check_proxy_header_before_request) @@ -328,6 +330,23 @@ def make_session_permanent(): session.permanent = True +def create_beta_url(url): + url_created = urlparse(url) + url_list = list(url_created) + url_list[1] = "beta.notify.gov" + url_for_redirect = urlunparse(url_list) + return url_for_redirect + + +def redirect_notify_to_beta(): + if ( + current_app.config["NOTIFY_ENVIRONMENT"] == "production" + and "beta.notify.gov" not in request.url + ): + url_to_beta = create_beta_url(request.url) + return redirect(url_to_beta, 302) + + def load_service_before_request(): if "/static/" in request.url: request_ctx.service = None diff --git a/tests/app/main/test_beta_redirect.py b/tests/app/main/test_beta_redirect.py new file mode 100644 index 000000000..e192941af --- /dev/null +++ b/tests/app/main/test_beta_redirect.py @@ -0,0 +1,38 @@ +from flask import current_app + +from app import create_beta_url + + +def test_create_beta_url(): + url_for_redirect = create_beta_url("https://notify.gov/using-notify/get-started") + assert url_for_redirect == "https://beta.notify.gov/using-notify/get-started" + + +def test_no_redirect_notify_to_beta_non_production(monkeypatch, client_request): + monkeypatch.setitem(current_app.config, "NOTIFY_ENVIRONMENT", "development") + assert current_app.config["NOTIFY_ENVIRONMENT"] == "development" + + client_request.get_response_from_url( + "https://notify.gov/using-notify/get-started", + _expected_status=200 + ) + + +def test_redirect_notify_to_beta(monkeypatch, client_request): + monkeypatch.setitem(current_app.config, "NOTIFY_ENVIRONMENT", "production") + assert current_app.config["NOTIFY_ENVIRONMENT"] == "production" + + client_request.get_response_from_url( + "https://notify.gov/using-notify/get-started", + _expected_status=302 + ) + + +def test_no_redirect_beta_notify_to_beta(monkeypatch, client_request): + monkeypatch.setitem(current_app.config, "NOTIFY_ENVIRONMENT", "production") + assert current_app.config["NOTIFY_ENVIRONMENT"] == "production" + + client_request.get_response_from_url( + "https://beta.notify.gov/using-notify/get-started", + _expected_status=200 + ) diff --git a/tests/conftest.py b/tests/conftest.py index 9b6cc3efc..942e00526 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2392,6 +2392,7 @@ def _client(notify_admin): Do not use this fixture directly – use `client_request` instead """ with notify_admin.test_request_context(), notify_admin.test_client() as client: + client.allow_subdomain_redirects = True yield client