1
0
mirror of https://github.com/bigchaindb/bigchaindb.git synced 2024-06-10 20:05:02 +02:00
bigchaindb/tests/assets/test_digital_assets.py

184 lines
6.8 KiB
Python
Raw Normal View History

import pytest
from ..db.conftest import inputs
@pytest.mark.usefixtures('inputs')
def test_asset_transfer(b, user_vk, user_sk):
2016-10-07 15:46:21 +02:00
from bigchaindb.models import Transaction
tx_input = b.get_owned_ids(user_vk).pop()
2016-10-07 15:46:21 +02:00
tx_create = b.get_transaction(tx_input.txid)
tx_transfer = Transaction.transfer(tx_create.to_inputs(), [user_vk], tx_create.asset)
tx_transfer_signed = tx_transfer.sign([user_sk])
2016-10-07 15:46:21 +02:00
assert tx_transfer_signed.validate(b) == tx_transfer_signed
assert tx_transfer_signed.asset.data_id == tx_create.asset.data_id
def test_validate_bad_asset_creation(b, user_vk):
2016-10-07 15:46:21 +02:00
from bigchaindb_common.exceptions import AmountError
from bigchaindb.models import Transaction
2016-10-07 15:46:21 +02:00
tx = Transaction.create([b.me], [user_vk])
tx.asset.divisible = 1
tx_signed = tx.sign([b.me_private])
with pytest.raises(TypeError):
2016-10-07 15:46:21 +02:00
tx_signed.validate(b)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['asset'].update({'refillable': 1})
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['asset'].update({'updatable': 1})
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['asset'].update({'data': 'a'})
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['conditions'][0]['amount'] = 'a'
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['conditions'][0]['amount'] = 2
tx['transaction']['asset'].update({'divisible': False})
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(AmountError):
b.validate_transaction(tx_signed)
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
tx['transaction']['conditions'][0]['amount'] = 0
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, b.me_private)
with pytest.raises(AmountError):
b.validate_transaction(tx_signed)
@pytest.mark.usefixtures('inputs')
2016-09-09 10:53:28 +02:00
def test_validate_transfer_asset_id_mismatch(b, user_vk, user_sk):
from bigchaindb.util import get_hash_data
2016-10-07 15:46:21 +02:00
from bigchaindb_common.exceptions import AssetIdMismatch
tx_input = b.get_owned_ids(user_vk).pop()
tx = b.create_transaction(user_vk, user_vk, tx_input, 'TRANFER')
tx['transaction']['asset']['id'] = 'aaa'
tx['id'] = get_hash_data(tx['transaction'])
tx_signed = b.sign_transaction(tx, user_sk)
with pytest.raises(AssetIdMismatch):
b.validate_transaction(tx_signed)
def test_validate_asset_arguments(b):
2016-10-07 15:46:21 +02:00
from bigchaindb_common.exceptions import AmountError
with pytest.raises(TypeError):
b.create_transaction(b.me, b.me, None, 'CREATE', divisible=1)
with pytest.raises(TypeError):
b.create_transaction(b.me, b.me, None, 'CREATE', refillable=1)
with pytest.raises(TypeError):
b.create_transaction(b.me, b.me, None, 'CREATE', updatable=1)
with pytest.raises(TypeError):
b.create_transaction(b.me, b.me, None, 'CREATE', data='a')
with pytest.raises(TypeError):
b.create_transaction(b.me, b.me, None, 'CREATE', amount='a')
with pytest.raises(AmountError):
b.create_transaction(b.me, b.me, None, 'CREATE', divisible=False, amount=2)
with pytest.raises(AmountError):
b.create_transaction(b.me, b.me, None, 'CREATE', amount=0)
@pytest.mark.usefixtures('inputs')
def test_get_asset_id_create_transaction(b, user_vk):
from bigchaindb.assets import get_asset_id
tx_input = b.get_owned_ids(user_vk).pop()
tx_create = b.get_transaction(tx_input['txid'])
asset_id = get_asset_id(tx_create)
assert asset_id == tx_create['transaction']['asset']['id']
@pytest.mark.usefixtures('inputs')
def test_get_asset_id_transfer_transaction(b, user_vk, user_sk):
from bigchaindb.assets import get_asset_id
tx_input = b.get_owned_ids(user_vk).pop()
# create a transfer transaction
tx_transfer = b.create_transaction(user_vk, user_vk, tx_input, 'TRANSFER')
tx_transfer_signed = b.sign_transaction(tx_transfer, user_sk)
# create a block
block = b.create_block([tx_transfer_signed])
b.write_block(block, durability='hard')
# vote the block valid
vote = b.vote(block['id'], b.get_last_voted_block()['id'], True)
b.write_vote(vote)
asset_id = get_asset_id(tx_transfer)
assert asset_id == tx_transfer['transaction']['asset']['id']
@pytest.mark.usefixtures('inputs')
def test_asset_id_mismatch(b, user_vk):
from bigchaindb.assets import get_asset_id
2016-10-07 15:46:21 +02:00
from bigchaindb_common.exceptions import AssetIdMismatch
tx_input1, tx_input2 = b.get_owned_ids(user_vk)[:2]
tx1 = b.get_transaction(tx_input1['txid'])
tx2 = b.get_transaction(tx_input2['txid'])
with pytest.raises(AssetIdMismatch):
get_asset_id([tx1, tx2])
def test_get_asset_id_transaction_does_not_exist(b, user_vk):
2016-10-07 15:46:21 +02:00
from bigchaindb_common.exceptions import TransactionDoesNotExist
with pytest.raises(TransactionDoesNotExist):
b.create_transaction(user_vk, user_vk, {'txid': 'bored', 'cid': '0'}, 'TRANSFER')
@pytest.mark.usefixtures('inputs')
def test_get_txs_by_asset_id(b, user_vk, user_sk):
tx_input = b.get_owned_ids(user_vk).pop()
tx = b.get_transaction(tx_input['txid'])
asset_id = tx['transaction']['asset']['id']
txs = b.get_txs_by_asset_id(asset_id)
assert len(txs) == 1
assert txs[0]['id'] == tx['id']
assert txs[0]['transaction']['asset']['id'] == asset_id
# create a transfer transaction
tx_transfer = b.create_transaction(user_vk, user_vk, tx_input, 'TRANSFER')
tx_transfer_signed = b.sign_transaction(tx_transfer, user_sk)
# create the block
block = b.create_block([tx_transfer_signed])
b.write_block(block, durability='hard')
# vote the block valid
vote = b.vote(block['id'], b.get_last_voted_block()['id'], True)
b.write_vote(vote)
txs = b.get_txs_by_asset_id(asset_id)
assert len(txs) == 2
assert tx['id'] in [t['id'] for t in txs]
assert tx_transfer['id'] in [t['id'] for t in txs]
assert asset_id == txs[0]['transaction']['asset']['id']
assert asset_id == txs[1]['transaction']['asset']['id']