nucypher/tests/characters/test_alice_can_grant_and_re...

83 lines
2.9 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
def test_grant(alice, bob, mining_ursulas, three_agents):
ursula, *other_ursulas = mining_ursulas
2018-04-07 02:26:13 +00:00
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
n = 3
label = b"this_is_the_path_to_which_access_is_being_granted"
_token_agent, _miner_agent, policy_agent = three_agents
class MockPolicyCreation:
waited_for_receipt = False
tx_hash = "THIS HAS BEEN A TRANSACTION!"
def __init__(self, *args, **kwargs):
# TODO: Test that proper arguments are passed here once 316 is closed.
pass
def transact(self, payload):
# TODO: Make a meaningful assertion regarding the value.
assert payload['from'] == alice.ether_address
return self.tx_hash
2017-12-10 01:21:43 +00:00
@classmethod
def wait_for_receipt(cls, tx_hash):
assert tx_hash == cls.tx_hash
cls.waited_for_receipt = True
policy_agent.blockchain.wait_for_receipt = MockPolicyCreation.wait_for_receipt
policy_agent.contract.functions.createPolicy = MockPolicyCreation
policy = alice.grant(bob, label, m=2, n=n, expiration=policy_end_datetime)
# The number of accepted arrangements 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.
ursula = policy._accepted_arrangements[0].ursula
2017-12-10 01:21:43 +00:00
# Get the Arrangement from Ursula's datastore, looking up by hrac.
# This will be changed in 180, when we use the Arrangement ID.
proper_hrac = keccak_digest(bytes(alice.stamp) + bytes(bob.stamp) + label)
2018-04-02 04:16:51 +00:00
retrieved_policy = ursula.datastore.get_policy_arrangement(proper_hrac.hex().encode())
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]
2018-06-14 08:49:20 +00:00