Merge branch 'configobj'
[mediagoblin.git] / mediagoblin / tests / tools.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 import pkg_resources
19 import os, shutil
20
21 from paste.deploy import loadapp
22 from webtest import TestApp
23
24 from mediagoblin import util, mg_globals
25 from mediagoblin.config import read_mediagoblin_config
26 from mediagoblin.celery_setup import setup_celery_from_config
27 from mediagoblin.decorators import _make_safe
28 from mediagoblin.db.open import setup_connection_and_db_from_config
29
30
31 MEDIAGOBLIN_TEST_DB_NAME = '__mediagoblinunittests__'
32 TEST_SERVER_CONFIG = pkg_resources.resource_filename(
33 'mediagoblin.tests', 'test_server.ini')
34 TEST_APP_CONFIG = pkg_resources.resource_filename(
35 'mediagoblin.tests', 'test_mgoblin_app.ini')
36 TEST_USER_DEV = pkg_resources.resource_filename(
37 'mediagoblin.tests', 'test_user_dev')
38 MGOBLIN_APP = None
39 CELERY_SETUP = False
40
41 USER_DEV_DIRECTORIES_TO_SETUP = [
42 'media/public', 'media/queue',
43 'beaker/sessions/data', 'beaker/sessions/lock']
44
45
46 class BadCeleryEnviron(Exception): pass
47
48
49 def get_test_app(dump_old_app=True):
50 if os.environ.get('CELERY_CONFIG_MODULE'):
51 raise BadCeleryEnviron(
52 u"Sorry, you *ABSOLUTELY MUST *NOT* run nosetests with the\n"
53 u"CELERY_CONFIG_MODULE set to anything.")
54
55 global MGOBLIN_APP
56 global CELERY_SETUP
57
58 # Just return the old app if that exists and it's okay to set up
59 # and return
60 if MGOBLIN_APP and not dump_old_app:
61 return MGOBLIN_APP
62
63 # Remove and reinstall user_dev directories
64 if os.path.exists(TEST_USER_DEV):
65 shutil.rmtree(TEST_USER_DEV)
66
67 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
68 full_dir = os.path.join(TEST_USER_DEV, directory)
69 os.makedirs(full_dir)
70
71 # Get app config
72 global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG)
73 app_config = global_config['mediagoblin']
74
75 # Wipe database
76 # @@: For now we're dropping collections, but we could also just
77 # collection.remove() ?
78 connection, db = setup_connection_and_db_from_config(app_config)
79
80 collections_to_wipe = [
81 collection
82 for collection in db.collection_names()
83 if not collection.startswith('system.')]
84
85 for collection in collections_to_wipe:
86 db.drop_collection(collection)
87
88 # Don't need these anymore...
89 del(connection)
90 del(db)
91
92 # TODO: Drop and recreate indexes
93
94 # setup app and return
95 test_app = loadapp(
96 'config:' + TEST_SERVER_CONFIG)
97
98 app = TestApp(test_app)
99 MGOBLIN_APP = app
100
101 # setup celery
102 if not CELERY_SETUP:
103 setup_celery_from_config(
104 mg_globals.app_config, mg_globals.global_config,
105 set_environ=True)
106 CELERY_SETUP = True
107
108 return app
109
110
111 def setup_fresh_app(func):
112 """
113 Decorator to setup a fresh test application for this function.
114
115 Cleans out test buckets and passes in a new, fresh test_app.
116 """
117 def wrapper(*args, **kwargs):
118 test_app = get_test_app()
119 util.clear_test_buckets()
120 return func(test_app, *args, **kwargs)
121
122 return _make_safe(wrapper, func)