From d8a0e05602b1a74da0c8313d645d95922bb6cb69 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 10 Mar 2023 14:14:41 +0000 Subject: [PATCH] feature(backups): load from json --- selfprivacy_api/backup/__init__.py | 42 +++++++++++++++++---- tests/conftest.py | 20 ++++++++++ tests/data/turned_on.json | 60 ++++++++++++++++++++++++++++++ tests/test_graphql/test_backup.py | 10 +++++ 4 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 tests/data/turned_on.json diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 903e38b..f34f43d 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -3,6 +3,7 @@ from typing import List from selfprivacy_api.models.backup.snapshot import Snapshot from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass +from selfprivacy_api.utils import ReadUserData from selfprivacy_api.services import get_service_by_id from selfprivacy_api.services.service import Service @@ -11,7 +12,9 @@ from selfprivacy_api.backup.providers.provider import AbstractBackupProvider from selfprivacy_api.backup.providers import get_provider from selfprivacy_api.graphql.queries.providers import BackupProvider -# Singleton has a property of being persistent between tests. I don't know what to do with this yet + +# Singleton has a property of being persistent between tests. +# I don't know what to do with this yet # class Backups(metaclass=SingletonMetaclass): class Backups: """A singleton controller for backups""" @@ -22,24 +25,28 @@ class Backups: if test_repo_file != "": self.set_localfile_repo(test_repo_file) else: - self.lookup_provider() + self.provider = self.lookup_provider() def set_localfile_repo(self, file_path: str): ProviderClass = get_provider(BackupProvider.FILE) provider = ProviderClass(file_path) self.provider = provider - def lookup_provider(self): + @staticmethod + def construct_provider(kind: str, login: str, key: str): + provider_class = get_provider(BackupProvider[kind]) + return provider_class(login=login, key=key) + + def lookup_provider(self) -> AbstractBackupProvider: redis_provider = Backups.load_provider_redis() if redis_provider is not None: - self.provider = redis_provider + return redis_provider json_provider = Backups.load_provider_json() if json_provider is not None: - self.provider = json_provider + return json_provider - provider_class = get_provider(BackupProvider.MEMORY) - self.provider = provider_class(login="", key="") + return Backups.construct_provider("MEMORY", login="", key="") @staticmethod def load_provider_redis() -> AbstractBackupProvider: @@ -47,7 +54,26 @@ class Backups: @staticmethod def load_provider_json() -> AbstractBackupProvider: - pass + with ReadUserData() as user_data: + account = "" + key = "" + + if "backup" not in user_data.keys(): + if "backblaze" in user_data.keys(): + account = user_data["backblaze"]["accountId"] + key = user_data["backblaze"]["accountKey"] + provider_string = "BACKBLAZE" + return Backups.construct_provider( + kind=provider_string, login=account, key=key + ) + return None + + account = user_data["backup"]["accountId"] + key = user_data["backup"]["accountKey"] + provider_string = user_data["backup"]["provider"] + return Backups.construct_provider( + kind=provider_string, login=account, key=key + ) def back_up(self, service: Service): folder = service.get_location() diff --git a/tests/conftest.py b/tests/conftest.py index ea7a66a..7e8ae11 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,8 @@ # pylint: disable=unused-argument import os import pytest +from os import path + from fastapi.testclient import TestClient @@ -10,6 +12,10 @@ def pytest_generate_tests(metafunc): os.environ["TEST_MODE"] = "true" +def global_data_dir(): + return path.join(path.dirname(__file__), "data") + + @pytest.fixture def tokens_file(mocker, shared_datadir): """Mock tokens file.""" @@ -26,6 +32,20 @@ def jobs_file(mocker, shared_datadir): return mock +@pytest.fixture +def generic_userdata(mocker, tmpdir): + filename = "turned_on.json" + source_path = path.join(global_data_dir(), filename) + userdata_path = path.join(tmpdir, filename) + + with open(userdata_path, "w") as file: + with open(source_path, "r") as source: + file.write(source.read()) + + mock = mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=userdata_path) + return mock + + @pytest.fixture def huey_database(mocker, shared_datadir): """Mock huey database.""" diff --git a/tests/data/turned_on.json b/tests/data/turned_on.json new file mode 100644 index 0000000..c6b758b --- /dev/null +++ b/tests/data/turned_on.json @@ -0,0 +1,60 @@ +{ + "api": { + "token": "TEST_TOKEN", + "enableSwagger": false + }, + "bitwarden": { + "enable": true + }, + "databasePassword": "PASSWORD", + "domain": "test.tld", + "hashedMasterPassword": "HASHED_PASSWORD", + "hostname": "test-instance", + "nextcloud": { + "adminPassword": "ADMIN", + "databasePassword": "ADMIN", + "enable": true + }, + "resticPassword": "PASS", + "ssh": { + "enable": true, + "passwordAuthentication": true, + "rootKeys": [ + "ssh-ed25519 KEY test@pc" + ] + }, + "username": "tester", + "gitea": { + "enable": true + }, + "ocserv": { + "enable": true + }, + "pleroma": { + "enable": true + }, + "jitsi": { + "enable": true + }, + "autoUpgrade": { + "enable": true, + "allowReboot": true + }, + "timezone": "Europe/Moscow", + "sshKeys": [ + "ssh-rsa KEY test@pc" + ], + "dns": { + "provider": "CLOUDFLARE", + "apiKey": "TOKEN" + }, + "server": { + "provider": "HETZNER" + }, + "backup": { + "provider": "BACKBLAZE", + "accountId": "ID", + "accountKey": "KEY", + "bucket": "selfprivacy" + } +} diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 4c6b2dd..6d21c15 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -73,6 +73,16 @@ def file_backup(tmpdir) -> AbstractBackupProvider: return provider +def test_config_load(generic_userdata): + backups = Backups() + provider = backups.provider + + assert provider is not None + assert isinstance(provider, Backblaze) + assert provider.login == "ID" + assert provider.key == "KEY" + + def test_select_backend(): provider = providers.get_provider(BackupProvider.BACKBLAZE) assert provider is not None