Add tests for HTTP API returned errors

This commit is contained in:
Sylvain Bellemare 2016-12-08 14:29:30 +01:00 committed by Sylvain Bellemare
parent 209dba76a3
commit 6f90c8f3df

View File

@ -1,3 +1,4 @@
import builtins
import json
import pytest
@ -38,6 +39,7 @@ def test_post_create_transaction_endpoint(b, client):
def test_post_create_transaction_with_invalid_id(b, client):
from bigchaindb.common.exceptions import InvalidHash
from bigchaindb.models import Transaction
user_priv, user_pub = crypto.generate_key_pair()
@ -47,9 +49,14 @@ def test_post_create_transaction_with_invalid_id(b, client):
res = client.post(TX_ENDPOINT, data=json.dumps(tx))
assert res.status_code == 400
err_msg = ("The transaction's id '{}' isn't equal to "
"the hash of its body, i.e. it's not valid.").format(tx['id'])
assert res.json['message'] == (
'Invalid transaction ({}): {}'.format(InvalidHash.__name__, err_msg))
def test_post_create_transaction_with_invalid_signature(b, client):
from bigchaindb.common.exceptions import InvalidSignature
from bigchaindb.models import Transaction
user_priv, user_pub = crypto.generate_key_pair()
@ -59,6 +66,9 @@ def test_post_create_transaction_with_invalid_signature(b, client):
res = client.post(TX_ENDPOINT, data=json.dumps(tx))
assert res.status_code == 400
assert res.json['message'] == (
"Invalid transaction ({}): Fulfillment URI "
"couldn't been parsed".format(InvalidSignature.__name__))
def test_post_create_transaction_with_invalid_structure(client):
@ -78,6 +88,37 @@ def test_post_create_transaction_with_invalid_schema(client):
"Invalid transaction schema: 'version' is a required property")
@pytest.mark.parametrize('exc,msg', (
('AmountError', 'Do the math again!'),
('DoubleSpend', 'Nope! It is gone now!'),
('InvalidHash', 'Do not smoke that!'),
('InvalidSignature', 'Falsche Unterschrift!'),
('OperationError', 'Create and transfer!'),
('TransactionDoesNotExist', 'Hallucinations?'),
('TransactionOwnerError', 'Not yours!'),
('TransactionNotInValidBlock', 'Wait, maybe?'),
('ValueError', '?'),
))
def test_post_invalid_transaction(client, exc, msg, monkeypatch):
from bigchaindb.common import exceptions
try:
exc_cls = getattr(exceptions, exc)
except AttributeError:
exc_cls = getattr(builtins, 'ValueError')
def mock_validation(self_, tx):
raise exc_cls(msg)
monkeypatch.setattr(
'bigchaindb.Bigchain.validate_transaction', mock_validation)
monkeypatch.setattr(
'bigchaindb.models.Transaction.from_dict', lambda tx: None)
res = client.post(TX_ENDPOINT, data=json.dumps({}))
assert res.status_code == 400
assert (res.json['message'] ==
'Invalid transaction ({}): {}'.format(exc, msg))
@pytest.mark.usefixtures('inputs')
def test_post_transfer_transaction_endpoint(b, client, user_pk, user_sk):
sk, pk = crypto.generate_key_pair()