From 9f096ed2c0d315d36cc66c24ad24ffda659340af Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 19 Jun 2023 11:09:10 +0000 Subject: [PATCH] feature(backups): actually dealing with situation when the provider is not configured --- selfprivacy_api/backup/__init__.py | 47 +++++++++++-------- selfprivacy_api/backup/backuppers/__init__.py | 6 ++- .../backup/backuppers/none_backupper.py | 5 +- selfprivacy_api/backup/providers/provider.py | 2 +- tests/test_graphql/test_api_backup.py | 2 +- tests/test_graphql/test_backup.py | 3 +- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 329f81f..29fac73 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -168,38 +168,47 @@ class Backups: if redis_provider is not None: return redis_provider - json_provider = Backups.load_provider_json() + try: + json_provider = Backups.load_provider_json() + except FileNotFoundError: + json_provider = None + if json_provider is not None: Storage.store_provider(json_provider) return json_provider - memory_provider = Backups.construct_provider("MEMORY", login="", key="") - Storage.store_provider(memory_provider) - return memory_provider + none_provider = Backups.construct_provider( + "NONE", login="", key="", location="" + ) + Storage.store_provider(none_provider) + return none_provider @staticmethod - def load_provider_json() -> AbstractBackupProvider: + def load_provider_json() -> Optional[AbstractBackupProvider]: with ReadUserData() as user_data: - account = "" - key = "" + provider_dict = { + "provider": "", + "accountId": "", + "accountKey": "", + "bucket": "", + } if "backup" not in user_data.keys(): if "backblaze" in user_data.keys(): - account = user_data["backblaze"]["accountId"] - key = user_data["backblaze"]["accountKey"] - location = user_data["backblaze"]["bucket"] - provider_string = "BACKBLAZE" - return Backups.construct_provider( - kind=provider_string, login=account, key=key, location=location - ) + provider_dict.update(user_data["backblaze"]) + provider_dict["provider"] = "BACKBLAZE" + return None + else: + provider_dict.update(user_data["backup"]) + + if provider_dict == DEFAULT_JSON_PROVIDER: return None - account = user_data["backup"]["accountId"] - key = user_data["backup"]["accountKey"] - provider_string = user_data["backup"]["provider"] - location = user_data["backup"]["bucket"] return Backups.construct_provider( - kind=provider_string, login=account, key=key, location=location + kind=provider_dict["provider"], + login=provider_dict["accountId"], + key=provider_dict["accountKey"], + location=provider_dict["bucket"], ) def reset_provider_json() -> AbstractBackupProvider: diff --git a/selfprivacy_api/backup/backuppers/__init__.py b/selfprivacy_api/backup/backuppers/__init__.py index 908c1fc..61ef1ba 100644 --- a/selfprivacy_api/backup/backuppers/__init__.py +++ b/selfprivacy_api/backup/backuppers/__init__.py @@ -9,7 +9,11 @@ class AbstractBackuper(ABC): pass @abstractmethod - def is_initted(self, repo_name: str) -> bool: + def is_initted(self) -> bool: + raise NotImplementedError + + @abstractmethod + def set_creds(self, account: str, key: str, repo: str): raise NotImplementedError @abstractmethod diff --git a/selfprivacy_api/backup/backuppers/none_backupper.py b/selfprivacy_api/backup/backuppers/none_backupper.py index 0c9509e..de51d6a 100644 --- a/selfprivacy_api/backup/backuppers/none_backupper.py +++ b/selfprivacy_api/backup/backuppers/none_backupper.py @@ -5,9 +5,12 @@ from selfprivacy_api.backup.backuppers import AbstractBackuper class NoneBackupper(AbstractBackuper): - def is_initted(self, repo_name: str) -> bool: + def is_initted(self, repo_name: str = "") -> bool: return False + def set_creds(self, account: str, key: str, repo: str): + pass + def start_backup(self, folders: List[str], repo_name: str): raise NotImplementedError diff --git a/selfprivacy_api/backup/providers/provider.py b/selfprivacy_api/backup/providers/provider.py index c6da12d..dddc53f 100644 --- a/selfprivacy_api/backup/providers/provider.py +++ b/selfprivacy_api/backup/providers/provider.py @@ -11,7 +11,7 @@ from selfprivacy_api.backup.backuppers.none_backupper import NoneBackupper class AbstractBackupProvider(ABC): @property def backuper(self) -> AbstractBackuper: - return NoneBackupper + return NoneBackupper() name = "NONE" diff --git a/tests/test_graphql/test_api_backup.py b/tests/test_graphql/test_api_backup.py index 90381c4..d65d6f1 100644 --- a/tests/test_graphql/test_api_backup.py +++ b/tests/test_graphql/test_api_backup.py @@ -230,7 +230,7 @@ def test_remove(authorized_client, generic_userdata): assert_ok(data) configuration = data["configuration"] - assert configuration["provider"] == "BACKBLAZE" + assert configuration["provider"] == "NONE" assert configuration["locationId"] == "" assert configuration["locationName"] == "" # still generated every time it is missing diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 645b2fa..428e3dd 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -15,6 +15,7 @@ from selfprivacy_api.backup import Backups import selfprivacy_api.backup.providers as providers from selfprivacy_api.backup.providers import AbstractBackupProvider from selfprivacy_api.backup.providers.backblaze import Backblaze + from selfprivacy_api.backup.tasks import start_backup, restore_snapshot from selfprivacy_api.backup.storage import Storage from selfprivacy_api.backup.jobs import get_backup_job @@ -126,7 +127,7 @@ def test_json_reset(generic_userdata): Backups.reset() provider = Backups.provider() assert provider is not None - assert isinstance(provider, Backblaze) + assert isinstance(provider, AbstractBackupProvider) assert provider.login == "" assert provider.key == "" assert provider.location == ""