From 07f4da2f2306a4e3dbce29e135ea33ab92b4b21d Mon Sep 17 00:00:00 2001 From: inexcode Date: Sat, 20 Aug 2022 22:48:44 +0400 Subject: [PATCH] Reorganize tests --- tests/test_block_device_utils.py | 12 +- .../{_test_system.py => test_system.py} | 115 +++++++++--------- tests/test_graphql/test_system/turned_on.json | 4 +- tests/test_jobs.py | 18 +++ tests/test_rest_endpoints/data/jobs.json | 1 + tests/test_rest_endpoints/data/tokens.json | 14 +++ .../services/data/tokens.json | 0 .../services/test_bitwarden.py | 0 .../test_bitwarden/enable_undefined.json | 0 .../services/test_bitwarden/turned_off.json | 0 .../services/test_bitwarden/turned_on.json | 0 .../services/test_bitwarden/undefined.json | 0 .../services/test_gitea.py | 0 .../services/test_gitea/enable_undefined.json | 0 .../services/test_gitea/turned_off.json | 0 .../services/test_gitea/turned_on.json | 0 .../services/test_gitea/undefined.json | 0 .../services/test_mailserver.py | 0 .../services/test_nextcloud.py | 0 .../test_nextcloud/enable_undefined.json | 0 .../services/test_nextcloud/turned_off.json | 0 .../services/test_nextcloud/turned_on.json | 0 .../services/test_nextcloud/undefined.json | 0 .../services/test_ocserv.py | 0 .../test_ocserv/enable_undefined.json | 0 .../services/test_ocserv/turned_off.json | 0 .../services/test_ocserv/turned_on.json | 0 .../services/test_ocserv/undefined.json | 0 .../services/test_pleroma.py | 0 .../test_pleroma/enable_undefined.json | 0 .../services/test_pleroma/turned_off.json | 0 .../services/test_pleroma/turned_on.json | 0 .../services/test_pleroma/undefined.json | 0 .../services/test_restic.py | 0 .../services/test_restic/no_values.json | 0 .../services/test_restic/some_values.json | 0 .../services/test_restic/undefined.json | 0 .../services/test_services.py | 70 ++++++----- .../services/test_ssh.py | 0 .../services/test_ssh/all_off.json | 0 .../test_ssh/root_and_admin_have_keys.json | 0 .../services/test_ssh/some_users.json | 0 .../services/test_ssh/turned_off.json | 0 .../services/test_ssh/turned_on.json | 0 .../services/test_ssh/undefined.json | 0 .../services/test_ssh/undefined_values.json | 0 tests/{ => test_rest_endpoints}/test_auth.py | 4 +- .../{ => test_rest_endpoints}/test_system.py | 0 .../test_system/domain | 0 .../test_system/no_values.json | 0 .../test_system/turned_off.json | 0 .../test_system/turned_on.json | 0 .../test_system/undefined.json | 0 tests/{ => test_rest_endpoints}/test_users.py | 0 .../test_users/no_users.json | 0 .../test_users/one_user.json | 0 .../test_users/some_users.json | 0 .../test_users/undefined.json | 0 58 files changed, 143 insertions(+), 95 deletions(-) rename tests/test_graphql/{_test_system.py => test_system.py} (89%) create mode 100644 tests/test_rest_endpoints/data/jobs.json create mode 100644 tests/test_rest_endpoints/data/tokens.json rename tests/{ => test_rest_endpoints}/services/data/tokens.json (100%) rename tests/{ => test_rest_endpoints}/services/test_bitwarden.py (100%) rename tests/{ => test_rest_endpoints}/services/test_bitwarden/enable_undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_bitwarden/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_bitwarden/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_bitwarden/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_gitea.py (100%) rename tests/{ => test_rest_endpoints}/services/test_gitea/enable_undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_gitea/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_gitea/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_gitea/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_mailserver.py (100%) rename tests/{ => test_rest_endpoints}/services/test_nextcloud.py (100%) rename tests/{ => test_rest_endpoints}/services/test_nextcloud/enable_undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_nextcloud/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_nextcloud/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_nextcloud/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ocserv.py (100%) rename tests/{ => test_rest_endpoints}/services/test_ocserv/enable_undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ocserv/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ocserv/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ocserv/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_pleroma.py (100%) rename tests/{ => test_rest_endpoints}/services/test_pleroma/enable_undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_pleroma/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_pleroma/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_pleroma/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_restic.py (100%) rename tests/{ => test_rest_endpoints}/services/test_restic/no_values.json (100%) rename tests/{ => test_rest_endpoints}/services/test_restic/some_values.json (100%) rename tests/{ => test_rest_endpoints}/services/test_restic/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_services.py (75%) rename tests/{ => test_rest_endpoints}/services/test_ssh.py (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/all_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/root_and_admin_have_keys.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/some_users.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/undefined.json (100%) rename tests/{ => test_rest_endpoints}/services/test_ssh/undefined_values.json (100%) rename tests/{ => test_rest_endpoints}/test_auth.py (99%) rename tests/{ => test_rest_endpoints}/test_system.py (100%) rename tests/{ => test_rest_endpoints}/test_system/domain (100%) rename tests/{ => test_rest_endpoints}/test_system/no_values.json (100%) rename tests/{ => test_rest_endpoints}/test_system/turned_off.json (100%) rename tests/{ => test_rest_endpoints}/test_system/turned_on.json (100%) rename tests/{ => test_rest_endpoints}/test_system/undefined.json (100%) rename tests/{ => test_rest_endpoints}/test_users.py (100%) rename tests/{ => test_rest_endpoints}/test_users/no_users.json (100%) rename tests/{ => test_rest_endpoints}/test_users/one_user.json (100%) rename tests/{ => test_rest_endpoints}/test_users/some_users.json (100%) rename tests/{ => test_rest_endpoints}/test_users/undefined.json (100%) diff --git a/tests/test_block_device_utils.py b/tests/test_block_device_utils.py index a22dad0..2676e6c 100644 --- a/tests/test_block_device_utils.py +++ b/tests/test_block_device_utils.py @@ -186,7 +186,9 @@ def resize_block_mock(mocker): return mock -def test_call_resize_from_block_device(lsblk_singular_mock, resize_block_mock, authorized_client): +def test_call_resize_from_block_device( + lsblk_singular_mock, resize_block_mock, authorized_client +): block_device = BlockDevice(json.loads(VOLUME_LSBLK_OUTPUT)["blockdevices"][0]) block_device.resize() assert resize_block_mock.call_count == 1 @@ -223,7 +225,9 @@ def test_get_stats_from_block_device(lsblk_singular_mock, authorized_client): ] -def test_mount_block_device(lsblk_singular_mock, only_root_in_userdata, authorized_client): +def test_mount_block_device( + lsblk_singular_mock, only_root_in_userdata, authorized_client +): block_device = BlockDevice(json.loads(SINGLE_LSBLK_OUTPUT)["blockdevices"][0]) result = block_device.mount() assert result is False @@ -270,7 +274,9 @@ def test_mount_block_device_when_undefined( ) -def test_unmount_block_device(lsblk_singular_mock, only_root_in_userdata, authorized_client): +def test_unmount_block_device( + lsblk_singular_mock, only_root_in_userdata, authorized_client +): block_device = BlockDevice(json.loads(SINGLE_LSBLK_OUTPUT)["blockdevices"][0]) result = block_device.unmount() assert result is True diff --git a/tests/test_graphql/_test_system.py b/tests/test_graphql/test_system.py similarity index 89% rename from tests/test_graphql/_test_system.py rename to tests/test_graphql/test_system.py index 738f1f4..fd4937d 100644 --- a/tests/test_graphql/_test_system.py +++ b/tests/test_graphql/test_system.py @@ -124,6 +124,7 @@ def mock_dkim_key(mocker): autospec=True, return_value="I am a DKIM key", ) + return mock API_PYTHON_VERSION_INFO = """ @@ -137,7 +138,7 @@ def test_graphql_get_python_version_wrong_auth( wrong_auth_client, mock_subprocess_check_output ): """Test wrong auth""" - response = wrong_auth_client.get( + response = wrong_auth_client.post( "/graphql", json={ "query": generate_system_query([API_PYTHON_VERSION_INFO]), @@ -149,7 +150,7 @@ def test_graphql_get_python_version_wrong_auth( def test_graphql_get_python_version(authorized_client, mock_subprocess_check_output): """Test get python version""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ "query": generate_system_query([API_PYTHON_VERSION_INFO]), @@ -173,7 +174,7 @@ def test_graphql_get_system_version_unauthorized( wrong_auth_client, mock_subprocess_check_output ): """Test wrong auth""" - response = wrong_auth_client.get( + response = wrong_auth_client.post( "/graphql", json={ "query": generate_system_query([API_SYSTEM_VERSION_INFO]), @@ -188,7 +189,7 @@ def test_graphql_get_system_version_unauthorized( def test_graphql_get_system_version(authorized_client, mock_subprocess_check_output): """Test get system version""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ "query": generate_system_query([API_SYSTEM_VERSION_INFO]), @@ -209,7 +210,7 @@ domainInfo { hostname provider requiredDnsRecords { - type + recordType name content ttl @@ -219,14 +220,14 @@ domainInfo { """ -def dns_record(type="A", name="test.tld", content=None, ttl=3600, priority=None): +def dns_record(record_type="A", name="test-domain.tld", content=None, ttl=3600, priority=None): if content is None: - if type == "A": + if record_type == "A": content = "157.90.247.192" - elif type == "AAAA": + elif record_type == "AAAA": content = "fe80::9400:ff:fef1:34ae" return { - "type": type, + "recordType": record_type, "name": name, "content": content, "ttl": ttl, @@ -237,7 +238,7 @@ def dns_record(type="A", name="test.tld", content=None, ttl=3600, priority=None) def is_dns_record_in_array(records, dns_record) -> bool: for record in records: if ( - record["type"] == dns_record["type"] + record["recordType"] == dns_record["recordType"] and record["name"] == dns_record["name"] and record["content"] == dns_record["content"] and record["ttl"] == dns_record["ttl"] @@ -248,10 +249,10 @@ def is_dns_record_in_array(records, dns_record) -> bool: def test_graphql_get_domain( - authorized_client, domain_file, mock_get_ip4, mock_get_ip6, turned_on + authorized_client, domain_file, mock_get_ip4, mock_get_ip6, turned_on, mock_dkim_key ): """Test get domain""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ "query": generate_system_query([API_GET_DOMAIN_INFO]), @@ -263,53 +264,53 @@ def test_graphql_get_domain( assert ( response.json()["data"]["system"]["domainInfo"]["hostname"] == "test-instance" ) - assert response.json()["data"]["system"]["domainInfo"]["provider"] == "HETZNER" + assert response.json()["data"]["system"]["domainInfo"]["provider"] == "CLOUDFLARE" dns_records = response.json()["data"]["system"]["domainInfo"]["requiredDnsRecords"] assert is_dns_record_in_array(dns_records, dns_record()) - assert is_dns_record_in_array(dns_records, dns_record(type="AAAA")) - assert is_dns_record_in_array(dns_records, dns_record(name="api.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(record_type="AAAA")) + assert is_dns_record_in_array(dns_records, dns_record(name="api")) assert is_dns_record_in_array( - dns_records, dns_record(name="api.test.tld", type="AAAA") + dns_records, dns_record(name="api", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="cloud.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="cloud")) assert is_dns_record_in_array( - dns_records, dns_record(name="cloud.test.tld", type="AAAA") + dns_records, dns_record(name="cloud", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="git.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="git")) assert is_dns_record_in_array( - dns_records, dns_record(name="git.test.tld", type="AAAA") + dns_records, dns_record(name="git", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="meet.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="meet")) assert is_dns_record_in_array( - dns_records, dns_record(name="meet.test.tld", type="AAAA") + dns_records, dns_record(name="meet", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="password.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="password")) assert is_dns_record_in_array( - dns_records, dns_record(name="password.test.tld", type="AAAA") + dns_records, dns_record(name="password", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="social.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="social")) assert is_dns_record_in_array( - dns_records, dns_record(name="social.test.tld", type="AAAA") + dns_records, dns_record(name="social", record_type="AAAA") ) - assert is_dns_record_in_array(dns_records, dns_record(name="vpn.test.tld")) + assert is_dns_record_in_array(dns_records, dns_record(name="vpn")) assert is_dns_record_in_array( - dns_records, dns_record(name="vpn.test.tld", type="AAAA") + dns_records, dns_record(name="vpn", record_type="AAAA") ) assert is_dns_record_in_array( dns_records, - dns_record(name="test.tld", type="MX", content="test.tld", priority=10), + dns_record(name="test-domain.tld", record_type="MX", content="test-domain.tld", priority=10), ) assert is_dns_record_in_array( dns_records, dns_record( - name="_dmarc.test.tld", type="TXT", content="v=DMARC1; p=none", ttl=18000 + name="_dmarc", record_type="TXT", content="v=DMARC1; p=none", ttl=18000 ), ) assert is_dns_record_in_array( dns_records, dns_record( - name="test.tld", - type="TXT", + name="test-domain.tld", + record_type="TXT", content="v=spf1 a mx ip4:157.90.247.192 -all", ttl=18000, ), @@ -317,8 +318,8 @@ def test_graphql_get_domain( assert is_dns_record_in_array( dns_records, dns_record( - name="selector._domainkey.test.tld", - type="TXT", + name="selector._domainkey", + record_type="TXT", content="I am a DKIM key", ttl=18000, ), @@ -334,7 +335,7 @@ settings { def test_graphql_get_timezone_unauthorized(client, turned_on): """Test get timezone without auth""" - response = client.get( + response = client.post( "/graphql", json={ "query": generate_system_query([API_GET_TIMEZONE]), @@ -346,7 +347,7 @@ def test_graphql_get_timezone_unauthorized(client, turned_on): def test_graphql_get_timezone(authorized_client, turned_on): """Test get timezone""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ "query": generate_system_query([API_GET_TIMEZONE]), @@ -359,7 +360,7 @@ def test_graphql_get_timezone(authorized_client, turned_on): def test_graphql_get_timezone_on_undefined(authorized_client, undefined_config): """Test get timezone when none is defined in config""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ "query": generate_system_query([API_GET_TIMEZONE]), @@ -484,7 +485,7 @@ def test_graphql_change_timezone_with_invalid_timezone(authorized_client, turned API_GET_AUTO_UPGRADE_SETTINGS_QUERY = """ settings { autoUpgrade { - enableAutoUpgrade + enable allowReboot } } @@ -493,10 +494,10 @@ settings { def test_graphql_get_auto_upgrade_unauthorized(client, turned_on): """Test get auto upgrade settings without auth""" - response = client.get( + response = client.post( "/graphql", json={ - "query": API_GET_AUTO_UPGRADE_SETTINGS_QUERY, + "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) assert response.status_code == 200 @@ -505,66 +506,66 @@ def test_graphql_get_auto_upgrade_unauthorized(client, turned_on): def test_graphql_get_auto_upgrade(authorized_client, turned_on): """Test get auto upgrade settings""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ - "query": API_GET_AUTO_UPGRADE_SETTINGS_QUERY, + "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) assert response.status_code == 200 assert response.json().get("data") is not None assert ( - response.json()["data"]["settings"]["autoUpgrade"]["enableAutoUpgrade"] is True + response.json()["data"]["system"]["settings"]["autoUpgrade"]["enable"] is True ) - assert response.json()["data"]["settings"]["autoUpgrade"]["allowReboot"] is True + assert response.json()["data"]["system"]["settings"]["autoUpgrade"]["allowReboot"] is True def test_graphql_get_auto_upgrade_on_undefined(authorized_client, undefined_config): """Test get auto upgrade settings when none is defined in config""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ - "query": API_GET_AUTO_UPGRADE_SETTINGS_QUERY, + "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) assert response.status_code == 200 assert response.json().get("data") is not None assert ( - response.json()["data"]["settings"]["autoUpgrade"]["enableAutoUpgrade"] is True + response.json()["data"]["system"]["settings"]["autoUpgrade"]["enable"] is True ) - assert response.json()["data"]["settings"]["autoUpgrade"]["allowReboot"] is False + assert response.json()["data"]["system"]["settings"]["autoUpgrade"]["allowReboot"] is False def test_graphql_get_auto_upgrade_without_vlaues(authorized_client, no_values): """Test get auto upgrade settings without values""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ - "query": API_GET_AUTO_UPGRADE_SETTINGS_QUERY, + "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) assert response.status_code == 200 assert response.json().get("data") is not None assert ( - response.json()["data"]["settings"]["autoUpgrade"]["enableAutoUpgrade"] is True + response.json()["data"]["system"]["settings"]["autoUpgrade"]["enable"] is True ) - assert response.json()["data"]["settings"]["autoUpgrade"]["allowReboot"] is False + assert response.json()["data"]["system"]["settings"]["autoUpgrade"]["allowReboot"] is False def test_graphql_get_auto_upgrade_turned_off(authorized_client, turned_off): """Test get auto upgrade settings when turned off""" - response = authorized_client.get( + response = authorized_client.post( "/graphql", json={ - "query": API_GET_AUTO_UPGRADE_SETTINGS_QUERY, + "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) assert response.status_code == 200 assert response.json().get("data") is not None assert ( - response.json()["data"]["settings"]["autoUpgrade"]["enableAutoUpgrade"] is False + response.json()["data"]["system"]["settings"]["autoUpgrade"]["enable"] is False ) - assert response.json()["data"]["settings"]["autoUpgrade"]["allowReboot"] is False + assert response.json()["data"]["system"]["settings"]["autoUpgrade"]["allowReboot"] is False API_CHANGE_AUTO_UPGRADE_SETTINGS = """ @@ -805,7 +806,7 @@ def test_graphql_change_auto_upgrade_with_empty_input(authorized_client, turned_ API_PULL_SYSTEM_CONFIGURATION_MUTATION = """ -mutation testPullSystemConfiguration() { +mutation testPullSystemConfiguration { pullRepositoryChanges { success message diff --git a/tests/test_graphql/test_system/turned_on.json b/tests/test_graphql/test_system/turned_on.json index 337e47f..99a023c 100644 --- a/tests/test_graphql/test_system/turned_on.json +++ b/tests/test_graphql/test_system/turned_on.json @@ -33,7 +33,7 @@ }, "username": "tester", "gitea": { - "enable": false + "enable": true }, "ocserv": { "enable": true @@ -49,4 +49,4 @@ "sshKeys": [ "ssh-rsa KEY test@pc" ] -} \ No newline at end of file +} diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 2732335..87f1386 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -30,3 +30,21 @@ def test_jobs(authorized_client, jobs_file, shared_datadir): ) assert jobs.get_jobs() == [test_job] + + +@pytest.fixture +def mock_subprocess_run(mocker): + mock = mocker.patch("subprocess.run", autospec=True) + return mock + + +@pytest.fixture +def mock_shutil_move(mocker): + mock = mocker.patch("shutil.move", autospec=True) + return mock + + +@pytest.fixture +def mock_shutil_chown(mocker): + mock = mocker.patch("shutil.chown", autospec=True) + return mock diff --git a/tests/test_rest_endpoints/data/jobs.json b/tests/test_rest_endpoints/data/jobs.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/test_rest_endpoints/data/jobs.json @@ -0,0 +1 @@ +{} diff --git a/tests/test_rest_endpoints/data/tokens.json b/tests/test_rest_endpoints/data/tokens.json new file mode 100644 index 0000000..9be9d02 --- /dev/null +++ b/tests/test_rest_endpoints/data/tokens.json @@ -0,0 +1,14 @@ +{ + "tokens": [ + { + "token": "TEST_TOKEN", + "name": "test_token", + "date": "2022-01-14 08:31:10.789314" + }, + { + "token": "TEST_TOKEN2", + "name": "test_token2", + "date": "2022-01-14 08:31:10.789314" + } + ] +} \ No newline at end of file diff --git a/tests/services/data/tokens.json b/tests/test_rest_endpoints/services/data/tokens.json similarity index 100% rename from tests/services/data/tokens.json rename to tests/test_rest_endpoints/services/data/tokens.json diff --git a/tests/services/test_bitwarden.py b/tests/test_rest_endpoints/services/test_bitwarden.py similarity index 100% rename from tests/services/test_bitwarden.py rename to tests/test_rest_endpoints/services/test_bitwarden.py diff --git a/tests/services/test_bitwarden/enable_undefined.json b/tests/test_rest_endpoints/services/test_bitwarden/enable_undefined.json similarity index 100% rename from tests/services/test_bitwarden/enable_undefined.json rename to tests/test_rest_endpoints/services/test_bitwarden/enable_undefined.json diff --git a/tests/services/test_bitwarden/turned_off.json b/tests/test_rest_endpoints/services/test_bitwarden/turned_off.json similarity index 100% rename from tests/services/test_bitwarden/turned_off.json rename to tests/test_rest_endpoints/services/test_bitwarden/turned_off.json diff --git a/tests/services/test_bitwarden/turned_on.json b/tests/test_rest_endpoints/services/test_bitwarden/turned_on.json similarity index 100% rename from tests/services/test_bitwarden/turned_on.json rename to tests/test_rest_endpoints/services/test_bitwarden/turned_on.json diff --git a/tests/services/test_bitwarden/undefined.json b/tests/test_rest_endpoints/services/test_bitwarden/undefined.json similarity index 100% rename from tests/services/test_bitwarden/undefined.json rename to tests/test_rest_endpoints/services/test_bitwarden/undefined.json diff --git a/tests/services/test_gitea.py b/tests/test_rest_endpoints/services/test_gitea.py similarity index 100% rename from tests/services/test_gitea.py rename to tests/test_rest_endpoints/services/test_gitea.py diff --git a/tests/services/test_gitea/enable_undefined.json b/tests/test_rest_endpoints/services/test_gitea/enable_undefined.json similarity index 100% rename from tests/services/test_gitea/enable_undefined.json rename to tests/test_rest_endpoints/services/test_gitea/enable_undefined.json diff --git a/tests/services/test_gitea/turned_off.json b/tests/test_rest_endpoints/services/test_gitea/turned_off.json similarity index 100% rename from tests/services/test_gitea/turned_off.json rename to tests/test_rest_endpoints/services/test_gitea/turned_off.json diff --git a/tests/services/test_gitea/turned_on.json b/tests/test_rest_endpoints/services/test_gitea/turned_on.json similarity index 100% rename from tests/services/test_gitea/turned_on.json rename to tests/test_rest_endpoints/services/test_gitea/turned_on.json diff --git a/tests/services/test_gitea/undefined.json b/tests/test_rest_endpoints/services/test_gitea/undefined.json similarity index 100% rename from tests/services/test_gitea/undefined.json rename to tests/test_rest_endpoints/services/test_gitea/undefined.json diff --git a/tests/services/test_mailserver.py b/tests/test_rest_endpoints/services/test_mailserver.py similarity index 100% rename from tests/services/test_mailserver.py rename to tests/test_rest_endpoints/services/test_mailserver.py diff --git a/tests/services/test_nextcloud.py b/tests/test_rest_endpoints/services/test_nextcloud.py similarity index 100% rename from tests/services/test_nextcloud.py rename to tests/test_rest_endpoints/services/test_nextcloud.py diff --git a/tests/services/test_nextcloud/enable_undefined.json b/tests/test_rest_endpoints/services/test_nextcloud/enable_undefined.json similarity index 100% rename from tests/services/test_nextcloud/enable_undefined.json rename to tests/test_rest_endpoints/services/test_nextcloud/enable_undefined.json diff --git a/tests/services/test_nextcloud/turned_off.json b/tests/test_rest_endpoints/services/test_nextcloud/turned_off.json similarity index 100% rename from tests/services/test_nextcloud/turned_off.json rename to tests/test_rest_endpoints/services/test_nextcloud/turned_off.json diff --git a/tests/services/test_nextcloud/turned_on.json b/tests/test_rest_endpoints/services/test_nextcloud/turned_on.json similarity index 100% rename from tests/services/test_nextcloud/turned_on.json rename to tests/test_rest_endpoints/services/test_nextcloud/turned_on.json diff --git a/tests/services/test_nextcloud/undefined.json b/tests/test_rest_endpoints/services/test_nextcloud/undefined.json similarity index 100% rename from tests/services/test_nextcloud/undefined.json rename to tests/test_rest_endpoints/services/test_nextcloud/undefined.json diff --git a/tests/services/test_ocserv.py b/tests/test_rest_endpoints/services/test_ocserv.py similarity index 100% rename from tests/services/test_ocserv.py rename to tests/test_rest_endpoints/services/test_ocserv.py diff --git a/tests/services/test_ocserv/enable_undefined.json b/tests/test_rest_endpoints/services/test_ocserv/enable_undefined.json similarity index 100% rename from tests/services/test_ocserv/enable_undefined.json rename to tests/test_rest_endpoints/services/test_ocserv/enable_undefined.json diff --git a/tests/services/test_ocserv/turned_off.json b/tests/test_rest_endpoints/services/test_ocserv/turned_off.json similarity index 100% rename from tests/services/test_ocserv/turned_off.json rename to tests/test_rest_endpoints/services/test_ocserv/turned_off.json diff --git a/tests/services/test_ocserv/turned_on.json b/tests/test_rest_endpoints/services/test_ocserv/turned_on.json similarity index 100% rename from tests/services/test_ocserv/turned_on.json rename to tests/test_rest_endpoints/services/test_ocserv/turned_on.json diff --git a/tests/services/test_ocserv/undefined.json b/tests/test_rest_endpoints/services/test_ocserv/undefined.json similarity index 100% rename from tests/services/test_ocserv/undefined.json rename to tests/test_rest_endpoints/services/test_ocserv/undefined.json diff --git a/tests/services/test_pleroma.py b/tests/test_rest_endpoints/services/test_pleroma.py similarity index 100% rename from tests/services/test_pleroma.py rename to tests/test_rest_endpoints/services/test_pleroma.py diff --git a/tests/services/test_pleroma/enable_undefined.json b/tests/test_rest_endpoints/services/test_pleroma/enable_undefined.json similarity index 100% rename from tests/services/test_pleroma/enable_undefined.json rename to tests/test_rest_endpoints/services/test_pleroma/enable_undefined.json diff --git a/tests/services/test_pleroma/turned_off.json b/tests/test_rest_endpoints/services/test_pleroma/turned_off.json similarity index 100% rename from tests/services/test_pleroma/turned_off.json rename to tests/test_rest_endpoints/services/test_pleroma/turned_off.json diff --git a/tests/services/test_pleroma/turned_on.json b/tests/test_rest_endpoints/services/test_pleroma/turned_on.json similarity index 100% rename from tests/services/test_pleroma/turned_on.json rename to tests/test_rest_endpoints/services/test_pleroma/turned_on.json diff --git a/tests/services/test_pleroma/undefined.json b/tests/test_rest_endpoints/services/test_pleroma/undefined.json similarity index 100% rename from tests/services/test_pleroma/undefined.json rename to tests/test_rest_endpoints/services/test_pleroma/undefined.json diff --git a/tests/services/test_restic.py b/tests/test_rest_endpoints/services/test_restic.py similarity index 100% rename from tests/services/test_restic.py rename to tests/test_rest_endpoints/services/test_restic.py diff --git a/tests/services/test_restic/no_values.json b/tests/test_rest_endpoints/services/test_restic/no_values.json similarity index 100% rename from tests/services/test_restic/no_values.json rename to tests/test_rest_endpoints/services/test_restic/no_values.json diff --git a/tests/services/test_restic/some_values.json b/tests/test_rest_endpoints/services/test_restic/some_values.json similarity index 100% rename from tests/services/test_restic/some_values.json rename to tests/test_rest_endpoints/services/test_restic/some_values.json diff --git a/tests/services/test_restic/undefined.json b/tests/test_rest_endpoints/services/test_restic/undefined.json similarity index 100% rename from tests/services/test_restic/undefined.json rename to tests/test_rest_endpoints/services/test_restic/undefined.json diff --git a/tests/services/test_services.py b/tests/test_rest_endpoints/services/test_services.py similarity index 75% rename from tests/services/test_services.py rename to tests/test_rest_endpoints/services/test_services.py index 03ae104..1108e8c 100644 --- a/tests/services/test_services.py +++ b/tests/test_rest_endpoints/services/test_services.py @@ -12,68 +12,78 @@ def call_args_asserts(mocked_object): assert mocked_object.call_count == 7 assert mocked_object.call_args_list[0][0][0] == [ "systemctl", - "status", + "show", "dovecot2.service", ] assert mocked_object.call_args_list[1][0][0] == [ "systemctl", - "status", + "show", "postfix.service", ] assert mocked_object.call_args_list[2][0][0] == [ "systemctl", - "status", + "show", "vaultwarden.service", ] assert mocked_object.call_args_list[3][0][0] == [ "systemctl", - "status", + "show", "gitea.service", ] assert mocked_object.call_args_list[4][0][0] == [ "systemctl", - "status", + "show", "phpfpm-nextcloud.service", ] assert mocked_object.call_args_list[5][0][0] == [ "systemctl", - "status", + "show", "ocserv.service", ] assert mocked_object.call_args_list[6][0][0] == [ "systemctl", - "status", + "show", "pleroma.service", ] -class ProcessMock: - """Mock subprocess.Popen""" +SUCCESSFUL_STATUS = b""" +Type=oneshot +ExitType=main +Restart=no +NotifyAccess=none +RestartUSec=100ms +LoadState=loaded +ActiveState=active +FreezerState=running +SubState=exited +""" - def __init__(self, args, **kwargs): - self.args = args - self.kwargs = kwargs - - def communicate(): - return (b"", None) - - returncode = 0 - - -class BrokenServiceMock(ProcessMock): - returncode = 3 +FAILED_STATUS = b""" +Type=oneshot +ExitType=main +Restart=no +NotifyAccess=none +RestartUSec=100ms +LoadState=loaded +ActiveState=failed +FreezerState=running +SubState=exited +""" @pytest.fixture def mock_subproccess_popen(mocker): - mock = mocker.patch("subprocess.Popen", autospec=True, return_value=ProcessMock) + mock = mocker.patch( + "subprocess.check_output", autospec=True, return_value=SUCCESSFUL_STATUS + ) return mock @pytest.fixture def mock_broken_service(mocker): mock = mocker.patch( - "subprocess.Popen", autospec=True, return_value=BrokenServiceMock + "subprocess.check_output", autospec=True, return_value=FAILED_STATUS ) return mock @@ -116,13 +126,13 @@ def test_no_dkim_key(authorized_client, mock_broken_service): response = authorized_client.get("/services/status") assert response.status_code == 200 assert response.json() == { - "imap": 3, - "smtp": 3, + "imap": 1, + "smtp": 1, "http": 0, - "bitwarden": 3, - "gitea": 3, - "nextcloud": 3, - "ocserv": 3, - "pleroma": 3, + "bitwarden": 1, + "gitea": 1, + "nextcloud": 1, + "ocserv": 1, + "pleroma": 1, } call_args_asserts(mock_broken_service) diff --git a/tests/services/test_ssh.py b/tests/test_rest_endpoints/services/test_ssh.py similarity index 100% rename from tests/services/test_ssh.py rename to tests/test_rest_endpoints/services/test_ssh.py diff --git a/tests/services/test_ssh/all_off.json b/tests/test_rest_endpoints/services/test_ssh/all_off.json similarity index 100% rename from tests/services/test_ssh/all_off.json rename to tests/test_rest_endpoints/services/test_ssh/all_off.json diff --git a/tests/services/test_ssh/root_and_admin_have_keys.json b/tests/test_rest_endpoints/services/test_ssh/root_and_admin_have_keys.json similarity index 100% rename from tests/services/test_ssh/root_and_admin_have_keys.json rename to tests/test_rest_endpoints/services/test_ssh/root_and_admin_have_keys.json diff --git a/tests/services/test_ssh/some_users.json b/tests/test_rest_endpoints/services/test_ssh/some_users.json similarity index 100% rename from tests/services/test_ssh/some_users.json rename to tests/test_rest_endpoints/services/test_ssh/some_users.json diff --git a/tests/services/test_ssh/turned_off.json b/tests/test_rest_endpoints/services/test_ssh/turned_off.json similarity index 100% rename from tests/services/test_ssh/turned_off.json rename to tests/test_rest_endpoints/services/test_ssh/turned_off.json diff --git a/tests/services/test_ssh/turned_on.json b/tests/test_rest_endpoints/services/test_ssh/turned_on.json similarity index 100% rename from tests/services/test_ssh/turned_on.json rename to tests/test_rest_endpoints/services/test_ssh/turned_on.json diff --git a/tests/services/test_ssh/undefined.json b/tests/test_rest_endpoints/services/test_ssh/undefined.json similarity index 100% rename from tests/services/test_ssh/undefined.json rename to tests/test_rest_endpoints/services/test_ssh/undefined.json diff --git a/tests/services/test_ssh/undefined_values.json b/tests/test_rest_endpoints/services/test_ssh/undefined_values.json similarity index 100% rename from tests/services/test_ssh/undefined_values.json rename to tests/test_rest_endpoints/services/test_ssh/undefined_values.json diff --git a/tests/test_auth.py b/tests/test_rest_endpoints/test_auth.py similarity index 99% rename from tests/test_auth.py rename to tests/test_rest_endpoints/test_auth.py index 5430e3a..1083be5 100644 --- a/tests/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -2,12 +2,10 @@ # pylint: disable=unused-argument # pylint: disable=missing-function-docstring import datetime -import json -import re import pytest from mnemonic import Mnemonic -from .common import read_json, write_json +from tests.common import read_json, write_json TOKENS_FILE_CONTETS = { diff --git a/tests/test_system.py b/tests/test_rest_endpoints/test_system.py similarity index 100% rename from tests/test_system.py rename to tests/test_rest_endpoints/test_system.py diff --git a/tests/test_system/domain b/tests/test_rest_endpoints/test_system/domain similarity index 100% rename from tests/test_system/domain rename to tests/test_rest_endpoints/test_system/domain diff --git a/tests/test_system/no_values.json b/tests/test_rest_endpoints/test_system/no_values.json similarity index 100% rename from tests/test_system/no_values.json rename to tests/test_rest_endpoints/test_system/no_values.json diff --git a/tests/test_system/turned_off.json b/tests/test_rest_endpoints/test_system/turned_off.json similarity index 100% rename from tests/test_system/turned_off.json rename to tests/test_rest_endpoints/test_system/turned_off.json diff --git a/tests/test_system/turned_on.json b/tests/test_rest_endpoints/test_system/turned_on.json similarity index 100% rename from tests/test_system/turned_on.json rename to tests/test_rest_endpoints/test_system/turned_on.json diff --git a/tests/test_system/undefined.json b/tests/test_rest_endpoints/test_system/undefined.json similarity index 100% rename from tests/test_system/undefined.json rename to tests/test_rest_endpoints/test_system/undefined.json diff --git a/tests/test_users.py b/tests/test_rest_endpoints/test_users.py similarity index 100% rename from tests/test_users.py rename to tests/test_rest_endpoints/test_users.py diff --git a/tests/test_users/no_users.json b/tests/test_rest_endpoints/test_users/no_users.json similarity index 100% rename from tests/test_users/no_users.json rename to tests/test_rest_endpoints/test_users/no_users.json diff --git a/tests/test_users/one_user.json b/tests/test_rest_endpoints/test_users/one_user.json similarity index 100% rename from tests/test_users/one_user.json rename to tests/test_rest_endpoints/test_users/one_user.json diff --git a/tests/test_users/some_users.json b/tests/test_rest_endpoints/test_users/some_users.json similarity index 100% rename from tests/test_users/some_users.json rename to tests/test_rest_endpoints/test_users/some_users.json diff --git a/tests/test_users/undefined.json b/tests/test_rest_endpoints/test_users/undefined.json similarity index 100% rename from tests/test_users/undefined.json rename to tests/test_rest_endpoints/test_users/undefined.json