1
0
mirror of https://github.com/bigchaindb/bigchaindb.git synced 2024-06-30 05:32:01 +02:00

implement /votes endpoint

This commit is contained in:
diminator 2017-01-10 11:34:10 +01:00
parent 15b91fea99
commit 5608a36616
No known key found for this signature in database
GPG Key ID: C3D8590E6D0D439A
3 changed files with 104 additions and 0 deletions

View File

@ -4,6 +4,7 @@ from bigchaindb.web.views import (
info,
statuses,
transactions as tx, unspents,
votes,
)
@ -25,6 +26,7 @@ ROUTES_API_V1 = [
r('transactions/<string:tx_id>', tx.TransactionApi),
r('transactions', tx.TransactionListApi),
r('unspents/', unspents.UnspentListApi),
r('votes/', votes.VotesApi),
]

View File

@ -0,0 +1,37 @@
"""This module provides the blueprint for the statuses API endpoints.
For more information please refer to the documentation on ReadTheDocs:
- https://docs.bigchaindb.com/projects/server/en/latest/drivers-clients/
http-client-server-api.html
"""
from flask import current_app
from flask_restful import Resource, reqparse
from bigchaindb import backend
from bigchaindb.web.views.base import make_error
class VotesApi(Resource):
def get(self):
"""API endpoint to get details about the status of a transaction or a block.
Return:
A ``dict`` in the format ``{'status': <status>}``, where
``<status>`` is one of "valid", "invalid", "undecided", "backlog".
"""
parser = reqparse.RequestParser()
parser.add_argument('block_id', type=str)
args = parser.parse_args(strict=True)
if sum(arg is not None for arg in args.values()) != 1:
return make_error(400, "Provide the block_id query parameter.")
pool = current_app.config['bigchain_pool']
votes = []
with pool() as bigchain:
if args['block_id']:
votes = list(backend.query.get_votes_by_block_id(bigchain.connection, args['block_id']))
return votes

65
tests/web/test_votes.py Normal file
View File

@ -0,0 +1,65 @@
import pytest
from bigchaindb.models import Transaction
VOTES_ENDPOINT = '/api/v1/votes'
@pytest.mark.bdb
@pytest.mark.usefixtures('inputs')
def test_get_votes_endpoint(b, client):
tx = Transaction.create([b.me], [([b.me], 1)])
tx = tx.sign([b.me_private])
block = b.create_block([tx])
b.write_block(block)
# vote the block valid
vote = b.vote(block.id, b.get_last_voted_block().id, True)
b.write_vote(vote)
res = client.get(VOTES_ENDPOINT + "?block_id=" + block.id)
assert vote == res.json[0]
assert len(res.json) == 1
assert res.status_code == 200
@pytest.mark.bdb
@pytest.mark.usefixtures('inputs')
def test_get_votes_multiple(b, client):
tx = Transaction.create([b.me], [([b.me], 1)])
tx = tx.sign([b.me_private])
block = b.create_block([tx])
b.write_block(block)
last_block = b.get_last_voted_block().id
# vote the block valid
vote_valid = b.vote(block.id, last_block, True)
b.write_vote(vote_valid)
# vote the block valid
vote_invalid = b.vote(block.id, last_block, False)
b.write_vote(vote_invalid)
res = client.get(VOTES_ENDPOINT + "?block_id=" + block.id)
assert len(res.json) == 2
assert res.status_code == 200
@pytest.mark.bdb
def test_get_votes_endpoint_returns_empty_list_not_found(client):
res = client.get(VOTES_ENDPOINT + "?block_id=123")
assert [] == res.json
assert res.status_code == 200
@pytest.mark.bdb
def test_get_status_endpoint_returns_400_bad_query_params(client):
res = client.get(VOTES_ENDPOINT)
assert res.status_code == 400
res = client.get(VOTES_ENDPOINT + "?ts_id=123")
assert res.status_code == 400
res = client.get(VOTES_ENDPOINT + "?tx_id=123&block_id=123")
assert res.status_code == 400