2020-12-06 15:40:22 +01:00
|
|
|
import logging
|
2021-06-17 18:44:01 +02:00
|
|
|
import os
|
2020-12-06 15:40:22 +01:00
|
|
|
import sys
|
|
|
|
|
import traceback
|
|
|
|
|
|
2021-03-22 20:04:02 +01:00
|
|
|
from click.testing import CliRunner
|
|
|
|
|
from cryptoadvance.specter.cli import server
|
|
|
|
|
from mock import MagicMock, call, patch
|
2020-12-06 15:40:22 +01:00
|
|
|
|
2020-12-20 15:03:23 +01:00
|
|
|
mock_config_dict = {
|
2023-02-07 19:00:44 +01:00
|
|
|
"HOST": "127.0.0.1",
|
2020-12-20 15:03:23 +01:00
|
|
|
"PORT": "123",
|
|
|
|
|
"DEBUG": "WURSTBROT",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_C": "AT",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_ST": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_L": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_O": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_OU": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_CN": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SERIAL_NUMBER": 123,
|
|
|
|
|
# We don't want to make a more sophisticated mock, so we simply set here
|
|
|
|
|
# the same value we set via CMD-Line
|
|
|
|
|
"CERT": "bla",
|
|
|
|
|
"KEY": "blub",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-06 15:40:22 +01:00
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.create_app")
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.init_app")
|
|
|
|
|
def test_server_host_and_port(init_app, create_app, caplog):
|
2023-02-15 14:58:33 +01:00
|
|
|
"""This test will fail if you have turned on live-logging in pyproject.toml (log_cli = 1 )"""
|
2020-12-06 15:40:22 +01:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
mock_app = MagicMock()
|
|
|
|
|
mock_app.config = MagicMock()
|
2020-12-20 15:03:23 +01:00
|
|
|
d = mock_config_dict
|
2020-12-06 15:40:22 +01:00
|
|
|
mock_app.config.__getitem__.side_effect = d.__getitem__
|
|
|
|
|
create_app.return_value = mock_app
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(server, ["--port", "456", "--host", "0.0.0.1"])
|
|
|
|
|
print(result.output)
|
|
|
|
|
if result.exception != None:
|
|
|
|
|
# Makes searching for issues much more convenient
|
|
|
|
|
traceback.print_tb(result.exception.__traceback__)
|
|
|
|
|
print(result.exception, file=sys.stderr)
|
|
|
|
|
print(mock_app.config.mock_calls)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
mock_app.config.__setitem__.assert_called_with("PORT", 456)
|
|
|
|
|
mock_app.run.assert_called_with(
|
|
|
|
|
debug="WURSTBROT", host="0.0.0.1", port="123", extra_files=["templates"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.create_app")
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.init_app")
|
|
|
|
|
def test_server_host_and_port(init_app, create_app, caplog):
|
2023-02-15 14:58:33 +01:00
|
|
|
"""This test will fail if you have turned on live-logging in pyproject.toml (log_cli = 1 )"""
|
2020-12-06 15:40:22 +01:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
mock_app = MagicMock()
|
|
|
|
|
mock_app.config = MagicMock()
|
2020-12-20 15:03:23 +01:00
|
|
|
d = mock_config_dict
|
2020-12-06 15:40:22 +01:00
|
|
|
mock_app.config.__getitem__.side_effect = d.__getitem__
|
|
|
|
|
create_app.return_value = mock_app
|
|
|
|
|
runner = CliRunner()
|
2021-06-17 18:44:01 +02:00
|
|
|
try:
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
server, ["--cert", "bla", "--key", "blub", "--no-filelog"]
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
# not sure why i need to do that in an isolated_filesystem ?!
|
|
|
|
|
tidy_up()
|
2020-12-06 15:40:22 +01:00
|
|
|
print(result.output)
|
|
|
|
|
if result.exception != None:
|
|
|
|
|
# Makes searching for issues much more convenient
|
|
|
|
|
traceback.print_tb(result.exception.__traceback__)
|
|
|
|
|
print(result.exception, file=sys.stderr)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
print(mock_app.config.mock_calls)
|
|
|
|
|
mock_app.config.__setitem__.call_count = 2
|
|
|
|
|
mock_app.config.__setitem__.assert_called_with("KEY", "blub")
|
|
|
|
|
mock_app.config.__setitem__.assert_any_call("CERT", "bla")
|
|
|
|
|
# This doesn't work as the tmp-directory is always different
|
|
|
|
|
# mock_app.run.assert_called_with(debug='WURSTBROT', host='0.0.0.1', port='123', extra_files=['templates'],ssl_context=('/tmp/tmpb_2552yg/bla', '/tmp/tmpb_2552yg/blub')))
|
|
|
|
|
# So let's check differently:
|
|
|
|
|
|
|
|
|
|
print(mock_app.run.call_args.kwargs)
|
|
|
|
|
# results in something like:
|
|
|
|
|
# {'debug': 'WURSTBROT', 'host': '127.0.0.1', 'port': '123', 'extra_files': ['templates'], 'ssl_context': ('/tmp/tmpnzivft_y/bla', '/tmp/tmpnzivft_y/blub')}
|
2020-12-20 15:03:23 +01:00
|
|
|
assert mock_app.run.call_args.kwargs["ssl_context"][0].endswith("bla")
|
|
|
|
|
assert mock_app.run.call_args.kwargs["ssl_context"][1].endswith("blub")
|
2020-12-06 15:40:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.create_app")
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.init_app")
|
|
|
|
|
def test_server_debug(init_app, create_app, caplog):
|
2023-02-15 14:58:33 +01:00
|
|
|
"""This test will fail if you have turned on live-logging in pyproject.toml (log_cli = 1 )"""
|
2020-12-06 15:40:22 +01:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
runner = CliRunner()
|
2021-03-22 20:04:02 +01:00
|
|
|
result = runner.invoke(server, ["--debug", "--no-filelog"])
|
2020-12-06 15:40:22 +01:00
|
|
|
print(result.output)
|
|
|
|
|
if result.exception != None:
|
|
|
|
|
# Makes searching for issues much more convenient
|
|
|
|
|
traceback.print_tb(result.exception.__traceback__)
|
|
|
|
|
print(result.exception)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
assert "We're now on level DEBUG on logger cryptoadvance" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.create_app")
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.init_app")
|
|
|
|
|
def test_server_datafolder(init_app, create_app, caplog):
|
2023-02-15 14:58:33 +01:00
|
|
|
"""This test will fail if you have turned on live-logging in pyproject.toml (log_cli = 1 )"""
|
2020-12-06 15:40:22 +01:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
mock_app = MagicMock()
|
|
|
|
|
mock_app.config = MagicMock()
|
2020-12-20 15:03:23 +01:00
|
|
|
d = mock_config_dict
|
2020-12-06 15:40:22 +01:00
|
|
|
mock_app.config.__getitem__.side_effect = d.__getitem__
|
|
|
|
|
create_app.return_value = mock_app
|
|
|
|
|
runner = CliRunner()
|
2021-03-22 20:04:02 +01:00
|
|
|
result = runner.invoke(
|
|
|
|
|
server, ["--specter-data-folder", "~/.specter-some-folder", "--no-filelog"]
|
|
|
|
|
)
|
2020-12-06 15:40:22 +01:00
|
|
|
print(result.output)
|
|
|
|
|
if result.exception != None:
|
|
|
|
|
# Makes searching for issues much more convenient
|
|
|
|
|
traceback.print_tb(result.exception.__traceback__)
|
|
|
|
|
print(result.exception, file=sys.stderr)
|
|
|
|
|
print(mock_app.config.mock_calls)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
mock_app.config.__setitem__.assert_called_once_with(
|
|
|
|
|
"SPECTER_DATA_FOLDER", "~/.specter-some-folder"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.create_app")
|
|
|
|
|
@patch("cryptoadvance.specter.cli.cli_server.init_app")
|
|
|
|
|
def test_server_config(init_app, create_app, caplog):
|
2023-02-15 14:58:33 +01:00
|
|
|
"""This test will fail if you have turned on live-logging in pyproject.toml (log_cli = 1 )"""
|
2020-12-06 15:40:22 +01:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
mock_app = MagicMock()
|
|
|
|
|
mock_app.config = MagicMock()
|
|
|
|
|
d = {
|
2023-02-07 19:00:44 +01:00
|
|
|
"HOST": "127.0.0.1",
|
2020-12-06 15:40:22 +01:00
|
|
|
"PORT": "123",
|
|
|
|
|
"DEBUG": "WURSTBROT",
|
2020-12-20 15:03:23 +01:00
|
|
|
"SPECTER_SSL_CERT_SUBJECT_C": "AT",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_ST": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_L": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_O": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_OU": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SUBJECT_CN": "Blub",
|
|
|
|
|
"SPECTER_SSL_CERT_SERIAL_NUMBER": 123,
|
2020-12-06 15:40:22 +01:00
|
|
|
# We don't want to make a more sophisticated mock, so we simply set here
|
|
|
|
|
# the same value we set via CMD-Line
|
|
|
|
|
"CERT": "bla",
|
|
|
|
|
"KEY": "blub",
|
|
|
|
|
}
|
|
|
|
|
mock_app.config.__getitem__.side_effect = d.__getitem__
|
|
|
|
|
create_app.return_value = mock_app
|
|
|
|
|
runner = CliRunner()
|
2021-06-17 18:44:01 +02:00
|
|
|
try:
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
result = runner.invoke(server, ["--config", "MuhConfig", "--no-filelog"])
|
|
|
|
|
finally:
|
|
|
|
|
# not sure why i need to do that in an isolated_filesystem ?!
|
|
|
|
|
tidy_up()
|
2020-12-06 15:40:22 +01:00
|
|
|
print(result.output)
|
|
|
|
|
if result.exception != None:
|
|
|
|
|
# Makes searching for issues much more convenient
|
|
|
|
|
traceback.print_tb(result.exception.__traceback__)
|
|
|
|
|
print(result.exception, file=sys.stderr)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
print(mock_app.config.mock_calls)
|
|
|
|
|
create_app.assert_called_once_with(config="cryptoadvance.specter.config.MuhConfig")
|
2021-06-17 18:44:01 +02:00
|
|
|
tidy_up()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tidy_up():
|
|
|
|
|
if os.path.exists("bla"):
|
|
|
|
|
os.remove("bla")
|
|
|
|
|
if os.path.exists("blub"):
|
|
|
|
|
os.remove("blub")
|