selfprivacy-rest-api/tests/test_root_daemon.py

68 lines
1.6 KiB
Python
Raw Normal View History

2024-10-30 10:10:57 +00:00
import pytest
2024-12-11 13:36:32 +00:00
import os
import asyncio
import threading
import subprocess
2024-10-30 10:10:57 +00:00
2024-11-19 22:43:27 +00:00
from selfprivacy_api.root_daemon import (
get_available_commands,
init,
main,
service_commands,
services,
)
2024-12-11 13:36:32 +00:00
import selfprivacy_api
import selfprivacy_api.root_daemon as root_daemon
from selfprivacy_api.utils.root_interface import call_root_function
2024-11-19 22:43:27 +00:00
from os.path import join, exists
2024-10-30 10:10:57 +00:00
from typing import List
from time import sleep
2024-10-30 10:10:57 +00:00
@pytest.fixture()
def test_socket(mocker, tmpdir):
socket_path = join(tmpdir, "test_socket.s")
mocker.patch(
"selfprivacy_api.root_daemon.SOCKET_PATH",
new=socket_path,
)
2024-11-19 22:43:27 +00:00
return socket_path
2024-10-30 10:10:57 +00:00
def is_in_strings(list: List[str], piece: str):
return any([piece in x for x in list])
2024-11-19 22:43:27 +00:00
2024-10-30 10:10:57 +00:00
def test_available_commands():
2024-11-19 22:43:27 +00:00
commands = get_available_commands()
2024-10-30 10:10:57 +00:00
assert commands != []
assert len(commands) >= len(services) * len(service_commands)
for service in services:
assert is_in_strings(commands, service)
2024-11-19 22:43:27 +00:00
2024-12-11 13:36:32 +00:00
def test_init():
2024-11-19 22:43:27 +00:00
sock = init()
2024-12-11 13:36:32 +00:00
assert exists(root_daemon.SOCKET_PATH)
2024-11-19 22:43:27 +00:00
assert sock is not None
2024-10-30 10:10:57 +00:00
2024-12-11 13:36:32 +00:00
def test_send_command():
root_daemon_file = selfprivacy_api.root_daemon.__file__
# this is a prototype of how we need to run it`
proc = subprocess.Popen(args=["python", root_daemon_file], shell=False)
# check that it did not error out
sleep(0.3)
finished = proc.poll()
assert finished is None
2024-12-11 13:36:32 +00:00
# thread = threading.Thread(target=start_root_daemon,args=[])
# thread.start()
answer = call_root_function("blabla")
assert answer == "done"
proc.kill()
# thread.join(timeout=1.0)