diff --git a/bigchaindb/web/views/unspents.py b/bigchaindb/web/views/unspents.py new file mode 100644 index 00000000..b3a52efe --- /dev/null +++ b/bigchaindb/web/views/unspents.py @@ -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) diff --git a/tests/web/test_unspents.py b/tests/web/test_unspents.py new file mode 100644 index 00000000..072ffe21 --- /dev/null +++ b/tests/web/test_unspents.py @@ -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