diff --git a/bigchaindb/web/views/blocks.py b/bigchaindb/web/views/blocks.py index 9d509460..7e840fe5 100644 --- a/bigchaindb/web/views/blocks.py +++ b/bigchaindb/web/views/blocks.py @@ -38,8 +38,9 @@ class BlockListApi(Resource): """API endpoint to get the related blocks for a transaction. Return: - A ``list`` of ``block_id`` that may be filtered when provided - a status query parameter: "valid", "invalid", "undecided". + A ``list`` of ``block_id``s that contain the given transaction. The + list may be filtered when provided a status query parameter: + "valid", "invalid", "undecided". """ parser = reqparse.RequestParser() parser.add_argument('tx_id', type=str, required=True) diff --git a/tests/web/test_blocks.py b/tests/web/test_blocks.py index c61bf27b..b0581061 100644 --- a/tests/web/test_blocks.py +++ b/tests/web/test_blocks.py @@ -38,11 +38,6 @@ def test_get_blocks_by_txid_endpoint(b, client): tx2 = Transaction.create([b.me], [([b.me], 10)]) tx2 = tx2.sign([b.me_private]) - res = client.get(BLOCKS_ENDPOINT + "?tx_id=" + tx.id) - # test if block is retrieved as undecided - assert res.status_code == 200 - assert len(res.json) == 0 - block_invalid = b.create_block([tx]) b.write_block(block_invalid) @@ -97,37 +92,64 @@ def test_get_blocks_by_txid_and_status_endpoint(b, client): block_invalid = b.create_block([tx]) b.write_block(block_invalid) - # vote the block invalid - vote = b.vote(block_invalid.id, b.get_last_voted_block().id, False) - b.write_vote(vote) - # create a new block containing the same tx (and tx2 to avoid block id collision) block_valid = b.create_block([tx, tx2]) b.write_block(block_valid) res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_INVALID)) - # test if block is retrieved as invalid + # test if no blocks are retrieved as invalid + assert res.status_code == 200 + assert len(res.json) == 0 + + res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_UNDECIDED)) + # test if both blocks are retrieved as undecided + assert res.status_code == 200 + assert block_valid.id in res.json + assert block_invalid.id in res.json + assert len(res.json) == 2 + + res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_VALID)) + # test if no blocks are retrieved as valid + assert res.status_code == 200 + assert len(res.json) == 0 + + # vote one of the blocks invalid + vote = b.vote(block_invalid.id, b.get_last_voted_block().id, False) + b.write_vote(vote) + + # vote the other block valid + vote = b.vote(block_valid.id, block_invalid.id, True) + b.write_vote(vote) + + res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_INVALID)) + # test if the invalid block is retrieved as invalid assert res.status_code == 200 assert block_invalid.id in res.json assert len(res.json) == 1 res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_UNDECIDED)) - # test if block is retrieved as undecided + # test if no blocks are retrieved as undecided assert res.status_code == 200 - assert block_valid.id in res.json - assert len(res.json) == 1 - - # vote the block valid - vote = b.vote(block_valid.id, block_invalid.id, True) - b.write_vote(vote) + assert len(res.json) == 0 res = client.get("{}?tx_id={}&status={}".format(BLOCKS_ENDPOINT, tx.id, Bigchain.BLOCK_VALID)) - # test if block is retrieved as valid + # test if the valid block is retrieved as valid assert res.status_code == 200 assert block_valid.id in res.json assert len(res.json) == 1 +@pytest.mark.bdb +def test_get_blocks_by_txid_endpoint_returns_empty_list_not_found(client): + res = client.get(BLOCKS_ENDPOINT + "?tx_id=") + assert res.status_code == 200 + assert len(res.json) == 0 + + res = client.get(BLOCKS_ENDPOINT + "?tx_id=123") + assert res.status_code == 200 + assert len(res.json) == 0 + + @pytest.mark.bdb def test_get_blocks_by_txid_endpoint_returns_400_bad_query_params(client): res = client.get(BLOCKS_ENDPOINT)