diff options
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r-- | mediagoblin/tests/__init__.py | 12 | ||||
-rw-r--r-- | mediagoblin/tests/fake_carrot_conf_bad.ini | 14 | ||||
-rw-r--r-- | mediagoblin/tests/fake_carrot_conf_empty.ini | 0 | ||||
-rw-r--r-- | mediagoblin/tests/fake_carrot_conf_good.ini | 13 | ||||
-rw-r--r-- | mediagoblin/tests/fake_celery_conf.ini | 9 | ||||
-rw-r--r-- | mediagoblin/tests/fake_celery_conf_mgdb.ini | 14 | ||||
-rw-r--r-- | mediagoblin/tests/fake_config_spec.ini | 10 | ||||
-rw-r--r-- | mediagoblin/tests/test_auth.py | 58 | ||||
-rw-r--r-- | mediagoblin/tests/test_celery_setup.py | 34 | ||||
-rw-r--r-- | mediagoblin/tests/test_config.py | 97 | ||||
-rw-r--r-- | mediagoblin/tests/test_globals.py | 41 | ||||
-rw-r--r-- | mediagoblin/tests/test_mgoblin_app.ini | 12 | ||||
-rw-r--r-- | mediagoblin/tests/test_server.ini (renamed from mediagoblin/tests/mgoblin_test_app.ini) | 11 | ||||
-rw-r--r-- | mediagoblin/tests/test_storage.py | 6 | ||||
-rw-r--r-- | mediagoblin/tests/test_tests.py | 10 | ||||
-rw-r--r-- | mediagoblin/tests/test_util.py | 19 | ||||
-rw-r--r-- | mediagoblin/tests/test_workbench.py | 50 | ||||
-rw-r--r-- | mediagoblin/tests/tools.py | 45 |
18 files changed, 352 insertions, 103 deletions
diff --git a/mediagoblin/tests/__init__.py b/mediagoblin/tests/__init__.py index c129cbf8..1f1e23e9 100644 --- a/mediagoblin/tests/__init__.py +++ b/mediagoblin/tests/__init__.py @@ -13,3 +13,15 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + +from mediagoblin import mg_globals + + +def setup_package(): + pass + +def teardown_package(): + if mg_globals.db_connection: + print "Killing db ..." + mg_globals.db_connection.drop_database(mg_globals.database.name) + print "... done" diff --git a/mediagoblin/tests/fake_carrot_conf_bad.ini b/mediagoblin/tests/fake_carrot_conf_bad.ini new file mode 100644 index 00000000..0c79b354 --- /dev/null +++ b/mediagoblin/tests/fake_carrot_conf_bad.ini @@ -0,0 +1,14 @@ +[carrotapp] +# Whether or not our carrots are going to be turned into cake. +## These should throw errors +carrotcake = slobber +num_carrots = GROSS + +# A message encouraging our users to eat their carrots. +encouragement_phrase = 586956856856 # shouldn't throw error + +# Something extra! +blah_blah = "blah!" # shouldn't throw error either + +[celery] +eat_celery_with_carrots = pants # yeah that's def an error right there. diff --git a/mediagoblin/tests/fake_carrot_conf_empty.ini b/mediagoblin/tests/fake_carrot_conf_empty.ini new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/mediagoblin/tests/fake_carrot_conf_empty.ini diff --git a/mediagoblin/tests/fake_carrot_conf_good.ini b/mediagoblin/tests/fake_carrot_conf_good.ini new file mode 100644 index 00000000..fed14d07 --- /dev/null +++ b/mediagoblin/tests/fake_carrot_conf_good.ini @@ -0,0 +1,13 @@ +[carrotapp] +# Whether or not our carrots are going to be turned into cake. +carrotcake = true +num_carrots = 88 + +# A message encouraging our users to eat their carrots. +encouragement_phrase = "I'd love it if you eat your carrots!" + +# Something extra! +blah_blah = "blah!" + +[celery] +eat_celery_with_carrots = False diff --git a/mediagoblin/tests/fake_celery_conf.ini b/mediagoblin/tests/fake_celery_conf.ini new file mode 100644 index 00000000..3e52ac3a --- /dev/null +++ b/mediagoblin/tests/fake_celery_conf.ini @@ -0,0 +1,9 @@ +['mediagoblin'] +# I got nothin' in this file! + +['celery'] +some_variable = floop +mail_port = 2000 +celeryd_eta_scheduler_precision = 1.3 +celery_result_persistent = true +celery_imports = foo.bar.baz, this.is.an.import diff --git a/mediagoblin/tests/fake_celery_conf_mgdb.ini b/mediagoblin/tests/fake_celery_conf_mgdb.ini new file mode 100644 index 00000000..52671c14 --- /dev/null +++ b/mediagoblin/tests/fake_celery_conf_mgdb.ini @@ -0,0 +1,14 @@ +['mediagoblin'] +db_host = mongodb.example.org +db_port = 8080 +db_name = captain_lollerskates + +['something'] +or = other + +['celery'] +some_variable = poolf +mail_port = 2020 +celeryd_eta_scheduler_precision = 3.1 +celery_result_persistent = false +celery_imports = baz.bar.foo, import.is.a.this diff --git a/mediagoblin/tests/fake_config_spec.ini b/mediagoblin/tests/fake_config_spec.ini new file mode 100644 index 00000000..9421ce36 --- /dev/null +++ b/mediagoblin/tests/fake_config_spec.ini @@ -0,0 +1,10 @@ +[carrotapp] +# Whether or not our carrots are going to be turned into cake. +carrotcake = boolean(default=False) +num_carrots = integer(default=1) + +# A message encouraging our users to eat their carrots. +encouragement_phrase = string() + +[celery] +eat_celery_with_carrots = boolean(default=True)
\ No newline at end of file diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index cdfeccab..b8389f8d 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -20,7 +20,7 @@ from nose.tools import assert_equal from mediagoblin.auth import lib as auth_lib from mediagoblin.tests.tools import setup_fresh_app -from mediagoblin import globals as mgoblin_globals +from mediagoblin import mg_globals from mediagoblin import util @@ -77,7 +77,7 @@ def test_register_views(test_app): # Make sure it rendered with the appropriate template assert util.TEMPLATE_TEST_CONTEXT.has_key( 'mediagoblin/auth/register.html') - + # Try to register without providing anything, should error # -------------------------------------------------------- @@ -137,7 +137,7 @@ def test_register_views(test_app): u'Passwords must match.'] ## At this point there should be no users in the database ;) - assert not mgoblin_globals.database.User.find().count() + assert not mg_globals.database.User.find().count() # Successful register # ------------------- @@ -158,7 +158,7 @@ def test_register_views(test_app): 'mediagoblin/auth/register_success.html') ## Make sure user is in place - new_user = mgoblin_globals.database.User.find_one( + new_user = mg_globals.database.User.find_one( {'username': 'happygirl'}) assert new_user assert new_user['status'] == u'needs_email_verification' @@ -182,7 +182,7 @@ def test_register_views(test_app): unicode(new_user['_id'])] assert parsed_get_params['token'] == [ new_user['verification_key']] - + ## Try verifying with bs verification key, shouldn't work util.clear_test_template_context() test_app.get( @@ -191,7 +191,7 @@ def test_register_views(test_app): context = util.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/auth/verify_email.html'] assert context['verification_successful'] == False - new_user = mgoblin_globals.database.User.find_one( + new_user = mg_globals.database.User.find_one( {'username': 'happygirl'}) assert new_user assert new_user['status'] == u'needs_email_verification' @@ -203,14 +203,12 @@ def test_register_views(test_app): context = util.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/auth/verify_email.html'] assert context['verification_successful'] == True - new_user = mgoblin_globals.database.User.find_one( + new_user = mg_globals.database.User.find_one( {'username': 'happygirl'}) assert new_user assert new_user['status'] == u'active' assert new_user['email_verified'] == True - ## TODO: Try logging in - # Uniqueness checks # ----------------- ## We shouldn't be able to register with that user twice @@ -221,7 +219,7 @@ def test_register_views(test_app): 'password': 'iamsohappy2', 'confirm_password': 'iamsohappy2', 'email': 'happygrrl2@example.org'}) - + context = util.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/auth/register.html'] form = context['register_form'] @@ -229,3 +227,43 @@ def test_register_views(test_app): u'Sorry, a user with that name already exists.'] ## TODO: Also check for double instances of an email address? + + +@setup_fresh_app +def test_authentication_views(test_app): + """ + Test logging in and logging out + """ + # Make a new user + test_user = mg_globals.database.User() + test_user['username'] = u'chris' + test_user['email'] = u'chris@example.com' + test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') + test_user.save() + + # Get login + test_app.get('/auth/login/') + # Make sure it rendered with the appropriate template + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/auth/login.html') + + # Log in as that user + util.clear_test_template_context() + response = test_app.post( + '/auth/login/', { + 'username': u'chris', + 'password': 'toast'}) + response.follow() + assert_equal( + urlparse.urlsplit(response.location)[2], + '/') + assert util.TEMPLATE_TEST_CONTEXT.has_key( + 'mediagoblin/root.html') + + # Make sure we're in the session or something + session = util.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']['request'].session + assert session['user_id'] == unicode(test_user['_id']) + + # Log out as that user + # Make sure we're not in the session + diff --git a/mediagoblin/tests/test_celery_setup.py b/mediagoblin/tests/test_celery_setup.py index 558eb458..8bf97ae4 100644 --- a/mediagoblin/tests/test_celery_setup.py +++ b/mediagoblin/tests/test_celery_setup.py @@ -17,6 +17,13 @@ import pkg_resources from mediagoblin import celery_setup +from mediagoblin.config import read_mediagoblin_config + + +TEST_CELERY_CONF_NOSPECIALDB = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_celery_conf.ini') +TEST_CELERY_CONF_MGSPECIALDB = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_celery_conf_mgdb.ini') def test_setup_celery_from_config(): @@ -27,14 +34,12 @@ def test_setup_celery_from_config(): for var in vars_to_wipe: delattr(module, var) + global_config, validation_result = read_mediagoblin_config( + TEST_CELERY_CONF_NOSPECIALDB) + app_config = global_config['mediagoblin'] + celery_setup.setup_celery_from_config( - {}, - {'something': {'or': 'other'}, - 'celery': {'some_variable': 'floop', - 'mail_port': '2000', - 'CELERYD_ETA_SCHEDULER_PRECISION': '1.3', - 'celery_result_persistent': 'true', - 'celery_imports': 'foo.bar.baz this.is.an.import'}}, + app_config, global_config, 'mediagoblin.tests.fake_celery_module', set_environ=False) from mediagoblin.tests import fake_celery_module @@ -53,17 +58,12 @@ def test_setup_celery_from_config(): _wipe_testmodule_clean(fake_celery_module) + global_config, validation_result = read_mediagoblin_config( + TEST_CELERY_CONF_MGSPECIALDB) + app_config = global_config['mediagoblin'] + celery_setup.setup_celery_from_config( - {'db_host': 'mongodb.example.org', - 'db_port': '8080', - 'db_name': 'captain_lollerskates', - 'celery_section': 'vegetable'}, - {'something': {'or': 'other'}, - 'vegetable': {'some_variable': 'poolf', - 'mail_port': '2020', - 'CELERYD_ETA_SCHEDULER_PRECISION': '3.1', - 'celery_result_persistent': 'false', - 'celery_imports': 'baz.bar.foo import.is.a.this'}}, + app_config, global_config, 'mediagoblin.tests.fake_celery_module', set_environ=False) from mediagoblin.tests import fake_celery_module diff --git a/mediagoblin/tests/test_config.py b/mediagoblin/tests/test_config.py new file mode 100644 index 00000000..244f05e5 --- /dev/null +++ b/mediagoblin/tests/test_config.py @@ -0,0 +1,97 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 Free Software Foundation, Inc +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import pkg_resources + +from mediagoblin import config + + +CARROT_CONF_GOOD = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_carrot_conf_good.ini') +CARROT_CONF_EMPTY = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_carrot_conf_empty.ini') +CARROT_CONF_BAD = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_carrot_conf_bad.ini') +FAKE_CONFIG_SPEC = pkg_resources.resource_filename( + 'mediagoblin.tests', 'fake_config_spec.ini') + + +def test_read_mediagoblin_config(): + # An empty file + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC) + + assert this_conf['carrotapp']['carrotcake'] == False + assert this_conf['carrotapp']['num_carrots'] == 1 + assert not this_conf['carrotapp'].has_key('encouragement_phrase') + assert this_conf['celery']['eat_celery_with_carrots'] == True + + # A good file + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_GOOD, FAKE_CONFIG_SPEC) + + assert this_conf['carrotapp']['carrotcake'] == True + assert this_conf['carrotapp']['num_carrots'] == 88 + assert this_conf['carrotapp']['encouragement_phrase'] == \ + "I'd love it if you eat your carrots!" + assert this_conf['carrotapp']['blah_blah'] == "blah!" + assert this_conf['celery']['eat_celery_with_carrots'] == False + + # A bad file + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_BAD, FAKE_CONFIG_SPEC) + + # These should still open but will have errors that we'll test for + # in test_generate_validation_report() + assert this_conf['carrotapp']['carrotcake'] == 'slobber' + assert this_conf['carrotapp']['num_carrots'] == 'GROSS' + assert this_conf['carrotapp']['encouragement_phrase'] == \ + "586956856856" + assert this_conf['carrotapp']['blah_blah'] == "blah!" + assert this_conf['celery']['eat_celery_with_carrots'] == "pants" + + +def test_generate_validation_report(): + # Empty + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC) + report = config.generate_validation_report(this_conf, validation_results) + assert report is None + + # Good + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_GOOD, FAKE_CONFIG_SPEC) + report = config.generate_validation_report(this_conf, validation_results) + assert report is None + + # Bad + this_conf, validation_results = config.read_mediagoblin_config( + CARROT_CONF_BAD, FAKE_CONFIG_SPEC) + report = config.generate_validation_report(this_conf, validation_results) + + assert report.startswith("""\ +There were validation problems loading this config file: +--------------------------------------------------------""") + + expected_warnings = [ + 'carrotapp:carrotcake = the value "slobber" is of the wrong type.', + 'carrotapp:num_carrots = the value "GROSS" is of the wrong type.', + 'celery:eat_celery_with_carrots = the value "pants" is of the wrong type.'] + warnings = report.splitlines()[2:] + + assert len(warnings) == 3 + for warning in expected_warnings: + assert warning in warnings diff --git a/mediagoblin/tests/test_globals.py b/mediagoblin/tests/test_globals.py index b285cdf5..63578d62 100644 --- a/mediagoblin/tests/test_globals.py +++ b/mediagoblin/tests/test_globals.py @@ -16,21 +16,30 @@ from nose.tools import assert_raises -from mediagoblin import globals as mg_globals +from mediagoblin import mg_globals -def test_setup_globals(): - mg_globals.setup_globals( - db_connection='my favorite db_connection!', - database='my favorite database!', - public_store='my favorite public_store!', - queue_store='my favorite queue_store!') - - assert mg_globals.db_connection == 'my favorite db_connection!' - assert mg_globals.database == 'my favorite database!' - assert mg_globals.public_store == 'my favorite public_store!' - assert mg_globals.queue_store == 'my favorite queue_store!' +class TestGlobals(object): + def setUp(self): + self.old_connection = mg_globals.db_connection + self.old_database = mg_globals.database - assert_raises( - AssertionError, - mg_globals.setup_globals, - no_such_global_foo = "Dummy") + def tearDown(self): + mg_globals.db_connection = self.old_connection + mg_globals.database = self.old_database + + def test_setup_globals(self): + mg_globals.setup_globals( + db_connection='my favorite db_connection!', + database='my favorite database!', + public_store='my favorite public_store!', + queue_store='my favorite queue_store!') + + assert mg_globals.db_connection == 'my favorite db_connection!' + assert mg_globals.database == 'my favorite database!' + assert mg_globals.public_store == 'my favorite public_store!' + assert mg_globals.queue_store == 'my favorite queue_store!' + + assert_raises( + AssertionError, + mg_globals.setup_globals, + no_such_global_foo = "Dummy") diff --git a/mediagoblin/tests/test_mgoblin_app.ini b/mediagoblin/tests/test_mgoblin_app.ini new file mode 100644 index 00000000..94eafb5a --- /dev/null +++ b/mediagoblin/tests/test_mgoblin_app.ini @@ -0,0 +1,12 @@ +[mediagoblin] +queuestore_base_dir = %(here)s/test_user_dev/media/queue +publicstore_base_dir = %(here)s/test_user_dev/media/public +publicstore_base_url = /mgoblin_media/ +direct_remote_path = /mgoblin_static/ +email_sender_address = "notice@mediagoblin.example.org" +email_debug_mode = true +db_name = __mediagoblin_tests__ + +# Celery shouldn't be set up by the paste app factory as it's set up +# elsewhere +celery_setup_elsewhere = true diff --git a/mediagoblin/tests/mgoblin_test_app.ini b/mediagoblin/tests/test_server.ini index abed2615..929a1ccf 100644 --- a/mediagoblin/tests/mgoblin_test_app.ini +++ b/mediagoblin/tests/test_server.ini @@ -10,16 +10,7 @@ use = egg:Paste#urlmap [app:mediagoblin] use = egg:mediagoblin#app filter-with = beaker -queuestore_base_dir = %(here)s/test_user_dev/media/queue -publicstore_base_dir = %(here)s/test_user_dev/media/public -publicstore_base_url = /mgoblin_media/ -direct_remote_path = /mgoblin_static/ -email_sender_address = "notice@mediagoblin.example.org" -email_debug_mode = true -db_name = __mediagoblin_tests__ -# Celery shouldn't be set up by the paste app factory as it's set up -# elsewhere -celery_setup_elsewhere = true +config = %(here)s/test_mgoblin_app.ini [app:publicstore_serve] use = egg:Paste#static diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 55b66e84..1800c29d 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -58,8 +58,8 @@ class FakeRemoteStorage(storage.BasicFileStorage): local_storage = False -def test_storage_system_from_paste_config(): - this_storage = storage.storage_system_from_paste_config( +def test_storage_system_from_config(): + this_storage = storage.storage_system_from_config( {'somestorage_base_url': 'http://example.org/moodia/', 'somestorage_base_dir': '/tmp/', 'somestorage_garbage_arg': 'garbage_arg', @@ -69,7 +69,7 @@ def test_storage_system_from_paste_config(): assert this_storage.base_dir == '/tmp/' assert this_storage.__class__ is storage.BasicFileStorage - this_storage = storage.storage_system_from_paste_config( + this_storage = storage.storage_system_from_config( {'somestorage_foobie': 'eiboof', 'somestorage_blech': 'hcelb', 'somestorage_garbage_arg': 'garbage_arg', diff --git a/mediagoblin/tests/test_tests.py b/mediagoblin/tests/test_tests.py index 3ecbfac7..8ac7f0a4 100644 --- a/mediagoblin/tests/test_tests.py +++ b/mediagoblin/tests/test_tests.py @@ -16,7 +16,7 @@ from mediagoblin.tests.tools import get_test_app -from mediagoblin import globals as mgoblin_globals +from mediagoblin import mg_globals def test_get_test_app_wipes_db(): @@ -24,15 +24,15 @@ def test_get_test_app_wipes_db(): Make sure we get a fresh database on every wipe :) """ get_test_app() - assert mgoblin_globals.database.User.find().count() == 0 + assert mg_globals.database.User.find().count() == 0 - new_user = mgoblin_globals.database.User() + new_user = mg_globals.database.User() new_user['username'] = u'lolcat' new_user['email'] = u'lol@cats.example.org' new_user['pw_hash'] = u'pretend_this_is_a_hash' new_user.save() - assert mgoblin_globals.database.User.find().count() == 1 + assert mg_globals.database.User.find().count() == 1 get_test_app() - assert mgoblin_globals.database.User.find().count() == 0 + assert mg_globals.database.User.find().count() == 0 diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 7b00a074..75e28aca 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -103,3 +103,22 @@ def test_locale_to_lower_lower(): # crazy renditions. Useful? assert util.locale_to_lower_lower('en-US') == 'en-us' assert util.locale_to_lower_lower('en_us') == 'en-us' + + +def test_html_cleaner(): + # Remove images + result = util.clean_html( + '<p>Hi everybody! ' + '<img src="http://example.org/huge-purple-barney.png" /></p>\n' + '<p>:)</p>') + assert result == ( + '<div>' + '<p>Hi everybody! </p>\n' + '<p>:)</p>' + '</div>') + + # Remove evil javascript + result = util.clean_html( + '<p><a href="javascript:nasty_surprise">innocent link!</a></p>') + assert result == ( + '<p><a href="">innocent link!</a></p>') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index 89f2ef33..953835a1 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -30,29 +30,29 @@ class TestWorkbench(object): def test_create_workbench(self): workbench = self.workbench_manager.create_workbench() - assert os.path.isdir(workbench) - assert workbench.startswith(self.workbench_manager.base_workbench_dir) + assert os.path.isdir(workbench.dir) + assert workbench.dir.startswith(self.workbench_manager.base_workbench_dir) + + def test_joinpath(self): + this_workbench = self.workbench_manager.create_workbench() + tmpname = this_workbench.joinpath('temp.txt') + assert tmpname == os.path.join(this_workbench.dir, 'temp.txt') + this_workbench.destroy_self() def test_destroy_workbench(self): # kill a workbench this_workbench = self.workbench_manager.create_workbench() - tmpfile = file(os.path.join(this_workbench, 'temp.txt'), 'w') + tmpfile_name = this_workbench.joinpath('temp.txt') + tmpfile = file(tmpfile_name, 'w') with tmpfile: tmpfile.write('lollerskates') - assert os.path.exists(os.path.join(this_workbench, 'temp.txt')) - - self.workbench_manager.destroy_workbench(this_workbench) - assert not os.path.exists(os.path.join(this_workbench, 'temp.txt')) - assert not os.path.exists(this_workbench) - - # make sure we can't kill other stuff though - dont_kill_this = tempfile.mkdtemp() + assert os.path.exists(tmpfile_name) - assert_raises( - workbench.WorkbenchOutsideScope, - self.workbench_manager.destroy_workbench, - dont_kill_this) + wb_dir = this_workbench.dir + this_workbench.destroy_self() + assert not os.path.exists(tmpfile_name) + assert not os.path.exists(wb_dir) def test_localized_file(self): tmpdir, this_storage = get_tmp_filestorage() @@ -65,8 +65,7 @@ class TestWorkbench(object): our_file.write('Our file') # with a local file storage - filename = self.workbench_manager.localized_file( - this_workbench, this_storage, filepath) + filename = this_workbench.localized_file(this_storage, filepath) assert filename == os.path.join( tmpdir, 'dir1/dir2/ourfile.txt') @@ -77,20 +76,19 @@ class TestWorkbench(object): with this_storage.get_file(filepath, 'w') as our_file: our_file.write('Our file') - filename = self.workbench_manager.localized_file( - this_workbench, this_storage, filepath) + filename = this_workbench.localized_file(this_storage, filepath) assert filename == os.path.join( - this_workbench, 'ourfile.txt') + this_workbench.dir, 'ourfile.txt') # fake remote file storage, filename_if_copying set - filename = self.workbench_manager.localized_file( - this_workbench, this_storage, filepath, 'thisfile') + filename = this_workbench.localized_file( + this_storage, filepath, 'thisfile') assert filename == os.path.join( - this_workbench, 'thisfile.txt') + this_workbench.dir, 'thisfile.txt') # fake remote file storage, filename_if_copying set, # keep_extension_if_copying set to false - filename = self.workbench_manager.localized_file( - this_workbench, this_storage, filepath, 'thisfile.text', False) + filename = this_workbench.localized_file( + this_storage, filepath, 'thisfile.text', False) assert filename == os.path.join( - this_workbench, 'thisfile.text') + this_workbench.dir, 'thisfile.text') diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py index 342b54b7..b1fe56a0 100644 --- a/mediagoblin/tests/tools.py +++ b/mediagoblin/tests/tools.py @@ -18,20 +18,25 @@ import pkg_resources import os, shutil -from paste.deploy import appconfig, loadapp +from paste.deploy import loadapp from webtest import TestApp -from mediagoblin import util +from mediagoblin import util, mg_globals +from mediagoblin.config import read_mediagoblin_config +from mediagoblin.celery_setup import setup_celery_from_config from mediagoblin.decorators import _make_safe from mediagoblin.db.open import setup_connection_and_db_from_config MEDIAGOBLIN_TEST_DB_NAME = '__mediagoblinunittests__' +TEST_SERVER_CONFIG = pkg_resources.resource_filename( + 'mediagoblin.tests', 'test_server.ini') TEST_APP_CONFIG = pkg_resources.resource_filename( - 'mediagoblin.tests', 'mgoblin_test_app.ini') + 'mediagoblin.tests', 'test_mgoblin_app.ini') TEST_USER_DEV = pkg_resources.resource_filename( 'mediagoblin.tests', 'test_user_dev') MGOBLIN_APP = None +CELERY_SETUP = False USER_DEV_DIRECTORIES_TO_SETUP = [ 'media/public', 'media/queue', @@ -42,12 +47,13 @@ class BadCeleryEnviron(Exception): pass def get_test_app(dump_old_app=True): - if not os.environ.get('CELERY_CONFIG_MODULE') == \ - 'mediagoblin.celery_setup.from_tests': + if os.environ.get('CELERY_CONFIG_MODULE'): raise BadCeleryEnviron( - u"Sorry, you *absolutely* must run nosetests with the\n" - u"mediagoblin.celery_setup.from_tests module. Like so:\n" - u"$ CELERY_CONFIG_MODULE=mediagoblin.celery_setup.from_tests ./bin/nosetests") + u"Sorry, you *ABSOLUTELY MUST *NOT* run nosetests with the\n" + u"CELERY_CONFIG_MODULE set to anything.") + + global MGOBLIN_APP + global CELERY_SETUP # Just return the old app if that exists and it's okay to set up # and return @@ -63,16 +69,13 @@ def get_test_app(dump_old_app=True): os.makedirs(full_dir) # Get app config - config = appconfig( - 'config:' + os.path.basename(TEST_APP_CONFIG), - relative_to=os.path.dirname(TEST_APP_CONFIG), - name='mediagoblin') + global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG) + app_config = global_config['mediagoblin'] # Wipe database # @@: For now we're dropping collections, but we could also just # collection.remove() ? - connection, db = setup_connection_and_db_from_config( - config.local_conf) + connection, db = setup_connection_and_db_from_config(app_config) collections_to_wipe = [ collection @@ -90,9 +93,19 @@ def get_test_app(dump_old_app=True): # setup app and return test_app = loadapp( - 'config:' + TEST_APP_CONFIG) + 'config:' + TEST_SERVER_CONFIG) + + app = TestApp(test_app) + MGOBLIN_APP = app + + # setup celery + if not CELERY_SETUP: + setup_celery_from_config( + mg_globals.app_config, mg_globals.global_config, + set_environ=True) + CELERY_SETUP = True - return TestApp(test_app) + return app def setup_fresh_app(func): |