specter-desktop/tests/test_managers_service.py
k9ert 9df0b77f8c
Feature: Spectrum addition (#1952)
* add spectrum

* rc3 of spectrum

* spectrum rc4

* missing __init__.py

* changes for rc6

* make cli-arguments work in electron

* update spectrum to remove simplejson

* Bugfix devices not shown

* additional checks for wallet_import

* nodes template magic to improve extensibility

* smaller bugfixes

* update spectrum

* keep python 3.9 in pre-commit-yaml

* specter_added_to_flask_app callback added

* useful logger + avoid writing to config with no node alias

* Using FlaskThread

* remove overriding chain property in node.py

* nodes_by_chain method added to node mananger

* icons for spectrum setup

* keep external_node and fix test

* bump spectrum version

* more visibility

* deactivate checker_threads for testing

Co-authored-by: k9ert <kim@swanbitcoin.com>
Co-authored-by: Manolis Mandrapilias <70536101+moneymanolis@users.noreply.github.com>
Co-authored-by: moneymanolis <moneymanolis@protonmail.com>
2022-11-30 13:13:16 +01:00

104 lines
4.1 KiB
Python

import logging
import sys
import pytest
from pathlib import PosixPath, Path
import os
from unittest.mock import MagicMock
from flask import Flask
from cryptoadvance.specter.managers.service_manager import ServiceManager
from cryptoadvance.specter.services.callbacks import after_serverpy_init_app
def test_ServiceManager2(mock_specter, mock_flaskapp, caplog):
ctx = mock_flaskapp.app_context()
ctx.push()
sm = ServiceManager(mock_specter, "alpha")
# We have passed the TestConfig which is (hopefully) not existing in the Swan Extension
# So the ServiceManager will move up the dependency tree of TestConfig until it finds
# a Config and will copy the keys into the flask-config
assert mock_flaskapp.config["SWAN_API_URL"] == "https://api.dev.swanbitcoin.com"
assert sm.services["swan"] != None
sm.execute_ext_callbacks(after_serverpy_init_app, scheduler=None)
@pytest.mark.skip(reason="The .buildenv directoy does not exist on the CI-Infra")
def test_ServiceManager_get_service_x_dirs(caplog):
caplog.set_level(logging.DEBUG)
try:
os.chdir("./pyinstaller")
# THis is usefull in the pytinstaller/specterd.spec
dirs = ServiceManager.get_service_x_dirs("templates")
# As the tests are executed in a development-environment (pip3 install -e .), the results we get back here
# are not the same than the one we would get back when really are building. Because in that case,
# the .buildenv would the environment and no symlinks would link to src/cryptoadavance...
expected_folder = (
f"../.buildenv/lib/python{sys.version_info[0]}.{sys.version_info[1]}/src"
)
# however, in the real build, you get something like:
# ../.buildenv/lib/python3.8/site-packages/cryptoadvance/specter/services/swan/templates
assert f"{expected_folder}/cryptoadvance/specterext/swan/templates" in [
str(dir) for dir in dirs
]
for path in dirs:
assert str(path).endswith("templates")
dirs = ServiceManager.get_service_x_dirs("static")
assert f"{expected_folder}/cryptoadvance/specterext/swan/static" in [
str(dir) for dir in dirs
]
print(dirs)
assert len(dirs) >= 2000
assert False
finally:
os.chdir("../")
def test_ServiceManager_get_service_packages(caplog):
caplog.set_level(logging.DEBUG)
packages = ServiceManager.get_service_packages()
assert "cryptoadvance.specterext.electrum.service" in packages
assert "cryptoadvance.specterext.electrum.devices.electrum" in packages
assert "cryptoadvance.specterext.swan.service" in packages
assert "cryptoadvance.specterext.electrum.service" in packages
assert "cryptoadvance.specterext.electrum.devices.electrum" in packages
def test_ServiceManager_make_path_relative(caplog):
caplog.set_level(logging.DEBUG)
arr = [
Path(
"/home/kim/src/specter-desktop/.buildenv/lib/python3.8/site-packages/cryptoadvance/specter/services/swan/templates"
),
Path("wurstbrot/something/.env/site-packages/the_rest"),
]
arr = [ServiceManager._make_path_relative(path) for path in arr]
assert arr[0] == PosixPath(
"site-packages/cryptoadvance/specter/services/swan/templates"
)
assert arr[1] == PosixPath("site-packages/the_rest")
@pytest.fixture
def mock_specter():
specter_mock = MagicMock()
specter_mock.config = {"services": {}}
return specter_mock
@pytest.fixture
def mock_flaskapp(mock_specter):
flaskapp_mock = Flask(__name__)
flaskapp_mock.config["EXTENSION_LIST"] = [
"cryptoadvance.specterext.swan.service",
]
flaskapp_mock.config["ISOLATED_CLIENT_EXT_URL_PREFIX"] = "/spc/ext"
flaskapp_mock.config["EXT_URL_PREFIX"] = "/ext"
# The ServiceManager is a flask-aware component. It will load all the services
# however, in order to configure them, he needs to know about the configuration
# of the specterApp.
flaskapp_mock.config[
"SPECTER_CONFIGURATION_CLASS_FULLNAME"
] = "cryptoadvance.specter.config.TestConfig"
return flaskapp_mock