mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-25 13:31:27 +00:00
feature(backups): load from json
This commit is contained in:
parent
4e329299e0
commit
ea6a047478
|
@ -3,6 +3,7 @@ from typing import List
|
||||||
from selfprivacy_api.models.backup.snapshot import Snapshot
|
from selfprivacy_api.models.backup.snapshot import Snapshot
|
||||||
|
|
||||||
from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass
|
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 import get_service_by_id
|
||||||
from selfprivacy_api.services.service import Service
|
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.backup.providers import get_provider
|
||||||
from selfprivacy_api.graphql.queries.providers import BackupProvider
|
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(metaclass=SingletonMetaclass):
|
||||||
class Backups:
|
class Backups:
|
||||||
"""A singleton controller for backups"""
|
"""A singleton controller for backups"""
|
||||||
|
@ -22,24 +25,28 @@ class Backups:
|
||||||
if test_repo_file != "":
|
if test_repo_file != "":
|
||||||
self.set_localfile_repo(test_repo_file)
|
self.set_localfile_repo(test_repo_file)
|
||||||
else:
|
else:
|
||||||
self.lookup_provider()
|
self.provider = self.lookup_provider()
|
||||||
|
|
||||||
def set_localfile_repo(self, file_path: str):
|
def set_localfile_repo(self, file_path: str):
|
||||||
ProviderClass = get_provider(BackupProvider.FILE)
|
ProviderClass = get_provider(BackupProvider.FILE)
|
||||||
provider = ProviderClass(file_path)
|
provider = ProviderClass(file_path)
|
||||||
self.provider = provider
|
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()
|
redis_provider = Backups.load_provider_redis()
|
||||||
if redis_provider is not None:
|
if redis_provider is not None:
|
||||||
self.provider = redis_provider
|
return redis_provider
|
||||||
|
|
||||||
json_provider = Backups.load_provider_json()
|
json_provider = Backups.load_provider_json()
|
||||||
if json_provider is not None:
|
if json_provider is not None:
|
||||||
self.provider = json_provider
|
return json_provider
|
||||||
|
|
||||||
provider_class = get_provider(BackupProvider.MEMORY)
|
return Backups.construct_provider("MEMORY", login="", key="")
|
||||||
self.provider = provider_class(login="", key="")
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_provider_redis() -> AbstractBackupProvider:
|
def load_provider_redis() -> AbstractBackupProvider:
|
||||||
|
@ -47,7 +54,26 @@ class Backups:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_provider_json() -> AbstractBackupProvider:
|
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):
|
def back_up(self, service: Service):
|
||||||
folder = service.get_location()
|
folder = service.get_location()
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
|
from os import path
|
||||||
|
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,6 +12,10 @@ def pytest_generate_tests(metafunc):
|
||||||
os.environ["TEST_MODE"] = "true"
|
os.environ["TEST_MODE"] = "true"
|
||||||
|
|
||||||
|
|
||||||
|
def global_data_dir():
|
||||||
|
return path.join(path.dirname(__file__), "data")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def tokens_file(mocker, shared_datadir):
|
def tokens_file(mocker, shared_datadir):
|
||||||
"""Mock tokens file."""
|
"""Mock tokens file."""
|
||||||
|
@ -26,6 +32,20 @@ def jobs_file(mocker, shared_datadir):
|
||||||
return mock
|
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
|
@pytest.fixture
|
||||||
def huey_database(mocker, shared_datadir):
|
def huey_database(mocker, shared_datadir):
|
||||||
"""Mock huey database."""
|
"""Mock huey database."""
|
||||||
|
|
60
tests/data/turned_on.json
Normal file
60
tests/data/turned_on.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -73,6 +73,16 @@ def file_backup(tmpdir) -> AbstractBackupProvider:
|
||||||
return provider
|
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():
|
def test_select_backend():
|
||||||
provider = providers.get_provider(BackupProvider.BACKBLAZE)
|
provider = providers.get_provider(BackupProvider.BACKBLAZE)
|
||||||
assert provider is not None
|
assert provider is not None
|
||||||
|
|
Loading…
Reference in a new issue