diff --git a/ckan/tests/controllers/test_package.py b/ckan/tests/controllers/test_package.py index 6489d7fe33f..ceaec0e16d0 100644 --- a/ckan/tests/controllers/test_package.py +++ b/ckan/tests/controllers/test_package.py @@ -734,7 +734,7 @@ def test_delete_on_non_existing_dataset(self): with app.flask_app.test_request_context(): url = url_for(controller='package', action='delete', - id='schrodingersdatset') + id='schrodingersdatset') response = app.post( url, @@ -1815,7 +1815,7 @@ def test_package_unfollow(self): with app.flask_app.test_request_context(): follow_url = url_for(controller='package', - action='follow', + action='follow', id=package['id']) app.post(follow_url, extra_environ=env, status=302) diff --git a/ckan/tests/legacy/__init__.py b/ckan/tests/legacy/__init__.py index 3a056855f95..a647c147899 100644 --- a/ckan/tests/legacy/__init__.py +++ b/ckan/tests/legacy/__init__.py @@ -35,6 +35,7 @@ import ckan.model as model from ckan import ckan_nose_plugin from ckan.common import json +from ckan.tests import helpers # evil hack as url_for is passed out url_for = h.url_for @@ -237,7 +238,8 @@ class WsgiAppCase(BaseCase): # Either that, or this file got imported somehow before the tests started # running, meaning the pylonsapp wasn't setup yet (which is done in # pylons.test.py:begin()) - app = paste.fixture.TestApp(wsgiapp) + #app = paste.fixture.TestApp(wsgiapp) + app = helpers._get_test_app() def config_abspath(file_path): @@ -415,6 +417,7 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs): params = json.dumps(kwargs) response = app.post('/api/action/{0}'.format(action), params=params, extra_environ={'Authorization': str(apikey)}, status=status) + assert '/api/3/action/help_show?name={0}'.format(action) \ in response.json['help'] diff --git a/ckan/tests/legacy/functional/api/base.py b/ckan/tests/legacy/functional/api/base.py index fefedae0f65..1a11b53c196 100644 --- a/ckan/tests/legacy/functional/api/base.py +++ b/ckan/tests/legacy/functional/api/base.py @@ -190,7 +190,7 @@ def loads(self, chars): raise Exception, "Couldn't loads string '%s': %s" % (chars, inst) def assert_json_response(self, res, expected_in_body=None): - content_type = res.header_dict['Content-Type'] + content_type = res.headers['Content-Type'] assert 'application/json' in content_type, content_type res_json = self.loads(res.body) if expected_in_body: diff --git a/ckan/tests/legacy/functional/api/model/test_package.py b/ckan/tests/legacy/functional/api/model/test_package.py index 22472d7dac3..593f49d7612 100644 --- a/ckan/tests/legacy/functional/api/model/test_package.py +++ b/ckan/tests/legacy/functional/api/model/test_package.py @@ -13,6 +13,7 @@ from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase import ckan.tests.legacy as tests +from ckan.tests import helpers # Todo: Remove this ckan.model stuff. import ckan.model as model @@ -65,7 +66,7 @@ def test_register_post_ok(self): assert_equal(pkg['extras'], self.package_fixture_data['extras']) # Check the value of the Location header. - location = res.header('Location') + location = res.headers['Location'] assert offset in location res = self.app.get(location, status=self.STATUS_200_OK) @@ -133,7 +134,7 @@ def test_register_post_with_group(self): package_fixture_data = self.package_fixture_data package_fixture_data['groups'] = groups data = self.dumps(package_fixture_data) - res = self.post_json(offset, data, status=self.STATUS_201_CREATED, + res = self.app.post(offset, data, status=self.STATUS_201_CREATED, extra_environ={'Authorization':str(user.apikey)}) # Check the database record. @@ -159,7 +160,7 @@ def test_register_post_with_group_not_authorized(self): package_fixture_data = self.package_fixture_data package_fixture_data['groups'] = groups data = self.dumps(package_fixture_data) - res = self.post_json(offset, data, status=self.STATUS_403_ACCESS_DENIED, + res = self.app.post(offset, data, status=self.STATUS_403_ACCESS_DENIED, extra_environ=self.extra_environ) del package_fixture_data['groups'] @@ -173,7 +174,7 @@ def test_register_post_with_group_not_found(self): package_fixture_data = self.package_fixture_data package_fixture_data['groups'] = groups data = self.dumps(package_fixture_data) - res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND, + res = self.app.post(offset, data, status=self.STATUS_404_NOT_FOUND, extra_environ=self.extra_environ) del package_fixture_data['groups'] @@ -187,7 +188,7 @@ def test_register_post_with_group_sysadmin(self): package_fixture_data = self.package_fixture_data package_fixture_data['groups'] = groups data = self.dumps(package_fixture_data) - res = self.post_json(offset, data, status=self.STATUS_201_CREATED, + res = self.app.post(offset, data, status=self.STATUS_201_CREATED, extra_environ={'Authorization':str(user.apikey)}) # Check the database record. model.Session.remove() @@ -207,7 +208,7 @@ def test_register_post_json(self): assert not self.get_package_by_name(self.package_fixture_data['name']) offset = self.package_offset() data = self.dumps(self.package_fixture_data) - res = self.post_json(offset, data, status=self.STATUS_201_CREATED, + res = self.app.post(offset, data, status=self.STATUS_201_CREATED, extra_environ=self.admin_extra_environ) # Check the database record. model.Session.remove() @@ -219,7 +220,7 @@ def test_register_post_bad_content_type(self): assert not self.get_package_by_name(self.package_fixture_data['name']) offset = self.package_offset() data = self.dumps(self.package_fixture_data) - res = self.http_request(offset, data, + res = self.app.post(offset, data, content_type='something/unheard_of', status=[self.STATUS_400_BAD_REQUEST, self.STATUS_201_CREATED], @@ -262,7 +263,7 @@ def test_register_post_indexerror(self): offset = self.package_offset() data = self.dumps(self.package_fixture_data) - self.post_json(offset, data, status=500, extra_environ=self.admin_extra_environ) + self.app.post(offset, data, status=500, extra_environ=self.admin_extra_environ) model.Session.remove() finally: SolrSettings.init(original_settings) @@ -273,7 +274,7 @@ def test_register_post_tag_too_long(self): assert not self.get_package_by_name(pkg['name']) offset = self.package_offset() data = self.dumps(pkg) - res = self.post_json(offset, data, status=self.STATUS_409_CONFLICT, + res = self.app.post(offset, data, status=self.STATUS_409_CONFLICT, extra_environ=self.admin_extra_environ) assert 'length is more than maximum 100' in res.body, res.body assert 'tagok' not in res.body @@ -714,8 +715,8 @@ def test_entity_delete_ok_without_request_headers(self): assert self.get_package_by_name(self.package_fixture_data['name']) # delete it offset = self.package_offset(self.package_fixture_data['name']) - res = self.delete_request(offset, status=self.STATUS_200_OK, - extra_environ=self.admin_extra_environ) + res = self.app.delete(offset, status=self.STATUS_200_OK, + extra_environ=self.admin_extra_environ) package = self.get_package_by_name(self.package_fixture_data['name']) self.assert_equal(package.state, 'deleted') model.Session.remove() diff --git a/ckan/tests/legacy/functional/api/model/test_relationships.py b/ckan/tests/legacy/functional/api/model/test_relationships.py index 0bdc30c84e5..069776d663d 100644 --- a/ckan/tests/legacy/functional/api/model/test_relationships.py +++ b/ckan/tests/legacy/functional/api/model/test_relationships.py @@ -1,14 +1,14 @@ # encoding: utf-8 -from nose.tools import assert_equal +from nose.tools import assert_equal from nose.plugins.skip import SkipTest from ckan import model from ckan.lib.create_test_data import CreateTestData from ckan.tests.legacy.functional.api.base import BaseModelApiTestCase -from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase -from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase +from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase +from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase class RelationshipsTestCase(BaseModelApiTestCase): @@ -115,7 +115,7 @@ def test_01_create_and_read_relationship(self): assert len(rels) == 1 self.check_relationship_dict(rels[0], 'annakarenina', 'parent_of', 'warandpeace', self.comment) - + def test_02_create_relationship_way_2(self): # Create a relationship using 2nd way self.create_annakarenina_parent_of_war_and_peace(way=2) @@ -189,7 +189,7 @@ def test_create_relationship_unknown(self): def create_annakarenina_parent_of_war_and_peace(self, way=1): # Create package relationship. # More than one 'way' to create a package. - # Todo: Redesign this in a RESTful style, so that a relationship is + # Todo: Redesign this in a RESTful style, so that a relationship is # created by posting a relationship to a relationship **register**. assert way in (1, 2, 3, 4) if way == 1: @@ -220,7 +220,7 @@ def create_annakarenina_parent_of_war_and_peace(self, way=1): assert_equal(rel['type'], 'child_of') assert_equal(rel['subject'], self.ref_package(self.war)) assert_equal(rel['object'], self.ref_package(self.anna)) - + # Check the model, directly. rels = self.anna.get_relationships() assert len(rels) == 1, rels @@ -271,7 +271,7 @@ def get_relationships(self, package1_name=u'annakarenina', type='relationships', if type: allowable_statuses.append(404) res = self.app.get(offset, status=allowable_statuses) - if res.status == 200: + if res.status_int == 200: res_dict = self.data_from_res(res) if res.body else [] return res_dict else: @@ -300,7 +300,7 @@ def check_relationships_rest(self, pkg1_name, pkg2_name=None, expected_relationships=[]): rels = self.get_relationships(package1_name=pkg1_name, package2_name=pkg2_name) - self.assert_len_relationships(rels, expected_relationships) + self.assert_len_relationships(rels, expected_relationships) for rel in rels: the_expected_rel = None for expected_rel in expected_relationships: diff --git a/ckan/tests/legacy/functional/api/model/test_vocabulary.py b/ckan/tests/legacy/functional/api/model/test_vocabulary.py index feb03af3717..ab6bb25f6f2 100644 --- a/ckan/tests/legacy/functional/api/model/test_vocabulary.py +++ b/ckan/tests/legacy/functional/api/model/test_vocabulary.py @@ -1,17 +1,17 @@ # encoding: utf-8 import ckan -import pylons.test -import paste.fixture import ckan.lib.helpers as helpers import ckan.lib.dictization.model_dictize as model_dictize +from ckan.tests import helpers as test_helpers + class TestVocabulary(object): @classmethod def setup_class(self): - self.app = paste.fixture.TestApp(pylons.test.pylonsapp) + self.app = test_helpers._get_test_app() @classmethod def teardown_class(self): diff --git a/ckan/tests/legacy/functional/api/test_activity.py b/ckan/tests/legacy/functional/api/test_activity.py index 1350b7acaba..114d91b6723 100644 --- a/ckan/tests/legacy/functional/api/test_activity.py +++ b/ckan/tests/legacy/functional/api/test_activity.py @@ -22,6 +22,8 @@ from ckan.common import json import ckan.tests.legacy as tests +from ckan.tests import helpers + ##def package_update(context, data_dict): ## # These tests call package_update directly which is really bad @@ -204,7 +206,7 @@ def setup_class(self): 'id': annakarenina.id, } self.users = [self.sysadmin_user, self.normal_user] - self.app = paste.fixture.TestApp(pylons.test.pylonsapp) + self.app = helpers._get_test_app() @classmethod def teardown_class(self): diff --git a/ckan/tests/legacy/functional/api/test_dashboard.py b/ckan/tests/legacy/functional/api/test_dashboard.py index 7482dd1a839..7b7364aa57d 100644 --- a/ckan/tests/legacy/functional/api/test_dashboard.py +++ b/ckan/tests/legacy/functional/api/test_dashboard.py @@ -12,6 +12,8 @@ import paste import pylons.test from ckan.tests.legacy import CreateTestData +from ckan.tests import helpers + class TestDashboard(object): '''Tests for the logic action functions related to the user's dashboard.''' @@ -34,7 +36,7 @@ def user_create(cls): def setup_class(cls): ckan.lib.search.clear_all() CreateTestData.create() - cls.app = paste.fixture.TestApp(pylons.test.pylonsapp) + cls.app = helpers._get_test_app() joeadmin = ckan.model.User.get('joeadmin') cls.joeadmin = { 'id': joeadmin.id, diff --git a/ckan/tests/legacy/functional/api/test_email_notifications.py b/ckan/tests/legacy/functional/api/test_email_notifications.py index 35bea72dbcb..f8505201836 100644 --- a/ckan/tests/legacy/functional/api/test_email_notifications.py +++ b/ckan/tests/legacy/functional/api/test_email_notifications.py @@ -10,13 +10,12 @@ import ckan.tests.legacy.pylons_controller as pylons_controller import ckan.config.middleware -import paste -import paste.deploy -import pylons.test +from ckan.tests import helpers from ckan.common import config + class TestEmailNotifications(mock_mail_server.SmtpServerHarness, pylons_controller.PylonsTestCase): @@ -25,7 +24,7 @@ def setup_class(cls): mock_mail_server.SmtpServerHarness.setup_class() pylons_controller.PylonsTestCase.setup_class() tests.CreateTestData.create() - cls.app = paste.fixture.TestApp(pylons.test.pylonsapp) + cls.app = helpers._get_test_app() joeadmin = model.User.get('joeadmin') cls.joeadmin = {'id': joeadmin.id, 'apikey': joeadmin.apikey, @@ -198,7 +197,7 @@ def setup_class(cls): mock_mail_server.SmtpServerHarness.setup_class() pylons_controller.PylonsTestCase.setup_class() tests.CreateTestData.create() - cls.app = paste.fixture.TestApp(pylons.test.pylonsapp) + cls.app = helpers._get_test_app() joeadmin = model.User.get('joeadmin') cls.joeadmin = {'id': joeadmin.id, 'apikey': joeadmin.apikey, @@ -338,7 +337,7 @@ def setup_class(cls): wsgiapp = ckan.config.middleware.make_app(config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() mock_mail_server.SmtpServerHarness.setup_class() pylons_controller.PylonsTestCase.setup_class() @@ -422,7 +421,7 @@ def setup_class(cls): wsgiapp = ckan.config.middleware.make_app(config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() mock_mail_server.SmtpServerHarness.setup_class() pylons_controller.PylonsTestCase.setup_class() diff --git a/ckan/tests/legacy/functional/api/test_follow.py b/ckan/tests/legacy/functional/api/test_follow.py index 909b2e77606..98f4bd23073 100644 --- a/ckan/tests/legacy/functional/api/test_follow.py +++ b/ckan/tests/legacy/functional/api/test_follow.py @@ -18,6 +18,8 @@ import ckan from ckan.tests.legacy import are_foreign_keys_supported, SkipTest, CreateTestData, call_action_api +from ckan.tests import helpers + def datetime_from_string(s): '''Return a standard datetime.datetime object initialised from a string in @@ -295,7 +297,7 @@ def setup_class(self): 'id': ckan.model.Group.get('david').id, 'name': ckan.model.Group.get('david').name, } - self.app = paste.fixture.TestApp(pylons.test.pylonsapp) + self.app = helpers._get_test_app() @classmethod def teardown_class(self): @@ -804,7 +806,7 @@ def setup_class(self): 'id': ckan.model.Group.get('david').id, 'name': ckan.model.Group.get('david').name, } - self.app = paste.fixture.TestApp(pylons.test.pylonsapp) + self.app = helpers._get_test_app() follow_user(self.app, self.testsysadmin['id'], self.testsysadmin['apikey'], self.joeadmin['id'], self.joeadmin['id'], self.testsysadmin['apikey']) @@ -1154,7 +1156,7 @@ def setup_class(self): 'id': ckan.model.Group.get('david').id, 'name': ckan.model.Group.get('david').name, } - self.app = paste.fixture.TestApp(pylons.test.pylonsapp) + self.app = helpers._get_test_app() follow_user(self.app, self.joeadmin['id'], self.joeadmin['apikey'], self.testsysadmin['id'], self.testsysadmin['id'], diff --git a/ckan/tests/legacy/functional/api/test_resource.py b/ckan/tests/legacy/functional/api/test_resource.py index e8c53593bcd..28c3a6bed15 100644 --- a/ckan/tests/legacy/functional/api/test_resource.py +++ b/ckan/tests/legacy/functional/api/test_resource.py @@ -45,7 +45,7 @@ def teardown_class(self): def test_good_input(self): offset = self.base_url + '/format_autocomplete?incomplete=cs' result = self.app.get(offset, status=200) - content_type = result.header_dict['Content-Type'] + content_type = result.headers['Content-Type'] assert 'application/json' in content_type, content_type res_json = self.loads(result.body) assert 'ResultSet' in res_json, res_json @@ -58,7 +58,7 @@ def test_good_input(self): def test_missing_format(self): offset = self.base_url + '/format_autocomplete?incomplete=incorrectformat' result = self.app.get(offset, status=200) - content_type = result.header_dict['Content-Type'] + content_type = result.headers['Content-Type'] assert 'application/json' in content_type, content_type res_json = self.loads(result.body) assert 'ResultSet' in res_json, res_json diff --git a/ckan/tests/legacy/functional/api/test_user.py b/ckan/tests/legacy/functional/api/test_user.py index e470ce2d8bd..b58daface79 100644 --- a/ckan/tests/legacy/functional/api/test_user.py +++ b/ckan/tests/legacy/functional/api/test_user.py @@ -13,6 +13,7 @@ from ckan.tests.legacy import url_for import ckan.config.middleware from ckan.common import json +from ckan.tests import helpers class TestUserApi(ControllerTestCase): @@ -25,8 +26,12 @@ def teardown_class(cls): model.repo.rebuild_db() def test_autocomplete(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='user_autocomplete', ver=2) + response = self.app.get( - url=url_for(controller='api', action='user_autocomplete', ver=2), + url, params={ 'q': u'sysadmin', }, @@ -35,11 +40,15 @@ def test_autocomplete(self): print response.json assert set(response.json[0].keys()) == set(['id', 'name', 'fullname']) assert_equal(response.json[0]['name'], u'testsysadmin') - assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8') + assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8') def test_autocomplete_multiple(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='user_autocomplete', ver=2) + response = self.app.get( - url=url_for(controller='api', action='user_autocomplete', ver=2), + url, params={ 'q': u'tes', }, @@ -49,8 +58,12 @@ def test_autocomplete_multiple(self): assert_equal(len(response.json), 2) def test_autocomplete_limit(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='user_autocomplete', ver=2) + response = self.app.get( - url=url_for(controller='api', action='user_autocomplete', ver=2), + url, params={ 'q': u'tes', 'limit': 1 @@ -72,7 +85,7 @@ def setup_class(cls): cls._original_config = config.copy() wsgiapp = ckan.config.middleware.make_app( config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() cls.sysadmin_user = model.User.get('testsysadmin') PylonsTestCase.setup_class() @@ -122,7 +135,7 @@ def setup_class(cls): config['ckan.auth.create_user_via_api'] = True wsgiapp = ckan.config.middleware.make_app( config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() PylonsTestCase.setup_class() cls.sysadmin_user = model.User.get('testsysadmin') @@ -170,7 +183,7 @@ def setup_class(cls): config['ckan.auth.create_user_via_web'] = False wsgiapp = ckan.config.middleware.make_app( config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() cls.sysadmin_user = model.User.get('testsysadmin') PylonsTestCase.setup_class() @@ -206,7 +219,7 @@ def setup_class(cls): config['ckan.auth.create_user_via_web'] = True wsgiapp = ckan.config.middleware.make_app( config['global_conf'], **config) - cls.app = paste.fixture.TestApp(wsgiapp) + cls.app = helpers._get_test_app() cls.sysadmin_user = model.User.get('testsysadmin') PylonsTestCase.setup_class() diff --git a/ckan/tests/legacy/functional/api/test_util.py b/ckan/tests/legacy/functional/api/test_util.py index 9ba2adb9afe..9065d00e7da 100644 --- a/ckan/tests/legacy/functional/api/test_util.py +++ b/ckan/tests/legacy/functional/api/test_util.py @@ -18,8 +18,12 @@ def teardown_class(cls): model.repo.rebuild_db() def test_package_slug_invalid(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='is_slug_valid', ver=2) + response = self.app.get( - url=url_for(controller='api', action='is_slug_valid', ver=2), + url, params={ 'type': u'package', 'slug': u'edit', @@ -27,10 +31,14 @@ def test_package_slug_invalid(self): status=200, ) assert_equal(response.body, '{"valid": false}') - assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8') + assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8') + + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='is_slug_valid', ver=2) response = self.app.get( - url=url_for(controller='api', action='is_slug_valid', ver=2), + url, params={ 'type': u'package', 'slug': u'new', @@ -38,11 +46,15 @@ def test_package_slug_invalid(self): status=200, ) assert_equal(response.body, '{"valid": false}') - assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8') + assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8') def test_package_slug_valid(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='is_slug_valid', ver=2) + response = self.app.get( - url=url_for(controller='api', action='is_slug_valid', ver=2), + url, params={ 'type': u'package', 'slug': u'A New Title * With & Funny CHARacters', @@ -50,10 +62,14 @@ def test_package_slug_valid(self): status=200, ) assert_equal(response.body, '{"valid": true}') - assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8') + assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8') + + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='is_slug_valid', ver=2) response = self.app.get( - url=url_for(controller='api', action='is_slug_valid', ver=2), + url, params={ 'type': u'package', 'slug': u'warandpeace', @@ -61,44 +77,64 @@ def test_package_slug_valid(self): status=200, ) assert_equal(response.body, '{"valid": false}') - assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8') + assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8') def test_markdown(self): markdown = '''##Title''' + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='markdown', ver=2) + response = self.app.get( - url=url_for(controller='api', action='markdown', ver=2), + url, params={'q': markdown}, status=200, ) assert_equal(response.body, '"

Title

"') def test_munge_package_name(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='munge_package_name', ver=2) + response = self.app.get( - url=url_for(controller='api', action='munge_package_name', ver=2), + url, params={'name': 'test name'}, status=200, ) assert_equal(response.body, '"test-name"') def test_munge_title_to_package_name(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='munge_title_to_package_name', ver=2) + response = self.app.get( - url=url_for(controller='api', action='munge_title_to_package_name', ver=2), + url, params={'name': 'Test title'}, status=200, ) assert_equal(response.body, '"test-title"') def test_munge_tag(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='munge_tag', ver=2) + response = self.app.get( - url=url_for(controller='api', action='munge_tag', ver=2), + url, params={'name': 'Test subject'}, status=200, ) assert_equal(response.body, '"test-subject"') def test_status(self): + + with self.app.flask_app.test_request_context(): + url = url_for(controller='api', action='status', ver=2) + response = self.app.get( - url=url_for(controller='api', action='status', ver=2), + url, params={}, status=200, ) diff --git a/ckan/tests/legacy/functional/test_activity.py b/ckan/tests/legacy/functional/test_activity.py index f40f1397d78..61eab03ea0d 100644 --- a/ckan/tests/legacy/functional/test_activity.py +++ b/ckan/tests/legacy/functional/test_activity.py @@ -4,7 +4,7 @@ from pylons.test import pylonsapp from paste.deploy.converters import asbool import paste.fixture -from routes import url_for +from ckan.lib.helpers import url_for from nose import SkipTest import ckan @@ -14,9 +14,10 @@ from ckan.logic.action.update import user_update, group_update from ckan.logic.action.delete import package_delete from ckan.tests.legacy.html_check import HtmlCheckMethods -from ckan.tests.legacy import CreateTestData +from ckan.tests.legacy import CreateTestData, WsgiAppCase -class TestActivity(HtmlCheckMethods): + +class TestActivity(WsgiAppCase, HtmlCheckMethods): """Test the rendering of activity streams into HTML pages. Activity streams are tested in detail elsewhere, this class just briefly @@ -29,7 +30,6 @@ def setup(cls): raise SkipTest('Activity streams not enabled') CreateTestData.create() cls.sysadmin_user = ckan.model.User.get('testsysadmin') - cls.app = paste.fixture.TestApp(pylonsapp) @classmethod def teardown(cls): @@ -52,7 +52,9 @@ def test_user_activity(self): 'allow_partial_update': True, } user = user_create(context, user_dict) - offset = url_for(controller='user', action='activity', id=user['id']) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='user', action='activity', id=user['id']) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s signed up' % user['fullname'] in stripped, stripped @@ -239,7 +241,9 @@ def test_user_activity(self): # The user's dashboard page should load successfully and have the # latest 15 activities on it. - offset = url_for(controller='user', action='dashboard') + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='user', action='dashboard') extra_environ = {'Authorization': str(ckan.model.User.get('billybeane').apikey)} result = self.app.post(offset, extra_environ=extra_environ, diff --git a/ckan/tests/legacy/functional/test_admin.py b/ckan/tests/legacy/functional/test_admin.py index 7737f036650..41ba685fc38 100644 --- a/ckan/tests/legacy/functional/test_admin.py +++ b/ckan/tests/legacy/functional/test_admin.py @@ -15,7 +15,8 @@ def teardown_class(self): #test that only sysadmins can access the /ckan-admin page def test_index(self): - url = url_for('ckanadmin', action='index') + with self.app.flask_app.test_request_context(): + url = url_for('ckanadmin', action='index') response = self.app.get(url, status=[403]) # random username response = self.app.get(url, status=[403], diff --git a/ckan/tests/legacy/functional/test_group.py b/ckan/tests/legacy/functional/test_group.py index 0223375f232..1e952879ad1 100644 --- a/ckan/tests/legacy/functional/test_group.py +++ b/ckan/tests/legacy/functional/test_group.py @@ -95,7 +95,9 @@ def test_sorting(self): def test_read_non_existent(self): name = u'group_does_not_exist' - offset = url_for(controller='group', action='read', id=name) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='group', action='read', id=name) res = self.app.get(offset, status=404) @@ -131,8 +133,10 @@ def teardown_class(self): model.repo.rebuild_db() def test_2_atom_feed(self): - offset = url_for(controller='group', action='history', - id=self.grp.name) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='group', action='history', + id=self.grp.name) offset = "%s?format=atom" % offset res = self.app.get(offset) assert ']') # edit the package - self.offset = url_for(controller='package', action='edit', id=self.editpkg_name) + + with self.app.flask_app.test_request_context(): + self.offset = url_for(controller='package', action='edit', id=self.editpkg_name) self.res = self.app.get(self.offset, extra_environ=self.extra_environ_admin) fv = self.res.forms['dataset-edit'] fv['title'] = u'New Title' @@ -547,7 +564,8 @@ def test_delete(self): plugins.load('test_package_controller_plugin') plugin = plugins.get_plugin('test_package_controller_plugin') - offset = url_for(controller='package', action='delete', + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='delete', id='warandpeace') # Since organizations, any owned dataset can be edited/deleted by any # user @@ -582,7 +600,9 @@ def teardown_class(self): def test_new_plugin_hook(self): plugins.load('test_package_controller_plugin') plugin = plugins.get_plugin('test_package_controller_plugin') - offset = url_for(controller='package', action='new') + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='new') res = self.app.get(offset, extra_environ=self.extra_environ_tester) new_name = u'plugged' fv = res.forms['dataset-edit'] @@ -597,7 +617,9 @@ def test_new_plugin_hook(self): def test_after_create_plugin_hook(self): plugins.load('test_package_controller_plugin') plugin = plugins.get_plugin('test_package_controller_plugin') - offset = url_for(controller='package', action='new') + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='new') res = self.app.get(offset, extra_environ=self.extra_environ_tester) new_name = u'plugged2' fv = res.forms['dataset-edit'] @@ -617,8 +639,8 @@ def test_new_indexerror(self): try: SolrSettings.init(bad_solr_url) new_package_name = u'new-package-missing-solr' - - offset = url_for(controller='package', action='new') + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='new') res = self.app.get(offset, extra_environ=self.extra_environ_tester) fv = res.forms['dataset-edit'] fv['name'] = new_package_name @@ -633,7 +655,9 @@ def test_new_indexerror(self): SolrSettings.init(solr_url) def test_change_locale(self): - offset = url_for(controller='package', action='new') + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='new') res = self.app.get(offset, extra_environ=self.extra_environ_tester) res = self.app.get('/de/dataset/new', extra_environ=self.extra_environ_tester) @@ -683,12 +707,16 @@ def teardown_class(self): model.repo.rebuild_db() def test_read(self): - offset = url_for(controller='package', action='read', id=self.non_active_name) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='read', id=self.non_active_name) res = self.app.get(offset, status=[404]) def test_read_as_admin(self): - offset = url_for(controller='package', action='read', id=self.non_active_name) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='package', action='read', id=self.non_active_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER':'testsysadmin'}) @@ -718,7 +746,9 @@ def setup_class(cls): cls.revision_ids = [rev[0].id for rev in cls.pkg1.all_related_revisions] # revision ids are newest first cls.revision_timestamps = [rev[0].timestamp for rev in cls.pkg1.all_related_revisions] - cls.offset = url_for(controller='package', action='history', id=cls.pkg1.name) + + with cls.app.flask_app.test_request_context(): + cls.offset = url_for(controller='package', action='history', id=cls.pkg1.name) @classmethod def teardown_class(cls): diff --git a/ckan/tests/legacy/functional/test_pagination.py b/ckan/tests/legacy/functional/test_pagination.py index 596bc5fa453..3a4fc7912c7 100644 --- a/ckan/tests/legacy/functional/test_pagination.py +++ b/ckan/tests/legacy/functional/test_pagination.py @@ -59,25 +59,41 @@ def teardown_class(self): model.repo.rebuild_db() def test_package_search_p1(self): - res = self.app.get(url_for(controller='package', action='search', q='groups:group_00')) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='package', action='search', q='groups:group_00') + + res = self.app.get(url) assert 'href="/dataset?q=groups%3Agroup_00&page=2"' in res pkg_numbers = scrape_search_results(res, 'dataset') assert_equal(['50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40', '39', '38', '37', '36', '35', '34', '33', '32', '31'], pkg_numbers) def test_package_search_p2(self): - res = self.app.get(url_for(controller='package', action='search', q='groups:group_00', page=2)) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='package', action='search', q='groups:group_00', page=2) + + res = self.app.get(url) assert 'href="/dataset?q=groups%3Agroup_00&page=1"' in res pkg_numbers = scrape_search_results(res, 'dataset') assert_equal(['30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11'], pkg_numbers) def test_group_datasets_read_p1(self): - res = self.app.get(url_for(controller='group', action='read', id='group_00')) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='group', action='read', id='group_00') + + res = self.app.get(url) assert 'href="/group/group_00?page=2' in res, res pkg_numbers = scrape_search_results(res, 'group_dataset') assert_equal(['50', '49', '48', '47', '46', '45', '44', '43', '42', '41', '40', '39', '38', '37', '36', '35', '34', '33', '32', '31'], pkg_numbers) def test_group_datasets_read_p2(self): - res = self.app.get(url_for(controller='group', action='read', id='group_00', page=2)) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='group', action='read', id='group_00', page=2) + + res = self.app.get(url) assert 'href="/group/group_00?page=1' in res, res pkg_numbers = scrape_search_results(res, 'group_dataset') assert_equal(['30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11'], pkg_numbers) @@ -101,12 +117,20 @@ def teardown_class(self): model.repo.rebuild_db() def test_group_index(self): - res = self.app.get(url_for(controller='group', action='index')) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='group', action='index') + + res = self.app.get(url) assert 'href="/group?q=&sort=&page=2"' in res, res grp_numbers = scrape_search_results(res, 'group') assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'], grp_numbers) - res = self.app.get(url_for(controller='group', action='index', page=2)) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='group', action='index', page=2) + + res = self.app.get(url) assert 'href="/group?q=&sort=&page=1"' in res grp_numbers = scrape_search_results(res, 'group') assert_equal(['21'], grp_numbers) @@ -130,12 +154,19 @@ def teardown_class(self): model.repo.rebuild_db() def test_users_index(self): - res = self.app.get(url_for(controller='user', action='index')) + + with self.app.flask_app.test_request_context(): + url = url_for(controller='user', action='index') + + res = self.app.get(url) assert 'href="/user?q=&order_by=name&page=2"' in res user_numbers = scrape_search_results(res, 'user') assert_equal(['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'], user_numbers) - res = self.app.get(url_for(controller='user', action='index', page=2)) + with self.app.flask_app.test_request_context(): + url = url_for(controller='user', action='index', page=2) + + res = self.app.get(url) assert 'href="/user?q=&order_by=name&page=1"' in res user_numbers = scrape_search_results(res, 'user') assert_equal(['20'], user_numbers) diff --git a/ckan/tests/legacy/functional/test_preview_interface.py b/ckan/tests/legacy/functional/test_preview_interface.py index deb7aa6f965..747834446e3 100644 --- a/ckan/tests/legacy/functional/test_preview_interface.py +++ b/ckan/tests/legacy/functional/test_preview_interface.py @@ -19,14 +19,16 @@ def setup_class(cls): cls.package = model.Package.get('annakarenina') cls.resource = cls.package.resources[0] - cls.url = h.url_for(controller='package', - action='resource_read', - id=cls.package.name, - resource_id=cls.resource.id) - cls.preview_url = h.url_for(controller='package', - action='resource_datapreview', - id=cls.package.id, - resource_id=cls.resource.id) + + with cls.app.flask_app.test_request_context(): + cls.url = h.url_for(controller='package', + action='resource_read', + id=cls.package.name, + resource_id=cls.resource.id) + cls.preview_url = h.url_for(controller='package', + action='resource_datapreview', + id=cls.package.id, + resource_id=cls.resource.id) @classmethod def teardown_class(cls): @@ -35,7 +37,9 @@ def teardown_class(cls): def test_hook(self): testpackage = self.package - resource_dict = model_dictize.resource_dictize(self.resource, {'model': model}) + + with self.app.flask_app.test_request_context(): + resource_dict = model_dictize.resource_dictize(self.resource, {'model': model}) context = { 'model': model, @@ -70,10 +74,12 @@ def test_hook(self): assert self.plugin.calls['preview_templates'] == 1, self.plugin.calls # test whether the json preview is used - preview_url = h.url_for(controller='package', - action='resource_datapreview', - id=testpackage.id, - resource_id=testpackage.resources[1].id) + + with self.app.flask_app.test_request_context(): + preview_url = h.url_for(controller='package', + action='resource_datapreview', + id=testpackage.id, + resource_id=testpackage.resources[1].id) result = self.app.get(preview_url, status=200) assert 'mock-json-preview' in result.body diff --git a/ckan/tests/legacy/functional/test_revision.py b/ckan/tests/legacy/functional/test_revision.py index af0f653fa1c..5312aad7c29 100644 --- a/ckan/tests/legacy/functional/test_revision.py +++ b/ckan/tests/legacy/functional/test_revision.py @@ -90,7 +90,9 @@ def get_package(self, name): def test_read(self): anna = model.Package.by_name(u'annakarenina') rev_id = anna.revision.id - offset = url_for(controller='revision', action='read', id='%s' % rev_id) + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='revision', action='read', id='%s' % rev_id) res = self.app.get(offset) assert 'Revision %s' % rev_id in res assert 'Revision: %s' % rev_id in res @@ -132,14 +134,18 @@ def test_list_format_atom(self): # Todo: Test for first revision on last page. # Todo: Test for last revision minus 50 on second page. # Page 1. (Implied id=1) - offset = url_for(controller='revision', action='list', format='atom') + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='revision', action='list', format='atom') res = self.app.get(offset) assert '' in res, res # Todo: Better test for 'days' request param. # - fake some older revisions and check they aren't included. - offset = url_for(controller='revision', action='list', format='atom', + + with self.app.flask_app.test_request_context(): + offset = url_for(controller='revision', action='list', format='atom', days=30) res = self.app.get(offset) assert ' section' the_html = self._get_html_from_res(html) @@ -33,13 +34,13 @@ def strip_tags(self, res): '''Call strip_tags on a TestResponse object to strip any and all HTML and normalise whitespace.''' if not isinstance(res, basestring): res = res.body.decode('utf-8') - return Stripper().strip(res) + return Stripper().strip(res) def check_named_element(self, html, tag_name, *html_to_find): '''Searches in the html and returns True if it can find a particular tag and all its subtags & data which contains all the of the html_to_find''' - named_element_re = re.compile('(<(%(tag)s\w*).*?(>.*?)' % {'tag':tag_name}) + named_element_re = re.compile('(<(%(tag)s\w*).*?(>.*?)' % {'tag':tag_name}) html_str = self._get_html_from_res(html) self._check_html(named_element_re, html_str.replace('\n', ''), html_to_find) @@ -60,13 +61,14 @@ def check_tag(self, html, *html_to_find): self._check_html(self.tag_re, html, html_to_find) def _get_html_from_res(self, html): - if isinstance(html, paste.fixture.TestResponse): + if isinstance(html, (paste.fixture.TestResponse, webtest.app.TestResponse)): html_str = html.body.decode('utf8') elif isinstance(html, unicode): html_str = html elif isinstance(html, str): html_str = html.decode('utf8') else: + import ipdb; ipdb.set_trace() raise TypeError return html_str # always unicode diff --git a/ckan/tests/legacy/lib/test_alphabet_pagination.py b/ckan/tests/legacy/lib/test_alphabet_pagination.py index 97d2773c634..c3f4cdaa428 100644 --- a/ckan/tests/legacy/lib/test_alphabet_pagination.py +++ b/ckan/tests/legacy/lib/test_alphabet_pagination.py @@ -8,6 +8,7 @@ from ckan.tests.legacy import regex_related from ckan.lib.create_test_data import CreateTestData from ckan import model +from ckan.tests import helpers other = 'Other' @@ -54,7 +55,9 @@ def test_01_package_page(self): page='A', other_text=other, ) - pager = page.pager() + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + pager = page.pager() assert_true( pager.startswith( '