From 3718c69e98b702518b9becd87c1970b2d6870c7f Mon Sep 17 00:00:00 2001 From: Vit Zikmund Date: Fri, 8 Mar 2024 12:39:09 +0100 Subject: [PATCH] chore(endpoint): test legacy endpoints turned off --- tests/conftest.py | 8 +++++-- tests/helpers.py | 13 +++++++++++ tests/test_middleware.py | 22 ++++++++++++++----- tests/transfer/test_basic_external_adapter.py | 18 +++++++++++---- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index fdfe08c..583b56b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ import pathlib import shutil from collections.abc import Generator +from typing import Any import flask import pytest @@ -10,6 +11,7 @@ from giftless.app import init_app from giftless.auth import allow_anon, authentication +from tests.helpers import legacy_endpoints_id @pytest.fixture @@ -22,12 +24,14 @@ def storage_path(tmp_path: pathlib.Path) -> Generator: shutil.rmtree(path) -@pytest.fixture -def app(storage_path: str) -> flask.Flask: +@pytest.fixture(params=[False], ids=legacy_endpoints_id) +def app(storage_path: str, request: Any) -> flask.Flask: """Session fixture to configure the Flask app.""" + legacy_endpoints = request.param app = init_app( additional_config={ "TESTING": True, + "LEGACY_ENDPOINTS": legacy_endpoints, "TRANSFER_ADAPTERS": { "basic": { "options": {"storage_options": {"path": storage_path}} diff --git a/tests/helpers.py b/tests/helpers.py index 9352d00..ee82f72 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -3,6 +3,8 @@ from pathlib import Path from typing import Any +import flask + def batch_request_payload( delete_keys: Sequence[str] = (), **kwargs: Any @@ -39,3 +41,14 @@ def create_file_in_storage( with Path(repo_path / filename).open("wb") as f: for c in (b"0" for _ in range(size)): f.write(c) + + +def legacy_endpoints_id(enabled: bool) -> str: + return "legacy-ep" if enabled else "current-ep" + + +def expected_uri_prefix(app: flask.Flask, *args: str) -> str: + core_prefix = "/".join(args) + if not app.config.get("LEGACY_ENDPOINTS"): + return core_prefix + ".git/info/lfs" + return core_prefix diff --git a/tests/test_middleware.py b/tests/test_middleware.py index acc4da2..11673b9 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -1,17 +1,21 @@ """Tests for using middleware and some specific middleware.""" from typing import Any, cast +import flask import pytest -from flask import Flask from flask.testing import FlaskClient from giftless.app import init_app -from .helpers import batch_request_payload +from .helpers import ( + batch_request_payload, + expected_uri_prefix, + legacy_endpoints_id, +) @pytest.fixture -def app(storage_path: str) -> Flask: +def app(storage_path: str) -> flask.Flask: """Session fixture to configure the Flask app.""" return init_app( additional_config={ @@ -36,7 +40,11 @@ def app(storage_path: str) -> Flask: @pytest.mark.usefixtures("_authz_full_access") +@pytest.mark.parametrize( + "app", [False, True], ids=legacy_endpoints_id, indirect=True +) def test_upload_request_with_x_forwarded_middleware( + app: flask.Flask, test_client: FlaskClient, ) -> None: """Test the ProxyFix middleware generates correct URLs if @@ -51,8 +59,10 @@ def test_upload_request_with_x_forwarded_middleware( json = cast(dict[str, Any], response.json) upload_action = json["objects"][0]["actions"]["upload"] href = upload_action["href"] - base_uri = "myorg/myrepo" - assert href == f"http://localhost/{base_uri}/objects/storage/12345678" + exp_uri_prefix = expected_uri_prefix(app, "myorg", "myrepo") + assert ( + href == f"http://localhost/{exp_uri_prefix}/objects/storage/12345678" + ) response = test_client.post( "/myorg/myrepo.git/info/lfs/objects/batch", @@ -71,5 +81,5 @@ def test_upload_request_with_x_forwarded_middleware( href = upload_action["href"] assert ( href - == f"https://mycompany.xyz:1234/lfs/{base_uri}/objects/storage/12345678" + == f"https://mycompany.xyz:1234/lfs/{exp_uri_prefix}/objects/storage/12345678" ) diff --git a/tests/transfer/test_basic_external_adapter.py b/tests/transfer/test_basic_external_adapter.py index ee3f636..cdcc206 100644 --- a/tests/transfer/test_basic_external_adapter.py +++ b/tests/transfer/test_basic_external_adapter.py @@ -2,11 +2,13 @@ from typing import Any from urllib.parse import urlencode +import flask import pytest from giftless.storage import ExternalStorage from giftless.storage.exc import ObjectNotFoundError from giftless.transfer import basic_external +from tests.helpers import expected_uri_prefix, legacy_endpoints_id def test_factory_returns_object() -> None: @@ -26,13 +28,17 @@ def test_factory_returns_object() -> None: @pytest.mark.usefixtures("app_context") -def test_upload_action_new_file() -> None: +@pytest.mark.parametrize( + "app", [False, True], ids=legacy_endpoints_id, indirect=True +) +def test_upload_action_new_file(app: flask.Flask) -> None: adapter = basic_external.factory( f"{__name__}:MockExternalStorageBackend", {}, 900, ) response = adapter.upload("myorg", "myrepo", "abcdef123456", 1234) + exp_uri_prefix = expected_uri_prefix(app, "myorg", "myrepo") assert response == { "oid": "abcdef123456", @@ -45,7 +51,7 @@ def test_upload_action_new_file() -> None: "expires_in": 900, }, "verify": { - "href": "http://giftless.local/myorg/myrepo/objects/storage/verify", + "href": f"http://giftless.local/{exp_uri_prefix}/objects/storage/verify", "header": {}, "expires_in": 43200, }, @@ -54,13 +60,17 @@ def test_upload_action_new_file() -> None: @pytest.mark.usefixtures("app_context") -def test_upload_action_extras_are_passed() -> None: +@pytest.mark.parametrize( + "app", [False, True], ids=legacy_endpoints_id, indirect=True +) +def test_upload_action_extras_are_passed(app: flask.Flask) -> None: adapter = basic_external.factory( f"{__name__}:MockExternalStorageBackend", {}, 900 ) response = adapter.upload( "myorg", "myrepo", "abcdef123456", 1234, {"filename": "foo.csv"} ) + exp_uri_prefix = expected_uri_prefix(app, "myorg", "myrepo") assert response == { "oid": "abcdef123456", @@ -73,7 +83,7 @@ def test_upload_action_extras_are_passed() -> None: "expires_in": 900, }, "verify": { - "href": "http://giftless.local/myorg/myrepo/objects/storage/verify", + "href": f"http://giftless.local/{exp_uri_prefix}/objects/storage/verify", "header": {}, "expires_in": 43200, },