2020-08-10 23:59:13 +00:00
|
|
|
from collections import defaultdict
|
2018-11-03 21:48:11 +00:00
|
|
|
|
2018-11-09 15:25:20 +00:00
|
|
|
import pytest
|
2021-05-19 05:12:25 +00:00
|
|
|
from eth_utils.crypto import keccak
|
2018-11-09 15:25:20 +00:00
|
|
|
|
2023-09-14 17:00:24 +00:00
|
|
|
from nucypher.blockchain.eth.actors import Operator
|
2023-10-03 20:06:10 +00:00
|
|
|
from nucypher.blockchain.eth.domains import (
|
2023-10-05 02:09:40 +00:00
|
|
|
DomainInfo,
|
2023-10-03 16:50:54 +00:00
|
|
|
TACoDomain,
|
2023-09-29 17:00:58 +00:00
|
|
|
)
|
|
|
|
from nucypher.config.constants import TEMPORARY_DOMAIN
|
2019-08-31 04:05:57 +00:00
|
|
|
from nucypher.crypto.powers import TransactingPower
|
2020-12-11 18:29:12 +00:00
|
|
|
from nucypher.network.nodes import Learner
|
2019-07-18 03:50:11 +00:00
|
|
|
from nucypher.utilities.logging import GlobalLoggerSettings
|
2023-10-05 12:27:44 +00:00
|
|
|
from tests.constants import (
|
|
|
|
MOCK_IP_ADDRESS,
|
|
|
|
TESTERCHAIN_CHAIN_ID,
|
|
|
|
TESTERCHAIN_CHAIN_INFO,
|
|
|
|
)
|
2019-12-17 02:21:49 +00:00
|
|
|
|
2023-05-04 17:12:51 +00:00
|
|
|
# Don't re-lock accounts in the background while making commitments
|
2019-08-31 16:52:56 +00:00
|
|
|
LOCK_FUNCTION = TransactingPower.lock_account
|
|
|
|
TransactingPower.lock_account = lambda *a, **k: True
|
2019-08-31 04:05:57 +00:00
|
|
|
|
2020-08-10 23:59:13 +00:00
|
|
|
# Global test character cache
|
|
|
|
global_mutable_where_everybody = defaultdict(list)
|
|
|
|
|
2020-08-27 21:46:15 +00:00
|
|
|
Learner._DEBUG_MODE = False
|
2020-08-27 20:59:51 +00:00
|
|
|
|
2020-12-11 18:29:12 +00:00
|
|
|
|
2019-02-26 04:48:31 +00:00
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
2021-05-18 20:21:48 +00:00
|
|
|
def __very_pretty_and_insecure_scrypt_do_not_use(request):
|
2019-02-26 04:48:31 +00:00
|
|
|
"""
|
|
|
|
# WARNING: DO NOT USE THIS CODE ANYWHERE #
|
|
|
|
|
|
|
|
Mocks Scrypt derivation function for the duration of
|
|
|
|
the test session in order to improve test performance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Capture Scrypt derivation method
|
|
|
|
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
|
|
|
|
original_derivation_function = Scrypt.derive
|
|
|
|
|
|
|
|
# Patch Method
|
2021-05-19 05:12:25 +00:00
|
|
|
def __insecure_derive(_scrypt, key_material: bytes):
|
2019-02-26 04:48:31 +00:00
|
|
|
"""Temporarily replaces Scrypt.derive for mocking"""
|
2021-05-19 05:12:25 +00:00
|
|
|
return keccak(key_material)
|
2019-02-26 04:48:31 +00:00
|
|
|
|
|
|
|
# Disable Scrypt KDF
|
|
|
|
Scrypt.derive = __insecure_derive
|
|
|
|
yield
|
|
|
|
# Re-Enable Scrypt KDF
|
|
|
|
Scrypt.derive = original_derivation_function
|
|
|
|
|
2019-02-22 05:38:54 +00:00
|
|
|
|
2021-01-03 00:52:18 +00:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def monkeysession():
|
2020-05-17 20:06:41 +00:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2022-12-07 11:14:59 +00:00
|
|
|
|
|
|
|
mpatch = MonkeyPatch()
|
|
|
|
yield mpatch
|
|
|
|
mpatch.undo()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def monkeymodule():
|
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
|
2020-05-17 20:06:41 +00:00
|
|
|
mpatch = MonkeyPatch()
|
|
|
|
yield mpatch
|
|
|
|
mpatch.undo()
|
|
|
|
|
|
|
|
|
2018-11-20 04:29:01 +00:00
|
|
|
#
|
2018-11-09 15:25:20 +00:00
|
|
|
# Pytest configuration
|
2018-11-20 04:29:01 +00:00
|
|
|
#
|
|
|
|
|
2018-06-04 18:00:08 +00:00
|
|
|
|
2022-10-30 15:34:57 +00:00
|
|
|
pytest_plugins = [
|
|
|
|
'pytest-nucypher', # Includes external fixtures module via plugin
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-05-07 02:11:20 +00:00
|
|
|
def pytest_collection_modifyitems(config, items):
|
2018-11-17 19:27:53 +00:00
|
|
|
log_level_name = config.getoption("--log-level", "info", skip=True)
|
2019-11-07 08:31:02 +00:00
|
|
|
GlobalLoggerSettings.stop_sentry_logging()
|
2019-07-18 03:50:11 +00:00
|
|
|
GlobalLoggerSettings.set_log_level(log_level_name)
|
|
|
|
GlobalLoggerSettings.start_text_file_logging()
|
|
|
|
GlobalLoggerSettings.start_json_file_logging()
|
2020-08-07 20:47:30 +00:00
|
|
|
|
|
|
|
|
2020-12-12 00:41:24 +00:00
|
|
|
# global_mutable_where_everybody = defaultdict(list) # TODO: cleanup
|
2020-08-07 20:47:30 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def check_character_state_after_test(request):
|
2020-08-24 14:07:40 +00:00
|
|
|
from nucypher.network.nodes import Learner
|
2020-08-07 20:47:30 +00:00
|
|
|
yield
|
2020-08-24 14:07:40 +00:00
|
|
|
if Learner._DEBUG_MODE:
|
|
|
|
module_name = request.module.__name__
|
|
|
|
|
|
|
|
test_learners = global_mutable_where_everybody.get(module_name, [])
|
|
|
|
# Those match the module name exactly; maybe there are some that we got by frame.
|
|
|
|
for maybe_frame, learners in global_mutable_where_everybody.items():
|
|
|
|
if f"{module_name}.py" in maybe_frame:
|
|
|
|
test_learners.extend(learners)
|
|
|
|
|
|
|
|
crashed = [learner for learner in test_learners if learner._crashed]
|
|
|
|
|
|
|
|
if any(crashed):
|
|
|
|
failure_message = ""
|
|
|
|
for learner in crashed:
|
|
|
|
failure_message += learner._crashed.getBriefTraceback()
|
|
|
|
pytest.fail(f"Some learners crashed:{failure_message}")
|
|
|
|
|
|
|
|
still_running = [learner for learner in test_learners if learner._learning_task.running]
|
|
|
|
|
|
|
|
if any(still_running):
|
|
|
|
offending_tests = set()
|
|
|
|
for learner in still_running:
|
|
|
|
offending_tests.add(learner._FOR_TEST)
|
|
|
|
try: # TODO: Deal with stop vs disenchant. Currently stop is only for Ursula.
|
|
|
|
learner.stop()
|
2020-09-22 05:21:07 +00:00
|
|
|
learner._finalize()
|
2020-08-24 14:07:40 +00:00
|
|
|
except AttributeError:
|
|
|
|
learner.disenchant()
|
|
|
|
pytest.fail(f"Learners remaining: {still_running}. Offending tests: {offending_tests} ")
|
2020-08-27 20:59:51 +00:00
|
|
|
|
|
|
|
still_tracking = [learner for learner in test_learners if hasattr(learner, 'work_tracker') and learner.work_tracker._tracking_task.running]
|
|
|
|
for tracker in still_tracking:
|
|
|
|
tracker.work_tracker.stop()
|
2020-12-10 00:49:29 +00:00
|
|
|
|
|
|
|
|
2020-12-26 06:00:03 +00:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
2020-12-12 01:16:14 +00:00
|
|
|
def mock_get_external_ip_from_url_source(session_mocker):
|
2020-12-26 06:00:03 +00:00
|
|
|
target = 'nucypher.cli.actions.configure.determine_external_ip_address'
|
|
|
|
session_mocker.patch(target, return_value=MOCK_IP_ADDRESS)
|
2021-02-07 16:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def disable_check_grant_requirements(session_mocker):
|
|
|
|
target = 'nucypher.characters.lawful.Alice._check_grant_requirements'
|
|
|
|
session_mocker.patch(target, return_value=MOCK_IP_ADDRESS)
|
2023-07-12 18:27:07 +00:00
|
|
|
|
|
|
|
|
2023-10-05 10:55:32 +00:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def mock_condition_blockchains(module_mocker):
|
2023-07-12 18:27:07 +00:00
|
|
|
"""adds testerchain's chain ID to permitted conditional chains"""
|
2023-10-05 10:55:32 +00:00
|
|
|
module_mocker.patch.dict(
|
2023-07-12 18:27:07 +00:00
|
|
|
"nucypher.policy.conditions.evm._CONDITION_CHAINS",
|
|
|
|
{TESTERCHAIN_CHAIN_ID: "eth-tester/pyevm"},
|
|
|
|
)
|
2023-10-05 02:09:40 +00:00
|
|
|
test_domain_info = DomainInfo(
|
2023-10-05 12:27:44 +00:00
|
|
|
TEMPORARY_DOMAIN, TESTERCHAIN_CHAIN_INFO, TESTERCHAIN_CHAIN_INFO
|
2023-09-29 17:00:58 +00:00
|
|
|
)
|
2023-08-22 08:45:37 +00:00
|
|
|
|
2023-10-05 10:55:32 +00:00
|
|
|
module_mocker.patch.object(
|
2023-10-05 02:09:40 +00:00
|
|
|
TACoDomain, "get_domain_info", return_value=test_domain_info
|
2023-08-22 08:45:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
|
|
def mock_multichain_configuration(module_mocker, testerchain):
|
|
|
|
module_mocker.patch.object(
|
2023-09-14 17:00:24 +00:00
|
|
|
Operator, "_make_condition_provider", return_value=testerchain.provider
|
2023-08-22 08:45:37 +00:00
|
|
|
)
|