From 906e5976ea538772414f5a52474846bdb9c0ddbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cimander?= Date: Mon, 20 Jun 2022 19:45:57 +0200 Subject: [PATCH] feat: Allow block_network.allowed_hosts configuration via fixture * Allow configuration of `block_network.allowed_hosts` via vcr_config fixture. * black formatting * Extended README for `allowed_hosts` configuration. * Changelog * Update docs/changelog.rst Co-authored-by: Dmitry Dygalo * Changelog(reference) * Changelog(undo pycharm local cache change, narf) Co-authored-by: Dmitry Dygalo --- README.rst | 11 +++++++++++ docs/changelog.rst | 3 +++ src/pytest_recording/plugin.py | 12 ++++++++++-- tests/test_blocking_network.py | 19 ++++++++++++++----- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 01d8fe2..5245372 100644 --- a/README.rst +++ b/README.rst @@ -194,6 +194,17 @@ Or via command-line option: $ pytest --record-mode=once --block-network --allowed-hosts=httpbin.*,localhost tests/ + +Or via `vcr_config` fixture: + +.. code:: python + + import pytest + + @pytest.fixture(autouse=True) + def vcr_config(): + return {"allowed_hosts": ["httpbin.*"]} + Additional resources -------------------- diff --git a/docs/changelog.rst b/docs/changelog.rst index 4b12783..6b52e00 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,8 @@ Changelog `Unreleased`_ ------------- +- Allow ``block_network.allowed_hosts`` configuration via ``vcr_config`` fixture. `#82`_ + `0.12.0`_ - 2021-07-08 ---------------------- @@ -194,6 +196,7 @@ Added .. _0.3.0: https://github.com/kiwicom/pytest-recording/compare/v0.2.0...v0.3.0 .. _0.2.0: https://github.com/kiwicom/pytest-recording/compare/v0.1.0...v0.2.0 +.. _#82: https://github.com/kiwicom/pytest-recording/pull/82 .. _#69: https://github.com/kiwicom/pytest-recording/issues/69 .. _#68: https://github.com/kiwicom/pytest-recording/issues/68 .. _#64: https://github.com/kiwicom/pytest-recording/issues/64 diff --git a/src/pytest_recording/plugin.py b/src/pytest_recording/plugin.py index effa8a5..635bb6f 100644 --- a/src/pytest_recording/plugin.py +++ b/src/pytest_recording/plugin.py @@ -94,9 +94,18 @@ 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: @@ -104,7 +113,6 @@ def block_network(request: SubRequest, record_mode: str, vcr_markers: List[Mark] # - 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: diff --git a/tests/test_blocking_network.py b/tests/test_blocking_network.py index 204e455..b9b17c2 100644 --- a/tests/test_blocking_network.py +++ b/tests/test_blocking_network.py @@ -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") @@ -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, ) )