handle mocking side-effects from ferveo public key mismatch scenarios

pull/3533/head
KPrasch 2024-07-31 23:25:34 +07:00
parent e65d9a9c73
commit 2291bf1b48
No known key found for this signature in database
6 changed files with 45 additions and 9 deletions

View File

@ -870,6 +870,11 @@ def clear_config_root(temp_config_root):
if temp_config_root.exists():
print(f"Removing {temp_config_root}")
shutil.rmtree(Path("/tmp/nucypher-test"))
yield
if DEFAULT_CONFIG_ROOT.exists():
raise RuntimeError(
f"{DEFAULT_CONFIG_ROOT} was used by a test. This is not permitted, please mock."
)
@pytest.fixture(scope="session", autouse=True)

View File

@ -1,5 +1,6 @@
import json
import pytest
from nucypher_core import Address, Conditions, RetrievalKit
from nucypher_core._nucypher_core import MessageKit
@ -15,6 +16,7 @@ def _policy_info_kwargs(enacted_policy):
)
@pytest.mark.usefixtures("mock_payment_method")
def test_retrieval_kit(enacted_policy, ursulas):
messages, message_kits = make_message_kits(enacted_policy.public_key)
@ -29,7 +31,9 @@ def test_retrieval_kit(enacted_policy, ursulas):
assert retrieval_kit.queried_addresses == retrieval_kit_back.queried_addresses
def test_single_retrieve(enacted_policy, bob, ursulas):
@pytest.mark.usefixtures("mock_payment_method")
def test_single_retrieve(enacted_policy, bob, ursulas, mocker):
bob.remember_node(ursulas[0])
bob.start_learning_loop()
messages, message_kits = make_message_kits(enacted_policy.public_key)
@ -42,6 +46,7 @@ def test_single_retrieve(enacted_policy, bob, ursulas):
assert cleartexts == messages
@pytest.mark.usefixtures("mock_payment_method")
def test_single_retrieve_conditions_set_directly_to_none(enacted_policy, bob, ursulas):
bob.start_learning_loop()
message = b"plaintext1"
@ -59,6 +64,7 @@ def test_single_retrieve_conditions_set_directly_to_none(enacted_policy, bob, ur
assert cleartexts == [message]
@pytest.mark.usefixtures("mock_payment_method")
def test_single_retrieve_conditions_empty_list(enacted_policy, bob, ursulas):
bob.start_learning_loop()
message = b"plaintext1"
@ -76,6 +82,7 @@ def test_single_retrieve_conditions_empty_list(enacted_policy, bob, ursulas):
assert cleartexts == [message]
@pytest.mark.usefixtures("mock_payment_method")
def test_use_external_cache(enacted_policy, bob, ursulas):
bob.start_learning_loop()

View File

@ -12,6 +12,7 @@ from tests.constants import MOCK_ETH_PROVIDER_URI
from tests.utils.middleware import MockRestMiddleware
@pytest.mark.usefixtures("mock_payment_method")
def test_bob_full_retrieve_flow(
ursulas, bob, alice, capsule_side_channel, treasure_map, enacted_policy
):
@ -35,6 +36,7 @@ def test_bob_full_retrieve_flow(
assert b"Welcome to flippering number 0." == delivered_cleartexts[0]
@pytest.mark.usefixtures("mock_payment_method")
def test_bob_retrieves(accounts, alice, ursulas):
"""A test to show that Bob can retrieve data from Ursula"""
@ -96,6 +98,7 @@ def test_bob_retrieves(accounts, alice, ursulas):
bob.disenchant()
@pytest.mark.usefixtures("mock_payment_method")
def test_bob_retrieves_with_treasure_map(
bob, ursulas, enacted_policy, capsule_side_channel
):
@ -118,6 +121,7 @@ def test_bob_retrieves_with_treasure_map(
# TODO: #2813 Without kfrag and arrangement storage by nodes,
@pytest.mark.skip()
@pytest.mark.usefixtures("mock_payment_method")
def test_bob_retrieves_too_late(bob, ursulas, enacted_policy, capsule_side_channel):
clock = Clock()
clock.advance(time.time())

View File

@ -26,7 +26,10 @@ def _policy_info_kwargs(enacted_policy):
)
def test_single_retrieve_with_truthy_conditions(enacted_policy, bob, ursulas, mocker):
@pytest.mark.usefixtures("mock_payment_method")
def test_single_retrieve_with_truthy_conditions(
enacted_policy, bob, ursulas, mocker, mock_payment_method
):
from nucypher_core import MessageKit
reencrypt_spy = mocker.spy(Ursula, '_reencrypt')
@ -68,7 +71,10 @@ def test_single_retrieve_with_truthy_conditions(enacted_policy, bob, ursulas, mo
assert reencrypt_spy.call_count == enacted_policy.threshold
def test_single_retrieve_with_falsy_conditions(enacted_policy, bob, ursulas, mocker):
@pytest.mark.usefixtures("mock_payment_method")
def test_single_retrieve_with_falsy_conditions(
enacted_policy, bob, ursulas, mocker, mock_payment_method
):
from nucypher_core import MessageKit
reencrypt_spy = mocker.spy(Ursula, '_reencrypt')
@ -124,6 +130,7 @@ FAILURE_CASE_EXCEPTION_CODE_MATCHING = [
"eval_failure_exception_class, middleware_exception_class",
FAILURE_CASE_EXCEPTION_CODE_MATCHING,
)
@pytest.mark.usefixtures("mock_payment_method")
def test_middleware_handling_of_failed_condition_responses(
eval_failure_exception_class,
middleware_exception_class,
@ -131,6 +138,7 @@ def test_middleware_handling_of_failed_condition_responses(
enacted_policy,
bob,
mock_rest_middleware,
mock_payment_method,
):
# we use a failed condition for reencryption to test conversion of response codes to middleware exceptions
from nucypher_core import MessageKit

View File

@ -26,6 +26,7 @@ from nucypher.cli.types import ChecksumAddress
from nucypher.config.characters import UrsulaConfiguration
from nucypher.crypto.powers import TransactingPower
from nucypher.network.nodes import Teacher
from nucypher.policy.payment import SubscriptionManagerPayment
from tests.constants import (
KEYFILE_NAME_TEMPLATE,
MOCK_KEYSTORE_PATH,
@ -33,6 +34,7 @@ from tests.constants import (
TEMPORARY_DOMAIN,
TESTERCHAIN_CHAIN_ID,
)
from tests.mock.agents import MockContractAgency
from tests.mock.interfaces import MockBlockchain
from tests.mock.io import MockStdinWrapper
from tests.utils.registry import MockRegistrySource, mock_registry_sources
@ -133,7 +135,6 @@ def test_registry(module_mocker):
@pytest.fixture(scope='module', autouse=True)
def mock_contract_agency():
# Patch
from tests.mock.agents import MockContractAgency
# Monkeypatch # TODO: Use better tooling for this monkeypatch?
get_agent = ContractAgency.get_agent
@ -295,3 +296,8 @@ def multichain_ursulas(ursulas, multichain_ids):
@pytest.fixture(scope="module")
def mock_prometheus(module_mocker):
return module_mocker.patch("nucypher.characters.lawful.start_prometheus_exporter")
@pytest.fixture(scope="module")
def mock_payment_method(module_mocker):
module_mocker.patch.object(SubscriptionManagerPayment, "verify", return_value=True)

View File

@ -19,7 +19,7 @@ BlockchainInterfaceFactory._interfaces[MOCK_ETH_PROVIDER_URI] = CACHED_MOCK_TEST
class MockContractAgent:
FAKE_CALL_RESULT = 1
FAKE_CALL_RESULT = 0
# Internal
__COLLECTION_MARKER = "contract_api" # decorator attribute
@ -53,7 +53,8 @@ class MockContractAgent:
def __setup_mock(self, agent_class: Type[Agent]) -> None:
api_methods: Iterable[Callable] = list(self.__collect_contract_api(agent_class=agent_class))
mock_methods, mock_properties = list(), dict()
mock_methods = list()
# mock_properties = dict()
for agent_interface in api_methods:
@ -88,7 +89,8 @@ class MockContractAgent:
self._REAL_METHODS = api_methods
def __get_interface_calls(self, interface: Enum) -> List[Callable]:
predicate = lambda method: bool(method.contract_api == interface)
def predicate(method):
return bool(method.contract_api == interface)
interface_calls = list(filter(predicate, self._MOCK_METHODS))
return interface_calls
@ -127,9 +129,13 @@ class MockContractAgent:
def get_unexpected_transactions(self, allowed: Union[Iterable[Callable], None]) -> List[Callable]:
if allowed:
predicate = lambda tx: tx not in allowed and tx.called
def predicate(tx):
return tx not in allowed and tx.called
else:
predicate = lambda tx: tx.called
def predicate(tx):
return tx.called
unexpected_transactions = list(filter(predicate, self.all_transactions))
return unexpected_transactions