nucypher/tests/fixtures.py

111 lines
3.8 KiB
Python
Raw Normal View History

import datetime
import os
2018-04-13 00:24:55 +00:00
import tempfile
import maya
import pytest
from constant_sorrow import constants
from sqlalchemy.engine import create_engine
2018-05-08 19:35:34 +00:00
from nucypher.characters import Alice, Bob
from nucypher.data_sources import DataSource
from nucypher.keystore import keystore
from nucypher.keystore.db import Base
from nucypher.keystore.keypairs import SigningKeypair
2018-06-06 08:32:25 +00:00
from tests.utilities import make_ursulas, MockRestMiddleware
@pytest.fixture(scope="module")
def idle_policy(alice, bob):
"""
Creates a Policy, in a manner typical of how Alice might do it, with a unique uri (soon to be "label" - see #183)
"""
n = int(constants.NUMBER_OF_URSULAS_IN_NETWORK)
random_label = b'label://' + os.urandom(32)
policy = alice.create_policy(bob, label=random_label, m=3, n=n)
return policy
@pytest.fixture(scope="module")
def enacted_policy(idle_policy, ursulas, mock_miner_agent, mock_token_agent):
_origin, ursula, *everybody_else = mock_miner_agent.blockchain.interface.w3.eth.accounts
mock_token_agent.token_airdrop(amount=100000*constants.M) # blocks
mock_miner_agent.spawn_random_miners(addresses=everybody_else)
mock_miner_agent.blockchain.time_travel(periods=1)
# Alice has a policy in mind and knows of enough qualifies Ursulas; she crafts an offer for them.
deposit = constants.NON_PAYMENT(b"0000000")
2018-04-07 02:26:13 +00:00
contract_end_datetime = maya.now() + datetime.timedelta(days=5)
2018-06-06 08:32:25 +00:00
network_middleware = MockRestMiddleware()
idle_policy.make_arrangements(network_middleware, deposit=deposit, quantity=3, expiration=contract_end_datetime)
idle_policy.enact(network_middleware) # REST call happens here, as does population of TreasureMap.
return idle_policy
@pytest.fixture(scope="module")
def alice(ursulas, mock_policy_agent, deployed_testerchain):
etherbase, alice, bob, *everyone_else = deployed_testerchain.interface.w3.eth.accounts
2018-06-06 08:32:25 +00:00
_alice = Alice(network_middleware=MockRestMiddleware(),
policy_agent=mock_policy_agent, ether_address=alice)
return _alice
@pytest.fixture(scope="module")
def bob(ursulas):
2018-06-06 08:32:25 +00:00
_bob = Bob(network_middleware=MockRestMiddleware())
return _bob
@pytest.fixture(scope="module")
def ursulas(deployed_testerchain):
etherbase, alice, bob, *everyone_else = deployed_testerchain.interface.w3.eth.accounts
ursula_addresses = everyone_else[:int(constants.NUMBER_OF_URSULAS_IN_NETWORK)]
_ursulas = make_ursulas(ether_addresses=ursula_addresses,
ursula_starting_port=int(constants.URSULA_PORT_SEED))
MockRestMiddleware._ursulas = _ursulas
yield _ursulas
2018-02-28 03:52:22 +00:00
# Remove the DBs that have been sprayed hither and yon.
MockRestMiddleware._ursulas = None
for port, ursula in enumerate(_ursulas, start=int(constants.URSULA_PORT_SEED)):
2018-02-28 03:52:22 +00:00
os.remove("test-{}".format(port))
@pytest.fixture(scope="module")
def treasure_map_is_set_on_dht(enacted_policy, ursulas):
2018-06-06 08:32:25 +00:00
network_middleware = MockRestMiddleware()
enacted_policy.publish_treasure_map(network_middleware, use_dht=True)
@pytest.fixture(scope="module")
def test_keystore():
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
test_keystore = keystore.KeyStore(engine)
yield test_keystore
@pytest.fixture(scope="module")
def capsule_side_channel(enacted_policy):
signing_keypair = SigningKeypair()
data_source = DataSource(policy_pubkey_enc=enacted_policy.public_key,
signing_keypair=signing_keypair)
message_kit, _signature = data_source.encapsulate_single_message(b"Welcome to the flippering.")
return message_kit, data_source
2018-04-13 00:24:55 +00:00
@pytest.fixture(scope="function")
def tempfile_path():
"""
User is responsible for closing the file given at the path.
"""
_, path = tempfile.mkstemp()
yield path
os.remove(path)