From 696cb406a829acab2012a23a0ae5f96a8897cdb2 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 3 Feb 2023 17:04:35 +0000 Subject: [PATCH] test(backup): dummy service --- .../services/test_service/__init__.py | 136 ++++++++++++++++++ .../services/test_service/bitwarden.svg | 3 + selfprivacy_api/services/test_service/icon.py | 5 + tests/test_graphql/test_backup.py | 14 ++ 4 files changed, 158 insertions(+) create mode 100644 selfprivacy_api/services/test_service/__init__.py create mode 100644 selfprivacy_api/services/test_service/bitwarden.svg create mode 100644 selfprivacy_api/services/test_service/icon.py diff --git a/selfprivacy_api/services/test_service/__init__.py b/selfprivacy_api/services/test_service/__init__.py new file mode 100644 index 0000000..53fe0cf --- /dev/null +++ b/selfprivacy_api/services/test_service/__init__.py @@ -0,0 +1,136 @@ +"""Class representing Bitwarden service""" +import base64 +import typing + +from selfprivacy_api.jobs import Job +from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus +from selfprivacy_api.utils import ReadUserData, get_domain +from selfprivacy_api.utils.block_devices import BlockDevice +import selfprivacy_api.utils.network as network_utils + +from selfprivacy_api.services.test_service.icon import BITWARDEN_ICON + + +class DummyService(Service): + """A test service""" + + def __init__(self, location): + self.loccation = location + + @staticmethod + def get_id() -> str: + """Return service id.""" + return "testservice" + + @staticmethod + def get_display_name() -> str: + """Return service display name.""" + return "Test Service" + + @staticmethod + def get_description() -> str: + """Return service description.""" + return "A small service used for test purposes. Does nothing." + + @staticmethod + def get_svg_icon() -> str: + """Read SVG icon from file and return it as base64 encoded string.""" + # return "" + return base64.b64encode(BITWARDEN_ICON.encode("utf-8")).decode("utf-8") + + @staticmethod + def get_url() -> typing.Optional[str]: + """Return service url.""" + domain = get_domain() + return f"https://password.{domain}" + + @staticmethod + def is_movable() -> bool: + return True + + @staticmethod + def is_required() -> bool: + return False + + @staticmethod + def is_enabled() -> bool: + return True + + @staticmethod + def get_status() -> ServiceStatus: + """ + Return Bitwarden status from systemd. + Use command return code to determine status. + + Return code 0 means service is running. + Return code 1 or 2 means service is in error stat. + Return code 3 means service is stopped. + Return code 4 means service is off. + """ + return 0 + + @staticmethod + def enable(): + pass + + @staticmethod + def disable(): + pass + + @staticmethod + def stop(): + pass + + @staticmethod + def start(): + pass + + @staticmethod + def restart(): + pass + + @staticmethod + def get_configuration(): + return {} + + @staticmethod + def set_configuration(config_items): + return super().set_configuration(config_items) + + @staticmethod + def get_logs(): + return "" + + @staticmethod + def get_storage_usage() -> int: + storage_usage = 0 + return storage_usage + + @staticmethod + def get_location() -> str: + with ReadUserData() as user_data: + if user_data.get("useBinds", False): + return user_data.get("bitwarden", {}).get("location", "sda1") + else: + return "sda1" + + @staticmethod + def get_dns_records() -> typing.List[ServiceDnsRecord]: + """Return list of DNS records for Bitwarden service.""" + return [ + ServiceDnsRecord( + type="A", + name="password", + content=network_utils.get_ip4(), + ttl=3600, + ), + ServiceDnsRecord( + type="AAAA", + name="password", + content=network_utils.get_ip6(), + ttl=3600, + ), + ] + + def move_to_volume(self, volume: BlockDevice) -> Job: + pass diff --git a/selfprivacy_api/services/test_service/bitwarden.svg b/selfprivacy_api/services/test_service/bitwarden.svg new file mode 100644 index 0000000..ced270c --- /dev/null +++ b/selfprivacy_api/services/test_service/bitwarden.svg @@ -0,0 +1,3 @@ + + + diff --git a/selfprivacy_api/services/test_service/icon.py b/selfprivacy_api/services/test_service/icon.py new file mode 100644 index 0000000..f9280e0 --- /dev/null +++ b/selfprivacy_api/services/test_service/icon.py @@ -0,0 +1,5 @@ +BITWARDEN_ICON = """ + + + +""" diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 70b3ce7..b63097c 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -1,3 +1,7 @@ +import pytest + +from selfprivacy_api.services.test_service import DummyService + import selfprivacy_api.backup.providers as providers from selfprivacy_api.backup.providers import AbstractBackupProvider @@ -5,7 +9,17 @@ from selfprivacy_api.backup.providers.backblaze import Backblaze from selfprivacy_api.graphql.queries.providers import BackupProvider +@pytest.fixture() +def test_service(tmpdir): + return DummyService(tmpdir) + + def test_select_backend(): provider = providers.get_provider(BackupProvider.BACKBLAZE) assert provider is not None assert provider == Backblaze + + +def test_backup(test_service): + # temporarily incomplete + assert test_service is not None