Merge pull request 'Fix pull system tests for non-nixos systems' (#10) from NaiJi/selfprivacy-rest-api:chdir-mock into master

Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/10
This commit is contained in:
Inex Code 2022-04-05 17:11:08 +03:00
commit 874acb1343

View file

@ -1,6 +1,9 @@
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
# pylint: disable=unused-argument # pylint: disable=unused-argument
# pylint: disable=missing-function-docstring
import json import json
import os
import pytest import pytest
from selfprivacy_api.utils import get_domain from selfprivacy_api.utils import get_domain
@ -76,6 +79,12 @@ def mock_subprocess_popen(mocker):
return mock return mock
@pytest.fixture
def mock_os_chdir(mocker):
mock = mocker.patch("os.chdir", autospec=True)
return mock
@pytest.fixture @pytest.fixture
def mock_broken_service(mocker): def mock_broken_service(mocker):
mock = mocker.patch( mock = mocker.patch(
@ -384,14 +393,22 @@ def test_pull_system_unauthorized(client, mock_subprocess_popen):
assert mock_subprocess_popen.call_count == 0 assert mock_subprocess_popen.call_count == 0
def test_pull_system(authorized_client, mock_subprocess_popen): def test_pull_system(authorized_client, mock_subprocess_popen, mock_os_chdir):
current_dir = os.getcwd()
response = authorized_client.get("/system/configuration/pull") response = authorized_client.get("/system/configuration/pull")
assert response.status_code == 200 assert response.status_code == 200
assert mock_subprocess_popen.call_count == 1 assert mock_subprocess_popen.call_count == 1
assert mock_subprocess_popen.call_args[0][0] == ["git", "pull"] assert mock_subprocess_popen.call_args[0][0] == ["git", "pull"]
assert mock_os_chdir.call_count == 2
assert mock_os_chdir.call_args_list[0][0][0] == "/etc/nixos"
assert mock_os_chdir.call_args_list[1][0][0] == current_dir
def test_pull_system_broken_repo(authorized_client, mock_broken_service): def test_pull_system_broken_repo(authorized_client, mock_broken_service, mock_os_chdir):
current_dir = os.getcwd()
response = authorized_client.get("/system/configuration/pull") response = authorized_client.get("/system/configuration/pull")
assert response.status_code == 500 assert response.status_code == 500
assert mock_broken_service.call_count == 1 assert mock_broken_service.call_count == 1
assert mock_os_chdir.call_count == 2
assert mock_os_chdir.call_args_list[0][0][0] == "/etc/nixos"
assert mock_os_chdir.call_args_list[1][0][0] == current_dir