feature(backups): resetting json config too

This commit is contained in:
Houkime 2023-06-16 15:09:39 +00:00
parent 47aee3c1f1
commit 79c3b9598c
3 changed files with 24 additions and 6 deletions

View file

@ -4,7 +4,7 @@ from os import statvfs
from selfprivacy_api.models.backup.snapshot import Snapshot from selfprivacy_api.models.backup.snapshot import Snapshot
from selfprivacy_api.utils import ReadUserData from selfprivacy_api.utils import ReadUserData, WriteUserData
from selfprivacy_api.services import get_service_by_id from selfprivacy_api.services import get_service_by_id
from selfprivacy_api.services.service import Service from selfprivacy_api.services.service import Service
@ -22,6 +22,13 @@ from selfprivacy_api.backup.jobs import (
) )
from selfprivacy_api.jobs import Jobs, JobStatus from selfprivacy_api.jobs import Jobs, JobStatus
DEFAULT_JSON_PROVIDER = {
"provider": "BACKBLAZE",
"accountId": "",
"accountKey": "",
"bucket": "",
}
class Backups: class Backups:
"""A singleton controller for backups""" """A singleton controller for backups"""
@ -147,8 +154,13 @@ class Backups:
return provider_class(login=login, key=key, location=location, repo_id=repo_id) return provider_class(login=login, key=key, location=location, repo_id=repo_id)
@staticmethod @staticmethod
def reset(): def reset(reset_json=True):
Storage.reset() Storage.reset()
if reset_json:
try:
Backups.reset_provider_json()
except FileNotFoundError: # if there is no userdata file, we do not need to reset it
pass
@staticmethod @staticmethod
def lookup_provider() -> AbstractBackupProvider: def lookup_provider() -> AbstractBackupProvider:
@ -190,6 +202,13 @@ class Backups:
kind=provider_string, login=account, key=key, location=location kind=provider_string, login=account, key=key, location=location
) )
def reset_provider_json() -> AbstractBackupProvider:
with WriteUserData() as user_data:
if "backblaze" in user_data.keys():
del user_data["backblaze"]
user_data["backup"] = DEFAULT_JSON_PROVIDER
@staticmethod @staticmethod
def load_provider_redis() -> AbstractBackupProvider: def load_provider_redis() -> AbstractBackupProvider:
provider_model = Storage.load_provider() provider_model = Storage.load_provider()

View file

@ -232,7 +232,7 @@ def test_remove(authorized_client, generic_userdata):
configuration = data["configuration"] configuration = data["configuration"]
assert configuration["provider"] == "BACKBLAZE" assert configuration["provider"] == "BACKBLAZE"
assert configuration["locationId"] == "" assert configuration["locationId"] == ""
assert configuration["locationName"] == "selfprivacy" assert configuration["locationName"] == ""
# still generated every time it is missing # still generated every time it is missing
assert len(configuration["encryptionKey"]) > 1 assert len(configuration["encryptionKey"]) > 1
assert configuration["isInitialized"] is False assert configuration["isInitialized"] is False

View file

@ -37,7 +37,7 @@ def backups(tmpdir):
@pytest.fixture() @pytest.fixture()
def backups_backblaze(generic_userdata): def backups_backblaze(generic_userdata):
Backups.reset() Backups.reset(reset_json=False)
@pytest.fixture() @pytest.fixture()
@ -101,7 +101,7 @@ def file_backup(tmpdir) -> AbstractBackupProvider:
def test_config_load(generic_userdata): def test_config_load(generic_userdata):
Backups.reset() Backups.reset(reset_json=False)
provider = Backups.provider() provider = Backups.provider()
assert provider is not None assert provider is not None
@ -445,7 +445,6 @@ def test_init_tracking_caching2(backups, raw_dummy_service):
# Storage # Storage
def test_provider_storage(backups_backblaze): def test_provider_storage(backups_backblaze):
Backups.reset()
provider = Backups.provider() provider = Backups.provider()
assert provider is not None assert provider is not None