2018-08-16 12:31:32 +02:00
|
|
|
# Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
# Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2016-02-24 02:38:30 +01:00
|
|
|
import json
|
2018-05-16 15:17:33 +02:00
|
|
|
from unittest.mock import Mock, patch
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2017-11-25 01:04:50 +01:00
|
|
|
import base58
|
2016-02-23 03:37:33 +01:00
|
|
|
import pytest
|
2017-11-25 01:04:50 +01:00
|
|
|
from cryptoconditions import Ed25519Sha256
|
|
|
|
from sha3 import sha3_256
|
|
|
|
|
2016-10-24 17:01:43 +02:00
|
|
|
from bigchaindb.common import crypto
|
2016-02-23 03:37:33 +01:00
|
|
|
|
|
|
|
|
2016-02-29 18:28:04 +01:00
|
|
|
TX_ENDPOINT = '/api/v1/transactions/'
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@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
|
2016-08-23 17:43:08 +02:00
|
|
|
assert res.status_code == 200
|
2016-02-24 02:38:30 +01:00
|
|
|
|
|
|
|
|
2016-06-08 18:39:40 +02:00
|
|
|
def test_get_transaction_returns_404_if_not_found(client):
|
|
|
|
res = client.get(TX_ENDPOINT + '123')
|
|
|
|
assert res.status_code == 404
|
|
|
|
|
2016-08-23 17:43:08 +02:00
|
|
|
res = client.get(TX_ENDPOINT + '123/')
|
|
|
|
assert res.status_code == 404
|
|
|
|
|
2016-06-08 18:39:40 +02:00
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2016-02-24 02:38:30 +01:00
|
|
|
def test_post_create_transaction_endpoint(b, client):
|
2016-09-29 10:29:41 +02:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
|
|
|
|
2016-11-09 13:32:18 +01:00
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)])
|
2016-09-29 10:29:41 +02:00
|
|
|
tx = tx.sign([user_priv])
|
|
|
|
|
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict()))
|
2017-01-25 12:22:34 +01:00
|
|
|
|
|
|
|
assert res.status_code == 202
|
|
|
|
|
2016-12-13 13:41:46 +01:00
|
|
|
assert res.json['inputs'][0]['owners_before'][0] == user_pub
|
|
|
|
assert res.json['outputs'][0]['public_keys'][0] == user_pub
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2016-09-29 10:29:41 +02:00
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2017-11-27 16:22:33 +01:00
|
|
|
@pytest.mark.parametrize('nested', [False, True])
|
|
|
|
@pytest.mark.parametrize('language,expected_status_code', [
|
2017-10-31 12:24:47 +01:00
|
|
|
('danish', 202), ('dutch', 202), ('english', 202), ('finnish', 202),
|
|
|
|
('french', 202), ('german', 202), ('hungarian', 202), ('italian', 202),
|
2017-11-03 07:08:09 +01:00
|
|
|
('norwegian', 202), ('portuguese', 202), ('romanian', 202), ('none', 202),
|
2017-10-31 12:24:47 +01:00
|
|
|
('russian', 202), ('spanish', 202), ('swedish', 202), ('turkish', 202),
|
|
|
|
('da', 202), ('nl', 202), ('en', 202), ('fi', 202), ('fr', 202),
|
|
|
|
('de', 202), ('hu', 202), ('it', 202), ('nb', 202), ('pt', 202),
|
|
|
|
('ro', 202), ('ru', 202), ('es', 202), ('sv', 202), ('tr', 202),
|
|
|
|
('any', 400)
|
2017-10-31 05:42:16 +01:00
|
|
|
])
|
|
|
|
@pytest.mark.language
|
2017-11-03 07:08:09 +01:00
|
|
|
def test_post_create_transaction_with_language(b, client, nested, language,
|
|
|
|
expected_status_code):
|
2017-10-31 05:42:16 +01:00
|
|
|
from bigchaindb.models import Transaction
|
2018-05-23 11:34:00 +02:00
|
|
|
from bigchaindb.backend.localmongodb.connection import LocalMongoDBConnection
|
2017-10-31 05:42:16 +01:00
|
|
|
|
2018-05-23 11:34:00 +02:00
|
|
|
if isinstance(b.connection, LocalMongoDBConnection):
|
2017-10-31 05:42:16 +01:00
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
2017-11-03 07:08:09 +01:00
|
|
|
lang_obj = {'language': language}
|
|
|
|
|
|
|
|
if nested:
|
|
|
|
asset = {'root': lang_obj}
|
|
|
|
else:
|
|
|
|
asset = lang_obj
|
2017-10-31 05:42:16 +01:00
|
|
|
|
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
2017-11-03 07:08:09 +01:00
|
|
|
asset=asset)
|
2017-10-31 05:42:16 +01:00
|
|
|
tx = tx.sign([user_priv])
|
|
|
|
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 = (
|
2017-11-03 07:08:09 +01:00
|
|
|
'Invalid transaction (ValidationError): MongoDB does not support '
|
|
|
|
'text search for the language "{}". If you do not understand this '
|
|
|
|
'error message then please rename key/field "language" to something '
|
|
|
|
'else like "lang".').format(language)
|
|
|
|
assert res.json['message'] == expected_error_message
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2017-11-23 10:50:19 +01:00
|
|
|
@pytest.mark.parametrize('field', ['asset', 'metadata'])
|
|
|
|
@pytest.mark.parametrize('value,err_key,expected_status_code', [
|
2017-11-01 12:56:16 +01:00
|
|
|
({'bad.key': 'v'}, 'bad.key', 400),
|
|
|
|
({'$bad.key': 'v'}, '$bad.key', 400),
|
|
|
|
({'$badkey': 'v'}, '$badkey', 400),
|
|
|
|
({'bad\x00key': 'v'}, 'bad\x00key', 400),
|
|
|
|
({'good_key': {'bad.key': 'v'}}, 'bad.key', 400),
|
|
|
|
({'good_key': 'v'}, 'good_key', 202)
|
2017-10-31 10:46:59 +01:00
|
|
|
])
|
2017-11-01 12:56:16 +01:00
|
|
|
def test_post_create_transaction_with_invalid_key(b, client, field, value,
|
|
|
|
err_key, expected_status_code):
|
2017-10-31 10:46:59 +01:00
|
|
|
from bigchaindb.models import Transaction
|
2018-05-23 11:34:00 +02:00
|
|
|
from bigchaindb.backend.localmongodb.connection import LocalMongoDBConnection
|
2017-10-31 10:46:59 +01:00
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
|
|
|
|
2018-05-23 11:34:00 +02:00
|
|
|
if isinstance(b.connection, LocalMongoDBConnection):
|
2017-11-01 12:56:16 +01:00
|
|
|
if field == 'asset':
|
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
|
|
|
asset=value)
|
|
|
|
elif field == 'metadata':
|
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
|
|
|
metadata=value)
|
2017-10-31 10:46:59 +01:00
|
|
|
tx = tx.sign([user_priv])
|
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict()))
|
|
|
|
|
|
|
|
assert res.status_code == expected_status_code
|
2018-05-16 15:17:33 +02:00
|
|
|
|
2017-10-31 10:46:59 +01:00
|
|
|
if res.status_code == 400:
|
|
|
|
expected_error_message = (
|
|
|
|
'Invalid transaction (ValidationError): Invalid key name "{}" '
|
2017-11-01 12:56:16 +01:00
|
|
|
'in {} object. The key name cannot contain characters '
|
|
|
|
'".", "$" or null characters').format(err_key, field)
|
2017-10-31 10:46:59 +01:00
|
|
|
assert res.json['message'] == expected_error_message
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2017-03-13 17:55:11 +01:00
|
|
|
@patch('bigchaindb.web.views.base.logger')
|
|
|
|
def test_post_create_transaction_with_invalid_id(mock_logger, b, client):
|
2016-12-08 14:29:30 +01:00
|
|
|
from bigchaindb.common.exceptions import InvalidHash
|
2016-09-29 10:29:41 +02:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
|
|
|
|
2016-11-09 13:32:18 +01:00
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)])
|
2016-09-29 10:29:41 +02:00
|
|
|
tx = tx.sign([user_priv]).to_dict()
|
2016-11-22 11:17:06 +01:00
|
|
|
tx['id'] = 'abcd' * 16
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2016-02-29 18:28:04 +01:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(tx))
|
2017-01-17 11:32:30 +01:00
|
|
|
expected_status_code = 400
|
|
|
|
expected_error_message = (
|
2017-03-13 17:55:11 +01:00
|
|
|
"Invalid transaction ({}): The transaction's id '{}' isn't equal to "
|
|
|
|
"the hash of its body, i.e. it's not valid."
|
2017-01-17 11:32:30 +01:00
|
|
|
).format(InvalidHash.__name__, tx['id'])
|
|
|
|
assert res.status_code == expected_status_code
|
|
|
|
assert res.json['message'] == expected_error_message
|
2017-03-13 17:55:11 +01:00
|
|
|
assert mock_logger.error.called
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
'HTTP API error: %(status)s - %(method)s:%(path)s - %(message)s' in
|
2017-03-13 17:55:11 +01:00
|
|
|
mock_logger.error.call_args[0]
|
|
|
|
)
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
{
|
|
|
|
'message': expected_error_message, 'status': expected_status_code,
|
|
|
|
'method': 'POST', 'path': TX_ENDPOINT
|
|
|
|
} in mock_logger.error.call_args[0]
|
2017-03-13 17:55:11 +01:00
|
|
|
)
|
|
|
|
# TODO put back caplog based asserts once possible
|
|
|
|
# assert caplog.records[0].args['status'] == expected_status_code
|
|
|
|
# assert caplog.records[0].args['message'] == expected_error_message
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2017-03-13 17:55:11 +01:00
|
|
|
@patch('bigchaindb.web.views.base.logger')
|
|
|
|
def test_post_create_transaction_with_invalid_signature(mock_logger,
|
|
|
|
b,
|
|
|
|
client):
|
2016-12-08 14:29:30 +01:00
|
|
|
from bigchaindb.common.exceptions import InvalidSignature
|
2016-09-29 10:29:41 +02:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
2016-08-23 17:43:08 +02:00
|
|
|
|
2017-11-25 01:04:50 +01:00
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)]).to_dict()
|
2017-06-14 18:42:07 +02:00
|
|
|
tx['inputs'][0]['fulfillment'] = 64 * '0'
|
2017-11-25 01:04:50 +01:00
|
|
|
tx['id'] = sha3_256(
|
|
|
|
json.dumps(
|
|
|
|
tx,
|
|
|
|
sort_keys=True,
|
|
|
|
separators=(',', ':'),
|
|
|
|
ensure_ascii=False,
|
|
|
|
).encode(),
|
|
|
|
).hexdigest()
|
2016-08-23 17:43:08 +02:00
|
|
|
|
2016-09-29 10:29:41 +02:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(tx))
|
2017-01-17 11:32:30 +01:00
|
|
|
expected_status_code = 400
|
|
|
|
expected_error_message = (
|
2017-01-23 17:30:37 +01:00
|
|
|
'Invalid transaction ({}): Fulfillment URI '
|
|
|
|
'couldn\'t been parsed'
|
2017-01-17 11:32:30 +01:00
|
|
|
).format(InvalidSignature.__name__)
|
|
|
|
assert res.status_code == expected_status_code
|
|
|
|
assert res.json['message'] == expected_error_message
|
2017-03-13 17:55:11 +01:00
|
|
|
assert mock_logger.error.called
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
'HTTP API error: %(status)s - %(method)s:%(path)s - %(message)s' in
|
2017-03-13 17:55:11 +01:00
|
|
|
mock_logger.error.call_args[0]
|
|
|
|
)
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
{
|
|
|
|
'message': expected_error_message, 'status': expected_status_code,
|
|
|
|
'method': 'POST', 'path': TX_ENDPOINT
|
|
|
|
} in mock_logger.error.call_args[0]
|
2017-03-13 17:55:11 +01:00
|
|
|
)
|
|
|
|
# TODO put back caplog based asserts once possible
|
|
|
|
# assert caplog.records[0].args['status'] == expected_status_code
|
|
|
|
# assert caplog.records[0].args['message'] == expected_error_message
|
2016-08-23 17:43:08 +02:00
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2016-11-22 11:17:06 +01:00
|
|
|
def test_post_create_transaction_with_invalid_structure(client):
|
|
|
|
res = client.post(TX_ENDPOINT, data='{}')
|
|
|
|
assert res.status_code == 400
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2017-03-13 17:55:11 +01:00
|
|
|
@patch('bigchaindb.web.views.base.logger')
|
|
|
|
def test_post_create_transaction_with_invalid_schema(mock_logger, client):
|
2016-11-28 19:31:42 +01:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
user_priv, user_pub = crypto.generate_key_pair()
|
2017-11-25 01:04:50 +01:00
|
|
|
tx = Transaction.create([user_pub], [([user_pub], 1)]).to_dict()
|
2016-11-28 19:31:42 +01:00
|
|
|
del tx['version']
|
2017-11-25 01:04:50 +01:00
|
|
|
ed25519 = Ed25519Sha256(public_key=base58.b58decode(user_pub))
|
|
|
|
message = json.dumps(
|
|
|
|
tx,
|
|
|
|
sort_keys=True,
|
|
|
|
separators=(',', ':'),
|
|
|
|
ensure_ascii=False,
|
|
|
|
).encode()
|
|
|
|
ed25519.sign(message, base58.b58decode(user_priv))
|
|
|
|
tx['inputs'][0]['fulfillment'] = ed25519.serialize_uri()
|
|
|
|
tx['id'] = sha3_256(
|
|
|
|
json.dumps(
|
|
|
|
tx,
|
|
|
|
sort_keys=True,
|
|
|
|
separators=(',', ':'),
|
|
|
|
ensure_ascii=False,
|
|
|
|
).encode(),
|
|
|
|
).hexdigest()
|
2016-11-28 19:31:42 +01:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(tx))
|
2017-01-17 11:32:30 +01:00
|
|
|
expected_status_code = 400
|
|
|
|
expected_error_message = (
|
2016-11-28 19:31:42 +01:00
|
|
|
"Invalid transaction schema: 'version' is a required property")
|
2017-01-17 11:32:30 +01:00
|
|
|
assert res.status_code == expected_status_code
|
|
|
|
assert res.json['message'] == expected_error_message
|
2017-03-13 17:55:11 +01:00
|
|
|
assert mock_logger.error.called
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
'HTTP API error: %(status)s - %(method)s:%(path)s - %(message)s' in
|
2017-03-13 17:55:11 +01:00
|
|
|
mock_logger.error.call_args[0]
|
|
|
|
)
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
{
|
|
|
|
'message': expected_error_message, 'status': expected_status_code,
|
|
|
|
'method': 'POST', 'path': TX_ENDPOINT
|
|
|
|
} in mock_logger.error.call_args[0]
|
2017-03-13 17:55:11 +01:00
|
|
|
)
|
|
|
|
# TODO put back caplog based asserts once possible
|
|
|
|
# assert caplog.records[0].args['status'] == expected_status_code
|
|
|
|
# assert caplog.records[0].args['message'] == expected_error_message
|
2016-11-28 19:31:42 +01:00
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2016-12-08 14:29:30 +01:00
|
|
|
@pytest.mark.parametrize('exc,msg', (
|
|
|
|
('AmountError', 'Do the math again!'),
|
|
|
|
('DoubleSpend', 'Nope! It is gone now!'),
|
|
|
|
('InvalidHash', 'Do not smoke that!'),
|
|
|
|
('InvalidSignature', 'Falsche Unterschrift!'),
|
2017-03-03 11:36:50 +01:00
|
|
|
('ValidationError', 'Create and transfer!'),
|
2017-03-08 13:01:52 +01:00
|
|
|
('InputDoesNotExist', 'Hallucinations?'),
|
2016-12-08 14:29:30 +01:00
|
|
|
('TransactionOwnerError', 'Not yours!'),
|
2017-03-03 11:36:50 +01:00
|
|
|
('ValidationError', '?'),
|
2016-12-08 14:29:30 +01:00
|
|
|
))
|
2017-03-13 17:55:11 +01:00
|
|
|
@patch('bigchaindb.web.views.base.logger')
|
|
|
|
def test_post_invalid_transaction(mock_logger, client, exc, msg, monkeypatch,):
|
2016-12-08 14:29:30 +01:00
|
|
|
from bigchaindb.common import exceptions
|
2017-03-03 11:36:50 +01:00
|
|
|
exc_cls = getattr(exceptions, exc)
|
2016-12-08 14:29:30 +01:00
|
|
|
|
|
|
|
def mock_validation(self_, tx):
|
|
|
|
raise exc_cls(msg)
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
TransactionMock = Mock(validate=mock_validation)
|
|
|
|
|
2016-12-08 14:29:30 +01:00
|
|
|
monkeypatch.setattr(
|
2018-05-16 15:17:33 +02:00
|
|
|
'bigchaindb.models.Transaction.from_dict', lambda tx: TransactionMock)
|
2016-12-08 14:29:30 +01:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps({}))
|
2017-01-17 11:32:30 +01:00
|
|
|
expected_status_code = 400
|
|
|
|
expected_error_message = 'Invalid transaction ({}): {}'.format(exc, msg)
|
|
|
|
assert res.status_code == expected_status_code
|
2016-12-08 14:29:30 +01:00
|
|
|
assert (res.json['message'] ==
|
|
|
|
'Invalid transaction ({}): {}'.format(exc, msg))
|
2017-03-13 17:55:11 +01:00
|
|
|
assert mock_logger.error.called
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
'HTTP API error: %(status)s - %(method)s:%(path)s - %(message)s' in
|
2017-03-13 17:55:11 +01:00
|
|
|
mock_logger.error.call_args[0]
|
|
|
|
)
|
|
|
|
assert (
|
2017-07-09 13:14:21 +02:00
|
|
|
{
|
|
|
|
'message': expected_error_message, 'status': expected_status_code,
|
|
|
|
'method': 'POST', 'path': TX_ENDPOINT
|
|
|
|
} in mock_logger.error.call_args[0]
|
2017-03-13 17:55:11 +01:00
|
|
|
)
|
|
|
|
# TODO put back caplog based asserts once possible
|
|
|
|
# assert caplog.records[2].args['status'] == expected_status_code
|
|
|
|
# assert caplog.records[2].args['message'] == expected_error_message
|
2016-12-08 14:29:30 +01:00
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
|
|
|
def test_post_transfer_transaction_endpoint(client, user_pk, user_sk, posted_create_tx):
|
2016-09-29 10:29:41 +02:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(),
|
|
|
|
[([user_pk], 1)],
|
|
|
|
asset_id=posted_create_tx.id)
|
2016-09-29 10:29:41 +02:00
|
|
|
transfer_tx = transfer_tx.sign([user_sk])
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2016-09-29 10:29:41 +02:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict()))
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2017-01-25 12:22:34 +01:00
|
|
|
assert res.status_code == 202
|
|
|
|
|
2016-12-13 13:41:46 +01:00
|
|
|
assert res.json['inputs'][0]['owners_before'][0] == user_pk
|
2018-05-16 15:17:33 +02:00
|
|
|
assert res.json['outputs'][0]['public_keys'][0] == user_pk
|
2016-02-24 02:38:30 +01:00
|
|
|
|
2016-08-19 13:56:08 +02:00
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
|
|
|
def test_post_invalid_transfer_transaction_returns_400(client, user_pk, posted_create_tx):
|
2016-09-29 10:29:41 +02:00
|
|
|
from bigchaindb.models import Transaction
|
2017-01-17 11:32:30 +01:00
|
|
|
from bigchaindb.common.exceptions import InvalidSignature
|
2016-09-29 10:29:41 +02:00
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
transfer_tx = Transaction.transfer(posted_create_tx.to_inputs(),
|
|
|
|
[([user_pk], 1)],
|
|
|
|
asset_id=posted_create_tx.id)
|
2017-11-25 01:04:50 +01:00
|
|
|
transfer_tx._hash()
|
2016-08-17 12:18:52 +02:00
|
|
|
|
2016-09-29 10:29:41 +02:00
|
|
|
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict()))
|
2017-01-17 11:32:30 +01:00
|
|
|
expected_status_code = 400
|
|
|
|
expected_error_message = 'Invalid transaction ({}): {}'.format(
|
|
|
|
InvalidSignature.__name__, 'Transaction signature is invalid.')
|
|
|
|
assert res.status_code == expected_status_code
|
|
|
|
assert res.json['message'] == expected_error_message
|
2017-01-18 15:36:46 +01:00
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2018-05-09 13:41:22 +02:00
|
|
|
def test_post_wrong_asset_division_transfer_returns_400(b, client, user_pk):
|
|
|
|
from bigchaindb.models import Transaction
|
2018-05-16 15:17:33 +02:00
|
|
|
from bigchaindb.common.exceptions import AmountError
|
2018-05-09 13:41:22 +02:00
|
|
|
|
|
|
|
priv_key, pub_key = crypto.generate_key_pair()
|
|
|
|
|
|
|
|
create_tx = Transaction.create([pub_key],
|
|
|
|
[([pub_key], 10)],
|
|
|
|
asset={'test': 'asset'}).sign([priv_key])
|
|
|
|
res = client.post(TX_ENDPOINT + '?mode=commit', data=json.dumps(create_tx.to_dict()))
|
|
|
|
assert res.status_code == 202
|
|
|
|
|
|
|
|
transfer_tx = Transaction.transfer(create_tx.to_inputs(),
|
|
|
|
[([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()))
|
2018-05-16 15:17:33 +02:00
|
|
|
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`'
|
2018-05-09 13:41:22 +02:00
|
|
|
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.json['message'] == expected_error_message
|
|
|
|
|
|
|
|
|
2017-01-18 15:36:46 +01:00
|
|
|
def test_transactions_get_list_good(client):
|
|
|
|
from functools import partial
|
2017-01-18 17:13:38 +01:00
|
|
|
|
2017-01-23 14:07:22 +01:00
|
|
|
def get_txs_patched(conn, **args):
|
2017-11-30 15:04:14 +01:00
|
|
|
"""Patch `get_transactions_filtered` so that rather than return an array
|
2017-01-23 14:07:22 +01:00
|
|
|
of transactions it returns an array of shims with a to_dict() method
|
|
|
|
that reports one of the arguments passed to `get_transactions_filtered`.
|
|
|
|
"""
|
2017-01-18 15:36:46 +01:00
|
|
|
return [type('', (), {'to_dict': partial(lambda a: a, arg)})
|
|
|
|
for arg in sorted(args.items())]
|
|
|
|
|
|
|
|
asset_id = '1' * 64
|
|
|
|
|
Refactor tendermint directory to project root (#2401)
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Fixed flake8 complaint about too many blank lines
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Updating from master changed BigchainDB.process_post_response to a private method, so I had to align with that.
* Fixed a couple stale references to bigchaindb.Bigchain in docstrings
* Missed a reference to `Bigchain` in a patch call...
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
2018-07-25 16:59:25 +02:00
|
|
|
with patch('bigchaindb.BigchainDB.get_transactions_filtered', get_txs_patched):
|
2017-02-22 12:33:10 +01:00
|
|
|
url = TX_ENDPOINT + '?asset_id=' + asset_id
|
2017-01-18 15:36:46 +01:00
|
|
|
assert client.get(url).json == [
|
|
|
|
['asset_id', asset_id],
|
|
|
|
['operation', None]
|
|
|
|
]
|
2017-02-22 12:33:10 +01:00
|
|
|
url = TX_ENDPOINT + '?asset_id=' + asset_id + '&operation=CREATE'
|
2017-01-18 15:36:46 +01:00
|
|
|
assert client.get(url).json == [
|
|
|
|
['asset_id', asset_id],
|
|
|
|
['operation', 'CREATE']
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_transactions_get_list_bad(client):
|
2017-01-18 17:13:38 +01:00
|
|
|
def should_not_be_called():
|
|
|
|
assert False
|
Refactor tendermint directory to project root (#2401)
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Problem: core.py contains an unused class, `Bigchain`
Solution: Remove core.py. Refactor BigchainDB Class to remove inheritance from Bigchain.
* Fixed flake8 complaint about too many blank lines
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Attempting to fix Sphinx docs. This may result in some redundant commits, as I don't know what I'm doing, and I can't experiment without running the CI...
Sorry in advance!
* Updating from master changed BigchainDB.process_post_response to a private method, so I had to align with that.
* Fixed a couple stale references to bigchaindb.Bigchain in docstrings
* Missed a reference to `Bigchain` in a patch call...
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
* Problem: BigchainDB class should be part of project root
Solution: Removed the /tendermint directory and moved its contents to project root
* Problem: Flake8 complained that imports were not at the top of the file
Solution: Had to play around with the order of imports to avoid cyclic dependencies, but its working and style compliant now
* Problem: Stale reference to /tendermint directory in the index
Solution: Removed the references to /tendermint
* Problem: Flake8 complaining of unused import in __init__.py
Solution: The import is there so I can import App directly from bigchaindb, rather than from bigchaindb.core (which I think improves code readability. I added a # noqa to silence Flake8.
* Problem: Stale references to `bigchaindb.tendermint.BigchainDB` in the rst files cause Sphinx to fail
Solution: Updated the autodoc files to use `bigchaindb.BigchainDB` instead
* Problem: Stale reference to the `tendermint` directory in an @patch in a disabled test
Solution: Updated the @patch for completeness
2018-07-25 16:59:25 +02:00
|
|
|
with patch('bigchaindb.BigchainDB.get_transactions_filtered',
|
2017-01-18 15:36:46 +01:00
|
|
|
lambda *_, **__: should_not_be_called()):
|
|
|
|
# Test asset id validated
|
2017-02-22 12:33:10 +01:00
|
|
|
url = TX_ENDPOINT + '?asset_id=' + '1' * 63
|
2017-01-18 15:36:46 +01:00
|
|
|
assert client.get(url).status_code == 400
|
|
|
|
# Test operation validated
|
2017-02-22 12:33:10 +01:00
|
|
|
url = TX_ENDPOINT + '?asset_id=' + '1' * 64 + '&operation=CEATE'
|
2017-01-18 15:36:46 +01:00
|
|
|
assert client.get(url).status_code == 400
|
|
|
|
# Test asset ID required
|
2017-02-22 12:33:10 +01:00
|
|
|
url = TX_ENDPOINT + '?operation=CREATE'
|
2017-01-18 15:36:46 +01:00
|
|
|
assert client.get(url).status_code == 400
|
2017-06-14 15:42:04 +02:00
|
|
|
|
|
|
|
|
2018-01-11 13:26:06 +01:00
|
|
|
@patch('requests.post')
|
|
|
|
@pytest.mark.parametrize('mode', [
|
|
|
|
('', 'broadcast_tx_async'),
|
|
|
|
('?mode=async', 'broadcast_tx_async'),
|
|
|
|
('?mode=sync', 'broadcast_tx_sync'),
|
|
|
|
('?mode=commit', 'broadcast_tx_commit'),
|
|
|
|
])
|
|
|
|
def test_post_transaction_valid_modes(mock_post, client, mode):
|
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
from bigchaindb.common.crypto import generate_key_pair
|
2018-09-06 15:49:00 +02:00
|
|
|
|
|
|
|
def _mock_post(*args, **kwargs):
|
|
|
|
return Mock(json=Mock(return_value={'result': {'code': 0}}))
|
|
|
|
|
|
|
|
mock_post.side_effect = _mock_post
|
|
|
|
|
2018-01-11 13:26:06 +01:00
|
|
|
alice = generate_key_pair()
|
|
|
|
tx = Transaction.create([alice.public_key],
|
|
|
|
[([alice.public_key], 1)],
|
|
|
|
asset=None) \
|
|
|
|
.sign([alice.private_key])
|
|
|
|
mode_endpoint = TX_ENDPOINT + mode[0]
|
|
|
|
client.post(mode_endpoint, data=json.dumps(tx.to_dict()))
|
|
|
|
args, kwargs = mock_post.call_args
|
|
|
|
assert mode[1] == kwargs['json']['method']
|
|
|
|
|
|
|
|
|
2018-05-16 15:17:33 +02:00
|
|
|
@pytest.mark.abci
|
2018-01-11 13:26:06 +01:00
|
|
|
def test_post_transaction_invalid_mode(client):
|
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
from bigchaindb.common.crypto import generate_key_pair
|
|
|
|
alice = generate_key_pair()
|
|
|
|
tx = Transaction.create([alice.public_key],
|
|
|
|
[([alice.public_key], 1)],
|
|
|
|
asset=None) \
|
|
|
|
.sign([alice.private_key])
|
|
|
|
mode_endpoint = TX_ENDPOINT + '?mode=nope'
|
|
|
|
response = client.post(mode_endpoint, data=json.dumps(tx.to_dict()))
|
|
|
|
assert '400 BAD REQUEST' in response.status
|
2018-01-12 11:06:27 +01:00
|
|
|
assert 'Mode must be "async", "sync" or "commit"' ==\
|
|
|
|
json.loads(response.data.decode('utf8'))['message']['mode']
|