From a41fd3bb15f77aaddc138cf62baaa0b2b9ee6d6e Mon Sep 17 00:00:00 2001 From: amercader Date: Thu, 2 Jun 2016 11:50:42 +0100 Subject: [PATCH] Fix tests using url_for The Flask url_for function requires an application context to be present when generating a url_for. This is obviuosly not the case when calling it from a test, so we need to wrap the function on a test_request_context: with app.flask_app.test_request_context(): url_for(...) --- ckan/tests/controllers/test_admin.py | 36 +- ckan/tests/controllers/test_api.py | 101 ++-- ckan/tests/controllers/test_feed.py | 21 +- ckan/tests/controllers/test_group.py | 249 ++++++--- ckan/tests/controllers/test_home.py | 49 +- ckan/tests/controllers/test_organization.py | 247 ++++++--- ckan/tests/controllers/test_package.py | 585 +++++++++++++------- ckan/tests/controllers/test_tags.py | 20 +- ckan/tests/controllers/test_user.py | 147 +++-- ckan/tests/controllers/test_util.py | 26 +- ckan/tests/lib/test_helpers.py | 122 ++-- ckan/tests/lib/test_mailer.py | 33 +- 12 files changed, 1079 insertions(+), 557 deletions(-) diff --git a/ckan/tests/controllers/test_admin.py b/ckan/tests/controllers/test_admin.py index 9cab3cd7c0f..6b16605f1ac 100644 --- a/ckan/tests/controllers/test_admin.py +++ b/ckan/tests/controllers/test_admin.py @@ -1,7 +1,7 @@ from nose.tools import assert_true, assert_equal from bs4 import BeautifulSoup -from routes import url_for +from ckan.lib.helpers import url_for from pylons import config import ckan.model as model @@ -17,8 +17,10 @@ def _get_admin_config_page(app): user = factories.Sysadmin() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='admin', action='config') response = app.get( - url=url_for(controller='admin', action='config'), + url=url, extra_environ=env, ) return env, response @@ -28,8 +30,10 @@ def _reset_config(app): '''Reset config via action''' user = factories.Sysadmin() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='admin', action='reset_config') app.post( - url=url_for(controller='admin', action='reset_config'), + url=url, extra_environ=env, ) @@ -260,9 +264,9 @@ class TestTrashView(helpers.FunctionalTestBase): def test_trash_view_anon_user(self): '''An anon user shouldn't be able to access trash view.''' app = self._get_test_app() - - trash_url = url_for(controller='admin', action='trash') - trash_response = app.get(trash_url, status=403) + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') + app.get(trash_url, status=403) def test_trash_view_normal_user(self): '''A normal logged in user shouldn't be able to access trash view.''' @@ -270,7 +274,9 @@ def test_trash_view_normal_user(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - trash_url = url_for(controller='admin', action='trash') + + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') trash_response = app.get(trash_url, extra_environ=env, status=403) assert_true('Need to be system administrator to administer' in trash_response) @@ -281,7 +287,9 @@ def test_trash_view_sysadmin(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - trash_url = url_for(controller='admin', action='trash') + + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') trash_response = app.get(trash_url, extra_environ=env, status=200) # On the purge page assert_true('form-purge-packages' in trash_response) @@ -294,7 +302,8 @@ def test_trash_no_datasets(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - trash_url = url_for(controller='admin', action='trash') + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') trash_response = app.get(trash_url, extra_environ=env, status=200) trash_response_html = BeautifulSoup(trash_response.body) @@ -313,7 +322,8 @@ def test_trash_with_deleted_datasets(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - trash_url = url_for(controller='admin', action='trash') + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') trash_response = app.get(trash_url, extra_environ=env, status=200) trash_response_html = BeautifulSoup(trash_response.body) @@ -336,7 +346,8 @@ def test_trash_purge_deleted_datasets(self): assert_equal(pkgs_before_purge, 3) env = {'REMOTE_USER': user['name'].encode('ascii')} - trash_url = url_for(controller='admin', action='trash') + with app.flask_app.test_request_context(): + trash_url = url_for(controller='admin', action='trash') trash_response = app.get(trash_url, extra_environ=env, status=200) # submit the purge form @@ -362,7 +373,8 @@ def _update_config_option(self): sysadmin = factories.Sysadmin() env = {'REMOTE_USER': sysadmin['name'].encode('ascii')} app = self._get_test_app() - url = url_for(controller='admin', action='config') + with app.flask_app.test_request_context(): + url = url_for(controller='admin', action='config') response = app.get(url=url, extra_environ=env) form = response.forms[1] diff --git a/ckan/tests/controllers/test_api.py b/ckan/tests/controllers/test_api.py index 258337a487a..cc785b38930 100644 --- a/ckan/tests/controllers/test_api.py +++ b/ckan/tests/controllers/test_api.py @@ -5,7 +5,7 @@ import json import re -from routes import url_for +from ckan.lib.helpers import url_for from nose.tools import assert_equal, eq_ import ckan.tests.helpers as helpers @@ -29,9 +29,12 @@ def test_unicode_in_error_message_works_ok(self): def test_dataset_autocomplete_name(self): dataset = factories.Dataset(name='rivers') - url = url_for(controller='api', action='dataset_autocomplete', ver='/2') - assert_equal(url, '/api/2/util/dataset/autocomplete') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='dataset_autocomplete', + ver='/2') + assert_equal(url, '/api/2/util/dataset/autocomplete') response = app.get( url=url, @@ -53,7 +56,11 @@ def test_dataset_autocomplete_name(self): def test_dataset_autocomplete_title(self): dataset = factories.Dataset(name='test_ri', title='Rivers') - url = url_for(controller='api', action='dataset_autocomplete', ver='/2') + + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='dataset_autocomplete', + ver='/2') assert_equal(url, '/api/2/util/dataset/autocomplete') app = self._get_test_app() @@ -77,9 +84,12 @@ def test_dataset_autocomplete_title(self): def test_tag_autocomplete(self): factories.Dataset(tags=[{'name': 'rivers'}]) - url = url_for(controller='api', action='tag_autocomplete', ver='/2') - assert_equal(url, '/api/2/util/tag/autocomplete') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='tag_autocomplete', + ver='/2') + assert_equal(url, '/api/2/util/tag/autocomplete') response = app.get( url=url, @@ -95,10 +105,12 @@ def test_tag_autocomplete(self): 'application/json;charset=utf-8') def test_group_autocomplete_by_name(self): - org = factories.Group(name='rivers', title='Bridges') - url = url_for(controller='api', action='group_autocomplete', ver='/2') - assert_equal(url, '/api/2/util/group/autocomplete') + factories.Group(name='rivers', title='Bridges') app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='group_autocomplete', + ver='/2') + assert_equal(url, '/api/2/util/group/autocomplete') response = app.get( url=url, @@ -116,9 +128,11 @@ def test_group_autocomplete_by_name(self): 'application/json;charset=utf-8') def test_group_autocomplete_by_title(self): - org = factories.Group(name='frogs', title='Bugs') - url = url_for(controller='api', action='group_autocomplete', ver='/2') + factories.Group(name='frogs', title='Bugs') app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='group_autocomplete', + ver='/2') response = app.get( url=url, @@ -134,9 +148,12 @@ def test_group_autocomplete_by_title(self): def test_organization_autocomplete_by_name(self): org = factories.Organization(name='simple-dummy-org') - url = url_for(controller='api', action='organization_autocomplete', ver='/2') - assert_equal(url, '/api/2/util/organization/autocomplete') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='organization_autocomplete', + ver='/2') + assert_equal(url, '/api/2/util/organization/autocomplete') response = app.get( url=url, @@ -154,9 +171,12 @@ def test_organization_autocomplete_by_name(self): 'application/json;charset=utf-8') def test_organization_autocomplete_by_title(self): - org = factories.Organization(title='Simple dummy org') - url = url_for(controller='api', action='organization_autocomplete', ver='/2') + factories.Organization(title='Simple dummy org') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='api', action='organization_autocomplete', + ver='/2') response = app.get( url=url, @@ -172,12 +192,14 @@ def test_organization_autocomplete_by_title(self): def test_config_option_list_access_sysadmin(self): user = factories.Sysadmin() - url = url_for( - controller='api', - action='action', - logic_function='config_option_list', - ver='/3') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for( + controller='api', + action='action', + logic_function='config_option_list', + ver='/3') app.get( url=url, @@ -188,12 +210,14 @@ def test_config_option_list_access_sysadmin(self): def test_config_option_list_access_sysadmin_jsonp(self): user = factories.Sysadmin() - url = url_for( - controller='api', - action='action', - logic_function='config_option_list', - ver='/3') + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for( + controller='api', + action='action', + logic_function='config_option_list', + ver='/3') app.get( url=url, @@ -207,12 +231,13 @@ def test_jsonp_works_on_get_requests(self): dataset1 = factories.Dataset() dataset2 = factories.Dataset() - url = url_for( - controller='api', - action='action', - logic_function='package_list', - ver='/3') app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for( + controller='api', + action='action', + logic_function='package_list', + ver='/3') res = app.get( url=url, params={'callback': 'my_callback'}, @@ -229,15 +254,15 @@ def test_jsonp_does_not_work_on_post_requests(self): dataset1 = factories.Dataset() dataset2 = factories.Dataset() - - url = url_for( - controller='api', - action='action', - logic_function='package_list', - ver='/3', - callback='my_callback', - ) app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for( + controller='api', + action='action', + logic_function='package_list', + ver='/3', + callback='my_callback', + ) res = app.post( url=url, ) diff --git a/ckan/tests/controllers/test_feed.py b/ckan/tests/controllers/test_feed.py index 2293ae5118a..17a48305e16 100644 --- a/ckan/tests/controllers/test_feed.py +++ b/ckan/tests/controllers/test_feed.py @@ -1,6 +1,5 @@ -from routes import url_for +from ckan.lib.helpers import url_for -from ckan import model import ckan.tests.helpers as helpers import ckan.tests.factories as factories @@ -9,24 +8,30 @@ class TestFeedNew(helpers.FunctionalTestBase): def test_atom_feed_page_zero_gives_error(self): group = factories.Group() - offset = url_for(controller='feed', action='group', - id=group['name']) + '?page=0' + app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='feed', action='group', + id=group['name']) + '?page=0' res = app.get(offset, status=400) assert '"page" parameter must be a positive integer' in res, res def test_atom_feed_page_negative_gives_error(self): group = factories.Group() - offset = url_for(controller='feed', action='group', - id=group['name']) + '?page=-2' + app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='feed', action='group', + id=group['name']) + '?page=-2' res = app.get(offset, status=400) assert '"page" parameter must be a positive integer' in res, res def test_atom_feed_page_not_int_gives_error(self): group = factories.Group() - offset = url_for(controller='feed', action='group', - id=group['name']) + '?page=abc' + app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='feed', action='group', + id=group['name']) + '?page=abc' res = app.get(offset, status=400) assert '"page" parameter must be a positive integer' in res, res diff --git a/ckan/tests/controllers/test_group.py b/ckan/tests/controllers/test_group.py index b80724dc5b2..d977d6218e2 100644 --- a/ckan/tests/controllers/test_group.py +++ b/ckan/tests/controllers/test_group.py @@ -1,7 +1,7 @@ from bs4 import BeautifulSoup from nose.tools import assert_equal, assert_true -from routes import url_for +from ckan.lib.helpers import url_for import ckan.tests.helpers as helpers import ckan.model as model @@ -18,17 +18,22 @@ def setup(self): model.repo.rebuild_db() def test_bulk_process_throws_404_for_nonexistent_org(self): + app = self._get_test_app() - bulk_process_url = url_for(controller='organization', - action='bulk_process', id='does-not-exist') + with app.flask_app.test_request_context(): + bulk_process_url = url_for( + controller='organization', action='bulk_process', + id='does-not-exist') app.get(url=bulk_process_url, status=404) def test_page_thru_list_of_orgs_preserves_sort_order(self): orgs = [factories.Organization() for _ in range(35)] app = self._get_test_app() - org_url = url_for(controller='organization', - action='index', - sort='name desc') + + with app.flask_app.test_request_context(): + org_url = url_for(controller='organization', + action='index', + sort='name desc') response = app.get(url=org_url) assert orgs[-1]['name'] in response assert orgs[0]['name'] not in response @@ -40,9 +45,11 @@ def test_page_thru_list_of_orgs_preserves_sort_order(self): def test_page_thru_list_of_groups_preserves_sort_order(self): groups = [factories.Group() for _ in range(35)] app = self._get_test_app() - group_url = url_for(controller='group', - action='index', - sort='title desc') + + with app.flask_app.test_request_context(): + group_url = url_for(controller='group', + action='index', + sort='title desc') response = app.get(url=group_url) assert groups[-1]['title'] in response @@ -56,8 +63,11 @@ def test_page_thru_list_of_groups_preserves_sort_order(self): def _get_group_new_page(app): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='new') response = app.get( - url=url_for(controller='group', action='new'), + url=url, extra_environ=env, ) return env, response @@ -66,7 +76,9 @@ def _get_group_new_page(app): class TestGroupControllerNew(helpers.FunctionalTestBase): def test_not_logged_in(self): app = self._get_test_app() - app.get(url=url_for(controller='group', action='new'), + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='new') + app.get(url=url, status=403) def test_form_renders(self): @@ -116,9 +128,11 @@ def _get_group_edit_page(app, group_name=None): group = factories.Group(user=user) group_name = group['name'] env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for(controller='group', - action='edit', - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='edit', + id=group_name) response = app.get(url=url, extra_environ=env) return env, response, group_name @@ -126,16 +140,21 @@ def _get_group_edit_page(app, group_name=None): class TestGroupControllerEdit(helpers.FunctionalTestBase): def test_not_logged_in(self): app = self._get_test_app() - app.get(url=url_for(controller='group', action='new'), + + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='new') + app.get(url=url, status=403) def test_group_doesnt_exist(self): app = self._get_test_app() user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for(controller='group', - action='edit', - id='doesnt_exist') + + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='edit', + id='doesnt_exist') app.get(url=url, extra_environ=env, status=404) @@ -172,17 +191,19 @@ def test_all_fields_saved(self): class TestGroupRead(helpers.FunctionalTestBase): def setup(self): super(TestGroupRead, self).setup() - self.app = helpers._get_test_app() self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} self.group = factories.Group(user=self.user) def test_group_read(self): - response = self.app.get(url=url_for(controller='group', - action='read', - id=self.group['id']), - status=200, - extra_environ=self.user_env) + + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='read', + id=self.group['id']) + response = app.get(url=url, + status=200, + extra_environ=self.user_env) assert_in(self.group['title'], response) assert_in(self.group['description'], response) @@ -196,53 +217,69 @@ def setup(self): self.group = factories.Group(user=self.user) def test_owner_delete(self): - response = self.app.get(url=url_for(controller='group', - action='delete', - id=self.group['id']), - status=200, - extra_environ=self.user_env) + + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='delete', + id=self.group['id']) + + response = app.get(url=url, + status=200, + extra_environ=self.user_env) form = response.forms['group-confirm-delete-form'] - response = submit_and_follow(self.app, form, name='delete', + response = submit_and_follow(app, form, name='delete', extra_environ=self.user_env) group = helpers.call_action('group_show', id=self.group['id']) assert_equal(group['state'], 'deleted') def test_sysadmin_delete(self): + + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='delete', + id=self.group['id']) + sysadmin = factories.Sysadmin() extra_environ = {'REMOTE_USER': sysadmin['name'].encode('ascii')} - response = self.app.get(url=url_for(controller='group', - action='delete', - id=self.group['id']), - status=200, - extra_environ=extra_environ) + response = app.get(url=url, + status=200, + extra_environ=extra_environ) form = response.forms['group-confirm-delete-form'] - response = submit_and_follow(self.app, form, name='delete', + response = submit_and_follow(app, form, name='delete', extra_environ=self.user_env) group = helpers.call_action('group_show', id=self.group['id']) assert_equal(group['state'], 'deleted') def test_non_authorized_user_trying_to_delete_fails(self): + + app = self._get_test_app() user = factories.User() extra_environ = {'REMOTE_USER': user['name'].encode('ascii')} - self.app.get(url=url_for(controller='group', - action='delete', - id=self.group['id']), - status=403, - extra_environ=extra_environ) + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='delete', + id=self.group['id']) + + app.get(url=url, + status=403, + extra_environ=extra_environ) group = helpers.call_action('group_show', id=self.group['id']) assert_equal(group['state'], 'active') def test_anon_user_trying_to_delete_fails(self): - self.app.get(url=url_for(controller='group', - action='delete', - id=self.group['id']), - status=403) + + app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='delete', + id=self.group['id']) + + app.get(url=url, + status=403) group = helpers.call_action('group_show', id=self.group['id']) @@ -263,9 +300,11 @@ def _create_group(self, owner_username, users=None): def _get_group_add_member_page(self, app, user, group_name): env = {'REMOTE_USER': user['name'].encode('ascii')} - url = url_for(controller='group', - action='member_new', - id=group_name) + + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='member_new', + id=group_name) response = app.get(url=url, extra_environ=env) return env, response @@ -281,8 +320,9 @@ def test_membership_list(self): group = self._create_group(user_one['name'], other_users) - member_list_url = url_for(controller='group', action='members', - id=group['id']) + with app.flask_app.test_request_context(): + member_list_url = url_for(controller='group', action='members', + id=group['id']) member_list_response = app.get(member_list_url) assert_true('2 members' in member_list_response) @@ -368,8 +408,9 @@ def test_remove_member(self): group = self._create_group(user_one['name'], other_users) - remove_url = url_for(controller='group', action='member_delete', - user=user_two['id'], id=group['id']) + with app.flask_app.test_request_context(): + remove_url = url_for(controller='group', action='member_delete', + user=user_two['id'], id=group['id']) env = {'REMOTE_USER': user_one['name'].encode('ascii')} remove_response = app.post(remove_url, extra_environ=env, status=302) @@ -401,9 +442,11 @@ def test_group_follow(self): group = factories.Group() env = {'REMOTE_USER': user['name'].encode('ascii')} - follow_url = url_for(controller='group', - action='follow', - id=group['id']) + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='group', + action='follow', + id=group['id']) response = app.post(follow_url, extra_environ=env, status=302) response = response.follow() assert_true('You are now following {0}' @@ -417,9 +460,11 @@ def test_group_follow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='group', - action='follow', - id='not-here') + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='group', + action='follow', + id='not-here') response = app.post(follow_url, extra_environ=env, status=404) assert_true('Group not found' in response) @@ -430,13 +475,16 @@ def test_group_unfollow(self): group = factories.Group() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='group', - action='follow', - id=group['id']) + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='group', + action='follow', + id=group['id']) + unfollow_url = url_for(controller='group', action='unfollow', + id=group['id']) + app.post(follow_url, extra_environ=env, status=302) - unfollow_url = url_for(controller='group', action='unfollow', - id=group['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) unfollow_response = unfollow_response.follow() @@ -453,8 +501,10 @@ def test_group_unfollow_not_following(self): group = factories.Group() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='group', action='unfollow', - id=group['id']) + + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='group', action='unfollow', + id=group['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) unfollow_response = unfollow_response.follow() @@ -469,8 +519,10 @@ def test_group_unfollow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='group', action='unfollow', - id='not-here') + + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='group', action='unfollow', + id='not-here') unfollow_response = app.post(unfollow_url, extra_environ=env, status=404) assert_true('Group not found' in unfollow_response) @@ -483,13 +535,15 @@ def test_group_follower_list(self): group = factories.Group() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='group', - action='follow', - id=group['id']) - app.post(follow_url, extra_environ=env, status=302) - followers_url = url_for(controller='group', action='followers', - id=group['id']) + with app.flask_app.test_request_context(): + follow_url = url_for(controller='group', + action='follow', + id=group['id']) + followers_url = url_for(controller='group', action='followers', + id=group['id']) + + app.post(follow_url, extra_environ=env, status=302) # Only sysadmins can view the followers list pages followers_response = app.get(followers_url, extra_environ=env, @@ -503,17 +557,20 @@ class TestGroupSearch(helpers.FunctionalTestBase): def setup(self): super(TestGroupSearch, self).setup() - self.app = self._get_test_app() factories.Group(name='grp-one', title='AGrp One') factories.Group(name='grp-two', title='AGrp Two') factories.Group(name='grp-three', title='Grp Three') - self.search_url = url_for(controller='group', action='index') + + app = self._get_test_app() + with app.flask_app.test_request_context(): + self.search_url = url_for(controller='group', action='index') def test_group_search(self): '''Requesting group search (index) returns list of groups and search form.''' - index_response = self.app.get(self.search_url) + app = self._get_test_app() + index_response = app.get(self.search_url) index_response_html = BeautifulSoup(index_response.body) grp_names = index_response_html.select('ul.media-grid ' 'li.media-item ' @@ -528,7 +585,8 @@ def test_group_search(self): def test_group_search_results(self): '''Searching via group search form returns list of expected groups.''' - index_response = self.app.get(self.search_url) + app = self._get_test_app() + index_response = app.get(self.search_url) search_form = index_response.forms['group-search-form'] search_form['q'] = 'AGrp' search_response = webtest_submit(search_form) @@ -547,7 +605,8 @@ def test_group_search_results(self): def test_group_search_no_results(self): '''Searching with a term that doesn't apply returns no results.''' - index_response = self.app.get(self.search_url) + app = self._get_test_app() + index_response = app.get(self.search_url) search_form = index_response.forms['group-search-form'] search_form['q'] = 'No Results Here' search_response = webtest_submit(search_form) @@ -579,8 +638,9 @@ def test_group_search_within_org(self): factories.Dataset(name="ds-three", title="Dataset Three", groups=[{'id': grp['id']}]) - grp_url = url_for(controller='group', action='read', - id=grp['id']) + with app.flask_app.test_request_context(): + grp_url = url_for(controller='group', action='read', + id=grp['id']) grp_response = app.get(grp_url) grp_response_html = BeautifulSoup(grp_response.body) @@ -607,8 +667,9 @@ def test_group_search_within_org_results(self): factories.Dataset(name="ds-three", title="Dataset Three", groups=[{'id': grp['id']}]) - grp_url = url_for(controller='group', action='read', - id=grp['id']) + with app.flask_app.test_request_context(): + grp_url = url_for(controller='group', action='read', + id=grp['id']) grp_response = app.get(grp_url) search_form = grp_response.forms['group-datasets-search-form'] search_form['q'] = 'One' @@ -640,8 +701,9 @@ def test_group_search_within_org_no_results(self): factories.Dataset(name="ds-three", title="Dataset Three", groups=[{'id': grp['id']}]) - grp_url = url_for(controller='group', action='read', - id=grp['id']) + with app.flask_app.test_request_context(): + grp_url = url_for(controller='group', action='read', + id=grp['id']) grp_response = app.get(grp_url) search_form = grp_response.forms['group-datasets-search-form'] search_form['q'] = 'Nout' @@ -670,8 +732,9 @@ def test_group_index(self): name='test-group-{0}'.format(_i), title='Test Group {0}'.format(_i)) - url = url_for(controller='group', - action='index') + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='index') response = app.get(url) for i in xrange(1, 22): @@ -680,9 +743,10 @@ def test_group_index(self): assert 'Test Group 22' not in response - url = url_for(controller='group', - action='index', - page=1) + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='index', + page=1) response = app.get(url) for i in xrange(1, 22): @@ -691,9 +755,10 @@ def test_group_index(self): assert 'Test Group 22' not in response - url = url_for(controller='group', - action='index', - page=2) + with app.flask_app.test_request_context(): + url = url_for(controller='group', + action='index', + page=2) response = app.get(url) for i in xrange(22, 26): diff --git a/ckan/tests/controllers/test_home.py b/ckan/tests/controllers/test_home.py index 6bb6d3b28fc..061edd2f19f 100644 --- a/ckan/tests/controllers/test_home.py +++ b/ckan/tests/controllers/test_home.py @@ -11,7 +11,11 @@ class TestHome(helpers.FunctionalTestBase): def test_home_renders(self): app = self._get_test_app() - response = app.get(url_for('home')) + + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url) assert 'Welcome to CKAN' in response.body def test_template_head_end(self): @@ -19,14 +23,22 @@ def test_template_head_end(self): # test-core.ini sets ckan.template_head_end to this: test_link = '' - response = app.get(url_for('home')) + + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url) assert test_link in response.body def test_template_footer_end(self): app = self._get_test_app() # test-core.ini sets ckan.template_footer_end to this: test_html = 'TEST TEMPLATE_FOOTER_END TEST' - response = app.get(url_for('home')) + + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url) assert test_html in response.body def test_email_address_nag(self): @@ -40,10 +52,14 @@ def test_email_address_nag(self): model.Session.commit() env = {'REMOTE_USER': user.name.encode('ascii')} - response = app.get(url=url_for('home'), extra_environ=env) + with app.flask_app.test_request_context(): + url = url_for('home') + user_edit_url = url_for(controller='user', action='edit') + + response = app.get(url, extra_environ=env) assert 'update your profile' in response.body - assert url_for(controller='user', action='edit') in response.body + assert user_edit_url in response.body assert ' and add your email address.' in response.body def test_email_address_no_nag(self): @@ -51,7 +67,10 @@ def test_email_address_no_nag(self): user = factories.User(email='filled_in@nicely.com') env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get(url=url_for('home'), extra_environ=env) + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url, extra_environ=env) assert 'add your email address' not in response @@ -60,7 +79,11 @@ class TestI18nURLs(helpers.FunctionalTestBase): def test_right_urls_are_rendered_on_language_selector(self): app = self._get_test_app() - response = app.get(url_for('home')) + + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url) html = BeautifulSoup(response.body) select = html.find(id='field-lang-select') @@ -76,7 +99,11 @@ def test_right_urls_are_rendered_on_language_selector(self): def test_default_english_option_is_selected_on_language_selector(self): app = self._get_test_app() - response = app.get(url_for('home')) + + with app.flask_app.test_request_context(): + url = url_for('home') + + response = app.get(url) html = BeautifulSoup(response.body) select = html.find(id='field-lang-select') @@ -88,7 +115,11 @@ def test_default_english_option_is_selected_on_language_selector(self): def test_right_option_is_selected_on_language_selector(self): app = self._get_test_app() - response = app.get(url_for('home', locale='ca')) + + with app.flask_app.test_request_context(): + url = url_for('home', locale='ca') + + response = app.get(url) html = BeautifulSoup(response.body) select = html.find(id='field-lang-select') diff --git a/ckan/tests/controllers/test_organization.py b/ckan/tests/controllers/test_organization.py index 549a92aa9f1..118a86d0796 100644 --- a/ckan/tests/controllers/test_organization.py +++ b/ckan/tests/controllers/test_organization.py @@ -1,6 +1,6 @@ from bs4 import BeautifulSoup from nose.tools import assert_equal, assert_true -from routes import url_for +from ckan.lib.helpers import url_for from mock import patch from ckan.tests import factories, helpers @@ -10,19 +10,29 @@ class TestOrganizationNew(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationNew, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} - self.organization_new_url = url_for(controller='organization', - action='new') + + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + self.organization_new_url = url_for(controller='organization', + action='new') def test_not_logged_in(self): - self.app.get(url=url_for(controller='group', action='new'), - status=403) + + app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='group', action='new') + app.get(url, status=403) def test_name_required(self): - response = self.app.get(url=self.organization_new_url, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + response = app.get(url=self.organization_new_url, + extra_environ=self.user_env) form = response.forms['organization-edit-form'] response = webtest_submit(form, name='save', extra_environ=self.user_env) @@ -31,13 +41,16 @@ def test_name_required(self): assert_true('Name: Missing value' in response) def test_saved(self): - response = self.app.get(url=self.organization_new_url, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + response = app.get(url=self.organization_new_url, + extra_environ=self.user_env) form = response.forms['organization-edit-form'] form['name'] = u'saved' - response = submit_and_follow(self.app, form, name='save', + response = submit_and_follow(app, form, name='save', extra_environ=self.user_env) group = helpers.call_action('organization_show', id='saved') assert_equal(group['title'], u'') @@ -45,6 +58,7 @@ def test_saved(self): assert_equal(group['state'], 'active') def test_all_fields_saved(self): + app = helpers._get_test_app() response = app.get(url=self.organization_new_url, extra_environ=self.user_env) @@ -55,7 +69,7 @@ def test_all_fields_saved(self): form['description'] = 'Sciencey datasets' form['image_url'] = 'http://example.com/image.png' - response = submit_and_follow(self.app, form, name='save', + response = submit_and_follow(app, form, name='save', extra_environ=self.user_env) group = helpers.call_action('organization_show', id='all-fields-saved') assert_equal(group['title'], u'Science') @@ -65,33 +79,44 @@ def test_all_fields_saved(self): class TestOrganizationList(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationList, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} - self.organization_list_url = url_for(controller='organization', - action='index') + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + self.organization_list_url = url_for(controller='organization', + action='index') + + @patch('ckan.logic.auth.get.organization_list', + return_value={'success': False}) + def test_error_message_shown_when_no_organization_list_permission( + self, mock_check_access): - @patch('ckan.logic.auth.get.organization_list', return_value={'success': False}) - def test_error_message_shown_when_no_organization_list_permission(self, mock_check_access): - response = self.app.get(url=self.organization_list_url, - extra_environ=self.user_env, - status=403) + app = helpers._get_test_app() + + app.get(url=self.organization_list_url, + extra_environ=self.user_env, + status=403) class TestOrganizationRead(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationRead, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} self.organization = factories.Organization(user=self.user) def test_organization_read(self): - response = self.app.get(url=url_for(controller='organization', - action='read', - id=self.organization['id']), - status=200, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='organization', action='read', + id=self.organization['id']) + response = app.get(url=url, + status=200, + extra_environ=self.user_env) assert_in(self.organization['title'], response) assert_in(self.organization['description'], response) @@ -99,24 +124,33 @@ def test_organization_read(self): class TestOrganizationEdit(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationEdit, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} self.organization = factories.Organization(user=self.user) - self.organization_edit_url = url_for(controller='organization', - action='edit', - id=self.organization['id']) + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + self.organization_edit_url = url_for(controller='organization', + action='edit', + id=self.organization['id']) def test_group_doesnt_exist(self): - url = url_for(controller='organization', - action='edit', - id='doesnt_exist') - self.app.get(url=url, extra_environ=self.user_env, - status=404) + + app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='organization', + action='edit', + id='doesnt_exist') + app.get(url=url, extra_environ=self.user_env, + status=404) def test_saved(self): - response = self.app.get(url=self.organization_edit_url, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + response = app.get(url=self.organization_edit_url, + extra_environ=self.user_env) form = response.forms['organization-edit-form'] response = webtest_submit(form, name='save', @@ -128,8 +162,11 @@ def test_saved(self): assert_equal(group['state'], 'active') def test_all_fields_saved(self): - response = self.app.get(url=self.organization_edit_url, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + response = app.get(url=self.organization_edit_url, + extra_environ=self.user_env) form = response.forms['organization-edit-form'] form['name'] = u'all-fields-edited' @@ -149,59 +186,79 @@ def test_all_fields_saved(self): class TestOrganizationDelete(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationDelete, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} self.organization = factories.Organization(user=self.user) def test_owner_delete(self): - response = self.app.get(url=url_for(controller='organization', - action='delete', - id=self.organization['id']), - status=200, - extra_environ=self.user_env) + + app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='organization', + action='delete', + id=self.organization['id']) + response = app.get(url, status=200, extra_environ=self.user_env) form = response.forms['organization-confirm-delete-form'] - response = submit_and_follow(self.app, form, name='delete', + response = submit_and_follow(app, form, name='delete', extra_environ=self.user_env) organization = helpers.call_action('organization_show', id=self.organization['id']) assert_equal(organization['state'], 'deleted') def test_sysadmin_delete(self): + + app = helpers._get_test_app() + sysadmin = factories.Sysadmin() extra_environ = {'REMOTE_USER': sysadmin['name'].encode('ascii')} - response = self.app.get(url=url_for(controller='organization', - action='delete', - id=self.organization['id']), - status=200, - extra_environ=extra_environ) + with app.flask_app.test_request_context(): + url = url_for(controller='organization', + action='delete', + id=self.organization['id']) + + response = app.get(url=url, + status=200, + extra_environ=extra_environ) form = response.forms['organization-confirm-delete-form'] - response = submit_and_follow(self.app, form, name='delete', + response = submit_and_follow(app, form, name='delete', extra_environ=self.user_env) organization = helpers.call_action('organization_show', id=self.organization['id']) assert_equal(organization['state'], 'deleted') def test_non_authorized_user_trying_to_delete_fails(self): + + app = helpers._get_test_app() + user = factories.User() extra_environ = {'REMOTE_USER': user['name'].encode('ascii')} - self.app.get(url=url_for(controller='organization', - action='delete', - id=self.organization['id']), - status=403, - extra_environ=extra_environ) + with app.flask_app.test_request_context(): + url = url_for(controller='organization', + action='delete', + id=self.organization['id']) + + app.get(url=url, + status=403, + extra_environ=extra_environ) organization = helpers.call_action('organization_show', id=self.organization['id']) assert_equal(organization['state'], 'active') def test_anon_user_trying_to_delete_fails(self): - self.app.get(url=url_for(controller='organization', - action='delete', - id=self.organization['id']), - status=403) + + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='organization', + action='delete', + id=self.organization['id']) + + app.get(url=url, + status=403) organization = helpers.call_action('organization_show', id=self.organization['id']) @@ -211,19 +268,24 @@ def test_anon_user_trying_to_delete_fails(self): class TestOrganizationBulkProcess(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationBulkProcess, self).setup() - self.app = helpers._get_test_app() + self.user = factories.User() self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')} self.organization = factories.Organization(user=self.user) - self.organization_bulk_url = url_for(controller='organization', - action='bulk_process', - id=self.organization['id']) + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + self.organization_bulk_url = url_for(controller='organization', + action='bulk_process', + id=self.organization['id']) def test_make_private(self): + + app = helpers._get_test_app() + datasets = [factories.Dataset(owner_org=self.organization['id']) for i in range(0, 5)] - response = self.app.get(url=self.organization_bulk_url, - extra_environ=self.user_env) + response = app.get(url=self.organization_bulk_url, + extra_environ=self.user_env) form = response.forms[1] for v in form.fields.values(): try: @@ -239,11 +301,14 @@ def test_make_private(self): assert_equal(d['private'], True) def test_make_public(self): + + app = helpers._get_test_app() + datasets = [factories.Dataset(owner_org=self.organization['id'], private=True) for i in range(0, 5)] - response = self.app.get(url=self.organization_bulk_url, - extra_environ=self.user_env) + response = app.get(url=self.organization_bulk_url, + extra_environ=self.user_env) form = response.forms[1] for v in form.fields.values(): try: @@ -259,11 +324,14 @@ def test_make_public(self): assert_equal(d['private'], False) def test_delete(self): + + app = helpers._get_test_app() + datasets = [factories.Dataset(owner_org=self.organization['id'], private=True) for i in range(0, 5)] - response = self.app.get(url=self.organization_bulk_url, - extra_environ=self.user_env) + response = app.get(url=self.organization_bulk_url, + extra_environ=self.user_env) form = response.forms[1] for v in form.fields.values(): try: @@ -285,17 +353,23 @@ class TestOrganizationSearch(helpers.FunctionalTestBase): def setup(self): super(TestOrganizationSearch, self).setup() - self.app = self._get_test_app() factories.Organization(name='org-one', title='AOrg One') factories.Organization(name='org-two', title='AOrg Two') factories.Organization(name='org-three', title='Org Three') - self.search_url = url_for(controller='organization', action='index') + + app = helpers._get_test_app() + with app.flask_app.test_request_context(): + self.search_url = url_for(controller='organization', + action='index') def test_organization_search(self): + + app = helpers._get_test_app() + '''Requesting organization search (index) returns list of organizations and search form.''' - index_response = self.app.get(self.search_url) + index_response = app.get(self.search_url) index_response_html = BeautifulSoup(index_response.body) org_names = index_response_html.select('ul.media-grid ' 'li.media-item ' @@ -308,10 +382,13 @@ def test_organization_search(self): assert_true('Org Three' in org_names) def test_organization_search_results(self): + + app = helpers._get_test_app() + '''Searching via organization search form returns list of expected organizations.''' - index_response = self.app.get(self.search_url) + index_response = app.get(self.search_url) search_form = index_response.forms['organization-search-form'] search_form['q'] = 'AOrg' search_response = webtest_submit(search_form) @@ -328,9 +405,12 @@ def test_organization_search_results(self): assert_true('Org Three' not in org_names) def test_organization_search_no_results(self): + + app = helpers._get_test_app() + '''Searching with a term that doesn't apply returns no results.''' - index_response = self.app.get(self.search_url) + index_response = app.get(self.search_url) search_form = index_response.forms['organization-search-form'] search_form['q'] = 'No Results Here' search_response = webtest_submit(search_form) @@ -363,8 +443,9 @@ def test_organization_search_within_org(self): factories.Dataset(name="ds-three", title="Dataset Three", owner_org=org['id']) - org_url = url_for(controller='organization', action='read', - id=org['id']) + with app.flask_app.test_request_context(): + org_url = url_for(controller='organization', action='read', + id=org['id']) org_response = app.get(org_url) org_response_html = BeautifulSoup(org_response.body) @@ -392,8 +473,9 @@ def test_organization_search_within_org_results(self): factories.Dataset(name="ds-three", title="Dataset Three", owner_org=org['id']) - org_url = url_for(controller='organization', action='read', - id=org['id']) + with app.flask_app.test_request_context(): + org_url = url_for(controller='organization', action='read', + id=org['id']) org_response = app.get(org_url) search_form = org_response.forms['organization-datasets-search-form'] search_form['q'] = 'One' @@ -425,8 +507,9 @@ def test_organization_search_within_org_no_results(self): factories.Dataset(name="ds-three", title="Dataset Three", owner_org=org['id']) - org_url = url_for(controller='organization', action='read', - id=org['id']) + with app.flask_app.test_request_context(): + org_url = url_for(controller='organization', action='read', + id=org['id']) org_response = app.get(org_url) search_form = org_response.forms['organization-datasets-search-form'] search_form['q'] = 'Nout' diff --git a/ckan/tests/controllers/test_package.py b/ckan/tests/controllers/test_package.py index dd4524747ab..976af70bf07 100644 --- a/ckan/tests/controllers/test_package.py +++ b/ckan/tests/controllers/test_package.py @@ -6,7 +6,7 @@ assert_true, ) -from routes import url_for +from ckan.lib.helpers import url_for import ckan.model as model import ckan.plugins as p @@ -17,7 +17,6 @@ from ckan.tests.helpers import assert_in -assert_in = helpers.assert_in webtest_submit = helpers.webtest_submit submit_and_follow = helpers.submit_and_follow @@ -25,8 +24,10 @@ def _get_package_new_page(app): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') response = app.get( - url=url_for(controller='package', action='new'), + url=url, extra_environ=env, ) return env, response @@ -196,8 +197,11 @@ def test_dataset_edit_org_dropdown_visible_to_normal_user_with_orgs_available(se app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') response = app.get( - url=url_for(controller='package', action='new'), + url=url, extra_environ=env, ) @@ -218,9 +222,11 @@ def test_dataset_edit_org_dropdown_visible_to_normal_user_with_orgs_available(se assert_equal(pkg.state, 'active') # edit package page response - url = url_for(controller='package', - action='edit', - id=pkg.id) + + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=pkg.id) pkg_edit_response = app.get(url=url, extra_environ=env) # A field with the correct id is in the response form = pkg_edit_response.forms['dataset-edit'] @@ -241,8 +247,10 @@ def test_dataset_edit_org_dropdown_normal_user_can_remove_org(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') response = app.get( - url=url_for(controller='package', action='new'), + url=url, extra_environ=env, ) @@ -262,9 +270,10 @@ def test_dataset_edit_org_dropdown_normal_user_can_remove_org(self): assert_not_equal(pkg.owner_org, None) # edit package page response - url = url_for(controller='package', - action='edit', - id=pkg.id) + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=pkg.id) pkg_edit_response = app.get(url=url, extra_environ=env) # edit dataset @@ -287,8 +296,11 @@ def test_dataset_edit_org_dropdown_not_visible_to_normal_user_with_no_orgs_avail app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') response = app.get( - url=url_for(controller='package', action='new'), + url=url, extra_environ=env, ) @@ -308,9 +320,10 @@ def test_dataset_edit_org_dropdown_not_visible_to_normal_user_with_no_orgs_avail assert_equal(pkg.state, 'active') # edit package response - url = url_for(controller='package', - action='edit', - id=model.Package.by_name(u'my-dataset').id) + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=model.Package.by_name(u'my-dataset').id) pkg_edit_response = app.get(url=url, extra_environ=env) # A field with the correct id is in the response form = pkg_edit_response.forms['dataset-edit'] @@ -332,8 +345,10 @@ def test_dataset_edit_org_dropdown_visible_to_sysadmin_with_no_orgs_available(se app = self._get_test_app() # user in env is sysadmin env = {'REMOTE_USER': sysadmin['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') response = app.get( - url=url_for(controller='package', action='new'), + url=url, extra_environ=env, ) @@ -354,9 +369,10 @@ def test_dataset_edit_org_dropdown_visible_to_sysadmin_with_no_orgs_available(se assert_equal(pkg.state, 'active') # edit package page response - url = url_for(controller='package', - action='edit', - id=pkg.id) + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=pkg.id) pkg_edit_response = app.get(url=url, extra_environ=env) # A field with the correct id is in the response assert 'id="field-organizations"' in pkg_edit_response @@ -368,9 +384,13 @@ def test_unauthed_user_creating_dataset(self): # provide REMOTE_ADDR to idenfity as remote user, see # BaseController._identify_user() for details - response = app.post(url=url_for(controller='package', action='new'), - extra_environ={'REMOTE_ADDR': '127.0.0.1'}, - status=403) + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='new') + app.post( + url=url, + extra_environ={'REMOTE_ADDR': '127.0.0.1'}, + status=403 + ) class TestPackageEdit(helpers.FunctionalTestBase): @@ -388,10 +408,13 @@ def test_organization_admin_can_edit(self): dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + url, extra_environ=env, ) form = response.forms['dataset-edit'] @@ -409,10 +432,12 @@ def test_organization_editor_can_edit(self): dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + url, extra_environ=env, ) form = response.forms['dataset-edit'] @@ -430,10 +455,12 @@ def test_organization_member_cannot_edit(self): dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) + app.get( + url, extra_environ=env, status=403, ) @@ -444,19 +471,20 @@ def test_user_not_in_organization_cannot_edit(self): dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) + app.get( + url, extra_environ=env, status=403, ) env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.post( - url_for(controller='package', - action='edit', - id=dataset['name']), + + app.post( + url, {'notes': 'edited description'}, extra_environ=env, status=403, @@ -466,17 +494,17 @@ def test_anonymous_user_cannot_edit(self): organization = factories.Organization() dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() - response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) + app.get( + url, status=403, ) - response = app.post( - url_for(controller='package', - action='edit', - id=dataset['name']), + app.post( + url, {'notes': 'edited description'}, status=403, ) @@ -490,10 +518,13 @@ def test_validation_errors_for_dataset_name_appear(self): dataset = factories.Dataset(owner_org=organization['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id=dataset['name']) + response = app.get( - url_for(controller='package', - action='edit', - id=dataset['name']), + url, extra_environ=env, ) form = response.forms['dataset-edit'] @@ -508,10 +539,13 @@ def test_edit_a_dataset_that_does_not_exist_404s(self): user = factories.User() app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='edit', + id='does-not-exist') + response = app.get( - url_for(controller='package', - action='edit', - id='does-not-exist'), + url, extra_environ=env, expect_errors=True ) @@ -530,8 +564,11 @@ def setup(self): def test_read(self): dataset = factories.Dataset() app = helpers._get_test_app() - response = app.get(url_for(controller='package', action='read', - id=dataset['name'])) + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='read', + id=dataset['name']) + response = app.get(url) response.mustcontain('Test Dataset') response.mustcontain('Just another test dataset') @@ -555,13 +592,13 @@ def test_organization_members_can_read_private_datasets(self): ) app = helpers._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='read', + id=dataset['name']) for user, user_dict in members.items(): response = app.get( - url_for( - controller='package', - action='read', - id=dataset['name'] - ), + url, extra_environ={ 'REMOTE_USER': user_dict['name'].encode('ascii'), }, @@ -576,8 +613,13 @@ def test_anonymous_users_cannot_read_private_datasets(self): private=True, ) app = helpers._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='read', + id=dataset['name']) + response = app.get( - url_for(controller='package', action='read', id=dataset['name']), + url, status=404 ) assert_equal(404, response.status_int) @@ -590,8 +632,13 @@ def test_user_not_in_organization_cannot_read_private_datasets(self): private=True, ) app = helpers._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='read', + id=dataset['name']) + response = app.get( - url_for(controller='package', action='read', id=dataset['name']), + url, extra_environ={'REMOTE_USER': user['name'].encode('ascii')}, status=404 ) @@ -601,18 +648,20 @@ def test_read_rdf(self): ''' The RDF outputs now live in ckanext-dcat''' dataset1 = factories.Dataset() - offset = url_for(controller='package', action='read', - id=dataset1['name']) + ".rdf" app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='read', + id=dataset1['name']) + ".rdf" app.get(offset, status=404) def test_read_n3(self): ''' The RDF outputs now live in ckanext-dcat''' dataset1 = factories.Dataset() - offset = url_for(controller='package', action='read', - id=dataset1['name']) + ".n3" app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='read', + id=dataset1['name']) + ".n3" app.get(offset, status=404) @@ -626,8 +675,12 @@ def test_owner_delete(self): app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', id=dataset['name']) + response = app.post( - url_for(controller='package', action='delete', id=dataset['name']), + url, extra_environ=env, ) response = response.follow() @@ -638,9 +691,13 @@ def test_owner_delete(self): def test_delete_on_non_existing_dataset(self): app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', + id='schrodingersdatset') + response = app.post( - url_for(controller='package', action='delete', - id='schrodingersdatset'), + url, expect_errors=True, ) assert_equal(404, response.status_int) @@ -653,8 +710,11 @@ def test_sysadmin_can_delete_any_dataset(self): user = factories.Sysadmin() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', id=dataset['name']) + response = app.post( - url_for(controller='package', action='delete', id=dataset['name']), + url, extra_environ=env, ) response = response.follow() @@ -671,8 +731,12 @@ def test_anon_user_cannot_delete_owned_dataset(self): dataset = factories.Dataset(owner_org=owner_org['id']) app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', id=dataset['name']) + response = app.post( - url_for(controller='package', action='delete', id=dataset['name']), + url, status=403, ) response.mustcontain('Unauthorized to delete package') @@ -690,8 +754,12 @@ def test_logged_in_user_cannot_delete_owned_dataset(self): app = helpers._get_test_app() user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', id=dataset['name']) + response = app.post( - url_for(controller='package', action='delete', id=dataset['name']), + url, extra_environ=env, expect_errors=True ) @@ -711,8 +779,12 @@ def test_confirm_cancel_delete(self): app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='delete', id=dataset['name']) + response = app.get( - url_for(controller='package', action='delete', id=dataset['name']), + url, extra_environ=env, ) assert_equal(200, response.status_int) @@ -736,12 +808,16 @@ def test_manage_dataset_resource_listing_page(self): resource = factories.Resource(package_id=dataset['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get( - url_for( + + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='resources', id=dataset['name'], - ), + ) + + response = app.get( + url, extra_environ=env ) assert_in(resource['name'], response) @@ -755,12 +831,16 @@ def test_unauth_user_cannot_view_manage_dataset_resource_listing_page(self): resource = factories.Resource(package_id=dataset['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get( - url_for( + + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='resources', id=dataset['name'], - ), + ) + + response = app.get( + url, extra_environ=env ) assert_in(resource['name'], response) @@ -771,12 +851,16 @@ def test_404_on_manage_dataset_resource_listing_page_that_does_not_exist(self): user = factories.User() app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} - response = app.get( - url_for( + + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='resources', id='does-not-exist' - ), + ) + + response = app.get( + url, extra_environ=env, expect_errors=True ) @@ -788,12 +872,15 @@ def test_add_new_resource_with_link_and_download(self): env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, extra_environ=env ) @@ -803,13 +890,17 @@ def test_add_new_resource_with_link_and_download(self): 'go-dataset-complete') result = helpers.call_action('package_show', id=dataset['id']) - response = app.get( - url_for( + + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='resource_download', id=dataset['id'], resource_id=result['resources'][0]['id'] - ), + ) + + response = app.get( + url, extra_environ=env, ) assert_equal(302, response.status_int) @@ -825,12 +916,15 @@ def test_editor_can_add_new_resource(self): env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, extra_environ=env ) @@ -855,12 +949,15 @@ def test_admin_can_add_new_resource(self): env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, extra_environ=env ) @@ -885,22 +982,28 @@ def test_member_cannot_add_new_resource(self): env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, extra_environ=env, status=403, ) - response = app.post( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.post( + url, {'name': 'test', 'url': 'test', 'save': 'save', 'id': ''}, extra_environ=env, status=403, @@ -916,22 +1019,28 @@ def test_non_organization_users_cannot_add_new_resource(self): env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, extra_environ=env, status=403, ) - response = app.post( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.post( + url, {'name': 'test', 'url': 'test', 'save': 'save', 'id': ''}, extra_environ=env, status=403, @@ -944,21 +1053,27 @@ def test_anonymous_users_cannot_add_new_resource(self): ) app = helpers._get_test_app() - response = app.get( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.get( + url, status=403, ) - response = app.post( - url_for( + with app.flask_app.test_request_context(): + url = url_for( controller='package', action='new_resource', id=dataset['id'], - ), + ) + + response = app.post( + url, {'name': 'test', 'url': 'test', 'save': 'save', 'id': ''}, status=403, ) @@ -984,25 +1099,28 @@ def teardown_class(cls): def test_existent_resource_view_page_returns_ok_code(self): resource_view = factories.ResourceView() - url = url_for(controller='package', - action='resource_read', - id=resource_view['package_id'], - resource_id=resource_view['resource_id'], - view_id=resource_view['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=resource_view['package_id'], + resource_id=resource_view['resource_id'], + view_id=resource_view['id']) + app.get(url, status=200) def test_inexistent_resource_view_page_returns_not_found_code(self): resource_view = factories.ResourceView() - url = url_for(controller='package', - action='resource_read', - id=resource_view['package_id'], - resource_id=resource_view['resource_id'], - view_id='inexistent-view-id') - app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=resource_view['package_id'], + resource_id=resource_view['resource_id'], + view_id='inexistent-view-id') + app.get(url, status=404) @@ -1021,12 +1139,12 @@ def test_existing_resource_with_not_associated_dataset(self): dataset = factories.Dataset() resource = factories.Resource() - url = url_for(controller='package', - action='resource_read', - id=dataset['id'], - resource_id=resource['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=dataset['id'], + resource_id=resource['id']) app.get(url, status=404) def test_resource_read_logged_in_user(self): @@ -1038,12 +1156,12 @@ def test_resource_read_logged_in_user(self): dataset = factories.Dataset() resource = factories.Resource(package_id=dataset['id']) - url = url_for(controller='package', - action='resource_read', - id=dataset['id'], - resource_id=resource['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=dataset['id'], + resource_id=resource['id']) app.get(url, status=200, extra_environ=env) def test_resource_read_anon_user(self): @@ -1053,12 +1171,12 @@ def test_resource_read_anon_user(self): dataset = factories.Dataset() resource = factories.Resource(package_id=dataset['id']) - url = url_for(controller='package', - action='resource_read', - id=dataset['id'], - resource_id=resource['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=dataset['id'], + resource_id=resource['id']) app.get(url, status=200) def test_resource_read_sysadmin(self): @@ -1070,12 +1188,12 @@ def test_resource_read_sysadmin(self): dataset = factories.Dataset() resource = factories.Resource(package_id=dataset['id']) - url = url_for(controller='package', - action='resource_read', - id=dataset['id'], - resource_id=resource['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=dataset['id'], + resource_id=resource['id']) app.get(url, status=200, extra_environ=env) def test_user_not_in_organization_cannot_read_private_dataset(self): @@ -1088,12 +1206,12 @@ def test_user_not_in_organization_cannot_read_private_dataset(self): ) resource = factories.Resource(package_id=dataset['id']) - url = url_for(controller='package', - action='resource_read', - id=dataset['id'], - resource_id=resource['id']) - app = self._get_test_app() + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='resource_read', + id=dataset['id'], + resource_id=resource['id']) response = app.get(url, status=404, extra_environ=env) @@ -1120,14 +1238,16 @@ def test_organization_members_can_read_resources_in_private_datasets(self): app = helpers._get_test_app() + with app.flask_app.test_request_context(): + url = url_for( + controller='package', + action='resource_read', + id=dataset['name'], + resource_id=resource['id'], + ) for user, user_dict in members.items(): response = app.get( - url_for( - controller='package', - action='resource_read', - id=dataset['name'], - resource_id=resource['id'], - ), + url, extra_environ={ 'REMOTE_USER': user_dict['name'].encode('ascii'), }, @@ -1141,8 +1261,12 @@ def test_anonymous_users_cannot_read_private_datasets(self): private=True, ) app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='read', id=dataset['name']) + response = app.get( - url_for(controller='package', action='read', id=dataset['name']), + url, status=404 ) assert_equal(404, response.status_int) @@ -1158,9 +1282,13 @@ def test_dataset_owners_can_delete_resources(self): resource = factories.Resource(package_id=dataset['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id=resource['id']) + response = app.post( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id=resource['id']), + url, extra_environ=env, ) response = response.follow() @@ -1178,9 +1306,13 @@ def test_deleting_non_existing_resource_404s(self): dataset = factories.Dataset(owner_org=owner_org['id']) env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id='doesnotexist') + response = app.post( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id='doesnotexist'), + url, extra_environ=env, expect_errors=True ) @@ -1195,9 +1327,13 @@ def test_anon_users_cannot_delete_owned_resources(self): resource = factories.Resource(package_id=dataset['id']) app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id=resource['id']) + response = app.post( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id=resource['id']), + url, status=403, ) response.mustcontain('Unauthorized to delete package') @@ -1215,9 +1351,13 @@ def test_logged_in_users_cannot_delete_resources_they_do_not_own(self): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} app = helpers._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id=resource['id']) + response = app.post( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id=resource['id']), + url, extra_environ=env, expect_errors=True ) @@ -1232,9 +1372,13 @@ def test_sysadmins_can_delete_any_resource(self): sysadmin = factories.Sysadmin() app = helpers._get_test_app() env = {'REMOTE_USER': sysadmin['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id=resource['id']) + response = app.post( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id=resource['id']), + url, extra_environ=env, ) response = response.follow() @@ -1257,9 +1401,13 @@ def test_confirm_and_cancel_deleting_a_resource(self): resource = factories.Resource(package_id=dataset['id']) app = helpers._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='package', action='resource_delete', + id=dataset['name'], resource_id=resource['id']) + response = app.get( - url_for(controller='package', action='resource_delete', - id=dataset['name'], resource_id=resource['id']), + url, extra_environ=env, ) assert_equal(200, response.status_int) @@ -1282,8 +1430,9 @@ def setup_class(cls): def test_search_basic(self): dataset1 = factories.Dataset() - offset = url_for(controller='package', action='search') app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='search') page = app.get(offset) assert dataset1['name'] in page.body.decode('utf8') @@ -1292,8 +1441,10 @@ def test_search_sort_by_blank(self): factories.Dataset() # ?sort has caused an exception in the past - offset = url_for(controller='package', action='search') + '?sort' + app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='search') + '?sort' app.get(offset) def test_search_sort_by_bad(self): @@ -1302,9 +1453,11 @@ def test_search_sort_by_bad(self): # bad spiders try all sorts of invalid values for sort. They should get # a 400 error with specific error message. No need to alert the # administrator. - offset = url_for(controller='package', action='search') + \ - '?sort=gvgyr_fgevat+nfp' + app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='search') + \ + '?sort=gvgyr_fgevat+nfp' response = app.get(offset, status=[200, 400]) if response.status == 200: import sys @@ -1320,9 +1473,10 @@ def test_search_solr_syntax_error(self): # Whilst this could be due to a bad user input, it could also be # because CKAN mangled things somehow and therefore we flag it up to # the administrator and give a meaningless error, just in case - offset = url_for(controller='package', action='search') + \ - '?q=--included' app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='search') + \ + '?q=--included' search_response = app.get(offset) search_response_html = BeautifulSoup(search_response.body) @@ -1333,8 +1487,9 @@ def test_search_solr_syntax_error(self): def test_search_plugin_hooks(self): with p.use_plugin('test_package_controller_plugin') as plugin: - offset = url_for(controller='package', action='search') app = self._get_test_app() + with app.flask_app.test_request_context(): + offset = url_for(controller='package', action='search') app.get(offset) # get redirected ... @@ -1348,7 +1503,8 @@ def test_search_page_request(self): factories.Dataset(name="dataset-two", title='Dataset Two') factories.Dataset(name="dataset-three", title='Dataset Three') - search_url = url_for(controller='package', action='search') + with app.flask_app.test_request_context(): + search_url = url_for(controller='package', action='search') search_response = app.get(search_url) assert_true('3 datasets found' in search_response) @@ -1371,7 +1527,8 @@ def test_search_page_results(self): factories.Dataset(name="dataset-two", title='Dataset Two') factories.Dataset(name="dataset-three", title='Dataset Three') - search_url = url_for(controller='package', action='search') + with app.flask_app.test_request_context(): + search_url = url_for(controller='package', action='search') search_response = app.get(search_url) search_form = search_response.forms['dataset-search-form'] @@ -1396,7 +1553,8 @@ def test_search_page_no_results(self): factories.Dataset(name="dataset-two", title='Dataset Two') factories.Dataset(name="dataset-three", title='Dataset Three') - search_url = url_for(controller='package', action='search') + with app.flask_app.test_request_context(): + search_url = url_for(controller='package', action='search') search_response = app.get(search_url) search_form = search_response.forms['dataset-search-form'] @@ -1421,7 +1579,8 @@ def test_search_page_results_tag(self): factories.Dataset(name="dataset-two", title='Dataset Two') factories.Dataset(name="dataset-three", title='Dataset Three') - search_url = url_for(controller='package', action='search') + with app.flask_app.test_request_context(): + search_url = url_for(controller='package', action='search') search_response = app.get(search_url) assert_true('/dataset?tags=my-tag' in search_response) @@ -1449,7 +1608,8 @@ def test_search_page_results_private(self): factories.Dataset(name="dataset-two", title='Dataset Two') factories.Dataset(name="dataset-three", title='Dataset Three') - search_url = url_for(controller='package', action='search') + with app.flask_app.test_request_context(): + search_url = url_for(controller='package', action='search') search_response = app.get(search_url) search_response_html = BeautifulSoup(search_response.body) @@ -1473,9 +1633,11 @@ def test_package_follow(self): package = factories.Dataset() env = {'REMOTE_USER': user['name'].encode('ascii')} - follow_url = url_for(controller='package', - action='follow', - id=package['id']) + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='package', + action='follow', + id=package['id']) response = app.post(follow_url, extra_environ=env, status=302) response = response.follow() assert_true('You are now following {0}' @@ -1489,9 +1651,11 @@ def test_package_follow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='package', - action='follow', - id='not-here') + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='package', + action='follow', + id='not-here') response = app.post(follow_url, extra_environ=env, status=302) response = response.follow(status=404) assert_true('Dataset not found' in response) @@ -1503,13 +1667,16 @@ def test_package_unfollow(self): package = factories.Dataset() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='package', - action='follow', - id=package['id']) + + with app.flask_app.test_request_context(): + follow_url = url_for(controller='package', + action='follow', + id=package['id']) app.post(follow_url, extra_environ=env, status=302) - unfollow_url = url_for(controller='package', action='unfollow', - id=package['id']) + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='package', action='unfollow', + id=package['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) unfollow_response = unfollow_response.follow() @@ -1526,8 +1693,10 @@ def test_package_unfollow_not_following(self): package = factories.Dataset() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='package', action='unfollow', - id=package['id']) + + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='package', action='unfollow', + id=package['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) unfollow_response = unfollow_response.follow() @@ -1542,8 +1711,10 @@ def test_package_unfollow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='package', action='unfollow', - id='not-here') + + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='package', action='unfollow', + id='not-here') unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) unfollow_response = unfollow_response.follow(status=404) @@ -1557,13 +1728,16 @@ def test_package_follower_list(self): package = factories.Dataset() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='package', - action='follow', - id=package['id']) - app.post(follow_url, extra_environ=env, status=302) - followers_url = url_for(controller='package', action='followers', - id=package['id']) + with app.flask_app.test_request_context(): + follow_url = url_for(controller='package', + action='follow', + id=package['id']) + + followers_url = url_for(controller='package', action='followers', + id=package['id']) + + app.post(follow_url, extra_environ=env, status=302) # Only sysadmins can view the followers list pages followers_response = app.get(followers_url, extra_environ=env, @@ -1578,8 +1752,9 @@ def test_dataset_read(self): dataset = factories.Dataset() - url = url_for(controller='package', - action='read', - id=dataset['id']) + with app.flask_app.test_request_context(): + url = url_for(controller='package', + action='read', + id=dataset['id']) response = app.get(url) assert_in(dataset['title'], response) diff --git a/ckan/tests/controllers/test_tags.py b/ckan/tests/controllers/test_tags.py index a1a01ca6a96..e224e2b8aca 100644 --- a/ckan/tests/controllers/test_tags.py +++ b/ckan/tests/controllers/test_tags.py @@ -4,7 +4,7 @@ from nose.tools import assert_equal, assert_true, assert_false from bs4 import BeautifulSoup -from routes import url_for +from ckan.lib.helpers import url_for import ckan.tests.helpers as helpers from ckan.tests import factories @@ -31,7 +31,8 @@ def test_tags_listed_under_50(self): expected_tags = _make_tag_list(49) factories.Dataset(tags=expected_tags) - tag_index_url = url_for(controller='tag', action='index') + with app.flask_app.test_request_context(): + tag_index_url = url_for(controller='tag', action='index') tag_response = app.get(tag_index_url) tag_response_html = BeautifulSoup(tag_response.body) @@ -52,7 +53,8 @@ def test_tags_listed_over_50(self): expected_tags = _make_tag_list(51) factories.Dataset(tags=expected_tags) - tag_index_url = url_for(controller='tag', action='index') + with app.flask_app.test_request_context(): + tag_index_url = url_for(controller='tag', action='index') tag_response = app.get(tag_index_url) tag_response_html = BeautifulSoup(tag_response.body) @@ -75,7 +77,8 @@ def test_tag_search(self): expected_tags.append({'name': 'find-me'}) factories.Dataset(tags=expected_tags) - tag_index_url = url_for(controller='tag', action='index') + with app.flask_app.test_request_context(): + tag_index_url = url_for(controller='tag', action='index') tag_response = app.get(tag_index_url) search_form = tag_response.forms[1] @@ -97,7 +100,8 @@ def test_tag_search_no_results(self): expected_tags = _make_tag_list(50) factories.Dataset(tags=expected_tags) - tag_index_url = url_for(controller='tag', action='index') + with app.flask_app.test_request_context(): + tag_index_url = url_for(controller='tag', action='index') tag_response = app.get(tag_index_url) search_form = tag_response.forms[1] @@ -120,7 +124,8 @@ def test_tag_read_redirects_to_dataset_search(self): app = self._get_test_app() factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}]) - tag_url = url_for(controller='tag', action='read', id='find-me') + with app.flask_app.test_request_context(): + tag_url = url_for(controller='tag', action='read', id='find-me') tag_response = app.get(tag_url, status=302) assert_equal(tag_response.headers['Location'], 'http://localhost/dataset?tags=find-me') @@ -130,5 +135,6 @@ def test_tag_read_not_found(self): app = self._get_test_app() factories.Dataset(title='My Other Dataset', tags=[{'name': 'find-me'}]) - tag_url = url_for(controller='tag', action='read', id='not-here') + with app.flask_app.test_request_context(): + tag_url = url_for(controller='tag', action='read', id='not-here') app.get(tag_url, status=404) diff --git a/ckan/tests/controllers/test_user.py b/ckan/tests/controllers/test_user.py index cf78056b79c..b8c6b468205 100644 --- a/ckan/tests/controllers/test_user.py +++ b/ckan/tests/controllers/test_user.py @@ -1,7 +1,7 @@ from bs4 import BeautifulSoup from nose.tools import assert_true, assert_false, assert_equal -from routes import url_for +from ckan.lib.helpers import url_for import ckan.tests.helpers as helpers import ckan.tests.factories as factories @@ -16,8 +16,11 @@ def _get_user_edit_page(app): user = factories.User() env = {'REMOTE_USER': user['name'].encode('ascii')} + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit') + response = app.get( - url=url_for(controller='user', action='edit'), + url=url, extra_environ=env, ) return env, response, user @@ -26,7 +29,11 @@ def _get_user_edit_page(app): class TestRegisterUser(helpers.FunctionalTestBase): def test_register_a_user(self): app = helpers._get_test_app() - response = app.get(url=url_for(controller='user', action='register')) + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='register') + + response = app.get(url) form = response.forms['user-register-form'] form['name'] = 'newuser' @@ -45,7 +52,11 @@ def test_register_a_user(self): def test_register_user_bad_password(self): app = helpers._get_test_app() - response = app.get(url=url_for(controller='user', action='register')) + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='register') + + response = app.get(url) form = response.forms['user-register-form'] form['name'] = 'newuser' @@ -133,7 +144,8 @@ def test_user_logout_url_redirect(self): ''' app = self._get_test_app() - logout_url = url_for(controller='user', action='logout') + with app.flask_app.test_request_context(): + logout_url = url_for(controller='user', action='logout') logout_response = app.get(logout_url, status=302) final_response = helpers.webtest_maybe_follow(logout_response) @@ -148,12 +160,12 @@ def test_non_root_user_logout_url_redirect(self): ''' app = self._get_test_app() - logout_url = url_for(controller='user', action='logout') + with app.flask_app.test_request_context(): + logout_url = url_for(controller='user', action='logout') + # Remove the prefix otherwise the test app won't find the correct route + logout_url = logout_url.replace('/my/prefix', '') logout_response = app.get(logout_url, status=302) - try: - final_response = helpers.webtest_maybe_follow(logout_response) - except Exception as e: - assert_true('/my/prefix/user/logout' in e.message) + assert '/my/prefix/user/logout' in logout_response.headers['Location'] class TestUser(helpers.FunctionalTestBase): @@ -167,8 +179,12 @@ def test_own_datasets_show_up_on_user_dashboard(self): app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='dashboard_datasets') + response = app.get( - url=url_for(controller='user', action='dashboard_datasets'), + url, extra_environ=env, ) @@ -184,8 +200,12 @@ def test_other_datasets_dont_show_up_on_user_dashboard(self): app = self._get_test_app() env = {'REMOTE_USER': user2['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='dashboard_datasets') + response = app.get( - url=url_for(controller='user', action='dashboard_datasets'), + url, extra_environ=env, ) @@ -196,8 +216,12 @@ class TestUserEdit(helpers.FunctionalTestBase): def test_user_edit_no_user(self): app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit', id=None) + response = app.get( - url_for(controller='user', action='edit', id=None), + url, status=400 ) assert_true('No user specified' in response) @@ -206,8 +230,12 @@ def test_user_edit_unknown_user(self): '''Attempt to read edit user for an unknown user redirects to login page.''' app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit', id='unknown_person') + response = app.get( - url_for(controller='user', action='edit', id='unknown_person'), + url, status=403 ) @@ -217,8 +245,12 @@ def test_user_edit_not_logged_in(self): app = self._get_test_app() user = factories.User() username = user['name'] + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit', id=username) + response = app.get( - url_for(controller='user', action='edit', id=username), + url, status=403 ) @@ -226,8 +258,12 @@ def test_edit_user(self): user = factories.User(password='pass') app = self._get_test_app() env = {'REMOTE_USER': user['name'].encode('ascii')} + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit') + response = app.get( - url=url_for(controller='user', action='edit'), + url, extra_environ=env, ) # existing values in the form @@ -307,8 +343,12 @@ def test_edit_user_logged_in_username_change(self): login_form.submit() # Now the cookie is set, run the test + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit') + response = app.get( - url=url_for(controller='user', action='edit'), + url, ) # existing values in the form form = response.forms['user-edit-form'] @@ -318,7 +358,8 @@ def test_edit_user_logged_in_username_change(self): response = submit_and_follow(app, form, name='save') response = helpers.webtest_maybe_follow(response) - expected_url = url_for(controller='user', action='read', id='new-name') + with app.flask_app.test_request_context(): + expected_url = url_for(controller='user', action='read', id='new-name') assert response.request.path == expected_url def test_edit_user_logged_in_username_change_by_name(self): @@ -338,8 +379,12 @@ def test_edit_user_logged_in_username_change_by_name(self): login_form.submit() # Now the cookie is set, run the test + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit', id=user['name']) + response = app.get( - url=url_for(controller='user', action='edit', id=user['name']), + url, ) # existing values in the form form = response.forms['user-edit-form'] @@ -349,7 +394,8 @@ def test_edit_user_logged_in_username_change_by_name(self): response = submit_and_follow(app, form, name='save') response = helpers.webtest_maybe_follow(response) - expected_url = url_for(controller='user', action='read', id='new-name') + with app.flask_app.test_request_context(): + expected_url = url_for(controller='user', action='read', id='new-name') assert response.request.path == expected_url def test_edit_user_logged_in_username_change_by_id(self): @@ -369,8 +415,12 @@ def test_edit_user_logged_in_username_change_by_id(self): login_form.submit() # Now the cookie is set, run the test + + with app.flask_app.test_request_context(): + url = url_for(controller='user', action='edit', id=user['id']) + response = app.get( - url=url_for(controller='user', action='edit', id=user['id']), + url, ) # existing values in the form form = response.forms['user-edit-form'] @@ -380,7 +430,8 @@ def test_edit_user_logged_in_username_change_by_id(self): response = submit_and_follow(app, form, name='save') response = helpers.webtest_maybe_follow(response) - expected_url = url_for(controller='user', action='read', id='new-name') + with app.flask_app.test_request_context(): + expected_url = url_for(controller='user', action='read', id='new-name') assert response.request.path == expected_url def test_perform_reset_for_key_change(self): @@ -392,10 +443,12 @@ def test_perform_reset_for_key_change(self): key = user_obj.reset_key app = self._get_test_app() - offset = url_for(controller='user', - action='perform_reset', - id=user_obj.id, - key=user_obj.reset_key) + + with app.flask_app.test_request_context(): + offset = url_for(controller='user', + action='perform_reset', + id=user_obj.id, + key=user_obj.reset_key) response = app.post(offset, params=params, status=302) user_obj = helpers.model.User.by_name(user['name']) # Update user_obj @@ -446,7 +499,8 @@ def test_user_follow(self): user_two = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='user', + with app.flask_app.test_request_context(): + follow_url = url_for(controller='user', action='follow', id=user_two['id']) response = app.post(follow_url, extra_environ=env, status=302) @@ -462,7 +516,8 @@ def test_user_follow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='user', + with app.flask_app.test_request_context(): + follow_url = url_for(controller='user', action='follow', id='not-here') response = app.post(follow_url, extra_environ=env, status=302) @@ -476,12 +531,14 @@ def test_user_unfollow(self): user_two = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='user', + with app.flask_app.test_request_context(): + follow_url = url_for(controller='user', action='follow', id=user_two['id']) app.post(follow_url, extra_environ=env, status=302) - unfollow_url = url_for(controller='user', action='unfollow', + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='user', action='unfollow', id=user_two['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) @@ -499,7 +556,8 @@ def test_user_unfollow_not_following(self): user_two = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='user', action='unfollow', + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='user', action='unfollow', id=user_two['id']) unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) @@ -515,7 +573,8 @@ def test_user_unfollow_not_exist(self): user_one = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - unfollow_url = url_for(controller='user', action='unfollow', + with app.flask_app.test_request_context(): + unfollow_url = url_for(controller='user', action='unfollow', id='not-here') unfollow_response = app.post(unfollow_url, extra_environ=env, status=302) @@ -531,12 +590,14 @@ def test_user_follower_list(self): user_two = factories.User() env = {'REMOTE_USER': user_one['name'].encode('ascii')} - follow_url = url_for(controller='user', + with app.flask_app.test_request_context(): + follow_url = url_for(controller='user', action='follow', id=user_two['id']) app.post(follow_url, extra_environ=env, status=302) - followers_url = url_for(controller='user', action='followers', + with app.flask_app.test_request_context(): + followers_url = url_for(controller='user', action='followers', id=user_two['id']) # Only sysadmins can view the followers list pages @@ -551,7 +612,8 @@ def test_user_page_anon_access(self): '''Anon users can access the user list page''' app = self._get_test_app() - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200) assert_true('All Users - CKAN' in user_response) @@ -563,7 +625,8 @@ def test_user_page_lists_users(self): factories.User(fullname='User Two') factories.User(fullname='User Three') - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200) user_response_html = BeautifulSoup(user_response.body) @@ -582,7 +645,8 @@ def test_user_page_doesnot_list_deleted_users(self): factories.User(fullname='User Two') factories.User(fullname='User Three') - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200) user_response_html = BeautifulSoup(user_response.body) @@ -601,7 +665,8 @@ def test_user_page_anon_search(self): factories.User(fullname='Person Two') factories.User(fullname='Person Three') - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200) search_form = user_response.forms['user-search-form'] search_form['q'] = 'Person' @@ -623,7 +688,8 @@ def test_user_page_anon_search_not_by_email(self): factories.User(fullname='Person Two') factories.User(fullname='Person Three') - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200) search_form = user_response.forms['user-search-form'] search_form['q'] = 'useroneemail@example.com' @@ -643,7 +709,8 @@ def test_user_page_sysadmin_user(self): factories.User(fullname='Person Three') env = {'REMOTE_USER': sysadmin['name'].encode('ascii')} - user_url = url_for(controller='user', action='index') + with app.flask_app.test_request_context(): + user_url = url_for(controller='user', action='index') user_response = app.get(user_url, status=200, extra_environ=env) search_form = user_response.forms['user-search-form'] search_form['q'] = 'useroneemail@example.com' diff --git a/ckan/tests/controllers/test_util.py b/ckan/tests/controllers/test_util.py index 51572273c57..7296b659d66 100644 --- a/ckan/tests/controllers/test_util.py +++ b/ckan/tests/controllers/test_util.py @@ -2,7 +2,7 @@ from pylons.test import pylonsapp import paste.fixture -from routes import url_for as url_for +from ckan.lib.helpers import url_for as url_for import ckan.tests.helpers as helpers @@ -10,8 +10,12 @@ class TestUtil(helpers.FunctionalTestBase): def test_redirect_ok(self): app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='util', action='redirect') + response = app.get( - url=url_for(controller='util', action='redirect'), + url, params={'url': '/dataset'}, status=302, ) @@ -20,24 +24,36 @@ def test_redirect_ok(self): def test_redirect_external(self): app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='util', action='redirect') + response = app.get( - url=url_for(controller='util', action='redirect'), + url, params={'url': 'http://nastysite.com'}, status=403, ) def test_redirect_no_params(self): app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='util', action='redirect') + response = app.get( - url=url_for(controller='util', action='redirect'), + url, params={}, status=400, ) def test_redirect_no_params_2(self): app = self._get_test_app() + + with app.flask_app.test_request_context(): + url = url_for(controller='util', action='redirect') + response = app.get( - url=url_for(controller='util', action='redirect'), + url, params={'url': ''}, status=400, ) diff --git a/ckan/tests/lib/test_helpers.py b/ckan/tests/lib/test_helpers.py index d557250e4e6..06d0c6a7ee3 100644 --- a/ckan/tests/lib/test_helpers.py +++ b/ckan/tests/lib/test_helpers.py @@ -15,41 +15,61 @@ CkanUrlException = ckan.exceptions.CkanUrlException -class TestHelpersUrlForStatic(object): +class BaseUrlFor(object): + + def __init__(self): + + self.app = helpers._get_test_app() + + def url_for(self, *args, **kw): + with self.app.flask_app.test_request_context(): + return h.url_for(*args, **kw) + + def url_for_static(self, *args, **kw): + with self.app.flask_app.test_request_context(): + return h.url_for_static(*args, **kw) + + def url_for_static_or_external(self, *args, **kw): + with self.app.flask_app.test_request_context(): + return h.url_for_static_or_external(*args, **kw) + + +class TestHelpersUrlForStatic(BaseUrlFor): def test_url_for_static(self): url = '/assets/ckan.jpg' - eq_(h.url_for_static(url), url) + eq_(self.url_for_static(url), url) def test_url_for_static_adds_starting_slash_if_url_doesnt_have_it(self): slashless_url = 'ckan.jpg' url = '/' + slashless_url - eq_(h.url_for_static(slashless_url), url) + eq_(self.url_for_static(slashless_url), url) def test_url_for_static_converts_unicode_strings_to_regular_strings(self): url = u'/ckan.jpg' - assert isinstance(h.url_for_static(url), str) + assert isinstance(self.url_for_static(url), str) def test_url_for_static_raises_when_called_with_external_urls(self): url = 'http://assets.ckan.org/ckan.jpg' - nose.tools.assert_raises(CkanUrlException, h.url_for_static, url) + nose.tools.assert_raises(CkanUrlException, self.url_for_static, url) def test_url_for_static_raises_when_called_with_protocol_relative(self): url = '//assets.ckan.org/ckan.jpg' - nose.tools.assert_raises(CkanUrlException, h.url_for_static, url) + nose.tools.assert_raises(CkanUrlException, self.url_for_static, url) @helpers.change_config('ckan.site_url', 'http://example.com') @helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo') def test_url_for_static_with_root_path(self): url = '/my/custom/path/foo/my-asset/file.txt' - generated_url = h.url_for_static('/my-asset/file.txt') + generated_url = self.url_for_static('/my-asset/file.txt') eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') @helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo') def test_url_for_static_qualified_with_root_path(self): url = 'http://example.com/my/custom/path/foo/my-asset/file.txt' - generated_url = h.url_for_static('/my-asset/file.txt', qualified=True) + generated_url = self.url_for_static('/my-asset/file.txt', + qualified=True) eq_(generated_url, url) @helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path') @@ -57,106 +77,108 @@ def test_url_for_static_qualified_with_root_path(self): @helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo') def test_url_for_static_with_root_path_and_script_name_env(self): url = 'http://example.com/my/custom/path/foo/my-asset/file.txt' - generated_url = h.url_for_static('/my-asset/file.txt', qualified=True) + generated_url = self.url_for_static('/my-asset/file.txt', + qualified=True) eq_(generated_url, url) -class TestHelpersUrlForStaticOrExternal(object): +class TestHelpersUrlForStaticOrExternal(BaseUrlFor): def test_url_for_static_or_external(self): url = '/assets/ckan.jpg' - eq_(h.url_for_static_or_external(url), url) + eq_(self.url_for_static_or_external(url), url) def test_url_for_static_or_external_works_with_external_urls(self): url = 'http://assets.ckan.org/ckan.jpg' - eq_(h.url_for_static_or_external(url), url) + eq_(self.url_for_static_or_external(url), url) def test_url_for_static_or_external_converts_unicode_to_strings(self): url = u'/ckan.jpg' - assert isinstance(h.url_for_static_or_external(url), str) + assert isinstance(self.url_for_static_or_external(url), str) def test_url_for_static_or_external_adds_starting_slash_if_needed(self): slashless_url = 'ckan.jpg' url = '/' + slashless_url - eq_(h.url_for_static_or_external(slashless_url), url) + eq_(self.url_for_static_or_external(slashless_url), url) def test_url_for_static_or_external_works_with_protocol_relative_url(self): url = '//assets.ckan.org/ckan.jpg' - eq_(h.url_for_static_or_external(url), url) + eq_(self.url_for_static_or_external(url), url) -class TestHelpersUrlFor(object): +class TestHelpersUrlFor(BaseUrlFor): @helpers.change_config('ckan.site_url', 'http://example.com') def test_url_for_default(self): url = '/dataset/my_dataset' - generated_url = h.url_for(controller='package', action='read', id='my_dataset') + generated_url = self.url_for(controller='package', action='read', + id='my_dataset') eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') def test_url_for_with_locale(self): url = '/de/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - locale='de') + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + locale='de') eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') @helpers.change_config('ckan.root_path', '/foo/{{LANG}}') def test_url_for_with_locale_object(self): url = '/foo/de/dataset/my_dataset' - generated_url = h.url_for('/dataset/my_dataset', - locale=Locale('de')) + generated_url = self.url_for('/dataset/my_dataset', + locale=Locale('de')) eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') def test_url_for_not_qualified(self): url = '/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=False) + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=False) eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') def test_url_for_qualified(self): url = 'http://example.com/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=True) + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=True) eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') @helpers.change_config('ckan.root_path', '/my/prefix') def test_url_for_qualified_with_root_path(self): url = 'http://example.com/my/prefix/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=True) + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=True) eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') def test_url_for_qualified_with_locale(self): url = 'http://example.com/de/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=True, - locale='de') + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=True, + locale='de') eq_(generated_url, url) @helpers.change_config('ckan.site_url', 'http://example.com') @helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo') def test_url_for_qualified_with_root_path_and_locale(self): url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=True, - locale='de') + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=True, + locale='de') eq_(generated_url, url) @helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path') @@ -164,11 +186,11 @@ def test_url_for_qualified_with_root_path_and_locale(self): @helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo') def test_url_for_qualified_with_root_path_locale_and_script_name_env(self): url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset' - generated_url = h.url_for(controller='package', - action='read', - id='my_dataset', - qualified=True, - locale='de') + generated_url = self.url_for(controller='package', + action='read', + id='my_dataset', + qualified=True, + locale='de') eq_(generated_url, url) diff --git a/ckan/tests/lib/test_mailer.py b/ckan/tests/lib/test_mailer.py index e776e8ef581..c52ed1cff63 100644 --- a/ckan/tests/lib/test_mailer.py +++ b/ckan/tests/lib/test_mailer.py @@ -37,6 +37,10 @@ def setup_class(cls): model.Session.commit() SmtpServerHarness.setup_class() + # Used to provide a context for url_for + cls.app = helpers._get_test_app() + + @classmethod def teardown_class(cls): SmtpServerHarness.teardown_class() @@ -154,7 +158,9 @@ def test_send_reset_email(self): user = factories.User() user_obj = model.User.by_name(user['name']) - mailer.send_reset_link(user_obj) + # We need to provide a context as url_for is used internally + with self.app.flask_app.test_request_context(): + mailer.send_reset_link(user_obj) # check it went to the mock smtp server msgs = self.get_smtp_messages() @@ -163,7 +169,9 @@ def test_send_reset_email(self): assert_equal(msg[1], config['smtp.mail_from']) assert_equal(msg[2], [user['email']]) assert 'Reset' in msg[3], msg[3] - test_msg = mailer.get_reset_link_body(user_obj) + # We need to provide a context as url_for is used internally + with self.app.flask_app.test_request_context(): + test_msg = mailer.get_reset_link_body(user_obj) expected_body = self.mime_encode(test_msg, user['name']) @@ -174,8 +182,12 @@ def test_send_invite_email(self): user_obj = model.User.by_name(user['name']) assert user_obj.reset_key is None, user_obj - # send email - mailer.send_invite(user_obj) + # We need to provide a context as url_for is used internally + with self.app.flask_app.test_request_context(): + # send email + mailer.send_invite(user_obj) + + test_msg = mailer.get_invite_body(user_obj) # check it went to the mock smtp server msgs = self.get_smtp_messages() @@ -183,7 +195,6 @@ def test_send_invite_email(self): msg = msgs[0] assert_equal(msg[1], config['smtp.mail_from']) assert_equal(msg[2], [user['email']]) - test_msg = mailer.get_invite_body(user_obj) expected_body = self.mime_encode(test_msg, user['name']) @@ -197,8 +208,10 @@ def test_send_invite_email_with_group(self): group = factories.Group() role = 'member' - # send email - mailer.send_invite(user_obj, group_dict=group, role=role) + # We need to provide a context as url_for is used internally + with self.app.flask_app.test_request_context(): + # send email + mailer.send_invite(user_obj, group_dict=group, role=role) # check it went to the mock smtp server msgs = self.get_smtp_messages() @@ -214,8 +227,10 @@ def test_send_invite_email_with_org(self): org = factories.Organization() role = 'admin' - # send email - mailer.send_invite(user_obj, group_dict=org, role=role) + # We need to provide a context as url_for is used internally + with self.app.flask_app.test_request_context(): + # send email + mailer.send_invite(user_obj, group_dict=org, role=role) # check it went to the mock smtp server msgs = self.get_smtp_messages()