mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Test util (#396)
* Isolate handling of potential KeyError * Add tests for bigchaindb/util.py * Simplify the logic a bit * Raise the exception
This commit is contained in:
parent
2b4c230f7f
commit
681e347e75
@ -220,15 +220,14 @@ def create_tx(current_owners, new_owners, inputs, operation, payload=None):
|
|||||||
inputs = [inputs]
|
inputs = [inputs]
|
||||||
|
|
||||||
# handle payload
|
# handle payload
|
||||||
data = None
|
if payload is not None and not isinstance(payload, dict):
|
||||||
if isinstance(payload, (dict, type(None))):
|
|
||||||
data = {
|
|
||||||
'uuid': str(uuid.uuid4()),
|
|
||||||
'payload': payload
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
raise TypeError('`payload` must be an dict instance or None')
|
raise TypeError('`payload` must be an dict instance or None')
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'uuid': str(uuid.uuid4()),
|
||||||
|
'payload': payload
|
||||||
|
}
|
||||||
|
|
||||||
# handle inputs
|
# handle inputs
|
||||||
fulfillments = []
|
fulfillments = []
|
||||||
|
|
||||||
@ -397,13 +396,15 @@ def fulfill_threshold_signature_fulfillment(fulfillment, parsed_fulfillment, ful
|
|||||||
try:
|
try:
|
||||||
subfulfillment = parsed_fulfillment_copy.get_subcondition_from_vk(current_owner)[0]
|
subfulfillment = parsed_fulfillment_copy.get_subcondition_from_vk(current_owner)[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
exceptions.KeypairMismatchException('Public key {} cannot be found in the fulfillment'
|
raise exceptions.KeypairMismatchException(
|
||||||
.format(current_owner))
|
'Public key {} cannot be found in the fulfillment'.format(current_owner))
|
||||||
try:
|
try:
|
||||||
subfulfillment.sign(serialize(fulfillment_message), key_pairs[current_owner])
|
private_key = key_pairs[current_owner]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise exceptions.KeypairMismatchException('Public key {} is not a pair to any of the private keys'
|
raise exceptions.KeypairMismatchException(
|
||||||
.format(current_owner))
|
'Public key {} is not a pair to any of the private keys'.format(current_owner))
|
||||||
|
|
||||||
|
subfulfillment.sign(serialize(fulfillment_message), private_key)
|
||||||
parsed_fulfillment.add_subfulfillment(subfulfillment)
|
parsed_fulfillment.add_subfulfillment(subfulfillment)
|
||||||
|
|
||||||
return parsed_fulfillment
|
return parsed_fulfillment
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
from unittest.mock import patch, call
|
|
||||||
import pytest
|
|
||||||
import queue
|
import queue
|
||||||
|
from unittest.mock import patch, call
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from cryptoconditions import ThresholdSha256Fulfillment
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -142,3 +145,68 @@ def test_process_group_instantiates_and_start_processes(mock_process):
|
|||||||
for process in pg.processes:
|
for process in pg.processes:
|
||||||
process.start.assert_called_with()
|
process.start.assert_called_with()
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_tx_with_empty_inputs():
|
||||||
|
from bigchaindb.util import create_tx
|
||||||
|
tx = create_tx(None, None, [], None)
|
||||||
|
assert 'id' in tx
|
||||||
|
assert 'transaction' in tx
|
||||||
|
assert 'version' in tx
|
||||||
|
assert 'fulfillments' in tx['transaction']
|
||||||
|
assert 'conditions' in tx['transaction']
|
||||||
|
assert 'operation' in tx['transaction']
|
||||||
|
assert 'timestamp' in tx['transaction']
|
||||||
|
assert 'data' in tx['transaction']
|
||||||
|
assert len(tx['transaction']['fulfillments']) == 1
|
||||||
|
assert tx['transaction']['fulfillments'][0] == {
|
||||||
|
'current_owners': [], 'input': None, 'fulfillment': None, 'fid': 0}
|
||||||
|
|
||||||
|
|
||||||
|
def test_fulfill_threshold_signature_fulfillment_pubkey_notfound(monkeypatch):
|
||||||
|
from bigchaindb.exceptions import KeypairMismatchException
|
||||||
|
from bigchaindb.util import fulfill_threshold_signature_fulfillment
|
||||||
|
monkeypatch.setattr(
|
||||||
|
ThresholdSha256Fulfillment,
|
||||||
|
'get_subcondition_from_vk',
|
||||||
|
lambda x, y: []
|
||||||
|
)
|
||||||
|
fulfillment = {'current_owners': (None,)}
|
||||||
|
parsed_fulfillment = ThresholdSha256Fulfillment()
|
||||||
|
with pytest.raises(KeypairMismatchException):
|
||||||
|
fulfill_threshold_signature_fulfillment(
|
||||||
|
fulfillment, parsed_fulfillment, None, None)
|
||||||
|
|
||||||
|
|
||||||
|
def test_fulfill_threshold_signature_fulfillment_wrong_privkeys(monkeypatch):
|
||||||
|
from bigchaindb.exceptions import KeypairMismatchException
|
||||||
|
from bigchaindb.util import fulfill_threshold_signature_fulfillment
|
||||||
|
monkeypatch.setattr(
|
||||||
|
ThresholdSha256Fulfillment,
|
||||||
|
'get_subcondition_from_vk',
|
||||||
|
lambda x, y: (None,)
|
||||||
|
)
|
||||||
|
fulfillment = {'current_owners': ('alice-pub-key',)}
|
||||||
|
parsed_fulfillment = ThresholdSha256Fulfillment()
|
||||||
|
with pytest.raises(KeypairMismatchException):
|
||||||
|
fulfill_threshold_signature_fulfillment(
|
||||||
|
fulfillment, parsed_fulfillment, None, {})
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_hash_and_signature_invalid_hash(monkeypatch):
|
||||||
|
from bigchaindb.exceptions import InvalidHash
|
||||||
|
from bigchaindb.util import check_hash_and_signature
|
||||||
|
transaction = {'id': 'txid'}
|
||||||
|
monkeypatch.setattr('bigchaindb.util.get_hash_data', lambda tx: 'txhash')
|
||||||
|
with pytest.raises(InvalidHash):
|
||||||
|
check_hash_and_signature(transaction)
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_hash_and_signature_invalid_signature(monkeypatch):
|
||||||
|
from bigchaindb.exceptions import InvalidSignature
|
||||||
|
from bigchaindb.util import check_hash_and_signature
|
||||||
|
transaction = {'id': 'txid'}
|
||||||
|
monkeypatch.setattr('bigchaindb.util.get_hash_data', lambda tx: 'txid')
|
||||||
|
monkeypatch.setattr(
|
||||||
|
'bigchaindb.util.validate_fulfillments', lambda tx: False)
|
||||||
|
with pytest.raises(InvalidSignature):
|
||||||
|
check_hash_and_signature(transaction)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user