def get_test_app(dump_old_app=True): suicide_if_bad_celery_environ() # Make sure we've turned on testing testing._activate_testing() # Leave this imported as it sets up celery. from mediagoblin.init.celery import from_tests global MGOBLIN_APP # Just return the old app if that exists and it's okay to set up # and return if MGOBLIN_APP and not dump_old_app: return MGOBLIN_APP Session.rollback() Session.remove() # Remove and reinstall user_dev directories if os.path.exists(TEST_USER_DEV): shutil.rmtree(TEST_USER_DEV) for directory in USER_DEV_DIRECTORIES_TO_SETUP: full_dir = os.path.join(TEST_USER_DEV, directory) os.makedirs(full_dir) # Get app config global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG) app_config = global_config['mediagoblin'] # Run database setup/migrations run_dbupdate(app_config, global_config) # setup app and return test_app = loadapp( 'config:' + TEST_SERVER_CONFIG) # Re-setup celery setup_celery_app(app_config, global_config) # Insert the TestingMeddleware, which can do some # sanity checks on every request/response. # Doing it this way is probably not the cleanest way. # We'll fix it, when we have plugins! mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app)) app = TestApp(test_app) MGOBLIN_APP = app return app
if __name__ == "__main__": mg_dir = os.path.dirname(mediagoblin.__path__[0]) if mg_dir == "": mg_dir = "./" if os.path.exists(mg_dir + "/mediagoblin_local.ini"): config_file = mg_dir + "/mediagoblin_local.ini" elif os.path.exists(mg_dir + "/mediagoblin.ini"): config_file = mg_dir + "/mediagoblin.ini" else: raise Exception("Couldn't find mediagoblin.ini") mg_app = MediaGoblinApp(config_file, setup_celery=True) from mediagoblin.init.celery import setup_celery_app setup_celery_app( mg_globals.app_config, mg_globals.global_config, force_celery_always_eager=True) import sqlalchemy.exc from mediagoblin.db.base import Session from mediagoblin.media_types import FileTypeNotSupported from mediagoblin.media_types import type_match_handler from mediagoblin.submit.lib import run_process_media from mediagoblin.tools.text import convert_to_tag_list_of_dicts from mediagoblin.user_pages.lib import add_media_to_collection from . import ratings import logging mediagoblin.media_types._log.setLevel(logging.DEBUG)
def get_app(request, paste_config=None, mgoblin_config=None): """Create a MediaGoblin app for testing. Args: - request: Not an http request, but a pytest fixture request. We use this to make temporary directories that pytest automatically cleans up as needed. - paste_config: particular paste config used by this application. - mgoblin_config: particular mediagoblin config used by this application. """ paste_config = paste_config or TEST_SERVER_CONFIG mgoblin_config = mgoblin_config or TEST_APP_CONFIG # This is the directory we're copying the paste/mgoblin config stuff into run_dir = request.config._tmpdirhandler.mktemp( 'mgoblin_app', numbered=True) user_dev_dir = run_dir.mkdir('test_user_dev').strpath new_paste_config = run_dir.join('paste.ini').strpath new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath shutil.copyfile(paste_config, new_paste_config) shutil.copyfile(mgoblin_config, new_mgoblin_config) suicide_if_bad_celery_environ() # Make sure we've turned on testing testing._activate_testing() # Leave this imported as it sets up celery. from mediagoblin.init.celery import from_tests Session.rollback() Session.remove() # install user_dev directories for directory in USER_DEV_DIRECTORIES_TO_SETUP: full_dir = os.path.join(user_dev_dir, directory) os.makedirs(full_dir) # Get app config global_config, validation_result = read_mediagoblin_config(new_mgoblin_config) app_config = global_config['mediagoblin'] # Run database setup/migrations run_dbupdate(app_config, global_config) # setup app and return test_app = loadapp( 'config:' + new_paste_config) # Re-setup celery setup_celery_app(app_config, global_config) # Insert the TestingMeddleware, which can do some # sanity checks on every request/response. # Doing it this way is probably not the cleanest way. # We'll fix it, when we have plugins! mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app)) app = TestApp(test_app) return app