2017-12-10 01:21:43 +00:00
|
|
|
import datetime
|
2018-06-04 18:00:08 +00:00
|
|
|
import maya
|
2018-09-24 20:59:38 +00:00
|
|
|
import pytest
|
2018-07-20 21:48:37 +00:00
|
|
|
from umbral.fragments import KFrag
|
2018-02-11 09:06:39 +00:00
|
|
|
|
2018-05-08 19:35:34 +00:00
|
|
|
from nucypher.crypto.api import keccak_digest
|
2018-09-13 19:26:25 +00:00
|
|
|
from nucypher.utilities.sandbox.policy import MockPolicyCreation
|
2018-02-11 03:53:57 +00:00
|
|
|
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-09-25 01:16:04 +00:00
|
|
|
@pytest.mark.skip(reason="to be implemented")
|
2018-09-24 20:59:38 +00:00
|
|
|
@pytest.mark.usefixtures('blockchain_ursulas')
|
|
|
|
def test_mocked_decentralized_grant(blockchain_alice, blockchain_bob, three_agents):
|
2018-09-25 01:16:04 +00:00
|
|
|
|
2018-06-23 03:27:43 +00:00
|
|
|
# Monkey patch KFrag repr for better debugging.
|
|
|
|
KFrag.__repr__ = lambda kfrag: "KFrag: {}".format(bytes(kfrag)[:10].hex())
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-09-13 19:26:25 +00:00
|
|
|
# Monkey patch Policy Creation
|
2018-09-25 01:16:04 +00:00
|
|
|
_token_agent, _miner_agent, policy_agent = three_agents
|
2018-06-17 07:49:12 +00:00
|
|
|
policy_agent.blockchain.wait_for_receipt = MockPolicyCreation.wait_for_receipt
|
|
|
|
policy_agent.contract.functions.createPolicy = MockPolicyCreation
|
|
|
|
|
2018-09-25 01:16:04 +00:00
|
|
|
# Setup the policy details
|
|
|
|
n = 3
|
|
|
|
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
|
|
|
|
label = b"this_is_the_path_to_which_access_is_being_granted"
|
|
|
|
|
|
|
|
# Create the Policy, Grating access to Bob
|
2018-09-24 20:59:38 +00:00
|
|
|
policy = blockchain_alice.grant(blockchain_bob, label, m=2, n=n, expiration=policy_end_datetime)
|
2018-06-23 03:27:43 +00:00
|
|
|
|
|
|
|
# The number of accepted arrangements at least the number of Ursulas we're using (n)
|
|
|
|
assert len(policy._accepted_arrangements) >= n
|
2018-06-17 07:49:12 +00:00
|
|
|
|
2018-06-23 03:27:43 +00:00
|
|
|
# The number of actually enacted arrangements is exactly equal to n.
|
|
|
|
assert len(policy._enacted_arrangements) == n
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-06-23 03:27:43 +00:00
|
|
|
# Let's look at the enacted arrangements.
|
|
|
|
for kfrag in policy.kfrags:
|
|
|
|
arrangement = policy._enacted_arrangements[kfrag]
|
2017-12-10 01:21:43 +00:00
|
|
|
|
2018-06-23 03:27: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.
|
2018-09-24 20:59:38 +00:00
|
|
|
proper_hrac = keccak_digest(bytes(blockchain_alice.stamp) + bytes(blockchain_bob.stamp) + label)
|
2018-09-06 18:44:03 +00:00
|
|
|
retrieved_policy = arrangement.ursula.datastore.get_policy_arrangement(arrangement.id.hex().encode())
|
2018-10-19 01:06:34 +00:00
|
|
|
retrieved_kfrag = KFrag.from_bytes(retrieved_policy.kfrag)
|
2018-02-12 21:33:34 +00:00
|
|
|
|
2018-06-23 03:27:43 +00:00
|
|
|
assert kfrag == retrieved_kfrag
|
2018-09-25 01:16:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('federated_ursulas')
|
|
|
|
def test_federated_grant(federated_alice, federated_bob):
|
|
|
|
|
|
|
|
# Monkey patch KFrag repr for better debugging.
|
|
|
|
KFrag.__repr__ = lambda kfrag: "KFrag: {}".format(bytes(kfrag)[:10].hex())
|
|
|
|
|
|
|
|
# Setup the policy details
|
|
|
|
n = 3
|
|
|
|
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
|
|
|
|
label = b"this_is_the_path_to_which_access_is_being_granted"
|
|
|
|
|
|
|
|
# Create the Policy, Grating access to Bob
|
|
|
|
policy = federated_alice.grant(federated_bob, label, m=2, n=n, expiration=policy_end_datetime)
|
|
|
|
|
|
|
|
# The number of accepted arrangements at least the number of Ursulas we're using (n)
|
|
|
|
assert len(policy._accepted_arrangements) >= n
|
|
|
|
|
|
|
|
# The number of actually enacted arrangements is exactly equal to n.
|
|
|
|
assert len(policy._enacted_arrangements) == n
|
|
|
|
|
|
|
|
# Let's look at the enacted arrangements.
|
|
|
|
for kfrag in policy.kfrags:
|
|
|
|
arrangement = policy._enacted_arrangements[kfrag]
|
|
|
|
|
|
|
|
# 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(federated_alice.stamp) + bytes(federated_bob.stamp) + label)
|
|
|
|
retrieved_policy = arrangement.ursula.datastore.get_policy_arrangement(arrangement.id.hex().encode())
|
2018-10-19 01:06:34 +00:00
|
|
|
retrieved_kfrag = KFrag.from_bytes(retrieved_policy.kfrag)
|
2018-09-25 01:16:04 +00:00
|
|
|
|
|
|
|
assert kfrag == retrieved_kfrag
|