Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/quetz-server-1.10.0' into quetz-…
Browse files Browse the repository at this point in the history
…server-0.10.0
  • Loading branch information
janjagusch committed Oct 6, 2023
2 parents 25ab73d + 396553a commit 67f0532
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .quetz-server-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.0
0.10.2
13 changes: 13 additions & 0 deletions src/quetz_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class Package:
latest_change: str



def _assert_file_is_package(file: Path):
"""Raises an error if the file in question does not look like a conda package"""
valid_suffixes =[".tar.bz2", ".conda"]
file_has_valid_suffix = any(file.name.endswith(suffix) for suffix in valid_suffixes)
if not file_has_valid_suffix:
raise ValueError(f"File {file} does not look like a conda package. It should end in one of {valid_suffixes}.")

return False

@dataclass
class QuetzClient:
session: requests.Session
Expand Down Expand Up @@ -172,6 +182,9 @@ def yield_packages(

def post_file_to_channel(self, channel: str, file: Path, force: bool = False):
file_path = Path(file)

_assert_file_is_package(file_path)

url = f"{self.url}/api/channels/{channel}/upload/{file_path.name}"
body_bytes = file_path.read_bytes()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_live_set_role(
role,
):
live_client.set_role("bob", role)
actual_alice_role = live_client.get_role("bob")
assert next(actual_alice_role).role == role
actual_bob_role = live_client.get_role("bob")
assert next(actual_bob_role).role == role


def test_live_post_file_to_channel(
Expand Down
15 changes: 14 additions & 1 deletion tests/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from quetz_client.client import QuetzClient

from .conftest import temporary_package_file

from pathlib import Path

@pytest.mark.parametrize(
"role",
Expand Down Expand Up @@ -170,3 +170,16 @@ def test_mock_post_file_to_channel(
# thus we need to access all the requests
assert len(requests_mock.request_history) <= 2
assert any(r.method == "POST" for r in requests_mock.request_history)


def test_mock_post_file_to_channel_invalid_file(
mock_client: QuetzClient,
):
"""Test that the client refuses to upload an invalid file that is not a conda package
This test requires to mock server because no actual request are ever made.
The client should exit before talking to the server.
"""
file = Path("./wrong_suffix.txt")
with pytest.raises(ValueError):
mock_client.post_file_to_channel(channel="doesnotmatter", file=file)

0 comments on commit 67f0532

Please sign in to comment.