diff --git a/bigchaindb/backend/schema.py b/bigchaindb/backend/schema.py index 80bbfa59..bac40e1a 100644 --- a/bigchaindb/backend/schema.py +++ b/bigchaindb/backend/schema.py @@ -122,7 +122,7 @@ def validate_language_key(obj, key): """ backend = bigchaindb.config['database']['backend'] - if backend == 'mongodb': + if backend == 'localmongodb': data = obj.get(key, {}) if isinstance(data, dict): validate_all_values_for_key(data, 'language', validate_language) diff --git a/bigchaindb/common/utils.py b/bigchaindb/common/utils.py index 9ad448f5..ca7ec6cb 100644 --- a/bigchaindb/common/utils.py +++ b/bigchaindb/common/utils.py @@ -69,7 +69,7 @@ def validate_txn_obj(obj_name, obj, key, validation_fun): """ backend = bigchaindb.config['database']['backend'] - if backend == 'mongodb': + if backend == 'localmongodb': data = obj.get(key, {}) if isinstance(data, dict): validate_all_keys(obj_name, data, validation_fun) diff --git a/tests/conftest.py b/tests/conftest.py index 6ac0a14e..989da84e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -351,6 +351,14 @@ def signed_create_tx(b, create_tx): return create_tx.sign([b.me_private]) +@pytest.mark.abci +@pytest.fixture +def posted_create_tx(b, signed_create_tx): + res = b.post_transaction(signed_create_tx, 'broadcast_tx_commit') + assert res.status_code == 200 + return signed_create_tx + + @pytest.fixture def signed_transfer_tx(signed_create_tx, user_pk, user_sk): from bigchaindb.models import Transaction diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index e3f86e69..e5b6a0eb 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -1,5 +1,5 @@ import json -from unittest.mock import patch +from unittest.mock import Mock, patch import base58 import pytest @@ -12,18 +12,14 @@ from bigchaindb.common import crypto TX_ENDPOINT = '/api/v1/transactions/' -@pytest.mark.bdb -@pytest.mark.usefixtures('inputs') -def test_get_transaction_endpoint(b, client, user_pk): - input_tx = b.get_owned_ids(user_pk).pop() - tx = b.get_transaction(input_tx.txid) - res = client.get(TX_ENDPOINT + tx.id) - assert tx.to_dict() == res.json +@pytest.mark.abci +def test_get_transaction_endpoint(client, posted_create_tx): + res = client.get(TX_ENDPOINT + posted_create_tx.id) + assert posted_create_tx.to_dict() == res.json assert res.status_code == 200 -@pytest.mark.bdb -@pytest.mark.usefixtures('inputs') +@pytest.mark.tendermint def test_get_transaction_returns_404_if_not_found(client): res = client.get(TX_ENDPOINT + '123') assert res.status_code == 404 @@ -32,7 +28,7 @@ def test_get_transaction_returns_404_if_not_found(client): assert res.status_code == 404 -@pytest.mark.bdb +@pytest.mark.abci def test_post_create_transaction_endpoint(b, client): from bigchaindb.models import Transaction user_priv, user_pub = crypto.generate_key_pair() @@ -48,6 +44,7 @@ def test_post_create_transaction_endpoint(b, client): assert res.json['outputs'][0]['public_keys'][0] == user_pub +@pytest.mark.abci @pytest.mark.parametrize('nested', [False, True]) @pytest.mark.parametrize('language,expected_status_code', [ ('danish', 202), ('dutch', 202), ('english', 202), ('finnish', 202), @@ -60,7 +57,6 @@ def test_post_create_transaction_endpoint(b, client): ('any', 400) ]) @pytest.mark.language -@pytest.mark.bdb def test_post_create_transaction_with_language(b, client, nested, language, expected_status_code): from bigchaindb.models import Transaction @@ -89,6 +85,7 @@ def test_post_create_transaction_with_language(b, client, nested, language, assert res.json['message'] == expected_error_message +@pytest.mark.abci @pytest.mark.parametrize('field', ['asset', 'metadata']) @pytest.mark.parametrize('value,err_key,expected_status_code', [ ({'bad.key': 'v'}, 'bad.key', 400), @@ -98,7 +95,6 @@ def test_post_create_transaction_with_language(b, client, nested, language, ({'good_key': {'bad.key': 'v'}}, 'bad.key', 400), ({'good_key': 'v'}, 'good_key', 202) ]) -@pytest.mark.bdb def test_post_create_transaction_with_invalid_key(b, client, field, value, err_key, expected_status_code): from bigchaindb.models import Transaction @@ -116,6 +112,7 @@ def test_post_create_transaction_with_invalid_key(b, client, field, value, res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict())) assert res.status_code == expected_status_code + if res.status_code == 400: expected_error_message = ( 'Invalid transaction (ValidationError): Invalid key name "{}" ' @@ -124,6 +121,7 @@ def test_post_create_transaction_with_invalid_key(b, client, field, value, assert res.json['message'] == expected_error_message +@pytest.mark.abci @patch('bigchaindb.web.views.base.logger') def test_post_create_transaction_with_invalid_id(mock_logger, b, client): from bigchaindb.common.exceptions import InvalidHash @@ -158,6 +156,7 @@ def test_post_create_transaction_with_invalid_id(mock_logger, b, client): # assert caplog.records[0].args['message'] == expected_error_message +@pytest.mark.abci @patch('bigchaindb.web.views.base.logger') def test_post_create_transaction_with_invalid_signature(mock_logger, b, @@ -201,11 +200,13 @@ def test_post_create_transaction_with_invalid_signature(mock_logger, # assert caplog.records[0].args['message'] == expected_error_message +@pytest.mark.abci def test_post_create_transaction_with_invalid_structure(client): res = client.post(TX_ENDPOINT, data='{}') assert res.status_code == 400 +@pytest.mark.abci @patch('bigchaindb.web.views.base.logger') def test_post_create_transaction_with_invalid_schema(mock_logger, client): from bigchaindb.models import Transaction @@ -251,6 +252,7 @@ def test_post_create_transaction_with_invalid_schema(mock_logger, client): # assert caplog.records[0].args['message'] == expected_error_message +@pytest.mark.abci @pytest.mark.parametrize('exc,msg', ( ('AmountError', 'Do the math again!'), ('DoubleSpend', 'Nope! It is gone now!'), @@ -270,10 +272,10 @@ def test_post_invalid_transaction(mock_logger, client, exc, msg, monkeypatch,): def mock_validation(self_, tx): raise exc_cls(msg) + TransactionMock = Mock(validate=mock_validation) + monkeypatch.setattr( - 'bigchaindb.Bigchain.validate_transaction', mock_validation) - monkeypatch.setattr( - 'bigchaindb.models.Transaction.from_dict', lambda tx: None) + 'bigchaindb.models.Transaction.from_dict', lambda tx: TransactionMock) res = client.post(TX_ENDPOINT, data=json.dumps({})) expected_status_code = 400 expected_error_message = 'Invalid transaction ({}): {}'.format(exc, msg) @@ -296,19 +298,13 @@ def test_post_invalid_transaction(mock_logger, client, exc, msg, monkeypatch,): # assert caplog.records[2].args['message'] == expected_error_message -@pytest.mark.bdb -@pytest.mark.usefixtures('inputs') -def test_post_transfer_transaction_endpoint(b, client, user_pk, user_sk): - sk, pk = crypto.generate_key_pair() +@pytest.mark.abci +def test_post_transfer_transaction_endpoint(client, user_pk, user_sk, posted_create_tx): from bigchaindb.models import Transaction - user_priv, user_pub = crypto.generate_key_pair() - - input_valid = b.get_owned_ids(user_pk).pop() - create_tx = b.get_transaction(input_valid.txid) - transfer_tx = Transaction.transfer(create_tx.to_inputs(), - [([user_pub], 1)], - asset_id=create_tx.id) + transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(), + [([user_pk], 1)], + asset_id=posted_create_tx.id) transfer_tx = transfer_tx.sign([user_sk]) res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict())) @@ -316,22 +312,17 @@ def test_post_transfer_transaction_endpoint(b, client, user_pk, user_sk): assert res.status_code == 202 assert res.json['inputs'][0]['owners_before'][0] == user_pk - assert res.json['outputs'][0]['public_keys'][0] == user_pub + assert res.json['outputs'][0]['public_keys'][0] == user_pk -@pytest.mark.bdb -@pytest.mark.usefixtures('inputs') -def test_post_invalid_transfer_transaction_returns_400(b, client, user_pk): +@pytest.mark.abci +def test_post_invalid_transfer_transaction_returns_400(client, user_pk, posted_create_tx): from bigchaindb.models import Transaction from bigchaindb.common.exceptions import InvalidSignature - user_pub = crypto.generate_key_pair()[1] - - input_valid = b.get_owned_ids(user_pk).pop() - create_tx = b.get_transaction(input_valid.txid) - transfer_tx = Transaction.transfer(create_tx.to_inputs(), - [([user_pub], 1)], - asset_id=create_tx.id) + transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(), + [([user_pk], 1)], + asset_id=posted_create_tx.id) transfer_tx._hash() res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict())) @@ -342,10 +333,10 @@ def test_post_invalid_transfer_transaction_returns_400(b, client, user_pk): assert res.json['message'] == expected_error_message -@pytest.mark.tendermint +@pytest.mark.abci def test_post_wrong_asset_division_transfer_returns_400(b, client, user_pk): from bigchaindb.models import Transaction - from bigchaindb.common.exceptions import InputDoesNotExist + from bigchaindb.common.exceptions import AmountError priv_key, pub_key = crypto.generate_key_pair() @@ -359,8 +350,9 @@ def test_post_wrong_asset_division_transfer_returns_400(b, client, user_pk): [([pub_key], 20)], # 20 > 10 asset_id=create_tx.id).sign([priv_key]) res = client.post(TX_ENDPOINT + '?mode=commit', data=json.dumps(transfer_tx.to_dict())) - expected_error_message = 'Invalid transaction ({}): input `{}` doesn\'t exist'.format( - InputDoesNotExist.__name__, create_tx.id) + expected_error_message = \ + f'Invalid transaction ({AmountError.__name__}): ' + \ + 'The amount used in the inputs `10` needs to be same as the amount used in the outputs `20`' assert res.status_code == 400 assert res.json['message'] == expected_error_message @@ -410,6 +402,7 @@ def test_transactions_get_list_bad(client): assert client.get(url).status_code == 400 +@pytest.mark.tendermint def test_return_only_valid_transaction(client): from bigchaindb import Bigchain @@ -455,7 +448,7 @@ def test_post_transaction_valid_modes(mock_post, client, mode): assert mode[1] == kwargs['json']['method'] -@pytest.mark.tendermint +@pytest.mark.abci def test_post_transaction_invalid_mode(client): from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair