bigchaindb/tests/test_client.py

95 lines
2.7 KiB
Python

import pytest
@pytest.fixture
def client():
from bigchaindb.client import temp_client
return temp_client()
@pytest.fixture
def mock_requests_post(monkeypatch):
class MockResponse:
def __init__(self, json):
self._json = json
def json(self):
return self._json
def mockreturn(*args, **kwargs):
return MockResponse(kwargs.get('json'))
monkeypatch.setattr('requests.post', mockreturn)
def test_temp_client_returns_a_temp_client():
from bigchaindb.client import temp_client
client = temp_client()
assert client.public_key
assert client.private_key
def test_client_can_make_transactions(client):
tx = client.make_tx('a', 123)
assert tx['transaction']['current_owner'] == client.public_key
assert tx['transaction']['new_owner'] == 'a'
assert tx['transaction']['input'] == 123
def test_client_can_sign_transactions(client):
from bigchaindb import util
tx = client.make_tx('a', 123)
signed_tx = client.sign_tx(tx)
assert signed_tx['transaction']['current_owner'] == client.public_key
assert signed_tx['transaction']['new_owner'] == 'a'
assert signed_tx['transaction']['input'] == 123
assert util.verify_signature(signed_tx)
def test_client_can_push_transactions(mock_requests_post, client):
from bigchaindb import util
tx = client.make_tx('a', 123)
signed_tx = client.sign_tx(tx)
ret_tx = client.push_tx(signed_tx)
assert ret_tx['transaction']['current_owner'] == client.public_key
assert ret_tx['transaction']['new_owner'] == 'a'
assert ret_tx['transaction']['input'] == 123
assert util.verify_signature(ret_tx)
def test_client_can_create_transactions_using_shortcut_method(mock_requests_post, client):
from bigchaindb import util
tx = client.create()
# XXX: `CREATE` operations require the node that receives the transaction to modify the data in
# the transaction itself.
# `current_owner` will be overwritten with the public key of the node in the federation
# that will create the real transaction. `signature` will be overwritten with the new signature.
# Note that this scenario is ignored by this test.
assert tx['transaction']['current_owner'] == client.public_key
assert tx['transaction']['new_owner'] == client.public_key
assert tx['transaction']['input'] == None
assert util.verify_signature(tx)
def test_client_can_transfer_transactions_using_shortcut_method(mock_requests_post, client):
from bigchaindb import util
tx = client.transfer('a', 123)
assert tx['transaction']['current_owner'] == client.public_key
assert tx['transaction']['new_owner'] == 'a'
assert tx['transaction']['input'] == 123
assert util.verify_signature(tx)