2017-11-22 04:19:31 +00:00
|
|
|
import datetime
|
2018-02-28 03:57:55 +00:00
|
|
|
import os
|
2018-04-13 00:24:55 +00:00
|
|
|
import tempfile
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2018-04-12 03:17:39 +00:00
|
|
|
import maya
|
|
|
|
import pytest
|
2018-04-05 17:20:34 +00:00
|
|
|
from constant_sorrow import constants
|
2018-04-12 03:17:39 +00:00
|
|
|
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
|
2018-02-13 20:28:39 +00:00
|
|
|
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-12-15 04:36:52 +00:00
|
|
|
def idle_policy(alice, bob):
|
2017-11-22 04:19:31 +00:00
|
|
|
"""
|
2018-04-11 15:15:08 +00:00
|
|
|
Creates a Policy, in a manner typical of how Alice might do it, with a unique uri (soon to be "label" - see #183)
|
2017-11-22 04:19:31 +00:00
|
|
|
"""
|
2018-06-04 18:00:08 +00:00
|
|
|
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)
|
2018-04-11 15:15:08 +00:00
|
|
|
return policy
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-08 04:41:04 +00:00
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-06-04 18:00:08 +00:00
|
|
|
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)
|
|
|
|
|
2017-11-22 04:19:31 +00:00
|
|
|
# Alice has a policy in mind and knows of enough qualifies Ursulas; she crafts an offer for them.
|
2018-06-01 01:28:38 +00:00
|
|
|
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()
|
|
|
|
|
2018-06-04 20:51:05 +00:00
|
|
|
idle_policy.make_arrangements(network_middleware, deposit=deposit, quantity=3, expiration=contract_end_datetime)
|
2018-06-04 18:00:08 +00:00
|
|
|
idle_policy.enact(network_middleware) # REST call happens here, as does population of TreasureMap.
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-15 04:36:52 +00:00
|
|
|
return idle_policy
|
2017-11-22 04:19:31 +00:00
|
|
|
|
2017-12-08 04:41:04 +00:00
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-06-04 18:00:08 +00:00
|
|
|
def alice(ursulas, mock_policy_agent, deployed_testerchain):
|
|
|
|
|
|
|
|
etherbase, alice, bob, *everyone_else = deployed_testerchain.interface.w3.eth.accounts
|
2018-06-01 20:45:41 +00:00
|
|
|
|
2018-06-06 08:32:25 +00:00
|
|
|
_alice = Alice(network_middleware=MockRestMiddleware(),
|
2018-06-04 18:00:08 +00:00
|
|
|
policy_agent=mock_policy_agent, ether_address=alice)
|
2018-06-01 20:45:41 +00:00
|
|
|
|
|
|
|
return _alice
|
2017-11-22 04:19:31 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-04-17 08:46:13 +00:00
|
|
|
def bob(ursulas):
|
2018-06-06 08:32:25 +00:00
|
|
|
_bob = Bob(network_middleware=MockRestMiddleware())
|
2018-06-01 20:34:42 +00:00
|
|
|
return _bob
|
2017-11-22 04:19:31 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-06-04 18:00:08 +00:00
|
|
|
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)]
|
2018-06-01 20:45:41 +00:00
|
|
|
|
|
|
|
_ursulas = make_ursulas(ether_addresses=ursula_addresses,
|
2018-06-04 18:00:08 +00:00
|
|
|
ursula_starting_port=int(constants.URSULA_PORT_SEED))
|
2018-06-10 19:57:29 +00:00
|
|
|
|
|
|
|
MockRestMiddleware._ursulas = _ursulas
|
2018-06-01 20:45:41 +00:00
|
|
|
yield _ursulas
|
2018-02-28 03:52:22 +00:00
|
|
|
# Remove the DBs that have been sprayed hither and yon.
|
2018-06-10 19:57:29 +00:00
|
|
|
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))
|
2017-12-08 04:41:26 +00:00
|
|
|
|
|
|
|
|
2017-12-15 05:21:09 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2018-04-17 08:46:13 +00:00
|
|
|
def treasure_map_is_set_on_dht(enacted_policy, ursulas):
|
2018-06-06 08:32:25 +00:00
|
|
|
network_middleware = MockRestMiddleware()
|
2018-06-04 22:10:55 +00:00
|
|
|
enacted_policy.publish_treasure_map(network_middleware, use_dht=True)
|
2018-02-13 20:28:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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
|
2018-02-13 20:22:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2018-04-13 01:59:04 +00:00
|
|
|
def capsule_side_channel(enacted_policy):
|
2018-04-12 08:37:02 +00:00
|
|
|
signing_keypair = SigningKeypair()
|
2018-04-19 06:24:19 +00:00
|
|
|
data_source = DataSource(policy_pubkey_enc=enacted_policy.public_key,
|
2018-05-28 19:10:05 +00:00
|
|
|
signing_keypair=signing_keypair)
|
2018-04-12 08:37:02 +00:00
|
|
|
message_kit, _signature = data_source.encapsulate_single_message(b"Welcome to the flippering.")
|
|
|
|
return message_kit, data_source
|
2018-02-19 23:20:40 +00:00
|
|
|
|
|
|
|
|
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)
|