diff --git a/bigchaindb/web/server.py b/bigchaindb/web/server.py index 76681b2d..a062e4ba 100644 --- a/bigchaindb/web/server.py +++ b/bigchaindb/web/server.py @@ -2,6 +2,7 @@ from flask import Flask +from bigchaindb import Bigchain from bigchaindb.web import views @@ -14,6 +15,7 @@ def create_app(debug=False): app = Flask(__name__) app.debug = debug + app.config['bigchain'] = Bigchain() app.register_blueprint(views.basic_views, url_prefix='/api/v1') return app diff --git a/bigchaindb/web/views.py b/bigchaindb/web/views.py index ff46e94f..04d3992d 100644 --- a/bigchaindb/web/views.py +++ b/bigchaindb/web/views.py @@ -5,14 +5,22 @@ For more information please refer to the documentation in Apiary: """ import flask -from flask import request, Blueprint +from flask import current_app, request, Blueprint from bigchaindb import util -from bigchaindb import Bigchain basic_views = Blueprint('basic_views', __name__) -b = Bigchain() + + +@basic_views.record +def get_bigchain(state): + bigchain = state.app.config.get('bigchain') + + if bigchain is None: + raise Exception('This blueprint expects you to provide ' + 'database access through `bigchain`') + @basic_views.route('/transactions/') @@ -26,7 +34,9 @@ def get_transaction(tx_id): A JSON string containing the data about the transaction. """ - tx = b.get_transaction(tx_id) + bigchain = current_app.config['bigchain'] + + tx = bigchain.get_transaction(tx_id) return flask.jsonify(**tx) @@ -37,6 +47,7 @@ def create_transaction(): Return: A JSON string containing the data about the transaction. """ + bigchain = current_app.config['bigchain'] val = {} @@ -46,12 +57,12 @@ def create_transaction(): if tx['transaction']['operation'] == 'CREATE': tx = util.transform_create(tx) - tx = util.sign_tx(tx, b.me_private) + tx = util.sign_tx(tx, bigchain.me_private) if not util.verify_signature(tx): val['error'] = 'Invalid transaction signature' - val = b.write_transaction(tx) + val = bigchain.write_transaction(tx) return flask.jsonify(**tx) diff --git a/tests/conftest.py b/tests/conftest.py index 96be00f6..3781b2b6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,13 +7,14 @@ Tasks: """ import os +import copy import pytest DB_NAME = 'bigchain_test_{}'.format(os.getpid()) -config = { +CONFIG = { 'database': { 'name': DB_NAME }, @@ -36,7 +37,7 @@ def restore_config(request, node_config): @pytest.fixture(scope='module') def node_config(): - return config + return copy.deepcopy(CONFIG) @pytest.fixture @@ -50,7 +51,8 @@ def user_public_key(): @pytest.fixture -def b(): +def b(request, node_config): + restore_config(request, node_config) from bigchaindb import Bigchain return Bigchain() diff --git a/tests/test_commands.py b/tests/test_commands.py index 57ebdbe9..6e731c13 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -87,7 +87,7 @@ def test_bigchain_run_start_assume_yes_create_default_config(monkeypatch, mock_p value['return'] = newconfig monkeypatch.setattr(config_utils, 'write_config', mock_write_config) - monkeypatch.setattr(config_utils, 'file_config', lambda x: config_utils.dict_config(value['return'])) + monkeypatch.setattr(config_utils, 'file_config', lambda x: config_utils.dict_config(expected_config)) monkeypatch.setattr('os.path.exists', lambda path: False) args = Namespace(config=None, yes=True) diff --git a/tests/test_core.py b/tests/test_core.py index 6374b8be..9857b37f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,10 +1,8 @@ -import copy import pytest @pytest.fixture -def config(request): - import bigchaindb +def config(request, monkeypatch): config = { 'database': { 'host': 'host', @@ -18,13 +16,10 @@ def config(request): 'keyring': [], 'CONFIGURED': True, } - bigchaindb.config.update(config) - def fin(): - bigchaindb.config = bigchaindb._config - bigchaindb._config = copy.deepcopy(bigchaindb._config) - request.addfinalizer(fin) - return bigchaindb.config + monkeypatch.setattr('bigchaindb.config', config) + + return config def test_bigchain_class_default_initialization(config): diff --git a/tests/utils/test_config_utils.py b/tests/utils/test_config_utils.py index d57753cf..2cf57242 100644 --- a/tests/utils/test_config_utils.py +++ b/tests/utils/test_config_utils.py @@ -10,8 +10,8 @@ ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config) @pytest.fixture(scope='function', autouse=True) -def clean_config(): - bigchaindb.config = copy.deepcopy(ORIGINAL_CONFIG) +def clean_config(monkeypatch): + monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG)) def test_bigchain_instance_is_initialized_when_conf_provided(): diff --git a/tests/web/conftest.py b/tests/web/conftest.py index b3e50341..099f2fd3 100644 --- a/tests/web/conftest.py +++ b/tests/web/conftest.py @@ -4,7 +4,8 @@ from ..db import conftest @pytest.fixture(autouse=True) def restore_config(request, node_config): - conftest.restore_config(request, node_config) + from bigchaindb import config_utils + config_utils.dict_config(node_config) @pytest.fixture(scope='module', autouse=True) @@ -21,9 +22,10 @@ def cleanup_tables(request, node_config): def app(request, node_config): # XXX: For whatever reason this fixture runs before `restore_config`, # so we need to manually call it. - conftest.restore_config(request, node_config) + restore_config(request, node_config) + from bigchaindb.web import server - app = server.create_app() + app = server.create_app(debug=True) return app diff --git a/tests/web/test_basic_views.py b/tests/web/test_basic_views.py index 9bea7782..04a1c292 100644 --- a/tests/web/test_basic_views.py +++ b/tests/web/test_basic_views.py @@ -12,7 +12,6 @@ TX_ENDPOINT = '/api/v1/transactions/' def test_get_transaction_endpoint(b, client, user_public_key): input_tx = b.get_owned_ids(user_public_key).pop() tx = b.get_transaction(input_tx) - res = client.get(TX_ENDPOINT + input_tx) assert tx == res.json