mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge pull request #1029 from bigchaindb/feat/api/votes
Implement votes endpoint
This commit is contained in:
commit
0751b2bf0b
@ -208,7 +208,9 @@ def count_backlog(conn):
|
|||||||
|
|
||||||
@register_query(MongoDBConnection)
|
@register_query(MongoDBConnection)
|
||||||
def write_vote(conn, vote):
|
def write_vote(conn, vote):
|
||||||
return conn.db['votes'].insert_one(vote)
|
conn.db['votes'].insert_one(vote)
|
||||||
|
vote.pop('_id')
|
||||||
|
return vote
|
||||||
|
|
||||||
|
|
||||||
@register_query(MongoDBConnection)
|
@register_query(MongoDBConnection)
|
||||||
|
@ -5,6 +5,7 @@ from bigchaindb.web.views import (
|
|||||||
statuses,
|
statuses,
|
||||||
transactions as tx,
|
transactions as tx,
|
||||||
unspents,
|
unspents,
|
||||||
|
votes,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ ROUTES_API_V1 = [
|
|||||||
r('transactions/<string:tx_id>', tx.TransactionApi),
|
r('transactions/<string:tx_id>', tx.TransactionApi),
|
||||||
r('transactions', tx.TransactionListApi),
|
r('transactions', tx.TransactionListApi),
|
||||||
r('unspents/', unspents.UnspentListApi),
|
r('unspents/', unspents.UnspentListApi),
|
||||||
|
r('votes/', votes.VotesApi),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
29
bigchaindb/web/views/votes.py
Normal file
29
bigchaindb/web/views/votes.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""This module provides the blueprint for the votes 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
|
||||||
|
|
||||||
|
|
||||||
|
class VotesApi(Resource):
|
||||||
|
def get(self):
|
||||||
|
"""API endpoint to get details about votes on a block.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
A list of votes voting for a block with ID ``block_id``.
|
||||||
|
"""
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('block_id', type=str, required=True)
|
||||||
|
|
||||||
|
args = parser.parse_args(strict=True)
|
||||||
|
|
||||||
|
pool = current_app.config['bigchain_pool']
|
||||||
|
with pool() as bigchain:
|
||||||
|
votes = list(backend.query.get_votes_by_block_id(bigchain.connection, args['block_id']))
|
||||||
|
|
||||||
|
return votes
|
@ -312,7 +312,8 @@ def test_write_vote(structurally_valid_vote):
|
|||||||
query.write_vote(conn, structurally_valid_vote)
|
query.write_vote(conn, structurally_valid_vote)
|
||||||
# retrieve the vote
|
# retrieve the vote
|
||||||
vote_db = conn.db.votes.find_one(
|
vote_db = conn.db.votes.find_one(
|
||||||
{'node_pubkey': structurally_valid_vote['node_pubkey']}
|
{'node_pubkey': structurally_valid_vote['node_pubkey']},
|
||||||
|
{'_id': False}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert vote_db == structurally_valid_vote
|
assert vote_db == structurally_valid_vote
|
||||||
|
69
tests/web/test_votes.py
Normal file
69
tests/web/test_votes.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
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_endpoint_multiple_votes(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=")
|
||||||
|
assert [] == res.json
|
||||||
|
assert res.status_code == 200
|
||||||
|
|
||||||
|
res = client.get(VOTES_ENDPOINT + "?block_id=123")
|
||||||
|
assert [] == res.json
|
||||||
|
assert res.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.bdb
|
||||||
|
def test_get_votes_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
|
Loading…
x
Reference in New Issue
Block a user