From caf7c0dfc474ce2b1511269539c74625a51bfd45 Mon Sep 17 00:00:00 2001 From: Rodolphe Marques Date: Thu, 7 Apr 2016 17:38:16 +0200 Subject: [PATCH] Added argument validation to create_transaction. Fixed some tests --- bigchaindb/util.py | 10 +++++++++- tests/db/test_bigchain_api.py | 6 ++++-- tests/test_client.py | 24 +++++++++++++++--------- tests/test_util.py | 4 ++-- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/bigchaindb/util.py b/bigchaindb/util.py index c767a425..acaf009a 100644 --- a/bigchaindb/util.py +++ b/bigchaindb/util.py @@ -143,6 +143,14 @@ def create_tx(current_owners, new_owners, inputs, operation, payload=None): } """ + # validate arguments (owners and inputs should be lists) + if not isinstance(current_owners, list): + current_owners = [current_owners] + if not isinstance(new_owners, list): + new_owners = [new_owners] + if not isinstance(inputs, list): + inputs = [inputs] + # handle payload data = None if payload is not None: @@ -329,6 +337,6 @@ def transform_create(tx): payload = None if transaction['data'] and 'payload' in transaction['data']: payload = transaction['data']['payload'] - new_tx = create_tx(b.me, transaction['current_owner'], None, 'CREATE', payload=payload) + new_tx = create_tx(b.me, transaction['fulfillments'][0]['current_owners'], None, 'CREATE', payload=payload) return new_tx diff --git a/tests/db/test_bigchain_api.py b/tests/db/test_bigchain_api.py index 6349a824..4960ad25 100644 --- a/tests/db/test_bigchain_api.py +++ b/tests/db/test_bigchain_api.py @@ -26,8 +26,8 @@ class TestBigchainApi(object): def test_create_transaction(self, b): tx = b.create_transaction('a', 'b', 'c', 'd') - assert sorted(tx) == sorted(['id', 'transaction']) - assert sorted(tx['transaction']) == sorted(['current_owner', 'new_owner', 'input', 'operation', + assert sorted(tx) == sorted(['id', 'transaction', 'version']) + assert sorted(tx['transaction']) == sorted(['conditions', 'fulfillments', 'operation', 'timestamp', 'data']) def test_create_transaction_with_unsupported_payload_raises(self, b): @@ -51,6 +51,8 @@ class TestBigchainApi(object): assert tx['transaction']['data'] == tx_calculated['data'] # assert tx_hash == tx_calculated_hash + # TODO: Make sure that this is covered when merged with dimi's code + @pytest.mark.skipif(reason='We no longer check signatures, only fulfillments of conditions') def test_transaction_signature(self, b): sk, vk = generate_key_pair() tx = b.create_transaction(vk, 'b', 'c', 'd') diff --git a/tests/test_client.py b/tests/test_client.py index f5e15cad..de1a3d6a 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -21,6 +21,14 @@ def mock_requests_post(monkeypatch): monkeypatch.setattr('requests.post', mockreturn) +@pytest.fixture +def mock_bigchaindb_sign(monkeypatch): + def mockreturn(transaction, private_key): + return transaction + + monkeypatch.setattr('bigchaindb.util.sign_tx', mockreturn) + + def test_temp_client_returns_a_temp_client(): from bigchaindb.client import temp_client @@ -39,21 +47,19 @@ def test_client_can_create_assets(mock_requests_post, client): # `current_owner` will be overwritten with the public key of the node in the federation # that will create the real transaction. `signature` will be overwritten with the new signature. # Note that this scenario is ignored by this test. - assert tx['transaction']['current_owner'] == client.public_key - assert tx['transaction']['new_owner'] == client.public_key - assert tx['transaction']['input'] == None + assert tx['transaction']['fulfillments'][0]['current_owners'][0] == client.public_key + assert tx['transaction']['conditions'][0]['new_owners'][0] == client.public_key + assert tx['transaction']['fulfillments'][0]['input'] is None assert util.verify_signature(tx) -def test_client_can_transfer_assets(mock_requests_post, client): +def test_client_can_transfer_assets(mock_requests_post, mock_bigchaindb_sign, client): from bigchaindb import util tx = client.transfer('a', 123) - assert tx['transaction']['current_owner'] == client.public_key - assert tx['transaction']['new_owner'] == 'a' - assert tx['transaction']['input'] == 123 - - assert util.verify_signature(tx) + assert tx['transaction']['fulfillments'][0]['current_owners'][0] == client.public_key + assert tx['transaction']['conditions'][0]['new_owners'][0] == 'a' + assert tx['transaction']['fulfillments'][0]['input'] == 123 diff --git a/tests/test_util.py b/tests/test_util.py index f4708b59..166dd17d 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -6,7 +6,7 @@ def test_transform_create(b, user_private_key, user_public_key): tx = util.transform_create(tx) tx = util.sign_tx(tx, b.me_private) - assert tx['transaction']['current_owner'] == b.me - assert tx['transaction']['new_owner'] == user_public_key + assert tx['transaction']['fulfillments'][0]['current_owners'][0] == b.me + assert tx['transaction']['conditions'][0]['new_owners'][0] == user_public_key assert util.verify_signature(tx)