Skip to content

Commit

Permalink
[#3196] Fix legacy tests dependant on url_for
Browse files Browse the repository at this point in the history
Wrapping calls to url_for (and functions that call it firther down the
line) with the Flask test_request_context, update functional tests to
use the helpers._get_test_app function rather than old paster ones.
  • Loading branch information
amercader committed Sep 1, 2016
1 parent 43767c7 commit 2d83e0d
Show file tree
Hide file tree
Showing 30 changed files with 457 additions and 202 deletions.
4 changes: 2 additions & 2 deletions ckan/tests/controllers/test_package.py
Expand Up @@ -734,7 +734,7 @@ def test_delete_on_non_existing_dataset(self):

with app.flask_app.test_request_context():
url = url_for(controller='package', action='delete',
id='schrodingersdatset')
id='schrodingersdatset')

response = app.post(
url,
Expand Down Expand Up @@ -1815,7 +1815,7 @@ def test_package_unfollow(self):

with app.flask_app.test_request_context():
follow_url = url_for(controller='package',
action='follow',
action='follow',
id=package['id'])
app.post(follow_url, extra_environ=env, status=302)

Expand Down
5 changes: 4 additions & 1 deletion ckan/tests/legacy/__init__.py
Expand Up @@ -35,6 +35,7 @@
import ckan.model as model
from ckan import ckan_nose_plugin
from ckan.common import json
from ckan.tests import helpers

# evil hack as url_for is passed out
url_for = h.url_for
Expand Down Expand Up @@ -237,7 +238,8 @@ class WsgiAppCase(BaseCase):
# Either that, or this file got imported somehow before the tests started
# running, meaning the pylonsapp wasn't setup yet (which is done in
# pylons.test.py:begin())
app = paste.fixture.TestApp(wsgiapp)
#app = paste.fixture.TestApp(wsgiapp)
app = helpers._get_test_app()


def config_abspath(file_path):
Expand Down Expand Up @@ -415,6 +417,7 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs):
params = json.dumps(kwargs)
response = app.post('/api/action/{0}'.format(action), params=params,
extra_environ={'Authorization': str(apikey)}, status=status)

assert '/api/3/action/help_show?name={0}'.format(action) \
in response.json['help']

Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/legacy/functional/api/base.py
Expand Up @@ -190,7 +190,7 @@ def loads(self, chars):
raise Exception, "Couldn't loads string '%s': %s" % (chars, inst)

def assert_json_response(self, res, expected_in_body=None):
content_type = res.header_dict['Content-Type']
content_type = res.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(res.body)
if expected_in_body:
Expand Down
23 changes: 12 additions & 11 deletions ckan/tests/legacy/functional/api/model/test_package.py
Expand Up @@ -13,6 +13,7 @@
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase

import ckan.tests.legacy as tests
from ckan.tests import helpers

# Todo: Remove this ckan.model stuff.
import ckan.model as model
Expand Down Expand Up @@ -65,7 +66,7 @@ def test_register_post_ok(self):
assert_equal(pkg['extras'], self.package_fixture_data['extras'])

# Check the value of the Location header.
location = res.header('Location')
location = res.headers['Location']

assert offset in location
res = self.app.get(location, status=self.STATUS_200_OK)
Expand Down Expand Up @@ -133,7 +134,7 @@ def test_register_post_with_group(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})

# Check the database record.
Expand All @@ -159,7 +160,7 @@ def test_register_post_with_group_not_authorized(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_403_ACCESS_DENIED,
res = self.app.post(offset, data, status=self.STATUS_403_ACCESS_DENIED,
extra_environ=self.extra_environ)
del package_fixture_data['groups']

Expand All @@ -173,7 +174,7 @@ def test_register_post_with_group_not_found(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND,
res = self.app.post(offset, data, status=self.STATUS_404_NOT_FOUND,
extra_environ=self.extra_environ)
del package_fixture_data['groups']

Expand All @@ -187,7 +188,7 @@ def test_register_post_with_group_sysadmin(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})
# Check the database record.
model.Session.remove()
Expand All @@ -207,7 +208,7 @@ def test_register_post_json(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ=self.admin_extra_environ)
# Check the database record.
model.Session.remove()
Expand All @@ -219,7 +220,7 @@ def test_register_post_bad_content_type(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
res = self.http_request(offset, data,
res = self.app.post(offset, data,
content_type='something/unheard_of',
status=[self.STATUS_400_BAD_REQUEST,
self.STATUS_201_CREATED],
Expand Down Expand Up @@ -262,7 +263,7 @@ def test_register_post_indexerror(self):
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)

self.post_json(offset, data, status=500, extra_environ=self.admin_extra_environ)
self.app.post(offset, data, status=500, extra_environ=self.admin_extra_environ)
model.Session.remove()
finally:
SolrSettings.init(original_settings)
Expand All @@ -273,7 +274,7 @@ def test_register_post_tag_too_long(self):
assert not self.get_package_by_name(pkg['name'])
offset = self.package_offset()
data = self.dumps(pkg)
res = self.post_json(offset, data, status=self.STATUS_409_CONFLICT,
res = self.app.post(offset, data, status=self.STATUS_409_CONFLICT,
extra_environ=self.admin_extra_environ)
assert 'length is more than maximum 100' in res.body, res.body
assert 'tagok' not in res.body
Expand Down Expand Up @@ -714,8 +715,8 @@ def test_entity_delete_ok_without_request_headers(self):
assert self.get_package_by_name(self.package_fixture_data['name'])
# delete it
offset = self.package_offset(self.package_fixture_data['name'])
res = self.delete_request(offset, status=self.STATUS_200_OK,
extra_environ=self.admin_extra_environ)
res = self.app.delete(offset, status=self.STATUS_200_OK,
extra_environ=self.admin_extra_environ)
package = self.get_package_by_name(self.package_fixture_data['name'])
self.assert_equal(package.state, 'deleted')
model.Session.remove()
Expand Down
16 changes: 8 additions & 8 deletions ckan/tests/legacy/functional/api/model/test_relationships.py
@@ -1,14 +1,14 @@
# encoding: utf-8

from nose.tools import assert_equal
from nose.tools import assert_equal
from nose.plugins.skip import SkipTest

from ckan import model
from ckan.lib.create_test_data import CreateTestData

from ckan.tests.legacy.functional.api.base import BaseModelApiTestCase
from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase
from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase

class RelationshipsTestCase(BaseModelApiTestCase):

Expand Down Expand Up @@ -115,7 +115,7 @@ def test_01_create_and_read_relationship(self):
assert len(rels) == 1
self.check_relationship_dict(rels[0],
'annakarenina', 'parent_of', 'warandpeace', self.comment)

def test_02_create_relationship_way_2(self):
# Create a relationship using 2nd way
self.create_annakarenina_parent_of_war_and_peace(way=2)
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_create_relationship_unknown(self):
def create_annakarenina_parent_of_war_and_peace(self, way=1):
# Create package relationship.
# More than one 'way' to create a package.
# Todo: Redesign this in a RESTful style, so that a relationship is
# Todo: Redesign this in a RESTful style, so that a relationship is
# created by posting a relationship to a relationship **register**.
assert way in (1, 2, 3, 4)
if way == 1:
Expand Down Expand Up @@ -220,7 +220,7 @@ def create_annakarenina_parent_of_war_and_peace(self, way=1):
assert_equal(rel['type'], 'child_of')
assert_equal(rel['subject'], self.ref_package(self.war))
assert_equal(rel['object'], self.ref_package(self.anna))

# Check the model, directly.
rels = self.anna.get_relationships()
assert len(rels) == 1, rels
Expand Down Expand Up @@ -271,7 +271,7 @@ def get_relationships(self, package1_name=u'annakarenina', type='relationships',
if type:
allowable_statuses.append(404)
res = self.app.get(offset, status=allowable_statuses)
if res.status == 200:
if res.status_int == 200:
res_dict = self.data_from_res(res) if res.body else []
return res_dict
else:
Expand Down Expand Up @@ -300,7 +300,7 @@ def check_relationships_rest(self, pkg1_name, pkg2_name=None,
expected_relationships=[]):
rels = self.get_relationships(package1_name=pkg1_name,
package2_name=pkg2_name)
self.assert_len_relationships(rels, expected_relationships)
self.assert_len_relationships(rels, expected_relationships)
for rel in rels:
the_expected_rel = None
for expected_rel in expected_relationships:
Expand Down
6 changes: 3 additions & 3 deletions ckan/tests/legacy/functional/api/model/test_vocabulary.py
@@ -1,17 +1,17 @@
# encoding: utf-8

import ckan
import pylons.test
import paste.fixture
import ckan.lib.helpers as helpers
import ckan.lib.dictization.model_dictize as model_dictize

from ckan.tests import helpers as test_helpers


class TestVocabulary(object):

@classmethod
def setup_class(self):
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
self.app = test_helpers._get_test_app()

@classmethod
def teardown_class(self):
Expand Down
4 changes: 3 additions & 1 deletion ckan/tests/legacy/functional/api/test_activity.py
Expand Up @@ -22,6 +22,8 @@
from ckan.common import json
import ckan.tests.legacy as tests

from ckan.tests import helpers


##def package_update(context, data_dict):
## # These tests call package_update directly which is really bad
Expand Down Expand Up @@ -204,7 +206,7 @@ def setup_class(self):
'id': annakarenina.id,
}
self.users = [self.sysadmin_user, self.normal_user]
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
self.app = helpers._get_test_app()

@classmethod
def teardown_class(self):
Expand Down
4 changes: 3 additions & 1 deletion ckan/tests/legacy/functional/api/test_dashboard.py
Expand Up @@ -12,6 +12,8 @@
import paste
import pylons.test
from ckan.tests.legacy import CreateTestData
from ckan.tests import helpers


class TestDashboard(object):
'''Tests for the logic action functions related to the user's dashboard.'''
Expand All @@ -34,7 +36,7 @@ def user_create(cls):
def setup_class(cls):
ckan.lib.search.clear_all()
CreateTestData.create()
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
cls.app = helpers._get_test_app()
joeadmin = ckan.model.User.get('joeadmin')
cls.joeadmin = {
'id': joeadmin.id,
Expand Down
13 changes: 6 additions & 7 deletions ckan/tests/legacy/functional/api/test_email_notifications.py
Expand Up @@ -10,13 +10,12 @@
import ckan.tests.legacy.pylons_controller as pylons_controller
import ckan.config.middleware

import paste
import paste.deploy
import pylons.test
from ckan.tests import helpers

from ckan.common import config



class TestEmailNotifications(mock_mail_server.SmtpServerHarness,
pylons_controller.PylonsTestCase):

Expand All @@ -25,7 +24,7 @@ def setup_class(cls):
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
tests.CreateTestData.create()
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
cls.app = helpers._get_test_app()
joeadmin = model.User.get('joeadmin')
cls.joeadmin = {'id': joeadmin.id,
'apikey': joeadmin.apikey,
Expand Down Expand Up @@ -198,7 +197,7 @@ def setup_class(cls):
mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
tests.CreateTestData.create()
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
cls.app = helpers._get_test_app()
joeadmin = model.User.get('joeadmin')
cls.joeadmin = {'id': joeadmin.id,
'apikey': joeadmin.apikey,
Expand Down Expand Up @@ -338,7 +337,7 @@ def setup_class(cls):

wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
cls.app = helpers._get_test_app()

mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
Expand Down Expand Up @@ -422,7 +421,7 @@ def setup_class(cls):

wsgiapp = ckan.config.middleware.make_app(config['global_conf'],
**config)
cls.app = paste.fixture.TestApp(wsgiapp)
cls.app = helpers._get_test_app()

mock_mail_server.SmtpServerHarness.setup_class()
pylons_controller.PylonsTestCase.setup_class()
Expand Down
8 changes: 5 additions & 3 deletions ckan/tests/legacy/functional/api/test_follow.py
Expand Up @@ -18,6 +18,8 @@
import ckan
from ckan.tests.legacy import are_foreign_keys_supported, SkipTest, CreateTestData, call_action_api

from ckan.tests import helpers


def datetime_from_string(s):
'''Return a standard datetime.datetime object initialised from a string in
Expand Down Expand Up @@ -295,7 +297,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
self.app = helpers._get_test_app()

@classmethod
def teardown_class(self):
Expand Down Expand Up @@ -804,7 +806,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
self.app = helpers._get_test_app()
follow_user(self.app, self.testsysadmin['id'],
self.testsysadmin['apikey'], self.joeadmin['id'],
self.joeadmin['id'], self.testsysadmin['apikey'])
Expand Down Expand Up @@ -1154,7 +1156,7 @@ def setup_class(self):
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
self.app = helpers._get_test_app()

follow_user(self.app, self.joeadmin['id'], self.joeadmin['apikey'],
self.testsysadmin['id'], self.testsysadmin['id'],
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/legacy/functional/api/test_resource.py
Expand Up @@ -45,7 +45,7 @@ def teardown_class(self):
def test_good_input(self):
offset = self.base_url + '/format_autocomplete?incomplete=cs'
result = self.app.get(offset, status=200)
content_type = result.header_dict['Content-Type']
content_type = result.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
Expand All @@ -58,7 +58,7 @@ def test_good_input(self):
def test_missing_format(self):
offset = self.base_url + '/format_autocomplete?incomplete=incorrectformat'
result = self.app.get(offset, status=200)
content_type = result.header_dict['Content-Type']
content_type = result.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
Expand Down

0 comments on commit 2d83e0d

Please sign in to comment.