diff --git a/ckanext/example_idatasetform/tests/test_controllers.py b/ckanext/example_idatasetform/tests/test_controllers.py index dfeda2163f6..16bf9694c2f 100644 --- a/ckanext/example_idatasetform/tests/test_controllers.py +++ b/ckanext/example_idatasetform/tests/test_controllers.py @@ -1,5 +1,5 @@ from nose.tools import assert_equal -from routes import url_for +from ckan.lib.helpers import url_for import ckan.plugins as plugins import ckan.tests.helpers as helpers @@ -12,8 +12,10 @@ def _get_package_edit_page(app, package_name): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='edit', id=package_name) response = app.get( - url=url_for(controller='package', action='edit', id=package_name), + url, extra_environ=env, ) return env, response diff --git a/ckanext/example_igroupform/tests/test_controllers.py b/ckanext/example_igroupform/tests/test_controllers.py index 1a930348fd6..5b3a15d9d6f 100644 --- a/ckanext/example_igroupform/tests/test_controllers.py +++ b/ckanext/example_igroupform/tests/test_controllers.py @@ -1,5 +1,5 @@ from nose.tools import assert_equal -from routes import url_for +from ckan.lib.helpers import url_for import ckan.plugins as plugins import ckan.tests.helpers as helpers @@ -17,8 +17,10 @@ def _get_group_new_page(app, group_type): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for('%s_new' % group_type) response = app.get( - url_for('%s_new' % group_type), + url, extra_environ=env, ) return env, response @@ -41,7 +43,9 @@ def test_about(self): group = factories.Group(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_about' % custom_group_type, + + with app.flask_app.test_request_context(): + url = url_for('%s_about' % custom_group_type, id=group_name) response = app.get(url=url, extra_environ=env) response.mustcontain(group_name) @@ -52,8 +56,10 @@ def test_bulk_process(self): group = factories.Group(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_bulk_process' % custom_group_type, - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for('%s_bulk_process' % custom_group_type, + id=group_name) try: response = app.get(url=url, extra_environ=env) except Exception as e: @@ -67,8 +73,10 @@ def test_delete(self): group = factories.Group(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_action' % custom_group_type, action='delete', - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for('%s_action' % custom_group_type, action='delete', + id=group_name) response = app.get(url=url, extra_environ=env) @@ -89,8 +97,10 @@ def test_about(self): group = factories.Organization(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_about' % custom_group_type, - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for('%s_about' % custom_group_type, + id=group_name) response = app.get(url=url, extra_environ=env) response.mustcontain(group_name) @@ -100,8 +110,10 @@ def test_bulk_process(self): group = factories.Organization(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_bulk_process' % custom_group_type, - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for('%s_bulk_process' % custom_group_type, + id=group_name) response = app.get(url=url, extra_environ=env) def test_delete(self): @@ -110,8 +122,10 @@ def test_delete(self): group = factories.Organization(user=user, type=custom_group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_action' % custom_group_type, action='delete', - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for('%s_action' % custom_group_type, action='delete', + id=group_name) response = app.get(url=url, extra_environ=env) @@ -193,8 +207,9 @@ def _get_group_edit_page(app, group_type, group_name=None): group = factories.Group(user=user, type=group_type) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_edit' % group_type, - id=group_name) + with app.flask_app.test_request_context(): + url = url_for('%s_edit' % group_type, + id=group_name) response = app.get(url=url, extra_environ=env) return env, response, group_name @@ -214,8 +229,10 @@ def test_group_doesnt_exist(self): app = self._get_test_app() user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_edit' % custom_group_type, - id='doesnt_exist') + + with app.flask_app.test_request_context(): + url = url_for('%s_edit' % custom_group_type, + id='doesnt_exist') app.get(url=url, extra_environ=env, status=404) @@ -255,8 +272,10 @@ def test_group_doesnt_exist(self): app = self._get_test_app() user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for('%s_edit' % group_type, - id='doesnt_exist') + + with app.flask_app.test_request_context(): + url = url_for('%s_edit' % group_type, + id='doesnt_exist') app.get(url=url, extra_environ=env, status=404) diff --git a/ckanext/example_itranslation/tests/test_plugin.py b/ckanext/example_itranslation/tests/test_plugin.py index 9e25b6f8c2c..29089c6a0ca 100644 --- a/ckanext/example_itranslation/tests/test_plugin.py +++ b/ckanext/example_itranslation/tests/test_plugin.py @@ -17,48 +17,60 @@ def teardown_class(cls): def test_translated_string_in_extensions_templates(self): app = self._get_test_app() - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index', - locale='fr'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index', + locale='fr') + + response = app.get(url) assert_true('This is a itranslated string' in response.body) assert_false('This is an untranslated string' in response.body) # double check the untranslated strings - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index') + + response = app.get(url) assert_true('This is an untranslated string' in response.body) assert_false('This is a itranslated string' in response.body) def test_translated_string_in_core_templates(self): app = self._get_test_app() - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index', - locale='fr'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index', + locale='fr') + + response = app.get(url) assert_true('Overwritten string in ckan.mo' in response.body) assert_false('Connexion' in response.body) # double check the untranslated strings - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index') + + response = app.get(url) assert_true('Log in' in response.body) assert_false('Overwritten string in ckan.mo' in response.body) # check that we have only overwritten 'fr' - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index', - locale='de'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index', + locale='de') + + response = app.get(url) assert_true('Einloggen' in response.body) assert_false('Overwritten string in ckan.mo' in response.body) def test_english_translation_replaces_default_english_string(self): app = self._get_test_app() - response = app.get( - url=plugins.toolkit.url_for(controller='home', action='index'), - ) + + with app.flask_app.test_request_context(): + url = plugins.toolkit.url_for(controller='home', action='index') + + response = app.get(url) assert_true('Replaced' in response.body) assert_false('Register' in response.body) diff --git a/ckanext/imageview/tests/test_view.py b/ckanext/imageview/tests/test_view.py index f105aea611b..201b4f4d481 100644 --- a/ckanext/imageview/tests/test_view.py +++ b/ckanext/imageview/tests/test_view.py @@ -1,4 +1,4 @@ -from routes import url_for +from ckan.lib.helpers import url_for import ckan.plugins as p @@ -37,8 +37,9 @@ def test_view_shown_on_resource_page_with_image_url(self): resource_id=resource['id'], image_url='http://some.image.png') - url = url_for(controller='package', action='resource_read', - id=dataset['name'], resource_id=resource['id']) + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_read', + id=dataset['name'], resource_id=resource['id']) response = app.get(url) diff --git a/ckanext/multilingual/tests/test_multilingual_plugin.py b/ckanext/multilingual/tests/test_multilingual_plugin.py index 4bdf4875305..dbccae522a3 100644 --- a/ckanext/multilingual/tests/test_multilingual_plugin.py +++ b/ckanext/multilingual/tests/test_multilingual_plugin.py @@ -1,12 +1,13 @@ import ckan.plugins as p import ckanext.multilingual.plugin as mulilingual_plugin -import ckan.lib.helpers + +from ckan.lib.helpers import url_for import ckan.lib.create_test_data import ckan.logic.action.update import ckan.model as model import ckan.tests.legacy import ckan.tests.legacy.html_check -import routes +from ckan.tests import helpers import paste.fixture import pylons.test @@ -16,8 +17,8 @@ class TestDatasetTermTranslation(ckan.tests.legacy.html_check.HtmlCheckMethods): 'Test the translation of datasets by the multilingual_dataset plugin.' @classmethod - def setup(cls): - cls.app = paste.fixture.TestApp(pylons.test.pylonsapp) + def setup_class(cls): + cls.app = helpers._get_test_app() if not p.plugin_loaded('multilingual_dataset'): p.load('multilingual_dataset') @@ -59,7 +60,7 @@ def setup(cls): context, data_dict) @classmethod - def teardown(cls): + def teardown_class(cls): p.unload('multilingual_dataset') p.unload('multilingual_group') p.unload('multilingual_tag') @@ -74,29 +75,30 @@ def test_user_read_translation(self): # It is testsysadmin who created the dataset, so testsysadmin whom # we'd expect to see the datasets for. - for user_name in ('testsysadmin',): - offset = routes.url_for( - controller='user', action='read', id=user_name) - for (lang_code, translations) in ( - ('de', _create_test_data.german_translations), - ('fr', _create_test_data.french_translations), - ('en', _create_test_data.english_translations), - ('pl', {})): - response = self.app.get( - offset, - status=200, - extra_environ={'CKAN_LANG': lang_code, - 'CKAN_CURRENT_URL': offset}) - terms = ('A Novel By Tolstoy') - for term in terms: - if term in translations: - assert translations[term] in response, response - elif term in _create_test_data.english_translations: - assert (_create_test_data.english_translations[term] - in response) - else: - assert term in response - assert 'this should not be rendered' not in response + with self.app.flask_app.test_request_context(): + for user_name in ('testsysadmin',): + offset = url_for( + controller='user', action='read', id=user_name) + for (lang_code, translations) in ( + ('de', _create_test_data.german_translations), + ('fr', _create_test_data.french_translations), + ('en', _create_test_data.english_translations), + ('pl', {})): + response = self.app.get( + offset, + status=200, + extra_environ={'CKAN_LANG': lang_code, + 'CKAN_CURRENT_URL': offset}) + terms = ('A Novel By Tolstoy') + for term in terms: + if term in translations: + assert translations[term] in response, response + elif term in _create_test_data.english_translations: + assert (_create_test_data.english_translations[term] + in response) + else: + assert term in response + assert 'this should not be rendered' not in response def test_org_read_translation(self): for (lang_code, translations) in ( diff --git a/ckanext/reclineview/tests/test_view.py b/ckanext/reclineview/tests/test_view.py index f726d81ab8d..a9b0d925153 100644 --- a/ckanext/reclineview/tests/test_view.py +++ b/ckanext/reclineview/tests/test_view.py @@ -12,15 +12,14 @@ from ckan.tests import helpers, factories -class BaseTestReclineViewBase(tests.WsgiAppCase): +class BaseTestReclineViewBase(object): @classmethod def setup_class(cls): - cls.config_templates = config['ckan.legacy_templates'] - config['ckan.legacy_templates'] = 'false' - wsgiapp = middleware.make_app(config['global_conf'], **config) + + cls.app = helpers._get_test_app() + p.load(cls.view_type) - cls.app = paste.fixture.TestApp(wsgiapp) cls.p = cls.view_class() create_test_data.CreateTestData.create() @@ -30,7 +29,6 @@ def setup_class(cls): @classmethod def teardown_class(cls): - config['ckan.legacy_templates'] = cls.config_templates p.unload(cls.view_type) model.repo.rebuild_db() @@ -42,8 +40,10 @@ def test_can_view(self): assert not self.p.can_view(data_dict) def test_title_description_iframe_shown(self): - url = h.url_for(controller='package', action='resource_read', - id=self.package.name, resource_id=self.resource_id) + + with self.app.flask_app.test_request_context(): + url = h.url_for(controller='package', action='resource_read', + id=self.package.name, resource_id=self.resource_id) result = self.app.get(url) assert self.resource_view['title'] in result assert self.resource_view['description'] in result @@ -84,16 +84,12 @@ class TestReclineViewDatastoreOnly(helpers.FunctionalTestBase): @classmethod def setup_class(cls): + + cls.app = helpers._get_test_app() if not p.plugin_loaded('recline_view'): p.load('recline_view') if not p.plugin_loaded('datastore'): p.load('datastore') - app_config = config.copy() - app_config['ckan.legacy_templates'] = 'false' - app_config['ckan.plugins'] = 'recline_view datastore' - app_config['ckan.views.default_views'] = 'recline_view' - wsgiapp = middleware.make_app(config['global_conf'], **app_config) - cls.app = paste.fixture.TestApp(wsgiapp) @classmethod def teardown_class(cls): @@ -109,12 +105,16 @@ def test_create_datastore_only_view(self): 'fields': [{'id': 'a'}, {'id': 'b'}], 'records': [{'a': 1, 'b': 'xyz'}, {'a': 2, 'b': 'zzz'}] } - result = helpers.call_action('datastore_create', **data) - resource_id = result['resource_id'] + # datastore_create will call internally (or trigger something that + # calls it) so we need a Flask app context + with self.app.flask_app.test_request_context(): + result = helpers.call_action('datastore_create', **data) + + resource_id = result['resource_id'] - url = h.url_for(controller='package', action='resource_read', - id=dataset['id'], resource_id=resource_id) + url = h.url_for(controller='package', action='resource_read', + id=dataset['id'], resource_id=resource_id) result = self.app.get(url) diff --git a/ckanext/stats/tests/test_stats_plugin.py b/ckanext/stats/tests/test_stats_plugin.py index f6671632f4a..a1b83aa2592 100644 --- a/ckanext/stats/tests/test_stats_plugin.py +++ b/ckanext/stats/tests/test_stats_plugin.py @@ -1,7 +1,5 @@ import os -from ckan.tests.legacy import url_for - from ckanext.stats.tests import StatsFixture class TestStatsPlugin(StatsFixture): diff --git a/ckanext/textview/tests/test_view.py b/ckanext/textview/tests/test_view.py index 06f9c90d1dc..c8d59312b8f 100644 --- a/ckanext/textview/tests/test_view.py +++ b/ckanext/textview/tests/test_view.py @@ -9,6 +9,7 @@ import ckanext.textview.plugin as plugin import ckan.lib.create_test_data as create_test_data import ckan.config.middleware as middleware +from ckan.tests import helpers def _create_test_view(view_type): @@ -27,16 +28,14 @@ def _create_test_view(view_type): return resource_view, package, resource_id -class TestTextView(tests.WsgiAppCase): +class TestTextView(helpers.FunctionalTestBase): view_type = 'text_view' @classmethod def setup_class(cls): - cls.config_templates = config['ckan.legacy_templates'] - config['ckan.legacy_templates'] = 'false' - wsgiapp = middleware.make_app(config['global_conf'], **config) - plugins.load('text_view') - cls.app = paste.fixture.TestApp(wsgiapp) + + super(TestTextView, cls).setup_class() + cls.p = plugin.TextView() create_test_data.CreateTestData.create() @@ -46,10 +45,13 @@ def setup_class(cls): @classmethod def teardown_class(cls): - config['ckan.legacy_templates'] = cls.config_templates plugins.unload('text_view') model.repo.rebuild_db() + @classmethod + def _apply_config_changes(cls, config): + config['ckan.plugins'] = 'text_view' + def test_can_view(self): url_same_domain = urlparse.urljoin( config.get('ckan.site_url', '//localhost:5000'), @@ -77,17 +79,21 @@ def test_can_view(self): assert not self.p.can_view(data_dict) def test_title_description_iframe_shown(self): - url = h.url_for(controller='package', action='resource_read', - id=self.package.name, resource_id=self.resource_id) - result = self.app.get(url) + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = h.url_for(controller='package', action='resource_read', + id=self.package.name, resource_id=self.resource_id) + result = app.get(url) assert self.resource_view['title'] in result assert self.resource_view['description'] in result assert 'data-module="data-viewer"' in result.body def test_js_included(self): - url = h.url_for(controller='package', action='resource_view', - id=self.package.name, resource_id=self.resource_id, - view_id=self.resource_view['id']) - result = self.app.get(url) + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = h.url_for(controller='package', action='resource_view', + id=self.package.name, resource_id=self.resource_id, + view_id=self.resource_view['id']) + result = app.get(url) assert (('text_view.js' in result.body) or ('text_view.min.js' in result.body))