1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
21 from paste
.deploy
import loadapp
22 from webtest
import TestApp
24 from mediagoblin
import util
25 from mediagoblin
.config
import read_mediagoblin_config
26 from mediagoblin
.decorators
import _make_safe
27 from mediagoblin
.db
.open import setup_connection_and_db_from_config
30 MEDIAGOBLIN_TEST_DB_NAME
= u
'__mediagoblin_tests__'
31 TEST_SERVER_CONFIG
= pkg_resources
.resource_filename(
32 'mediagoblin.tests', 'test_paste.ini')
33 TEST_APP_CONFIG
= pkg_resources
.resource_filename(
34 'mediagoblin.tests', 'test_mgoblin_app.ini')
35 TEST_USER_DEV
= pkg_resources
.resource_filename(
36 'mediagoblin.tests', 'test_user_dev')
39 USER_DEV_DIRECTORIES_TO_SETUP
= [
40 'media/public', 'media/queue',
41 'beaker/sessions/data', 'beaker/sessions/lock']
43 BAD_CELERY_MESSAGE
= """\
44 Sorry, you *absolutely* must run nosetests with the
45 mediagoblin.celery_setup.from_tests module. Like so:
46 $ CELERY_CONFIG_MODULE=mediagoblin.celery_setup.from_tests ./bin/nosetests"""
49 class BadCeleryEnviron(Exception): pass
52 def suicide_if_bad_celery_environ():
53 if not os
.environ
.get('CELERY_CONFIG_MODULE') == \
54 'mediagoblin.celery_setup.from_tests':
55 raise BadCeleryEnviron(BAD_CELERY_MESSAGE
)
58 def get_test_app(dump_old_app
=True):
59 suicide_if_bad_celery_environ()
61 # Leave this imported as it sets up celery.
62 from mediagoblin
.celery_setup
import from_tests
66 # Just return the old app if that exists and it's okay to set up
68 if MGOBLIN_APP
and not dump_old_app
:
71 # Remove and reinstall user_dev directories
72 if os
.path
.exists(TEST_USER_DEV
):
73 shutil
.rmtree(TEST_USER_DEV
)
75 for directory
in USER_DEV_DIRECTORIES_TO_SETUP
:
76 full_dir
= os
.path
.join(TEST_USER_DEV
, directory
)
80 global_config
, validation_result
= read_mediagoblin_config(TEST_APP_CONFIG
)
81 app_config
= global_config
['mediagoblin']
84 # @@: For now we're dropping collections, but we could also just
85 # collection.remove() ?
86 connection
, db
= setup_connection_and_db_from_config(app_config
)
87 assert db
.name
== MEDIAGOBLIN_TEST_DB_NAME
89 collections_to_wipe
= [
91 for collection
in db
.collection_names()
92 if not collection
.startswith('system.')]
94 for collection
in collections_to_wipe
:
95 db
.drop_collection(collection
)
97 # TODO: Drop and recreate indexes
99 # setup app and return
101 'config:' + TEST_SERVER_CONFIG
)
103 app
= TestApp(test_app
)
109 def setup_fresh_app(func
):
111 Decorator to setup a fresh test application for this function.
113 Cleans out test buckets and passes in a new, fresh test_app.
115 def wrapper(*args
, **kwargs
):
116 test_app
= get_test_app()
117 util
.clear_test_buckets()
118 return func(test_app
, *args
, **kwargs
)
120 return _make_safe(wrapper
, func
)