From 9dacc12b3cb92f9a93b4e7da69eca73f74f5aad3 Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Mon, 28 Nov 2016 19:31:42 +0100 Subject: [PATCH] Handle transaction schema validation errors in POST /transactions --- bigchaindb/web/views/transactions.py | 12 +++++++++++- tests/web/test_transactions.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/bigchaindb/web/views/transactions.py b/bigchaindb/web/views/transactions.py index 2ed19b7c..e2abeb48 100644 --- a/bigchaindb/web/views/transactions.py +++ b/bigchaindb/web/views/transactions.py @@ -6,7 +6,11 @@ For more information please refer to the documentation on ReadTheDocs: from flask import current_app, request, Blueprint from flask_restful import Resource, Api -from bigchaindb.common.exceptions import ValidationError, InvalidSignature +from bigchaindb.common.exceptions import ( + ValidationError, + InvalidSignature, + SchemaValidationError, +) import bigchaindb from bigchaindb.models import Transaction @@ -98,6 +102,12 @@ class TransactionListApi(Resource): try: tx_obj = Transaction.from_dict(tx) + except SchemaValidationError as e: + return make_error( + 400, + message='Invalid transaction schema: {}'.format( + e.__cause__.message) + ) except (ValidationError, InvalidSignature): return make_error(400, 'Invalid transaction') diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index 94c3f22f..f5071603 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -66,6 +66,18 @@ def test_post_create_transaction_with_invalid_structure(client): assert res.status_code == 400 +def test_post_create_transaction_with_invalid_schema(client): + from bigchaindb.models import Transaction + user_priv, user_pub = crypto.generate_key_pair() + tx = Transaction.create( + [user_pub], [([user_pub], 1)]).sign([user_priv]).to_dict() + del tx['version'] + res = client.post(TX_ENDPOINT, data=json.dumps(tx)) + assert res.status_code == 400 + assert res.json['message'] == ( + "Invalid transaction schema: 'version' is a required property") + + @pytest.mark.usefixtures('inputs') def test_post_transfer_transaction_endpoint(b, client, user_pk, user_sk): sk, pk = crypto.generate_key_pair()