Skip to content

Commit 1ba75be

Browse files
authored
Add tests for test helper utilities (#10893)
1 parent 7df479b commit 1ba75be

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

tests/test_helpers.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import keyring
99
import pytest
10+
import requests
1011

1112
from poetry.core.packages.package import Package
1213
from poetry.core.packages.utils.link import Link
@@ -21,6 +22,7 @@
2122
from tests.helpers import flatten_dict
2223
from tests.helpers import get_dependency
2324
from tests.helpers import get_package
25+
from tests.helpers import http_setup_redirect
2426
from tests.helpers import isolated_environment
2527
from tests.helpers import make_entry_point_from_plugin
2628
from tests.helpers import mock_clone
@@ -35,6 +37,7 @@
3537
from pathlib import Path
3638

3739
from pytest_mock import MockerFixture
40+
from responses import RequestsMock
3841

3942

4043
class TestGetPackage:
@@ -311,18 +314,35 @@ class FakePlugin:
311314
__module__ = "my_plugin.plugin"
312315

313316
ep = make_entry_point_from_plugin("test-plugin", FakePlugin)
314-
assert isinstance(ep, metadata.EntryPoint)
317+
315318
assert ep.name == "test-plugin"
316319
assert ep.group == "poetry.plugin"
317320
assert ep.value == "my_plugin.plugin:FakePlugin"
321+
assert ep.dist is None
318322

319323
def test_creates_entry_point_without_group_attr(self) -> None:
320324
class NoGroupPlugin:
321325
__module__ = "my_plugin.plugin"
322-
__name__ = "NoGroupPlugin"
323326

324327
ep = make_entry_point_from_plugin("test-plugin", NoGroupPlugin)
328+
329+
assert ep.name == "test-plugin"
325330
assert ep.group is None
331+
assert ep.value == "my_plugin.plugin:NoGroupPlugin"
332+
333+
def test_creates_entry_point_with_dist(self, tmp_path: Path) -> None:
334+
class FakePlugin:
335+
group = "poetry.plugin"
336+
__module__ = "my_plugin.plugin"
337+
338+
dist = metadata.Distribution.at(tmp_path)
339+
340+
ep = make_entry_point_from_plugin("test-plugin", FakePlugin, dist=dist)
341+
342+
assert ep.name == "test-plugin"
343+
assert ep.group == "poetry.plugin"
344+
assert ep.value == "my_plugin.plugin:FakePlugin"
345+
assert ep.dist is dist
326346

327347

328348
class TestMockMetadataEntryPoints:
@@ -386,6 +406,39 @@ def test_handles_empty_dict(self) -> None:
386406
assert flatten_dict({}) == {}
387407

388408

409+
class TestHttpSetupRedirect:
410+
def test_redirects_to_original_url(self, http: RequestsMock) -> None:
411+
http_setup_redirect(http, "GET")
412+
413+
response = requests.get(
414+
"https://redirect.example.com/files/demo.whl?download=1",
415+
allow_redirects=False,
416+
)
417+
418+
assert response.status_code == 301
419+
assert (
420+
response.headers["Location"]
421+
== "https://example.com/files/demo.whl?download=1"
422+
)
423+
424+
def test_uses_custom_status_code(self, http: RequestsMock) -> None:
425+
http_setup_redirect(http, "GET", status_code=307)
426+
427+
response = requests.get(
428+
"https://redirect.example.com/files/demo.whl",
429+
allow_redirects=False,
430+
)
431+
432+
assert response.status_code == 307
433+
assert response.headers["Location"] == "https://example.com/files/demo.whl"
434+
435+
def test_only_registers_requested_methods(self, http: RequestsMock) -> None:
436+
http_setup_redirect(http, "GET")
437+
438+
with pytest.raises(requests.exceptions.ConnectionError):
439+
requests.post("https://redirect.example.com/files/demo.whl")
440+
441+
389442
class TestSwitchWorkingDirectory:
390443
@pytest.mark.parametrize("remove", [False, True])
391444
@pytest.mark.parametrize("raise_error", [False, True])

0 commit comments

Comments
 (0)