From 1e77129f4fa0b0ceaea76ea3944a4a2d92a225d0 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 6 Oct 2023 10:45:46 +0000 Subject: [PATCH] test(service): restart --- .../services/test_service/__init__.py | 6 +- tests/test_graphql/test_services.py | 63 ++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/selfprivacy_api/services/test_service/__init__.py b/selfprivacy_api/services/test_service/__init__.py index 60aea3b..e4ed4cc 100644 --- a/selfprivacy_api/services/test_service/__init__.py +++ b/selfprivacy_api/services/test_service/__init__.py @@ -22,7 +22,7 @@ class DummyService(Service): """A test service""" folders: List[str] = [] - startstop_delay = 0 + startstop_delay = 0.0 backuppable = True def __init_subclass__(cls, folders: List[str]): @@ -151,8 +151,8 @@ class DummyService(Service): cls.set_enabled(False) @classmethod - def set_delay(cls, new_delay): - cls.startstop_delay = new_delay + def set_delay(cls, new_delay_sec: float) -> None: + cls.startstop_delay = new_delay_sec @classmethod def stop(cls): diff --git a/tests/test_graphql/test_services.py b/tests/test_graphql/test_services.py index fcd2b85..0b652c5 100644 --- a/tests/test_graphql/test_services.py +++ b/tests/test_graphql/test_services.py @@ -1,8 +1,10 @@ import pytest +from typing import Generator from selfprivacy_api.graphql.mutations.services_mutations import ServicesMutations import selfprivacy_api.services as service_module from selfprivacy_api.services.service import Service, ServiceStatus +from selfprivacy_api.services.test_service import DummyService import tests.test_graphql.test_api_backup from tests.test_common import raw_dummy_service, dummy_service @@ -11,7 +13,7 @@ from tests.test_graphql.common import assert_ok, get_data @pytest.fixture() -def only_dummy_service(dummy_service): +def only_dummy_service(dummy_service) -> Generator[DummyService, None, None]: # because queries to services that are not really there error out back_copy = service_module.services.copy() service_module.services.clear() @@ -37,6 +39,22 @@ mutation TestStartService($service_id: String!) { } """ +API_RESTART_MUTATION = """ +mutation TestRestartService($service_id: String!) { + services { + restartService(serviceId: $service_id) { + success + message + code + service { + id + status + } + } + } +} +""" + API_ENABLE_MUTATION = """ mutation TestStartService($service_id: String!) { services { @@ -148,6 +166,21 @@ def api_start_by_name(client, service_id: str) -> dict: return response +def api_restart(client, service: Service) -> dict: + return api_restart_by_name(client, service.get_id()) + + +def api_restart_by_name(client, service_id: str) -> dict: + response = client.post( + "/graphql", + json={ + "query": API_RESTART_MUTATION, + "variables": {"service_id": service_id}, + }, + ) + return response + + def api_stop(client, service: Service) -> dict: return api_stop_by_name(client, service.get_id()) @@ -225,6 +258,17 @@ def test_start_return_value(authorized_client, only_dummy_service): assert service["status"] == ServiceStatus.ACTIVE.value +def test_restart(authorized_client, only_dummy_service): + dummy_service = only_dummy_service + dummy_service.set_delay(0.3) + mutation_response = api_restart(authorized_client, dummy_service) + data = get_data(mutation_response)["services"]["restartService"] + assert_ok(data) + service = data["service"] + assert service["id"] == dummy_service.get_id() + assert service["status"] == ServiceStatus.RELOADING.value + + def test_stop_return_value(authorized_client, only_dummy_service): dummy_service = only_dummy_service mutation_response = api_stop(authorized_client, dummy_service) @@ -251,6 +295,14 @@ def test_start_unauthorized(client, only_dummy_service): assert mutation_response.json().get("data") is None +def test_restart_unauthorized(client, only_dummy_service): + dummy_service = only_dummy_service + mutation_response = api_restart(client, dummy_service) + + assert mutation_response.status_code == 200 + assert mutation_response.json().get("data") is None + + def test_stop_unauthorized(client, only_dummy_service): dummy_service = only_dummy_service mutation_response = api_stop(client, dummy_service) @@ -284,6 +336,15 @@ def test_start_nonexistent(authorized_client, only_dummy_service): assert data["service"] is None +def test_restart_nonexistent(authorized_client, only_dummy_service): + dummy_service = only_dummy_service + mutation_response = api_restart_by_name(authorized_client, "bogus_service") + data = get_data(mutation_response)["services"]["restartService"] + assert_notfound(data) + + assert data["service"] is None + + def test_stop_nonexistent(authorized_client, only_dummy_service): dummy_service = only_dummy_service mutation_response = api_stop_by_name(authorized_client, "bogus_service")