diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -29,7 +29,7 @@ from swh.web.auth.utils import ADMIN_LIST_DEPOSIT_PERMISSION from swh.web.common.exc import BadInputExc from swh.web.common.typing import QueryParameters -from swh.web.config import get_config, search +from swh.web.config import SWH_WEB_SERVER_NAME, get_config, search SWH_WEB_METRICS_REGISTRY = CollectorRegistry(auto_describe=True) @@ -242,6 +242,31 @@ return ip +def is_swh_web_development(request: HttpRequest) -> bool: + """Indicate if we are running a development version of swh-web. + """ + site_base_url = request.build_absolute_uri("/") + return any( + host in site_base_url for host in ("localhost", "127.0.0.1", "testserver") + ) + + +def is_swh_web_staging(request: HttpRequest) -> bool: + """Indicate if we are running a staging version of swh-web. + """ + config = get_config() + site_base_url = request.build_absolute_uri("/") + return any( + server_name in site_base_url for server_name in config["staging_server_names"] + ) + + +def is_swh_web_production(request: HttpRequest) -> bool: + """Indicate if we are running the public production version of swh-web. + """ + return SWH_WEB_SERVER_NAME in request.build_absolute_uri("/") + + browsers_supported_image_mimes = set( [ "image/gif", @@ -269,7 +294,7 @@ # To avoid django.template.base.VariableDoesNotExist errors # when rendering templates when standard Django user is logged in. request.user.backend = "django.contrib.auth.backends.ModelBackend" - site_base_url = request.build_absolute_uri("/") + return { "swh_object_icons": swh_object_icons, "available_languages": None, @@ -277,16 +302,11 @@ "oidc_enabled": bool(config["keycloak"]["server_url"]), "browsers_supported_image_mimes": browsers_supported_image_mimes, "keycloak": config["keycloak"], - "site_base_url": site_base_url, + "site_base_url": request.build_absolute_uri("/"), "DJANGO_SETTINGS_MODULE": os.environ["DJANGO_SETTINGS_MODULE"], "status": config["status"], - "swh_web_dev": "localhost" in site_base_url, - "swh_web_staging": any( - [ - server_name in site_base_url - for server_name in config["staging_server_names"] - ] - ), + "swh_web_dev": is_swh_web_development(request), + "swh_web_staging": is_swh_web_staging(request), "swh_web_version": get_distribution("swh.web").version, "iframe_mode": False, "ADMIN_LIST_DEPOSIT_PERMISSION": ADMIN_LIST_DEPOSIT_PERMISSION, diff --git a/swh/web/config.py b/swh/web/config.py --- a/swh/web/config.py +++ b/swh/web/config.py @@ -15,9 +15,10 @@ from swh.vault import get_vault from swh.web import settings +SWH_WEB_SERVER_NAME = "archive.softwareheritage.org" SWH_WEB_INTERNAL_SERVER_NAME = "archive.internal.softwareheritage.org" -STAGING_SERVER_NAMES = [ +SWH_WEB_STAGING_SERVER_NAMES = [ "webapp.staging.swh.network", "webapp.internal.staging.swh.network", ] @@ -123,7 +124,7 @@ }, ), "counters_backend": ("string", "swh-storage"), # or "swh-counters" - "staging_server_names": ("list", STAGING_SERVER_NAMES), + "staging_server_names": ("list", SWH_WEB_STAGING_SERVER_NAMES), "instance_name": ("str", "archive-test.softwareheritage.org"), "give": ("dict", {"public_key": "", "token": ""}), } diff --git a/swh/web/tests/common/test_utils.py b/swh/web/tests/common/test_utils.py --- a/swh/web/tests/common/test_utils.py +++ b/swh/web/tests/common/test_utils.py @@ -15,7 +15,7 @@ from swh.web.common import utils from swh.web.common.exc import BadInputExc -from swh.web.config import get_config +from swh.web.config import SWH_WEB_SERVER_NAME, SWH_WEB_STAGING_SERVER_NAMES, get_config def test_shorten_path_noop(): @@ -297,3 +297,20 @@ else: # see swh/web/tests/data.py for origins added for tests assert utils.origin_visit_types() == ["git", "tar"] + + +@pytest.mark.parametrize("server_name", ["localhost", "127.0.0.1", "testserver"]) +def test_is_swh_web_development(request_factory, server_name): + request = request_factory.get("/", SERVER_NAME=server_name) + assert utils.is_swh_web_development(request) + + +@pytest.mark.parametrize("server_name", SWH_WEB_STAGING_SERVER_NAMES) +def test_is_swh_web_staging(request_factory, server_name): + request = request_factory.get("/", SERVER_NAME=server_name) + assert utils.is_swh_web_staging(request) + + +def test_is_swh_web_production(request_factory): + request = request_factory.get("/", SERVER_NAME=SWH_WEB_SERVER_NAME) + assert utils.is_swh_web_production(request) diff --git a/swh/web/tests/test_templates.py b/swh/web/tests/test_templates.py --- a/swh/web/tests/test_templates.py +++ b/swh/web/tests/test_templates.py @@ -11,7 +11,7 @@ from swh.web.auth.utils import ADMIN_LIST_DEPOSIT_PERMISSION from swh.web.common.utils import reverse -from swh.web.config import STAGING_SERVER_NAMES, get_config +from swh.web.config import SWH_WEB_SERVER_NAME, SWH_WEB_STAGING_SERVER_NAMES, get_config from swh.web.tests.django_asserts import assert_contains, assert_not_contains from swh.web.tests.utils import check_http_get_response, create_django_permission @@ -20,14 +20,19 @@ def test_layout_without_ribbon(client): url = reverse("swh-web-homepage") - resp = check_http_get_response(client, url, status_code=200) + resp = check_http_get_response( + client, url, status_code=200, server_name=SWH_WEB_SERVER_NAME + ) assert_not_contains(resp, "swh-corner-ribbon") def test_layout_with_staging_ribbon(client): url = reverse("swh-web-homepage") resp = check_http_get_response( - client, url, status_code=200, server_name=random.choice(STAGING_SERVER_NAMES), + client, + url, + status_code=200, + server_name=random.choice(SWH_WEB_STAGING_SERVER_NAMES), ) assert_contains(resp, "swh-corner-ribbon") assert_contains(resp, f"Staging
v{swh_web_version}")