Problem: mocking in abci test

Solution: create test scenario to test the voting
This commit is contained in:
codegeschrei 2018-08-14 14:53:17 +02:00
parent a535daf955
commit 4cba93962e
4 changed files with 44 additions and 27 deletions

View File

@ -152,15 +152,21 @@ def run_upsert_validator_approve(args, bigchain):
:return: a success message :return: a success message
:raises: OperationError if the write transaction fails for any reason :raises: OperationError if the write transaction fails for any reason
""" """
key = load_node_key(args.sk) key = load_node_key(args.sk)
tx = bigchain.get_transaction(args.election_id) tx = bigchain.get_transaction(args.election_id)
voting_power = ValidatorElection.current_validators(bigchain)[key.public_key] voting_power = ValidatorElection.current_validators(bigchain)[
approval_tx = ValidatorElectionVote.generate(tx.to_inputs(), [ key.public_key]
approval = ValidatorElectionVote.generate(tx.to_inputs(), [
([key.public_key], voting_power)], tx.id).sign([key.private_key]) ([key.public_key], voting_power)], tx.id).sign([key.private_key])
approval_tx.validate(bigchain) approval.validate(bigchain)
resp = bigchain.write_transaction(approval_tx, 'broadcast_tx_commit')
resp = bigchain.write_transaction(approval, 'broadcast_tx_commit')
if resp == (202, ''): if resp == (202, ''):
return 'Your vote has been submitted.' print('Your vote has been submitted.')
return approval.id
else: else:
raise OperationError('Failed to vote for election') raise OperationError('Failed to vote for election')

View File

@ -32,11 +32,10 @@ class ValidatorElection(Transaction):
"""Return a dictionary of validators with key as `public_key` and """Return a dictionary of validators with key as `public_key` and
value as the `voting_power` value as the `voting_power`
""" """
validators = {} validators = {}
for validator in bigchain.get_validators(): for validator in bigchain.get_validators():
# NOTE: we assume that Tendermint encodes public key in base64 # NOTE: we assume that Tendermint encodes public key in base64
public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['value'])) public_key = public_key_from_ed25519_key(key_from_base64(validator['pub_key']['data']))
validators[public_key] = validator['voting_power'] validators[public_key] = validator['voting_power']
return validators return validators
@ -114,7 +113,7 @@ class ValidatorElection(Transaction):
if not self.is_same_topology(current_validators, self.outputs): if not self.is_same_topology(current_validators, self.outputs):
raise UnequalValidatorSet('Validator set much be exactly same to the outputs of election') raise UnequalValidatorSet('Validator set much be exactly same to the outputs of election')
return True return self
@classmethod @classmethod
def generate(cls, initiator, voters, election_data, metadata=None): def generate(cls, initiator, voters, election_data, metadata=None):

View File

@ -411,37 +411,26 @@ def test_upsert_validator_new_without_tendermint(b, priv_validator_path, user_sk
assert b.get_transaction(resp) assert b.get_transaction(resp)
@pytest.mark.tendermint
@pytest.mark.abci @pytest.mark.abci
@pytest.mark.bdb @pytest.mark.bdb
def test_upsert_validator_approve(b, priv_validator_path, user_sk, monkeypatch): def test_upsert_validator_approve(b, priv_validator_path, user_sk, validators):
from bigchaindb.commands.bigchaindb import run_upsert_validator_new, \ from bigchaindb.commands.bigchaindb import run_upsert_validator_new, \
run_upsert_validator_approve run_upsert_validator_approve
def mock_get(): public_key = '8eJ8q9ZQpReWyQT5aFCiwtZ5wDZC4eDnCen88p3tQ6ie'
return [
{'pub_key': {'value': 'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=',
'type': 'tendermint/PubKeyEd25519'},
'voting_power': 10}
]
def mock_write(tx, mode):
b.store_transaction(tx)
return 202, ''
b.get_validators = mock_get
b.write_transaction = mock_write
monkeypatch.setattr('requests.get', mock_get)
public_key = 'CJxdItf4lz2PwEf4SmYNAu/c/VpmX39JEgC5YpH7fxg='
new_args = Namespace(action='new', new_args = Namespace(action='new',
public_key=public_key, public_key=public_key,
power=1, power=1,
node_id='12345', node_id='12345',
sk=priv_validator_path, sk=priv_validator_path,
config={}) config={})
election_id = run_upsert_validator_new(new_args, b) election_id = run_upsert_validator_new(new_args, b)
args = Namespace(action='approve', args = Namespace(action='approve',
election_id=election_id, election_id=election_id,
sk=priv_validator_path, sk=priv_validator_path,
config={}) config={})
approve = run_upsert_validator_approve(args, b) approve = run_upsert_validator_approve(args, b)
assert approve == 'Your vote has been submitted.'
assert b.get_transaction(approve)

View File

@ -661,7 +661,7 @@ def ed25519_node_keys(node_keys):
return node_keys_dict return node_keys_dict
@pytest.fixture(scope='session') @pytest.fixture
def node_keys(): def node_keys():
return {'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=': return {'zL/DasvKulXZzhSNFwx4cLRXKkSM9GPK7Y0nZ4FEylM=':
'cM5oW4J0zmUSZ/+QRoRlincvgCwR0pEjFoY//ZnnjD3Mv8Nqy8q6VdnOFI0XDHhwtFcqRIz0Y8rtjSdngUTKUw==', 'cM5oW4J0zmUSZ/+QRoRlincvgCwR0pEjFoY//ZnnjD3Mv8Nqy8q6VdnOFI0XDHhwtFcqRIz0Y8rtjSdngUTKUw==',
@ -673,7 +673,12 @@ def node_keys():
'uz8bYgoL4rHErWT1gjjrnA+W7bgD/uDQWSRKDmC8otc95wnnxJo1GxYlmh0OaqOkJaobpu13BcUcvITjRFiVgw=='} 'uz8bYgoL4rHErWT1gjjrnA+W7bgD/uDQWSRKDmC8otc95wnnxJo1GxYlmh0OaqOkJaobpu13BcUcvITjRFiVgw=='}
@pytest.fixture(scope='session') @pytest.fixture
def private_validator_keys(node_keys):
return list(node_keys.items())[0]
@pytest.fixture
def priv_validator_path(node_keys): def priv_validator_path(node_keys):
(public_key, private_key) = list(node_keys.items())[0] (public_key, private_key) = list(node_keys.items())[0]
priv_validator = { priv_validator = {
@ -695,3 +700,21 @@ def priv_validator_path(node_keys):
json.dump(priv_validator, socket) json.dump(priv_validator, socket)
socket.close() socket.close()
return path return path
@pytest.fixture
def validators(b, node_keys):
from bigchaindb.backend import query
(public_key, private_key) = list(node_keys.items())[0]
validator_set = [{'address': 'F5426F0980E36E03044F74DD414248D29ABCBDB2',
'pub_key': {
'data': public_key,
'type': 'ed25519'},
'voting_power': 10}]
validator_update = {'validators': validator_set,
'height': 4266642}
query.store_validator_set(b.connection, validator_update)