nucypher/tests/characters/test_alice_can_grant_and_re...

64 lines
2.4 KiB
Python
Raw Normal View History

2017-12-10 01:21:43 +00:00
import datetime
import maya
import pytest
2018-02-11 09:06:39 +00:00
from apistar.test import TestClient
from bytestring_splitter import BytestringSplitter
from constant_sorrow import constants
from umbral.fragments import KFrag
from umbral.keys import UmbralPublicKey
2018-02-11 09:06:39 +00:00
2018-05-08 19:35:34 +00:00
from nucypher.characters import Ursula
from nucypher.crypto.api import keccak_digest
from nucypher.crypto.powers import SigningPower, EncryptingPower
2017-12-10 01:21:43 +00:00
@pytest.mark.usefixtures('token_airdrop')
def test_grant(alice, bob, ursulas, mock_miner_agent):
_etherbase, ursula_address, *everybody_else = mock_miner_agent.blockchain.interface.w3.eth.accounts
mock_miner_agent.spawn_random_miners(addresses=everybody_else)
mock_miner_agent.blockchain.time_travel(periods=1)
ursula, *other_ursulas = ursulas
alice.learn_about_nodes(rest_address=ursula.ip_address, port=ursula.rest_port)
2018-04-07 02:26:13 +00:00
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
2017-12-10 01:21:43 +00:00
n = 5
uri = b"this_is_the_path_to_which_access_is_being_granted"
policy = alice.grant(bob, uri, m=3, n=n, expiration=policy_end_datetime)
2017-12-10 01:21:43 +00:00
# The number of policies is equal to the number of Ursulas we're using (n)
2018-04-02 04:16:51 +00:00
assert len(policy._accepted_arrangements) == n
2017-12-10 01:21:43 +00:00
# Let's look at the first Ursula.
2018-04-02 04:16:51 +00:00
ursula = list(policy._accepted_arrangements.values())[0].ursula
2017-12-10 01:21:43 +00:00
2018-02-12 21:12:41 +00:00
# Get the Policy from Ursula's datastore, looking up by hrac.
proper_hrac = keccak_digest(bytes(alice.stamp) + bytes(bob.stamp) + uri)
2018-04-02 04:16:51 +00:00
retrieved_policy = ursula.datastore.get_policy_arrangement(proper_hrac.hex().encode())
# TODO: Make this a legit KFrag, not bytes.
retrieved_k_frag = KFrag.from_bytes(retrieved_policy.k_frag)
# TODO: Implement KFrag.__eq__
found = False
for k_frag in policy.kfrags:
if bytes(k_frag) == bytes(retrieved_k_frag):
found = True
assert found
@pytest.mark.usefixtures('deployed_testerchain')
def test_alice_can_get_ursulas_keys_via_rest(ursulas):
mock_client = TestClient(ursulas[0].rest_app)
response = mock_client.get('http://localhost/public_keys')
2018-02-11 09:06:39 +00:00
splitter = BytestringSplitter(
(UmbralPublicKey, constants.PUBLIC_KEY_LENGTH),
(UmbralPublicKey, constants.PUBLIC_KEY_LENGTH)
2018-02-11 09:06:39 +00:00
)
signing_key, encrypting_key = splitter(response.content)
public_keys = {SigningPower: signing_key, EncryptingPower: encrypting_key}
stranger_ursula_from_public_keys = Ursula.from_public_keys(public_keys)
assert stranger_ursula_from_public_keys == ursulas[0]