Skip to content

Commit

Permalink
feat: Allow block_network.allowed_hosts configuration via fixture
Browse files Browse the repository at this point in the history
* 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 <Stranger6667@users.noreply.github.com>

* Changelog(reference)

* Changelog(undo pycharm local cache change, narf)

Co-authored-by: Dmitry Dygalo <Stranger6667@users.noreply.github.com>
  • Loading branch information
AndreCimander and Stranger6667 authored Jun 20, 2022
1 parent e326783 commit 906e597
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------

Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog
`Unreleased`_
-------------

- Allow ``block_network.allowed_hosts`` configuration via ``vcr_config`` fixture. `#82`_

`0.12.0`_ - 2021-07-08
----------------------

Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/pytest_recording/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,25 @@ 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 906e597

Please sign in to comment.