selfprivacy-rest-api/tests/test_rest_endpoints/services/test_services.py

139 lines
3.2 KiB
Python
Raw Normal View History

import base64
import json
import pytest
def read_json(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)
2021-11-30 21:53:39 +00:00
def call_args_asserts(mocked_object):
2022-08-10 23:36:36 +00:00
assert mocked_object.call_count == 7
assert mocked_object.call_args_list[0][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"dovecot2.service",
]
assert mocked_object.call_args_list[1][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"postfix.service",
]
assert mocked_object.call_args_list[2][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
2022-05-26 15:40:07 +00:00
"vaultwarden.service",
]
2022-08-10 23:36:36 +00:00
assert mocked_object.call_args_list[3][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"gitea.service",
]
2022-08-10 23:36:36 +00:00
assert mocked_object.call_args_list[4][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"phpfpm-nextcloud.service",
]
2022-08-10 23:36:36 +00:00
assert mocked_object.call_args_list[5][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"ocserv.service",
]
2022-08-10 23:36:36 +00:00
assert mocked_object.call_args_list[6][0][0] == [
"systemctl",
2022-08-20 18:48:44 +00:00
"show",
"pleroma.service",
]
2021-11-30 21:53:39 +00:00
2022-08-20 18:48:44 +00:00
SUCCESSFUL_STATUS = b"""
Type=oneshot
ExitType=main
Restart=no
NotifyAccess=none
RestartUSec=100ms
LoadState=loaded
ActiveState=active
FreezerState=running
SubState=exited
"""
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):
2022-08-20 18:48:44 +00:00
mock = mocker.patch(
"subprocess.check_output", autospec=True, return_value=SUCCESSFUL_STATUS
)
return mock
@pytest.fixture
def mock_broken_service(mocker):
mock = mocker.patch(
2022-08-20 18:48:44 +00:00
"subprocess.check_output", autospec=True, return_value=FAILED_STATUS
)
return mock
###############################################################################
def test_unauthorized(client, mock_subproccess_popen):
"""Test unauthorized"""
response = client.get("/services/status")
assert response.status_code == 401
def test_illegal_methods(authorized_client, mock_subproccess_popen):
response = authorized_client.post("/services/status")
assert response.status_code == 405
response = authorized_client.put("/services/status")
assert response.status_code == 405
response = authorized_client.delete("/services/status")
assert response.status_code == 405
def test_dkim_key(authorized_client, mock_subproccess_popen):
response = authorized_client.get("/services/status")
assert response.status_code == 200
2022-08-10 23:36:36 +00:00
assert response.json() == {
"imap": 0,
"smtp": 0,
"http": 0,
"bitwarden": 0,
"gitea": 0,
"nextcloud": 0,
"ocserv": 0,
"pleroma": 0,
}
call_args_asserts(mock_subproccess_popen)
def test_no_dkim_key(authorized_client, mock_broken_service):
response = authorized_client.get("/services/status")
assert response.status_code == 200
2022-08-10 23:36:36 +00:00
assert response.json() == {
2022-08-20 18:48:44 +00:00
"imap": 1,
"smtp": 1,
2022-08-10 23:36:36 +00:00
"http": 0,
2022-08-20 18:48:44 +00:00
"bitwarden": 1,
"gitea": 1,
"nextcloud": 1,
"ocserv": 1,
"pleroma": 1,
}
call_args_asserts(mock_broken_service)