Skip to content

Commit

Permalink
Fix ckan/ tests (no legacy) depending on url_for
Browse files Browse the repository at this point in the history
The Flask url_for function requires an application context to be present
when generating a url_for. This is obviously not the case when calling
it from a test, so we need to wrap the function on a
test_request_context (which will create an app context):

        with app.flask_app.test_request_context():
            url_for(...)

For tests that don't require an app like helpers or mailer this is done
automatically in the tests setup.

Note that all URLs generated now take ckan.site_url into account, so tests
checking for `http://localhost/some-url` will fail as the correct url is
`http://test.ckan.net/some-url`, as this is the site_url used on test-core.ini.
  • Loading branch information
amercader committed Jun 8, 2016
1 parent b96d32d commit 77e0cfa
Show file tree
Hide file tree
Showing 12 changed files with 1,022 additions and 512 deletions.
36 changes: 24 additions & 12 deletions ckan/tests/controllers/test_admin.py
Expand Up @@ -3,7 +3,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
Expand All @@ -19,8 +19,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
Expand All @@ -30,8 +32,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,
)

Expand Down Expand Up @@ -262,17 +266,19 @@ 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.'''
user = factories.User()
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)
Expand All @@ -283,7 +289,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)
Expand All @@ -296,7 +304,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)
Expand All @@ -315,7 +324,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)
Expand All @@ -338,7 +348,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
Expand All @@ -364,7 +375,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]
Expand Down
101 changes: 63 additions & 38 deletions ckan/tests/controllers/test_api.py
Expand Up @@ -7,7 +7,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
Expand All @@ -31,9 +31,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,
Expand All @@ -55,7 +58,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()

Expand All @@ -79,9 +86,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,
Expand All @@ -97,10 +107,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,
Expand All @@ -118,9 +130,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,
Expand All @@ -136,9 +150,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,
Expand All @@ -156,9 +173,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,
Expand All @@ -174,12 +194,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,
Expand All @@ -190,12 +212,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,
Expand All @@ -209,12 +233,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'},
Expand All @@ -231,15 +256,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,
)
Expand Down
22 changes: 14 additions & 8 deletions ckan/tests/controllers/test_feed.py
@@ -1,8 +1,8 @@
# encoding: utf-8

from routes import url_for

from ckan import model
from ckan.lib.helpers import url_for

import ckan.tests.helpers as helpers
import ckan.tests.factories as factories

Expand All @@ -11,24 +11,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

0 comments on commit 77e0cfa

Please sign in to comment.