bigchaindb/tests/web/test_outputs.py

132 lines
4.4 KiB
Python
Raw Normal View History

# 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
import pytest
from unittest.mock import MagicMock, patch
pytestmark = [pytest.mark.bdb, pytest.mark.usefixtures('inputs')]
OUTPUTS_ENDPOINT = '/api/v1/outputs/'
def test_get_outputs_endpoint(client, user_pk):
m = MagicMock()
m.txid = 'a'
m.output = 0
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_outputs_filtered') as gof:
gof.return_value = [m, m]
res = client.get(OUTPUTS_ENDPOINT + '?public_key={}'.format(user_pk))
assert res.json == [
{'transaction_id': 'a', 'output_index': 0},
{'transaction_id': 'a', 'output_index': 0}
]
assert res.status_code == 200
gof.assert_called_once_with(user_pk, None)
def test_get_outputs_endpoint_unspent(client, user_pk):
m = MagicMock()
m.txid = 'a'
m.output = 0
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_outputs_filtered') as gof:
gof.return_value = [m]
params = '?spent=False&public_key={}'.format(user_pk)
res = client.get(OUTPUTS_ENDPOINT + params)
assert res.json == [{'transaction_id': 'a', 'output_index': 0}]
assert res.status_code == 200
gof.assert_called_once_with(user_pk, False)
def test_get_outputs_endpoint_spent(client, user_pk):
m = MagicMock()
m.txid = 'a'
m.output = 0
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_outputs_filtered') as gof:
gof.return_value = [m]
params = '?spent=true&public_key={}'.format(user_pk)
res = client.get(OUTPUTS_ENDPOINT + params)
assert res.json == [{'transaction_id': 'a', 'output_index': 0}]
assert res.status_code == 200
gof.assert_called_once_with(user_pk, True)
def test_get_outputs_endpoint_without_public_key(client):
res = client.get(OUTPUTS_ENDPOINT)
assert res.status_code == 400
def test_get_outputs_endpoint_with_invalid_public_key(client):
expected = {'message': {'public_key': 'Invalid base58 ed25519 key'}}
res = client.get(OUTPUTS_ENDPOINT + '?public_key=abc')
assert expected == res.json
assert res.status_code == 400
def test_get_outputs_endpoint_with_invalid_spent(client, user_pk):
expected = {'message': {'spent': 'Boolean value must be "true" or "false" (lowercase)'}}
params = '?spent=tru&public_key={}'.format(user_pk)
res = client.get(OUTPUTS_ENDPOINT + params)
assert expected == res.json
assert res.status_code == 400
2017-02-17 09:58:55 +01:00
@pytest.mark.bdb
@pytest.mark.usefixtures('inputs')
def test_get_divisble_transactions_returns_500(b, client):
from bigchaindb.models import Transaction
from bigchaindb.common import crypto
import json
TX_ENDPOINT = '/api/v1/transactions'
def mine(tx_list):
b.store_bulk_transactions(tx_list)
2017-02-17 09:58:55 +01:00
alice_priv, alice_pub = crypto.generate_key_pair()
bob_priv, bob_pub = crypto.generate_key_pair()
carly_priv, carly_pub = crypto.generate_key_pair()
create_tx = Transaction.create([alice_pub], [([alice_pub], 4)])
create_tx.sign([alice_priv])
res = client.post(TX_ENDPOINT, data=json.dumps(create_tx.to_dict()))
assert res.status_code == 202
mine([create_tx])
transfer_tx = Transaction.transfer(create_tx.to_inputs(),
[([alice_pub], 3), ([bob_pub], 1)],
asset_id=create_tx.id)
transfer_tx.sign([alice_priv])
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx.to_dict()))
assert res.status_code == 202
mine([transfer_tx])
transfer_tx_carly = Transaction.transfer([transfer_tx.to_inputs()[1]],
2017-03-03 02:00:17 +01:00
[([carly_pub], 1)],
asset_id=create_tx.id)
2017-02-17 09:58:55 +01:00
transfer_tx_carly.sign([bob_priv])
res = client.post(TX_ENDPOINT, data=json.dumps(transfer_tx_carly.to_dict()))
assert res.status_code == 202
mine([transfer_tx_carly])
asset_id = create_tx.id
2017-03-07 12:58:48 +01:00
url = TX_ENDPOINT + '?asset_id=' + asset_id
2017-02-17 09:58:55 +01:00
assert client.get(url).status_code == 200
assert len(client.get(url).json) == 3
url = OUTPUTS_ENDPOINT + '?public_key=' + alice_pub
assert client.get(url).status_code == 200
url = OUTPUTS_ENDPOINT + '?public_key=' + bob_pub
assert client.get(url).status_code == 200
url = OUTPUTS_ENDPOINT + '?public_key=' + carly_pub
assert client.get(url).status_code == 200