Add unspents endpoint

This commit is contained in:
tim 2016-11-15 11:40:04 +01:00 committed by Sylvain Bellemare
parent f78c90ada8
commit 8e8bc90742
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,32 @@
from flask import current_app, Blueprint
from flask_restful import reqparse, Resource, Api
unspent_views = Blueprint('unspent_views', __name__)
unspent_api = Api(unspent_views)
class UnspentListApi(Resource):
def get(self):
"""API endpoint to retrieve a list of links to transactions's
conditions that have not been used in any previous transaction.
Returns:
A :obj:`list` of :cls:`str` of links to unfulfilled conditions.
"""
parser = reqparse.RequestParser()
parser.add_argument('owner_after', type=str, location='args',
required=True)
args = parser.parse_args()
pool = current_app.config['bigchain_pool']
with pool() as bigchain:
unspents = bigchain.get_owned_ids(args['owner_after'])
# NOTE: We pass '..' as a path to create a valid relative URI
return [u.to_uri('..') for u in unspents]
unspent_api.add_resource(UnspentListApi,
'/unspents/',
strict_slashes=False)

View File

@ -0,0 +1,26 @@
import pytest
UNSPENTS_ENDPOINT = '/api/v1/unspents/'
@pytest.mark.usefixtures('inputs')
def test_get_unspents_endpoint(b, client, user_vk):
expected = [u.to_uri('..') for u in b.get_owned_ids(user_vk)]
res = client.get(UNSPENTS_ENDPOINT + '?owner_after={}'.format(user_vk))
assert expected == res.json
assert res.status_code == 200
@pytest.mark.usefixtures('inputs')
def test_get_unspents_endpoint_without_owner_after(client):
res = client.get(UNSPENTS_ENDPOINT)
assert res.status_code == 400
@pytest.mark.usefixtures('inputs')
def test_get_unspents_endpoint_with_unused_owner_after(client):
expected = []
res = client.get(UNSPENTS_ENDPOINT + '?owner_after=abc')
assert expected == res.json
assert res.status_code == 200