Skip to content

Commit

Permalink
chore(endpoint): test legacy endpoints turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
vit-zikmund committed Mar 8, 2024
1 parent f7e07ac commit 3718c69
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import shutil
from collections.abc import Generator
from typing import Any

import flask
import pytest
Expand All @@ -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
Expand All @@ -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}}
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pathlib import Path
from typing import Any

import flask


def batch_request_payload(
delete_keys: Sequence[str] = (), **kwargs: Any
Expand Down Expand Up @@ -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
22 changes: 16 additions & 6 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -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={
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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"
)
18 changes: 14 additions & 4 deletions tests/transfer/test_basic_external_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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,
},
Expand All @@ -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",
Expand All @@ -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,
},
Expand Down

0 comments on commit 3718c69

Please sign in to comment.