Skip to content

Commit

Permalink
Allow configuration of block_network.allowed_hosts via vcr_config f…
Browse files Browse the repository at this point in the history
…ixture.
  • Loading branch information
AndreCimander committed May 1, 2022
1 parent 53f42b2 commit ba4d3bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/pytest_recording/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,21 @@ def block_network(request: SubRequest, record_mode: str, vcr_markers: List[Mark]
marker = request.node.get_closest_marker(name="block_network")
if marker is not None:
validate_block_network_mark(marker)
config = request.getfixturevalue("vcr_config")
# If network blocking is enabled there is one exception - if VCR is in recording mode (any mode except "none")
# Take `--allowed-hosts` with the most priority:
# - `block_network` mark
# - CLI option
# - vcr_config fixture
default_block = marker or request.config.getoption("--block-network")
allowed_hosts = getattr(marker, "kwargs", {}).get("allowed_hosts") or request.config.getoption("--allowed-hosts")
allowed_hosts = getattr(marker, "kwargs", {}).get("allowed_hosts") or request.config.getoption("--allowed-hosts") or config.get("allowed_hosts")
if isinstance(allowed_hosts, str):
allowed_hosts = allowed_hosts.split(",")
if vcr_markers:
# Take `record_mode` with the most priority:
# - Explicit CLI option
# - The `vcr_config` fixture
# - The `vcr` mark
config = request.getfixturevalue("vcr_config")
merged_config = merge_kwargs(config, vcr_markers)
# If `--record-mode` was not explicitly passed in CLI, then take one from the merged config
if request.config.getoption("--record-mode") is None:
Expand Down
19 changes: 14 additions & 5 deletions tests/test_blocking_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,27 @@ def test_no_vcr_mark_bytearray():


@pytest.mark.parametrize(
"marker, cmd_options",
"marker, cmd_options, vcr_cfg",
(
pytest.param('@pytest.mark.block_network(allowed_hosts=["127.0.0.*", "127.0.1.1"])', "", id="block_marker"),
pytest.param("", ("--block-network", "--allowed-hosts=127.0.0.*,127.0.1.1"), id="block_cmd"),
pytest.param('@pytest.mark.block_network(allowed_hosts=["127.0.0.*", "127.0.1.1"])', "", "", id="block_marker"),
pytest.param("", ("--block-network", "--allowed-hosts=127.0.0.*,127.0.1.1"), "", id="block_cmd"),
pytest.param(
"@pytest.mark.block_network()",
"",
"@pytest.fixture(autouse=True)\ndef vcr_config():\n return {'allowed_hosts': '127.0.0.*,127.0.1.1'}",
id="vcr_cfg",
),
),
)
def test_block_network_with_allowed_hosts(testdir, marker, cmd_options):
def test_block_network_with_allowed_hosts(testdir, marker, cmd_options, vcr_cfg):
testdir.makepyfile(
"""
import socket
import pytest
import requests
{vcr_cfg}
{marker}
def test_allowed(httpbin):
response = requests.get(httpbin.url + "/ip")
Expand All @@ -253,7 +261,8 @@ def test_blocked():
assert socket.socket.connect.__name__ == "network_guard"
assert socket.socket.connect_ex.__name__ == "network_guard"
""".format(
marker=marker
marker=marker,
vcr_cfg=vcr_cfg,
)
)

Expand Down

0 comments on commit ba4d3bc

Please sign in to comment.