From 83eef0e7b4b99e47ab5bcbb9a7e01c563adef7ad Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Thu, 4 Sep 2014 10:54:33 +0200 Subject: [PATCH] Prevent SMTP/ZODB connections during tests --- conftest.py | 9 ++++--- indico/core/config.py | 5 +++- indico/testing/fixtures/disallow.py | 40 +++++++++++++++++++++++++++++ indico/testing/fixtures/util.py | 1 + indico/web/flask/app.py | 6 +++-- 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 indico/testing/fixtures/disallow.py diff --git a/conftest.py b/conftest.py index be92dbfac3b..d7ad742e538 100644 --- a/conftest.py +++ b/conftest.py @@ -23,20 +23,21 @@ from indico.core.logger import Logger -pytest_plugins = ('indico.testing.fixtures.app', 'indico.testing.fixtures.database', 'indico.testing.fixtures.user', - 'indico.testing.fixtures.util') +pytest_plugins = ('indico.testing.fixtures.app', 'indico.testing.fixtures.database', 'indico.testing.fixtures.disallow', + 'indico.testing.fixtures.user', 'indico.testing.fixtures.util') def pytest_configure(config): config.indico_temp_dir = py.path.local(tempfile.mkdtemp(prefix='indicotesttmp.')) # Throw away all indico.conf options early Config.getInstance().reset({ + 'DBConnectionParams': ('localhost', 0), # invalid port - just so we never connect to a real ZODB! + 'SmtpServer': ('localhost', 0), # invalid port - just in case so we NEVER send emails! 'CacheBackend': 'null', 'Loggers': [], 'UploadedFilesTempDir': config.indico_temp_dir.strpath, 'XMLCacheDir': config.indico_temp_dir.strpath, - 'ArchiveDir': config.indico_temp_dir.strpath, - 'SmtpServer': ('localhost', 99999) # invalid port - just in case so we NEVER send emails! + 'ArchiveDir': config.indico_temp_dir.strpath }) # Make sure we don't write any log files (or worse: send emails) Logger.reset() diff --git a/indico/core/config.py b/indico/core/config.py index 41045fa2fc0..f27d7ab4a73 100644 --- a/indico/core/config.py +++ b/indico/core/config.py @@ -28,7 +28,7 @@ import sys import urlparse -from flask import request +from flask import request, current_app import MaKaC from indico.core.db import DBMgr @@ -788,6 +788,9 @@ def getCssStylesheetName(self): # and therefore the import will fail. import MaKaC.common.info as info + if current_app.config['TESTING']: + return 'Default.css' + with DBMgr.getInstance().global_connection(): defTemplate = info.HelperMaKaCInfo.getMaKaCInfoInstance().getDefaultTemplateSet() diff --git a/indico/testing/fixtures/disallow.py b/indico/testing/fixtures/disallow.py new file mode 100644 index 00000000000..ed939dbf85f --- /dev/null +++ b/indico/testing/fixtures/disallow.py @@ -0,0 +1,40 @@ +## This file is part of Indico. +## Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN). +## +## Indico is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License as +## published by the Free Software Foundation; either version 3 of the +## License, or (at your option) any later version. +## +## Indico is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Indico; if not, see . + +import pytest + + +@pytest.fixture(autouse=True) +def disallow_emails(monkeypatch): + """Prevents any code from connecting to a SMTP server""" + + def _fail(*args, **kwargs): + pytest.fail('Code tried to send an email unexpectedly') + + monkeypatch.setattr('smtplib.SMTP.connect', _fail) + monkeypatch.setattr('logging.handlers.SMTPHandler.emit', _fail) + + +@pytest.fixture(autouse=True) +def disallow_zodb(monkeypatch): + """Prevents any code from connecting to ZODB""" + + @staticmethod + def _fail(*args, **kwargs): + __tracebackhide__ = True + pytest.fail('Code tried to connect to ZODB') + + monkeypatch.setattr('indico.core.db.manager.DBMgr.getInstance', _fail) diff --git a/indico/testing/fixtures/util.py b/indico/testing/fixtures/util.py index 5083909e141..130fd124b7d 100644 --- a/indico/testing/fixtures/util.py +++ b/indico/testing/fixtures/util.py @@ -27,6 +27,7 @@ def monkeypatch_methods(monkeypatch): In case of classmethods the binding will not be changed, i.e. `cls` will keep pointing to the source class and not the target class. """ + def _monkeypatch_methods(target, cls): for name, method in inspect.getmembers(cls, inspect.ismethod): if method.im_self is None: diff --git a/indico/web/flask/app.py b/indico/web/flask/app.py index bd55e47f6e2..19dfe387638 100644 --- a/indico/web/flask/app.py +++ b/indico/web/flask/app.py @@ -242,7 +242,8 @@ def make_app(set_path=False, db_setup=True, testing=False): fix_root_path(app) configure_app(app, set_path) setup_jinja(app) - setup_assets() + with app.app_context(): + setup_assets() if db_setup: configure_db(app) @@ -252,7 +253,8 @@ def make_app(set_path=False, db_setup=True, testing=False): add_blueprints(app) if app.config['INDICO_COMPAT_ROUTES']: add_compat_blueprints(app) - add_plugin_blueprints(app) + if not app.config['TESTING']: + add_plugin_blueprints(app) Logger.init_app(app)