Problem: web transaction tests were broken or disabled. (#2278)

Solution: Fix or reenable the tests after the migration to BigchainDB 2.*.
This commit is contained in:
Lev Berman 2018-05-16 15:17:33 +02:00 committed by vrde
parent d44bec1b7d
commit 2e9c20491c
4 changed files with 46 additions and 45 deletions

View File

@ -122,7 +122,7 @@ def validate_language_key(obj, key):
""" """
backend = bigchaindb.config['database']['backend'] backend = bigchaindb.config['database']['backend']
if backend == 'mongodb': if backend == 'localmongodb':
data = obj.get(key, {}) data = obj.get(key, {})
if isinstance(data, dict): if isinstance(data, dict):
validate_all_values_for_key(data, 'language', validate_language) validate_all_values_for_key(data, 'language', validate_language)

View File

@ -69,7 +69,7 @@ def validate_txn_obj(obj_name, obj, key, validation_fun):
""" """
backend = bigchaindb.config['database']['backend'] backend = bigchaindb.config['database']['backend']
if backend == 'mongodb': if backend == 'localmongodb':
data = obj.get(key, {}) data = obj.get(key, {})
if isinstance(data, dict): if isinstance(data, dict):
validate_all_keys(obj_name, data, validation_fun) validate_all_keys(obj_name, data, validation_fun)

View File

@ -351,6 +351,14 @@ def signed_create_tx(b, create_tx):
return create_tx.sign([b.me_private]) 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 @pytest.fixture
def signed_transfer_tx(signed_create_tx, user_pk, user_sk): def signed_transfer_tx(signed_create_tx, user_pk, user_sk):
from bigchaindb.models import Transaction from bigchaindb.models import Transaction

View File

@ -1,5 +1,5 @@
import json import json
from unittest.mock import patch from unittest.mock import Mock, patch
import base58 import base58
import pytest import pytest
@ -12,18 +12,14 @@ from bigchaindb.common import crypto
TX_ENDPOINT = '/api/v1/transactions/' TX_ENDPOINT = '/api/v1/transactions/'
@pytest.mark.bdb @pytest.mark.abci
@pytest.mark.usefixtures('inputs') def test_get_transaction_endpoint(client, posted_create_tx):
def test_get_transaction_endpoint(b, client, user_pk): res = client.get(TX_ENDPOINT + posted_create_tx.id)
input_tx = b.get_owned_ids(user_pk).pop() assert posted_create_tx.to_dict() == res.json
tx = b.get_transaction(input_tx.txid)
res = client.get(TX_ENDPOINT + tx.id)
assert tx.to_dict() == res.json
assert res.status_code == 200 assert res.status_code == 200
@pytest.mark.bdb @pytest.mark.tendermint
@pytest.mark.usefixtures('inputs')
def test_get_transaction_returns_404_if_not_found(client): def test_get_transaction_returns_404_if_not_found(client):
res = client.get(TX_ENDPOINT + '123') res = client.get(TX_ENDPOINT + '123')
assert res.status_code == 404 assert res.status_code == 404
@ -32,7 +28,7 @@ def test_get_transaction_returns_404_if_not_found(client):
assert res.status_code == 404 assert res.status_code == 404
@pytest.mark.bdb @pytest.mark.abci
def test_post_create_transaction_endpoint(b, client): def test_post_create_transaction_endpoint(b, client):
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
user_priv, user_pub = crypto.generate_key_pair() 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 assert res.json['outputs'][0]['public_keys'][0] == user_pub
@pytest.mark.abci
@pytest.mark.parametrize('nested', [False, True]) @pytest.mark.parametrize('nested', [False, True])
@pytest.mark.parametrize('language,expected_status_code', [ @pytest.mark.parametrize('language,expected_status_code', [
('danish', 202), ('dutch', 202), ('english', 202), ('finnish', 202), ('danish', 202), ('dutch', 202), ('english', 202), ('finnish', 202),
@ -60,7 +57,6 @@ def test_post_create_transaction_endpoint(b, client):
('any', 400) ('any', 400)
]) ])
@pytest.mark.language @pytest.mark.language
@pytest.mark.bdb
def test_post_create_transaction_with_language(b, client, nested, language, def test_post_create_transaction_with_language(b, client, nested, language,
expected_status_code): expected_status_code):
from bigchaindb.models import Transaction 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 assert res.json['message'] == expected_error_message
@pytest.mark.abci
@pytest.mark.parametrize('field', ['asset', 'metadata']) @pytest.mark.parametrize('field', ['asset', 'metadata'])
@pytest.mark.parametrize('value,err_key,expected_status_code', [ @pytest.mark.parametrize('value,err_key,expected_status_code', [
({'bad.key': 'v'}, 'bad.key', 400), ({'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': {'bad.key': 'v'}}, 'bad.key', 400),
({'good_key': 'v'}, 'good_key', 202) ({'good_key': 'v'}, 'good_key', 202)
]) ])
@pytest.mark.bdb
def test_post_create_transaction_with_invalid_key(b, client, field, value, def test_post_create_transaction_with_invalid_key(b, client, field, value,
err_key, expected_status_code): err_key, expected_status_code):
from bigchaindb.models import Transaction 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())) res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict()))
assert res.status_code == expected_status_code assert res.status_code == expected_status_code
if res.status_code == 400: if res.status_code == 400:
expected_error_message = ( expected_error_message = (
'Invalid transaction (ValidationError): Invalid key name "{}" ' '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 assert res.json['message'] == expected_error_message
@pytest.mark.abci
@patch('bigchaindb.web.views.base.logger') @patch('bigchaindb.web.views.base.logger')
def test_post_create_transaction_with_invalid_id(mock_logger, b, client): def test_post_create_transaction_with_invalid_id(mock_logger, b, client):
from bigchaindb.common.exceptions import InvalidHash 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 # assert caplog.records[0].args['message'] == expected_error_message
@pytest.mark.abci
@patch('bigchaindb.web.views.base.logger') @patch('bigchaindb.web.views.base.logger')
def test_post_create_transaction_with_invalid_signature(mock_logger, def test_post_create_transaction_with_invalid_signature(mock_logger,
b, b,
@ -201,11 +200,13 @@ def test_post_create_transaction_with_invalid_signature(mock_logger,
# assert caplog.records[0].args['message'] == expected_error_message # assert caplog.records[0].args['message'] == expected_error_message
@pytest.mark.abci
def test_post_create_transaction_with_invalid_structure(client): def test_post_create_transaction_with_invalid_structure(client):
res = client.post(TX_ENDPOINT, data='{}') res = client.post(TX_ENDPOINT, data='{}')
assert res.status_code == 400 assert res.status_code == 400
@pytest.mark.abci
@patch('bigchaindb.web.views.base.logger') @patch('bigchaindb.web.views.base.logger')
def test_post_create_transaction_with_invalid_schema(mock_logger, client): def test_post_create_transaction_with_invalid_schema(mock_logger, client):
from bigchaindb.models import Transaction 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 # assert caplog.records[0].args['message'] == expected_error_message
@pytest.mark.abci
@pytest.mark.parametrize('exc,msg', ( @pytest.mark.parametrize('exc,msg', (
('AmountError', 'Do the math again!'), ('AmountError', 'Do the math again!'),
('DoubleSpend', 'Nope! It is gone now!'), ('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): def mock_validation(self_, tx):
raise exc_cls(msg) raise exc_cls(msg)
TransactionMock = Mock(validate=mock_validation)
monkeypatch.setattr( monkeypatch.setattr(
'bigchaindb.Bigchain.validate_transaction', mock_validation) 'bigchaindb.models.Transaction.from_dict', lambda tx: TransactionMock)
monkeypatch.setattr(
'bigchaindb.models.Transaction.from_dict', lambda tx: None)
res = client.post(TX_ENDPOINT, data=json.dumps({})) res = client.post(TX_ENDPOINT, data=json.dumps({}))
expected_status_code = 400 expected_status_code = 400
expected_error_message = 'Invalid transaction ({}): {}'.format(exc, msg) 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 # assert caplog.records[2].args['message'] == expected_error_message
@pytest.mark.bdb @pytest.mark.abci
@pytest.mark.usefixtures('inputs') def test_post_transfer_transaction_endpoint(client, user_pk, user_sk, posted_create_tx):
def test_post_transfer_transaction_endpoint(b, client, user_pk, user_sk):
sk, pk = crypto.generate_key_pair()
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
user_priv, user_pub = crypto.generate_key_pair() transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(),
[([user_pk], 1)],
input_valid = b.get_owned_ids(user_pk).pop() asset_id=posted_create_tx.id)
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 = transfer_tx.sign([user_sk]) transfer_tx = transfer_tx.sign([user_sk])
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict())) 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.status_code == 202
assert res.json['inputs'][0]['owners_before'][0] == user_pk 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.abci
@pytest.mark.usefixtures('inputs') def test_post_invalid_transfer_transaction_returns_400(client, user_pk, posted_create_tx):
def test_post_invalid_transfer_transaction_returns_400(b, client, user_pk):
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
from bigchaindb.common.exceptions import InvalidSignature from bigchaindb.common.exceptions import InvalidSignature
user_pub = crypto.generate_key_pair()[1] transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(),
[([user_pk], 1)],
input_valid = b.get_owned_ids(user_pk).pop() asset_id=posted_create_tx.id)
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._hash() transfer_tx._hash()
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict())) 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 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): def test_post_wrong_asset_division_transfer_returns_400(b, client, user_pk):
from bigchaindb.models import Transaction 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() 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 [([pub_key], 20)], # 20 > 10
asset_id=create_tx.id).sign([priv_key]) asset_id=create_tx.id).sign([priv_key])
res = client.post(TX_ENDPOINT + '?mode=commit', data=json.dumps(transfer_tx.to_dict())) res = client.post(TX_ENDPOINT + '?mode=commit', data=json.dumps(transfer_tx.to_dict()))
expected_error_message = 'Invalid transaction ({}): input `{}` doesn\'t exist'.format( expected_error_message = \
InputDoesNotExist.__name__, create_tx.id) 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.status_code == 400
assert res.json['message'] == expected_error_message 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 assert client.get(url).status_code == 400
@pytest.mark.tendermint
def test_return_only_valid_transaction(client): def test_return_only_valid_transaction(client):
from bigchaindb import Bigchain from bigchaindb import Bigchain
@ -455,7 +448,7 @@ def test_post_transaction_valid_modes(mock_post, client, mode):
assert mode[1] == kwargs['json']['method'] assert mode[1] == kwargs['json']['method']
@pytest.mark.tendermint @pytest.mark.abci
def test_post_transaction_invalid_mode(client): def test_post_transaction_invalid_mode(client):
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair from bigchaindb.common.crypto import generate_key_pair