bigchaindb/tests/test_config_utils.py
Sylvain Bellemare 64cafc62ad Merge common lib (#739)
* Planning release

* Clean up after move

* Add exceptions.py

* Add crypto.py

* Adjust setup to package structure

* Fix tests

* Add test coverage

* Comply to flake8

* Add test coverage

* Transfer-tx fulfillments validation

* Remove condition and fulfillment ids

* Fix signing logic

Specifically for transfer-tx with multiple inputs
and outputs.

* Compliance to legacy BDB models

* Adjust fulfillment validation interface

* Add serialization validation for txids

* Use __eq__ to compare objects

* Heavy refactor to comply with current implementation

* Add Transaction.create

* Correct fulfillment validation logic

* Add Transaction.create for hashlock conditions

* Add hashlock condition serialization

* Transaction.transfer add single input and outputs

* Small adjustments to transfer-tx interface

* Create transfer-tx interface

* Increase test coverage

* Adjust fulfillment (de)serialization

* Catch CC Error for Fulfillment

* Allow custom thresholds

* PR feedback

* Fix tests

* Rename Data to Metadata

* Add Asset exceptions

* Add basic Asset model

* More renaming of payload => data

* Add Asset into work-flow-functions

* Add Asset amount to condition

* add fulfillment exception

* initial integration of asset

* Make transaction.py compy to 79 chars

* Make util.py comply to 79 chars

* Make exceptions.py comply to 80 chars

* Renaming inp to input_

* fix pep8 issues

* Correct raised error

* Remove resolved TODOs

* prevent adding None as fulfillment / condition to Transaction

* Small modifications to support new cryptoconditions

* Improve documentation (#42)

* Add doc strings for Fulfillment cls

* Add doc strings for TransactionLink cls

* Add doc strings for Condition cls

* Add doc strings for Data cls

* Add doc strings for Transaction cls

* Add doc strings for Asset cls

* Extract common implementation

* Tx model: Add test for empty inputs

* WIP: Implement sign tx

* Add tests for:
    - Conditions; and
    - Fulfillments

Mostly on the (de)serialization part.

* Finalize serialization logic for tx class

* Add Tests for tx serialization logic

* Add fulfillment validation

* Add ThresholdCondition support

* WIP transfer

* Clean up after move

* Adjust setup to package structure

* Fix tests

* Add test coverage

* Add test coverage

* Transfer-tx fulfillments validation

* Remove condition and fulfillment ids

* Fix signing logic

Specifically for transfer-tx with multiple inputs
and outputs.

* Fix test case

* Compliance to legacy BDB models

* Adjust fulfillment validation interface

* Add serialization validation for txids

* Use __eq__ to compare objects

* Heavy refactor to comply with current implementation

* Add Transaction.create

* Add validation tests

* Add Transaction.create for hashlock conditions

* Add hashlock condition serialization

* Transaction.transfer add single input and outputs

* Small adjustments to transfer-tx interface

* Create transfer-tx interface

* Increase test coverage

* Adjust fulfillment (de)serialization

* Catch CC Error for Fulfillment

* Allow custom thresholds

* Rename Data to Metadata

* Add basic Asset model

* Add Asset into work-flow-functions

* Add Asset amount to condition

* initial integration of asset

* Make tests comply to 79 chars per line

* Fixed tests

* fix pep8 issues

* Correct raised error

* Add test for asset initialization

* Remove resolved TODOs

* prevent adding None as fulfillment / condition to Transaction

* Small modifications to support new cryptoconditions

* Extract common tests

* Copy conftest from bigchaindb-common - by @timdaub

* Replace bigchaindb_common pkg by bigchaindb.common
2016-10-25 10:21:20 +02:00

221 lines
6.7 KiB
Python

import copy
from unittest.mock import mock_open, patch
import pytest
import bigchaindb
ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config)
@pytest.fixture
def ignore_local_config_file(monkeypatch):
"""
This fixture's purpose is to override the one under
:module:`tests/conftest.py` so that the original behaviour of
:func:`bigchaindb.config_utils.file_config` is restored, so that it can be
tested.
"""
from bigchaindb.config_utils import file_config
monkeypatch.setattr('bigchaindb.config_utils.file_config', file_config)
@pytest.fixture(scope='function', autouse=True)
def clean_config(monkeypatch):
monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG))
def test_bigchain_instance_is_initialized_when_conf_provided():
from bigchaindb import config_utils
assert 'CONFIGURED' not in bigchaindb.config
config_utils.set_config({'keypair': {'public': 'a', 'private': 'b'}})
assert bigchaindb.config['CONFIGURED'] is True
b = bigchaindb.Bigchain()
assert b.me
assert b.me_private
def test_bigchain_instance_raises_when_not_configured(monkeypatch):
from bigchaindb import config_utils
from bigchaindb.common import exceptions
assert 'CONFIGURED' not in bigchaindb.config
# We need to disable ``bigchaindb.config_utils.autoconfigure`` to avoid reading
# from existing configurations
monkeypatch.setattr(config_utils, 'autoconfigure', lambda: 0)
with pytest.raises(exceptions.KeypairNotFoundException):
bigchaindb.Bigchain()
def test_map_leafs_iterator():
from bigchaindb import config_utils
mapping = {
'a': {'b': {'c': 1},
'd': {'z': 44}},
'b': {'d': 2},
'c': 3
}
result = config_utils.map_leafs(lambda x, path: x * 2, mapping)
assert result == {
'a': {'b': {'c': 2},
'd': {'z': 88}},
'b': {'d': 4},
'c': 6
}
result = config_utils.map_leafs(lambda x, path: path, mapping)
assert result == {
'a': {'b': {'c': ['a', 'b', 'c']},
'd': {'z': ['a', 'd', 'z']}},
'b': {'d': ['b', 'd']},
'c': ['c']
}
def test_update_types():
from bigchaindb import config_utils
raw = {
'a_string': 'test',
'an_int': '42',
'a_float': '3.14',
'a_list': 'a:b:c',
}
reference = {
'a_string': 'test',
'an_int': 42,
'a_float': 3.14,
'a_list': ['a', 'b', 'c'],
}
result = config_utils.update_types(raw, reference)
assert result == reference
def test_env_config(monkeypatch):
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_HOST': 'test-host',
'BIGCHAINDB_DATABASE_PORT': 'test-port'})
from bigchaindb import config_utils
result = config_utils.env_config({'database': {'host': None, 'port': None}})
expected = {'database': {'host': 'test-host', 'port': 'test-port'}}
assert result == expected
def test_autoconfigure_read_both_from_file_and_env(monkeypatch):
file_config = {
'database': {'host': 'test-host'},
'backlog_reassign_delay': 5
}
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda *args, **kwargs: file_config)
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_NAME': 'test-dbname',
'BIGCHAINDB_DATABASE_PORT': '4242',
'BIGCHAINDB_API_ENDPOINT': 'api://ipa',
'BIGCHAINDB_SERVER_BIND': '1.2.3.4:56',
'BIGCHAINDB_KEYRING': 'pubkey_0:pubkey_1:pubkey_2'})
import bigchaindb
from bigchaindb import config_utils
config_utils.autoconfigure()
assert bigchaindb.config == {
'CONFIGURED': True,
'server': {
'bind': '1.2.3.4:56',
'workers': None,
'threads': None,
},
'database': {
'host': 'test-host',
'port': 4242,
'name': 'test-dbname',
},
'keypair': {
'public': None,
'private': None,
},
'keyring': ['pubkey_0', 'pubkey_1', 'pubkey_2'],
'statsd': {
'host': 'localhost',
'port': 8125,
'rate': 0.01,
},
'api_endpoint': 'api://ipa',
'backlog_reassign_delay': 5
}
def test_autoconfigure_env_precedence(monkeypatch):
file_config = {
'database': {'host': 'test-host', 'name': 'bigchaindb', 'port': 28015}
}
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda *args, **kwargs: file_config)
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_NAME': 'test-dbname',
'BIGCHAINDB_DATABASE_PORT': '4242',
'BIGCHAINDB_SERVER_BIND': 'localhost:9985'})
import bigchaindb
from bigchaindb import config_utils
config_utils.autoconfigure()
assert bigchaindb.config['CONFIGURED']
assert bigchaindb.config['database']['host'] == 'test-host'
assert bigchaindb.config['database']['name'] == 'test-dbname'
assert bigchaindb.config['database']['port'] == 4242
assert bigchaindb.config['server']['bind'] == 'localhost:9985'
def test_update_config(monkeypatch):
import bigchaindb
from bigchaindb import config_utils
file_config = {
'database': {'host': 'test-host', 'name': 'bigchaindb', 'port': 28015}
}
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda *args, **kwargs: file_config)
config_utils.autoconfigure(config=file_config)
# update configuration, retaining previous changes
config_utils.update_config({'database': {'port': 28016, 'name': 'bigchaindb_other'}})
assert bigchaindb.config['database']['host'] == 'test-host'
assert bigchaindb.config['database']['name'] == 'bigchaindb_other'
assert bigchaindb.config['database']['port'] == 28016
def test_file_config():
from bigchaindb.config_utils import file_config, CONFIG_DEFAULT_PATH
with patch('builtins.open', mock_open(read_data='{}')) as m:
config = file_config()
m.assert_called_once_with(CONFIG_DEFAULT_PATH)
assert config == {}
def test_invalid_file_config():
from bigchaindb.config_utils import file_config
from bigchaindb.common import exceptions
with patch('builtins.open', mock_open(read_data='{_INVALID_JSON_}')):
with pytest.raises(exceptions.ConfigurationError):
file_config()
def test_write_config():
from bigchaindb.config_utils import write_config, CONFIG_DEFAULT_PATH
m = mock_open()
with patch('builtins.open', m):
write_config({})
m.assert_called_once_with(CONFIG_DEFAULT_PATH, 'w')
handle = m()
handle.write.assert_called_once_with('{}')