diff --git a/ckan/tests/legacy/__init__.py b/ckan/tests/legacy/__init__.py index 72ee85e01b1..12471c1b1b9 100644 --- a/ckan/tests/legacy/__init__.py +++ b/ckan/tests/legacy/__init__.py @@ -33,6 +33,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 @@ -235,7 +236,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): @@ -422,6 +424,7 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs): params = json.dumps(kwargs) response = app.post('/api/3/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 4e2171c07d7..8b8121a63a1 100644 --- a/ckan/tests/legacy/functional/api/base.py +++ b/ckan/tests/legacy/functional/api/base.py @@ -188,7 +188,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 b90dcfa13f4..ac6b09742df 100644 --- a/ckan/tests/legacy/functional/api/model/test_package.py +++ b/ckan/tests/legacy/functional/api/model/test_package.py @@ -11,6 +11,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 @@ -63,7 +64,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) @@ -131,7 +132,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. @@ -157,7 +158,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'] @@ -171,7 +172,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'] @@ -185,7 +186,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() @@ -205,7 +206,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() @@ -217,7 +218,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], @@ -251,7 +252,7 @@ def test_register_post_indexerror(self): """ Test that we can't add a package if Solr is down. """ - bad_solr_url = 'http://127.0.0.1/badsolrurl' + bad_solr_url = 'http://example.com/badsolrurl' original_settings = SolrSettings.get()[0] try: SolrSettings.init(bad_solr_url) @@ -260,7 +261,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) @@ -271,7 +272,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 @@ -641,7 +642,7 @@ def test_entity_update_indexerror(self): """ Test that we can't update a package if Solr is down. """ - bad_solr_url = 'http://127.0.0.1/badsolrurl' + bad_solr_url = 'http://example.com/badsolrurl' original_settings = SolrSettings.get()[0] try: SolrSettings.init(bad_solr_url) @@ -712,8 +713,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 319de10e6aa..afeabb119f9 100644 --- a/ckan/tests/legacy/functional/api/model/test_relationships.py +++ b/ckan/tests/legacy/functional/api/model/test_relationships.py @@ -1,12 +1,12 @@ -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): @@ -113,7 +113,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) @@ -187,7 +187,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: @@ -218,7 +218,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 @@ -269,7 +269,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: @@ -298,7 +298,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/test_resource.py b/ckan/tests/legacy/functional/api/test_resource.py index e6ef1932263..a43b6f0dba8 100644 --- a/ckan/tests/legacy/functional/api/test_resource.py +++ b/ckan/tests/legacy/functional/api/test_resource.py @@ -43,7 +43,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 @@ -56,7 +56,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 48d2b6f53b0..2d2c3bdc35d 100644 --- a/ckan/tests/legacy/functional/api/test_user.py +++ b/ckan/tests/legacy/functional/api/test_user.py @@ -23,8 +23,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', }, @@ -33,11 +37,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', }, @@ -47,8 +55,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 diff --git a/ckan/tests/legacy/functional/api/test_util.py b/ckan/tests/legacy/functional/api/test_util.py index 9bb236a47e4..ff9ae79f65a 100644 --- a/ckan/tests/legacy/functional/api/test_util.py +++ b/ckan/tests/legacy/functional/api/test_util.py @@ -16,8 +16,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', @@ -25,10 +29,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', @@ -36,11 +44,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', @@ -48,10 +60,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', @@ -59,44 +75,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 af724cfef15..5513cdda3c1 100644 --- a/ckan/tests/legacy/functional/test_activity.py +++ b/ckan/tests/legacy/functional/test_activity.py @@ -2,7 +2,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 @@ -12,9 +12,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 @@ -27,7 +28,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): @@ -50,7 +50,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 @@ -237,7 +239,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 954ceaf7b1e..3648fc127b5 100644 --- a/ckan/tests/legacy/functional/test_admin.py +++ b/ckan/tests/legacy/functional/test_admin.py @@ -13,7 +13,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 c67152ab9c5..2934a3c0a0a 100644 --- a/ckan/tests/legacy/functional/test_group.py +++ b/ckan/tests/legacy/functional/test_group.py @@ -93,7 +93,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) @@ -129,8 +131,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' @@ -545,7 +562,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 @@ -580,7 +598,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'] @@ -595,7 +615,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'] @@ -610,13 +632,13 @@ def test_after_create_plugin_hook(self): plugins.unload('test_package_controller_plugin') def test_new_indexerror(self): - bad_solr_url = 'http://127.0.0.1/badsolrurl' + bad_solr_url = 'http://example.com/badsolrurl' solr_url = SolrSettings.get()[0] 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 @@ -631,7 +653,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) @@ -681,12 +705,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'}) @@ -716,7 +744,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 6958ad56251..a07e69bef69 100644 --- a/ckan/tests/legacy/functional/test_pagination.py +++ b/ckan/tests/legacy/functional/test_pagination.py @@ -57,25 +57,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) @@ -99,12 +115,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) @@ -128,12 +152,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 8d01acddaa2..891b36fb0a3 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 5cc1374d8a0..ec16753a0dd 100644 --- a/ckan/tests/legacy/functional/test_revision.py +++ b/ckan/tests/legacy/functional/test_revision.py @@ -88,7 +88,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 @@ -130,14 +132,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) @@ -31,13 +32,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) @@ -58,13 +59,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/misc/test_format_text.py b/ckan/tests/legacy/misc/test_format_text.py index 4450534b97c..b314ae15a17 100644 --- a/ckan/tests/legacy/misc/test_format_text.py +++ b/ckan/tests/legacy/misc/test_format_text.py @@ -1,6 +1,15 @@ import ckan.lib.helpers as h +from ckan.tests import helpers -class TestFormatText: + +class TestFormatText(object): + + @classmethod + def setup_class(cls): + + # h.render_markdown calls url_for internally, so we need to have + # an app to provide a request context + cls.app = helpers._get_test_app() def test_markdown(self): instr = '''# Hello World @@ -12,129 +21,169 @@ def test_markdown(self): exp = '''

Hello World

Some bolded text.

Some italicized text.

''' - out = h.render_markdown(instr) + + with self.app.flask_app.test_request_context(): + out = h.render_markdown(instr) assert out == exp, out def test_markdown_blank(self): instr = None - out = h.render_markdown(instr) + + with self.app.flask_app.test_request_context(): + out = h.render_markdown(instr) assert out == '' def test_evil_markdown(self): instr = 'Evil