From 9a3800ac7bc4aa0765cffcade144cefa3684fbe3 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 6 Oct 2023 13:17:48 +0000 Subject: [PATCH] test(service): moving errors --- selfprivacy_api/services/service.py | 2 + .../services/test_service/__init__.py | 13 +++- tests/test_graphql/test_services.py | 73 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/selfprivacy_api/services/service.py b/selfprivacy_api/services/service.py index 8446133..fbe0007 100644 --- a/selfprivacy_api/services/service.py +++ b/selfprivacy_api/services/service.py @@ -247,6 +247,8 @@ class Service(ABC): @abstractmethod def move_to_volume(self, volume: BlockDevice) -> Job: + """Cannot raise errors. + Returns errors as an errored out Job instead.""" pass @classmethod diff --git a/selfprivacy_api/services/test_service/__init__.py b/selfprivacy_api/services/test_service/__init__.py index e4ed4cc..1cb5d9f 100644 --- a/selfprivacy_api/services/test_service/__init__.py +++ b/selfprivacy_api/services/test_service/__init__.py @@ -24,6 +24,7 @@ class DummyService(Service): folders: List[str] = [] startstop_delay = 0.0 backuppable = True + movable = True def __init_subclass__(cls, folders: List[str]): cls.folders = folders @@ -62,9 +63,9 @@ class DummyService(Service): domain = "test.com" return f"https://password.{domain}" - @staticmethod - def is_movable() -> bool: - return True + @classmethod + def is_movable(cls) -> bool: + return cls.movable @staticmethod def is_required() -> bool: @@ -137,6 +138,12 @@ class DummyService(Service): we can only set it up dynamically for tests via a classmethod""" cls.backuppable = new_value + @classmethod + def set_movable(cls, new_value: bool) -> None: + """For tests: because is_movale is static, + we can only set it up dynamically for tests via a classmethod""" + cls.movable = new_value + @classmethod def can_be_backed_up(cls) -> bool: """`True` if the service can be backed up.""" diff --git a/tests/test_graphql/test_services.py b/tests/test_graphql/test_services.py index 0b652c5..df409b9 100644 --- a/tests/test_graphql/test_services.py +++ b/tests/test_graphql/test_services.py @@ -110,6 +110,26 @@ allServices { } """ +API_MOVE_MUTATION = """ +mutation TestMoveService($input: MoveServiceInput!) { + services { + moveService(input: $input) { + success + message + code + job { + uid + status + } + service { + id + status + } + } + } +} +""" + def assert_notfound(data): assert_errorcode(data, 404) @@ -166,6 +186,26 @@ def api_start_by_name(client, service_id: str) -> dict: return response +def api_move(client, service: Service, location: str) -> dict: + return api_move_by_name(client, service.get_id(), location) + + +def api_move_by_name(client, service_id: str, location: str) -> dict: + response = client.post( + "/graphql", + json={ + "query": API_MOVE_MUTATION, + "variables": { + "input": { + "serviceId": service_id, + "location": location, + } + }, + }, + ) + return response + + def api_restart(client, service: Service) -> dict: return api_restart_by_name(client, service.get_id()) @@ -327,6 +367,16 @@ def test_disable_unauthorized(client, only_dummy_service): assert mutation_response.json().get("data") is None +def test_move_nonexistent(authorized_client, only_dummy_service): + dummy_service = only_dummy_service + mutation_response = api_move_by_name(authorized_client, "bogus_service", "sda1") + data = get_data(mutation_response)["services"]["moveService"] + assert_notfound(data) + + assert data["service"] is None + assert data["job"] is None + + def test_start_nonexistent(authorized_client, only_dummy_service): dummy_service = only_dummy_service mutation_response = api_start_by_name(authorized_client, "bogus_service") @@ -395,3 +445,26 @@ def test_stop_start(authorized_client, only_dummy_service): api_start(authorized_client, dummy_service) api_dummy_service = api_all_services(authorized_client)[0] assert api_dummy_service["status"] == ServiceStatus.ACTIVE.value + + +def test_move_immovable(authorized_client, only_dummy_service): + dummy_service = only_dummy_service + dummy_service.set_movable(False) + mutation_response = api_move(authorized_client, dummy_service, "sda1") + data = get_data(mutation_response)["services"]["moveService"] + assert_errorcode(data, 400) + + # is there a meaning in returning the service in this? + assert data["service"] is not None + assert data["job"] is None + + +def test_move_no_such_volume(authorized_client, only_dummy_service): + dummy_service = only_dummy_service + mutation_response = api_move(authorized_client, dummy_service, "bogus_volume") + data = get_data(mutation_response)["services"]["moveService"] + assert_notfound(data) + + # is there a meaning in returning the service in this? + assert data["service"] is not None + assert data["job"] is None