2017-12-10 01:21:43 +00:00
|
|
|
import datetime
|
|
|
|
|
2018-06-04 18:00:08 +00:00
|
|
|
import maya
|
2018-06-01 20:34:42 +00:00
|
|
|
import pytest
|
2018-02-11 09:06:39 +00:00
|
|
|
from apistar.test import TestClient
|
2018-06-04 18:00:08 +00:00
|
|
|
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
|
2018-02-11 03:53:57 +00:00
|
|
|
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-06-17 07:49:12 +00:00
|
|
|
def test_grant(alice, bob, mining_ursulas, three_agents):
|
2018-06-01 20:34:42 +00:00
|
|
|
|
2018-06-17 07:49:12 +00:00
|
|
|
ursula, *other_ursulas = mining_ursulas
|
2018-06-10 20:05:28 +00:00
|
|
|
|
2018-04-07 02:26:13 +00:00
|
|
|
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
|
2018-06-17 07:49:12 +00:00
|
|
|
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
|
|
|
|
2018-06-17 07:49:12 +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.
|
2018-06-17 07:49:12 +00:00
|
|
|
ursula = policy._accepted_arrangements[0].ursula
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-06-17 07:49:12 +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())
|
2018-02-12 21:33:34 +00:00
|
|
|
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
|
2018-02-06 07:10:43 +00:00
|
|
|
|
|
|
|
|
2018-06-04 18:00:08 +00:00
|
|
|
@pytest.mark.usefixtures('deployed_testerchain')
|
2018-04-18 04:55:25 +00:00
|
|
|
def test_alice_can_get_ursulas_keys_via_rest(ursulas):
|
2018-02-06 07:10:43 +00:00
|
|
|
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(
|
2018-06-04 18:00:08 +00:00
|
|
|
(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)
|
2018-06-04 18:00:08 +00:00
|
|
|
public_keys = {SigningPower: signing_key, EncryptingPower: encrypting_key}
|
|
|
|
stranger_ursula_from_public_keys = Ursula.from_public_keys(public_keys)
|
2018-02-06 07:10:43 +00:00
|
|
|
assert stranger_ursula_from_public_keys == ursulas[0]
|
2018-06-14 08:49:20 +00:00
|
|
|
|