Skip to content
This repository has been archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
Prevent SMTP/ZODB connections during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiefMaster committed Sep 10, 2014
1 parent a908850 commit 83eef0e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
9 changes: 5 additions & 4 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion indico/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
40 changes: 40 additions & 0 deletions indico/testing/fixtures/disallow.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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)
1 change: 1 addition & 0 deletions indico/testing/fixtures/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions indico/web/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 83eef0e

Please sign in to comment.