Fixing up tests.

pull/345/head
jMyles 2018-07-02 05:18:31 -07:00
parent bd215d975a
commit bcc159367f
6 changed files with 33 additions and 50 deletions

View File

@ -28,8 +28,6 @@ def test_bob_cannot_follow_the_treasure_map_in_isolation(enacted_federated_polic
def test_bob_already_knows_all_nodes_in_treasure_map(enacted_federated_policy, ursulas, bob, alice): def test_bob_already_knows_all_nodes_in_treasure_map(enacted_federated_policy, ursulas, bob, alice):
hrac = enacted_federated_policy.hrac()
# Bob knows of no Ursulas. # Bob knows of no Ursulas.
assert len(bob._known_nodes) == 0 assert len(bob._known_nodes) == 0
@ -38,8 +36,8 @@ def test_bob_already_knows_all_nodes_in_treasure_map(enacted_federated_policy, u
bob.remember_node(ursula) bob.remember_node(ursula)
# Now, Bob can get the TreasureMap all by himself, and doesn't need a side channel. # Now, Bob can get the TreasureMap all by himself, and doesn't need a side channel.
bob.get_treasure_map(alice.stamp, hrac) map = bob.get_treasure_map(alice.stamp, enacted_federated_policy.label)
unknown, known = bob.peek_at_treasure_map(hrac) unknown, known = bob.peek_at_treasure_map(map.public_id())
# He finds that he didn't need to discover any new nodes... # He finds that he didn't need to discover any new nodes...
assert len(unknown) == 0 assert len(unknown) == 0

View File

@ -82,7 +82,7 @@ def test_character_blockchain_power(testerchain):
_key_lookup[eth_utils.to_canonical_address(eth_address)] _key_lookup[eth_utils.to_canonical_address(eth_address)]
sig_pubkey = sig_privkey.public_key sig_pubkey = sig_privkey.public_key
signer = Character(is_me=True) signer = Character(is_me=True, checksum_address=eth_address)
signer._crypto_power.consume_power_up(BlockchainPower(testerchain, eth_address)) signer._crypto_power.consume_power_up(BlockchainPower(testerchain, eth_address))
# Due to testing backend, the account is already unlocked. # Due to testing backend, the account is already unlocked.

View File

@ -1,14 +1,10 @@
import asyncio import asyncio
import msgpack
import pytest import pytest
from constant_sorrow import constants, default_constant_splitter
from kademlia.utils import digest from kademlia.utils import digest
from nucypher.crypto.api import keccak_digest from nucypher.crypto.api import keccak_digest
from nucypher.crypto.kits import UmbralMessageKit from tests.utilities import MockRestMiddleware
from nucypher.policy.models import TreasureMap
from tests.utilities import TEST_EVENT_LOOP, MockRestMiddleware
@pytest.mark.usefixtures('testerchain') @pytest.mark.usefixtures('testerchain')
@ -54,7 +50,6 @@ def test_vladimir_illegal_interface_key_does_not_propagate(ursulas):
@pytest.mark.skip("What do we want this test to do now?") @pytest.mark.skip("What do we want this test to do now?")
def test_alice_finds_ursula_via_rest(alice, ursulas): def test_alice_finds_ursula_via_rest(alice, ursulas):
# Imagine alice knows of nobody. # Imagine alice knows of nobody.
alice._known_nodes = {} alice._known_nodes = {}
@ -68,15 +63,15 @@ def test_alice_finds_ursula_via_rest(alice, ursulas):
assert ursula.stamp.as_umbral_pubkey() in new_nodes assert ursula.stamp.as_umbral_pubkey() in new_nodes
def test_alice_creates_policy_group_with_correct_hrac(idle_federated_policy): def test_alice_creates_policy_with_correct_hrac(idle_federated_policy):
""" """
Alice creates a PolicyGroup. It has the proper HRAC, unique per her, Bob, and the uri (resource_id). Alice creates a Policy. It has the proper HRAC, unique per her, Bob, and the uri (resource_id).
""" """
alice = idle_federated_policy.alice alice = idle_federated_policy.alice
bob = idle_federated_policy.bob bob = idle_federated_policy.bob
assert idle_federated_policy.hrac() == keccak_digest( assert idle_federated_policy.hrac() == keccak_digest(
bytes(alice.stamp) + bytes(bob.stamp) + alice.__resource_id) bytes(alice.stamp) + bytes(bob.stamp) + idle_federated_policy.label)
def test_alice_sets_treasure_map(enacted_federated_policy, ursulas): def test_alice_sets_treasure_map(enacted_federated_policy, ursulas):
@ -86,9 +81,9 @@ def test_alice_sets_treasure_map(enacted_federated_policy, ursulas):
networky_stuff = MockRestMiddleware() networky_stuff = MockRestMiddleware()
enacted_federated_policy.publish_treasure_map(network_middleare=networky_stuff) enacted_federated_policy.publish_treasure_map(network_middleare=networky_stuff)
treasure_map_as_set_on_network = list(ursulas)[0].dht_server.storage[ treasure_map_as_set_on_network = list(ursulas)[0].treasure_maps[
digest(enacted_federated_policy.treasure_map_dht_key())] digest(enacted_federated_policy.treasure_map.public_id())]
assert treasure_map_as_set_on_network == constants.BYTESTRING_IS_TREASURE_MAP + bytes(enacted_federated_policy.treasure_map) assert treasure_map_as_set_on_network == enacted_federated_policy.treasure_map
@pytest.mark.skip("Needs cleanup.") @pytest.mark.skip("Needs cleanup.")
@ -104,8 +99,8 @@ def test_treasure_map_with_bad_id_does_not_propagate(idle_federated_policy, ursu
message_kit, signature = alice.encrypt_for(bob, treasure_map.packed_payload()) message_kit, signature = alice.encrypt_for(bob, treasure_map.packed_payload())
alice.network_middleware.put_treasure_map_on_node(node=ursulas[1], alice.network_middleware.put_treasure_map_on_node(node=ursulas[1],
map_id=illegal_policygroup_id, map_id=illegal_policygroup_id,
map_payload=message_kit.to_bytes()) map_payload=message_kit.to_bytes())
# setter = alice.server.set(illegal_policygroup_id, message_kit.to_bytes()) # setter = alice.server.set(illegal_policygroup_id, message_kit.to_bytes())
# _set_event = TEST_EVENT_LOOP.run_until_complete(setter) # _set_event = TEST_EVENT_LOOP.run_until_complete(setter)
@ -119,45 +114,37 @@ def test_treasure_map_stored_by_ursula_is_the_correct_one_for_bob(alice, bob, ur
""" """
The TreasureMap given by Alice to Ursula is the correct one for Bob; he can decrypt and read it. The TreasureMap given by Alice to Ursula is the correct one for Bob; he can decrypt and read it.
""" """
treasure_map_as_set_on_network = list(ursulas)[0].dht_server.storage[ treasure_map_as_set_on_network = list(ursulas)[0].treasure_maps[
digest(enacted_federated_policy.treasure_map_dht_key())] digest(enacted_federated_policy.treasure_map.public_id())]
header, payload = default_constant_splitter(treasure_map_as_set_on_networka) hrac_by_bob = bob.construct_policy_hrac(alice.stamp, enacted_federated_policy.label)
assert enacted_federated_policy.hrac() == hrac_by_bob
treasure_map = TreasureMap.from_bytes(payload) map_id_by_bob = bob.construct_map_id(alice.stamp, enacted_federated_policy.label)
assert map_id_by_bob == treasure_map_as_set_on_network.public_id()
assert header == constants.BYTESTRING_IS_TREASURE_MAP
tmap_message_kit = UmbralMessageKit.from_bytes(encrypted_treasure_map)
verified, treasure_map_as_decrypted_by_bob = bob.verify_from(alice,
tmap_message_kit,
decrypt=True)
assert treasure_map_as_decrypted_by_bob == enacted_federated_policy.treasure_map.packed_payload()
assert verified is True
@pytest.mark.usefixtures("treasure_map_is_set_on_dht")
def test_bob_can_retreive_the_treasure_map_and_decrypt_it(enacted_federated_policy, ursulas): def test_bob_can_retreive_the_treasure_map_and_decrypt_it(enacted_federated_policy, ursulas):
""" """
Above, we showed that the TreasureMap saved on the network is the correct one for Bob. Here, we show Above, we showed that the TreasureMap saved on the network is the correct one for Bob. Here, we show
that Bob can retrieve it with only the information about which he is privy pursuant to the PolicyGroup. that Bob can retrieve it with only the information about which he is privy pursuant to the PolicyGroup.
""" """
bob = enacted_federated_policy.bob bob = enacted_federated_policy.bob
_ = MockRestMiddleware()
# Of course, in the real world, Bob has sufficient information to reconstitute a PolicyGroup, gleaned, we presume, # Of course, in the real world, Bob has sufficient information to reconstitute a PolicyGroup, gleaned, we presume,
# through a side-channel with Alice. # through a side-channel with Alice.
# If Bob doesn't know about any Ursulas, he can't find the TreasureMap via the REST swarm: # If Bob doesn't know about any Ursulas, he can't find the TreasureMap via the REST swarm:
with pytest.raises(bob.NotEnoughUrsulas): with pytest.raises(bob.NotEnoughUrsulas):
treasure_map_from_wire = bob.get_treasure_map(enacted_federated_policy.alice.stamp, enacted_federated_policy.hrac()) treasure_map_from_wire = bob.get_treasure_map(enacted_federated_policy.alice.stamp,
enacted_federated_policy.label)
# Let's imagine he has learned about some - say, from the blockchain. # Let's imagine he has learned about some - say, from the blockchain.
bob._known_nodes = {u.interface_info_with_metadata(): u for u in ursulas} bob._known_nodes = {u.canonical_public_address: u for u in ursulas}
# Now try. # Now try.
treasure_map_from_wire = bob.get_treasure_map(enacted_federated_policy.alice.stamp, enacted_federated_policy.hrac()) treasure_map_from_wire = bob.get_treasure_map(enacted_federated_policy.alice.stamp,
enacted_federated_policy.label)
assert enacted_federated_policy.treasure_map == treasure_map_from_wire assert enacted_federated_policy.treasure_map == treasure_map_from_wire
@ -166,11 +153,5 @@ def test_treaure_map_is_legit(enacted_federated_policy):
""" """
Sure, the TreasureMap can get to Bob, but we also need to know that each Ursula in the TreasureMap is on the network. Sure, the TreasureMap can get to Bob, but we also need to know that each Ursula in the TreasureMap is on the network.
""" """
alice = enacted_federated_policy.alice for ursula_address in enacted_federated_policy.treasure_map:
for ursula_interface_id in enacted_federated_policy.treasure_map: assert ursula_address in enacted_federated_policy.bob._known_nodes
value = alice.server.get_now(ursula_interface_id)
header, signature, ursula_pubkey_sig, interface_info = dht_value_splitter(value, return_remainder=True)
assert header == constants.BYTESTRING_IS_URSULA_IFACE_INFO
port = msgpack.loads(interface_info)[1]
legal_ports = range(int(constants.NUMBER_OF_URSULAS_IN_NETWORK), int(constants.NUMBER_OF_URSULAS_IN_NETWORK+constants.URSULA_PORT_SEED))
assert port in legal_ports

View File

@ -1,8 +1,8 @@
def test_alice_enacts_policies_in_policy_group_via_rest(enacted_policy): def test_alice_enacts_policies_in_policy_group_via_rest(enacted_federated_policy):
""" """
Now that Alice has made a PolicyGroup, she can enact its policies, using Ursula's Public Key to encrypt each offer Now that Alice has made a PolicyGroup, she can enact its policies, using Ursula's Public Key to encrypt each offer
and transmitting them via REST. and transmitting them via REST.
""" """
ursula = list(enacted_policy._accepted_arrangements.values())[0].ursula ursula = list(enacted_federated_policy._accepted_arrangements)[0].ursula
policy_arrangement = ursula.datastore.get_policy_arrangement(enacted_policy.hrac().hex().encode()) policy_arrangement = ursula.datastore.get_policy_arrangement(enacted_federated_policy.hrac().hex().encode())
assert bool(policy_arrangement) # TODO: This can be a more poignant assertion. assert bool(policy_arrangement) # TODO: This can be a more poignant assertion.

View File

@ -1,2 +1,5 @@
import pytest
@pytest.mark.skip
def test_eager_learn_from_teacher(): def test_eager_learn_from_teacher():
assert False assert False

View File

@ -170,7 +170,8 @@ class MockRestMiddleware(RestMiddleware):
return mock_client.post('http://localhost/kFrag/{}/reencrypt'.format(hrac_as_hex), payload) return mock_client.post('http://localhost/kFrag/{}/reencrypt'.format(hrac_as_hex), payload)
def get_treasure_map_from_node(self, node, map_id): def get_treasure_map_from_node(self, node, map_id):
mock_client = TestClient(node.rest_app) running_node = _ALL_URSULAS[node.rest_interface.port]
mock_client = TestClient(running_node.rest_app)
return mock_client.get("http://localhost/treasure_map/{}".format(map_id)) return mock_client.get("http://localhost/treasure_map/{}".format(map_id))
def ursula_from_rest_interface(self, address, port): def ursula_from_rest_interface(self, address, port):