mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Mock the settings rather than monkeypatching external modules
This commit is contained in:
parent
5a56a7f055
commit
b551a4dec4
3 changed files with 21 additions and 31 deletions
|
@ -21,34 +21,25 @@ def get_qt_app() -> Application:
|
||||||
|
|
||||||
def generate_isolated_updater(
|
def generate_isolated_updater(
|
||||||
tmp_path: Path,
|
tmp_path: Path,
|
||||||
monkeypatch: MonkeyPatch,
|
mocker: MockerFixture,
|
||||||
app_mocker: Optional[MockerFixture] = None,
|
mock_app: bool = False,
|
||||||
) -> UpdaterThread:
|
) -> UpdaterThread:
|
||||||
"""Generate an Updater class with its own settings."""
|
"""Generate an Updater class with its own settings."""
|
||||||
if app_mocker:
|
app = mocker.MagicMock() if mock_app else get_qt_app()
|
||||||
app = app_mocker.MagicMock()
|
|
||||||
else:
|
|
||||||
app = get_qt_app()
|
|
||||||
|
|
||||||
dummy = Dummy()
|
dummy = Dummy()
|
||||||
# XXX: We can monkey-patch global state without wrapping it in a context manager, or
|
mocker.patch("dangerzone.settings.get_config_dir", return_value=tmp_path)
|
||||||
# worrying that it will leak between tests, for two reasons:
|
|
||||||
#
|
|
||||||
# 1. Parallel tests in PyTest take place in different processes.
|
|
||||||
# 2. The monkeypatch fixture tears down the monkey-patch after each test ends.
|
|
||||||
monkeypatch.setattr(util, "get_config_dir", lambda: tmp_path)
|
|
||||||
dangerzone = DangerzoneGui(app, isolation_provider=dummy)
|
dangerzone = DangerzoneGui(app, isolation_provider=dummy)
|
||||||
updater = UpdaterThread(dangerzone)
|
updater = UpdaterThread(dangerzone)
|
||||||
return updater
|
return updater
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def updater(
|
def updater(tmp_path: Path, mocker: MockerFixture) -> UpdaterThread:
|
||||||
tmp_path: Path, monkeypatch: MonkeyPatch, mocker: MockerFixture
|
return generate_isolated_updater(tmp_path, mocker, mock_app=True)
|
||||||
) -> UpdaterThread:
|
|
||||||
return generate_isolated_updater(tmp_path, monkeypatch, mocker)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def qt_updater(tmp_path: Path, monkeypatch: MonkeyPatch) -> UpdaterThread:
|
def qt_updater(tmp_path: Path, mocker: MockerFixture) -> UpdaterThread:
|
||||||
return generate_isolated_updater(tmp_path, monkeypatch)
|
return generate_isolated_updater(tmp_path, mocker, mock_app=False)
|
||||||
|
|
|
@ -48,9 +48,7 @@ def test_default_updater_settings(updater: UpdaterThread) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_pre_0_4_2_settings(
|
def test_pre_0_4_2_settings(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
tmp_path: Path, monkeypatch: MonkeyPatch, mocker: MockerFixture
|
|
||||||
) -> None:
|
|
||||||
"""Check settings of installations prior to 0.4.2.
|
"""Check settings of installations prior to 0.4.2.
|
||||||
|
|
||||||
Check that installations that have been upgraded from a version < 0.4.2 to >= 0.4.2
|
Check that installations that have been upgraded from a version < 0.4.2 to >= 0.4.2
|
||||||
|
@ -58,7 +56,7 @@ def test_pre_0_4_2_settings(
|
||||||
in their settings.json file.
|
in their settings.json file.
|
||||||
"""
|
"""
|
||||||
save_settings(tmp_path, default_settings_0_4_1())
|
save_settings(tmp_path, default_settings_0_4_1())
|
||||||
updater = generate_isolated_updater(tmp_path, monkeypatch, mocker)
|
updater = generate_isolated_updater(tmp_path, mocker, mock_app=True)
|
||||||
assert (
|
assert (
|
||||||
updater.dangerzone.settings.get_updater_settings() == default_updater_settings()
|
updater.dangerzone.settings.get_updater_settings() == default_updater_settings()
|
||||||
)
|
)
|
||||||
|
@ -83,12 +81,10 @@ def test_post_0_4_2_settings(
|
||||||
# version is 0.4.3.
|
# version is 0.4.3.
|
||||||
expected_settings = default_updater_settings()
|
expected_settings = default_updater_settings()
|
||||||
expected_settings["updater_latest_version"] = "0.4.3"
|
expected_settings["updater_latest_version"] = "0.4.3"
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(settings, "get_version", lambda: "0.4.3")
|
||||||
settings, "get_version", lambda: expected_settings["updater_latest_version"]
|
|
||||||
)
|
|
||||||
|
|
||||||
# Ensure that the Settings class will correct the latest version field to 0.4.3.
|
# Ensure that the Settings class will correct the latest version field to 0.4.3.
|
||||||
updater = generate_isolated_updater(tmp_path, monkeypatch, mocker)
|
updater = generate_isolated_updater(tmp_path, mocker, mock_app=True)
|
||||||
assert updater.dangerzone.settings.get_updater_settings() == expected_settings
|
assert updater.dangerzone.settings.get_updater_settings() == expected_settings
|
||||||
|
|
||||||
# Simulate an updater check that found a newer Dangerzone version (e.g., 0.4.4).
|
# Simulate an updater check that found a newer Dangerzone version (e.g., 0.4.4).
|
||||||
|
@ -118,9 +114,7 @@ def test_linux_no_check(updater: UpdaterThread, monkeypatch: MonkeyPatch) -> Non
|
||||||
assert updater.dangerzone.settings.get_updater_settings() == expected_settings
|
assert updater.dangerzone.settings.get_updater_settings() == expected_settings
|
||||||
|
|
||||||
|
|
||||||
def test_user_prompts(
|
def test_user_prompts(updater: UpdaterThread, mocker: MockerFixture) -> None:
|
||||||
updater: UpdaterThread, monkeypatch: MonkeyPatch, mocker: MockerFixture
|
|
||||||
) -> None:
|
|
||||||
"""Test prompting users to ask them if they want to enable update checks."""
|
"""Test prompting users to ask them if they want to enable update checks."""
|
||||||
# First run
|
# First run
|
||||||
#
|
#
|
||||||
|
@ -370,8 +364,6 @@ def test_update_errors(
|
||||||
def test_update_check_prompt(
|
def test_update_check_prompt(
|
||||||
qtbot: QtBot,
|
qtbot: QtBot,
|
||||||
qt_updater: UpdaterThread,
|
qt_updater: UpdaterThread,
|
||||||
monkeypatch: MonkeyPatch,
|
|
||||||
mocker: MockerFixture,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the prompt to enable update checks works properly."""
|
"""Test that the prompt to enable update checks works properly."""
|
||||||
# Force Dangerzone to check immediately for updates
|
# Force Dangerzone to check immediately for updates
|
||||||
|
|
|
@ -21,6 +21,13 @@ def default_settings_0_4_1() -> dict:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def save_settings(tmp_path: Path, settings: dict) -> None:
|
||||||
|
"""Mimic the way Settings save a dictionary to a settings.json file."""
|
||||||
|
settings_filename = tmp_path / "settings.json"
|
||||||
|
with open(settings_filename, "w") as settings_file:
|
||||||
|
json.dump(settings, settings_file, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def test_no_settings_file_creates_new_one(
|
def test_no_settings_file_creates_new_one(
|
||||||
tmp_path: Path,
|
tmp_path: Path,
|
||||||
mocker: MockerFixture,
|
mocker: MockerFixture,
|
||||||
|
|
Loading…
Reference in a new issue