Skip to content

Commit

Permalink
Update ckanext tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Dec 27, 2019
1 parent 0d0c1ed commit 973bc41
Show file tree
Hide file tree
Showing 28 changed files with 377 additions and 632 deletions.
2 changes: 2 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -472,6 +472,8 @@ def _url_for_pylons(*args, **kw):
not kw['ver'].startswith('/')):
kw['ver'] = '/%s' % kw['ver']

if args:
args = (six.ensure_str(args[0]), ) + args[1:]
# Try to build the URL with routes.url_for
return _routes_default_url_for(*args, **kw)

Expand Down
5 changes: 4 additions & 1 deletion ckan/logic/__init__.py
Expand Up @@ -35,7 +35,10 @@ def __init__(self, message=''):
super(ActionError, self).__init__(message)

def __str__(self):
return str(self.message)
msg = self.message
if not isinstance(msg, six.string_types):
msg = str(msg)
return six.ensure_text(msg)


class NotFound(ActionError):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/converters.py
Expand Up @@ -54,7 +54,7 @@ def free_tags_only(key, data, errors, context):
tag_number = key[1]
if not data.get(('tags', tag_number, 'vocabulary_id')):
return
for k in data.keys():
for k in list(data.keys()):
if k[0] == 'tags' and k[1] == tag_number:
del data[k]

Expand Down
2 changes: 1 addition & 1 deletion ckan/model/domain_object.py
Expand Up @@ -100,7 +100,7 @@ def as_dict(self):
return _dict

def __str__(self):
return self.__unicode__().encode('utf8')
return repr(self)

def __unicode__(self):
repr = u'<%s' % self.__class__.__name__
Expand Down
34 changes: 17 additions & 17 deletions ckan/tests/controllers/test_package.py
Expand Up @@ -475,8 +475,8 @@ class TestPackageRead(object):
def test_read(self, app):
dataset = factories.Dataset()
response = app.get(url_for("dataset.read", id=dataset["name"]))
response.mustcontain("Test Dataset")
response.mustcontain("Just another test dataset")
assert helpers.body_contains(response, "Test Dataset")
assert helpers.body_contains(response, "Just another test dataset")

def test_organization_members_can_read_private_datasets(self, app):
members = {
Expand Down Expand Up @@ -554,7 +554,7 @@ def test_read_dataset_as_it_used_to_be(self, app):
),
extra_environ=env,
)
response.mustcontain("Original title")
assert helpers.body_contains(response, "Original title")

def test_read_dataset_as_it_used_to_be_but_is_unmigrated(self, app):
# Renders the dataset using the activity detail, when that Activity was
Expand Down Expand Up @@ -667,7 +667,7 @@ def test_anon_user_cannot_delete_owned_dataset(self, app):
response = app.post(
url_for("dataset.delete", id=dataset["name"]), status=403
)
response.mustcontain("Unauthorized to delete package")
assert helpers.body_contains(response, "Unauthorized to delete package")

deleted = helpers.call_action("package_show", id=dataset["id"])
assert "active" == deleted["state"]
Expand All @@ -687,7 +687,7 @@ def test_logged_in_user_cannot_delete_owned_dataset(self, app):

)
assert 403 == response.status_int
response.mustcontain("Unauthorized to delete package")
assert helpers.body_contains(response, "Unauthorized to delete package")

def test_confirm_cancel_delete(self, app):
"""Test confirmation of deleting datasets
Expand All @@ -706,7 +706,7 @@ def test_confirm_cancel_delete(self, app):
)
assert 200 == response.status_int
message = "Are you sure you want to delete dataset - {name}?"
response.mustcontain(message.format(name=dataset["title"]))
assert helpers.body_contains(response, message.format(name=dataset["title"]))

form = response.forms["confirm-dataset-delete-form"]
response = form.submit("cancel")
Expand Down Expand Up @@ -932,7 +932,7 @@ def test_resource_view_create(self, app):
response = app.post(
url, {"title": "Test Image View"}, extra_environ=env
).follow(extra_environ=env)
response.mustcontain("Test Image View")
assert helpers.body_contains(response, "Test Image View")

def test_resource_view_edit(self, app):
user = factories.User()
Expand All @@ -955,7 +955,7 @@ def test_resource_view_edit(self, app):
response = app.post(
url, {"title": "Updated RV Title"}, extra_environ=env
).follow(extra_environ=env)
response.mustcontain("Updated RV Title")
assert helpers.body_contains(response, "Updated RV Title")

def test_resource_view_delete(self, app):
user = factories.User()
Expand All @@ -978,7 +978,7 @@ def test_resource_view_delete(self, app):
response = app.post(
url, {"delete": "Delete"}, extra_environ=env
).follow(extra_environ=env)
response.mustcontain("This resource has no views")
assert helpers.body_contains(response, "This resource has no views")

def test_existent_resource_view_page_returns_ok_code(self, app):
resource_view = factories.ResourceView()
Expand Down Expand Up @@ -1013,7 +1013,7 @@ def test_resource_view_description_is_rendered_as_markdown(self, app):
view_id=resource_view["id"],
)
response = app.get(url)
response.mustcontain("Some <strong>Markdown</strong>")
assert helpers.body_contains(response, "Some <strong>Markdown</strong>")


@pytest.mark.usefixtures("clean_db", "with_request_context")
Expand Down Expand Up @@ -1146,7 +1146,7 @@ def test_dataset_owners_can_delete_resources(self, app):
)
response = response.follow()
assert 200 == response.status_int
response.mustcontain("This dataset has no data")
assert helpers.body_contains(response, "This dataset has no data")

with pytest.raises(p.toolkit.ObjectNotFound):
helpers.call_action("resource_show", id=resource["id"])
Expand Down Expand Up @@ -1185,7 +1185,7 @@ def test_anon_users_cannot_delete_owned_resources(self, app):
),
status=403,
)
response.mustcontain("Unauthorized to delete package")
assert helpers.body_contains(response, "Unauthorized to delete package")

def test_logged_in_users_cannot_delete_resources_they_do_not_own(
self, app
Expand All @@ -1211,7 +1211,7 @@ def test_logged_in_users_cannot_delete_resources_they_do_not_own(

)
assert 403 == response.status_int
response.mustcontain("Unauthorized to delete package")
assert helpers.body_contains(response, "Unauthorized to delete package")

def test_sysadmins_can_delete_any_resource(self, app):
owner_org = factories.Organization()
Expand All @@ -1230,7 +1230,7 @@ def test_sysadmins_can_delete_any_resource(self, app):
)
response = response.follow()
assert 200 == response.status_int
response.mustcontain("This dataset has no data")
assert helpers.body_contains(response, "This dataset has no data")

with pytest.raises(p.toolkit.ObjectNotFound):
helpers.call_action("resource_show", id=resource["id"])
Expand All @@ -1257,7 +1257,7 @@ def test_confirm_and_cancel_deleting_a_resource(self, app):
)
assert 200 == response.status_int
message = "Are you sure you want to delete resource - {name}?"
response.mustcontain(message.format(name=resource["name"]))
assert helpers.body_contains(response, message.format(name=resource["name"]))

# cancelling sends us back to the resource edit page
form = response.forms["confirm-resource-delete-form"]
Expand Down Expand Up @@ -2100,5 +2100,5 @@ def test_simple(self, app):
response = app.get(
url_for("dataset.changes", id=activity.id), extra_environ=env
)
response.mustcontain("First")
response.mustcontain("Second")
assert helpers.body_contains(response, "First")
assert helpers.body_contains(response, "Second")
18 changes: 10 additions & 8 deletions ckan/tests/controllers/test_user.py
Expand Up @@ -114,12 +114,12 @@ def test_registered_user_login(self, app):
final_response = helpers.webtest_maybe_follow(submit_response)

# the response is the user dashboard, right?
final_response.mustcontain(
assert helpers.body_contains(
'<a href="/dashboard/">Dashboard</a>',
'<span class="username">{0}</span>'.format(user["fullname"]),
)
# and we're definitely not back on the login page.
final_response.mustcontain(no='<h1 class="page-heading">Login</h1>')
assert not helpers.body_contains('<h1 class="page-heading">Login</h1>')

def test_registered_user_login_bad_password(self, app):
"""
Expand All @@ -145,14 +145,16 @@ def test_registered_user_login_bad_password(self, app):
final_response = helpers.webtest_maybe_follow(submit_response)

# the response is the login page again
final_response.mustcontain(
'<h1 class="page-heading">Login</h1>',
"Login failed. Bad username or password.",
assert helpers.body_contains(final_response,
'<h1 class="page-heading">Login</h1>'
)
assert helpers.body_contains(final_response,
"Login failed. Bad username or password."
)
# and we're definitely not on the dashboard.
final_response.mustcontain(no='<a href="/dashboard">Dashboard</a>'),
final_response.mustcontain(
no='<span class="username">{0}</span>'.format(user["fullname"])
assert not helpers.body_contains(final_response, '<a href="/dashboard">Dashboard</a>'),
assert not helpers.body_contains(final_response,
'<span class="username">{0}</span>'.format(user["fullname"])
)

def test_user_logout_url_redirect(self, app):
Expand Down
14 changes: 12 additions & 2 deletions ckan/tests/helpers.py
Expand Up @@ -28,7 +28,7 @@
import smtplib

from flask.testing import Client as FlaskClient

from flask.wrappers import Response
import webtest
import nose.tools
import pytest
Expand Down Expand Up @@ -174,6 +174,15 @@ def body_contains(res, content):
return content in body


class CKANResponse(Response):
@property
def body(self):
return self.data

def __contains__(self, segment):
return body_contains(self, segment)


class CKANTestApp(object):
"""A wrapper around webtest.TestApp
Expand All @@ -195,11 +204,12 @@ def __init__(self, app):
self.app = app

def test_client(self, use_cookies=True):
return CKANTestClient(self.app, self.flask_app.response_class, use_cookies=use_cookies)
return CKANTestClient(self.app, CKANResponse, use_cookies=use_cookies)
self.flask_app.test_client_class = CKANTestClient
return self.flask_app.test_client()

def post(self, url, *args, **kwargs):
print(url)
res = self.test_client().post(url, *args, **kwargs)
return res

Expand Down
2 changes: 1 addition & 1 deletion ckanext/datapusher/tests/test_action.py
Expand Up @@ -314,7 +314,7 @@ def test_does_not_resubmit_if_a_dataset_field_changes_in_the_meantime(


@pytest.mark.ckan_config("ckan.plugins", "datapusher datastore")
@pytest.mark.usefixtures("clean_db", "with_plugins")
@pytest.mark.usefixtures("clean_db", "with_plugins", "with_request_context")
def test_duplicated_tasks(app):
def submit(res, user):
return helpers.call_action(
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datapusher/tests/test_controller.py
Expand Up @@ -7,7 +7,7 @@


@pytest.mark.ckan_config(u"ckan.plugins", u"datapusher datastore")
@pytest.mark.usefixtures(u"clean_db", u"with_plugins")
@pytest.mark.usefixtures(u"clean_db", u"with_plugins", u"with_request_context")
def test_resource_data(app):
if not tests.is_datastore_supported():
pytest.skip(u"Datastore not supported")
Expand Down
20 changes: 8 additions & 12 deletions ckanext/datapusher/tests/test_interfaces.py
Expand Up @@ -28,28 +28,28 @@ def after_upload(self, context, resource_dict, package_dict):
self.after_upload_calls += 1


@pytest.mark.ckan_config(
"ckan.plugins", "datastore datapusher test_datapusher_plugin"
)
@pytest.mark.usefixtures("with_plugins")
class TestInterace(object):
sysadmin_user = None
normal_user = None

@pytest.fixture(autouse=True)
def setup_class(self, clean_db):
def setup_class(self, clean_db, test_request_context):
if not tests.is_datastore_supported():
pytest.skip("Datastore not supported")

resource = factories.Resource(url_type="datastore")
self.dataset = factories.Dataset(resources=[resource])

self.sysadmin_user = factories.User(name="testsysadmin", sysadmin=True)
self.normal_user = factories.User(name="annafan")
with test_request_context():
self.sysadmin_user = factories.User(name="testsysadmin", sysadmin=True)
self.normal_user = factories.User(name="annafan")
engine = db.get_write_engine()
self.Session = orm.scoped_session(orm.sessionmaker(bind=engine))

@responses.activate
@pytest.mark.ckan_config(
"ckan.plugins", "datastore datapusher test_datapusher_plugin"
)
@pytest.mark.usefixtures("with_plugins")
def test_send_datapusher_creates_task(self, test_request_context):
responses.add(
responses.POST,
Expand Down Expand Up @@ -79,10 +79,6 @@ def test_send_datapusher_creates_task(self, test_request_context):
},
)

@pytest.mark.ckan_config(
"ckan.plugins", "datastore datapusher test_datapusher_plugin"
)
@pytest.mark.usefixtures("with_plugins")
def test_after_upload_called(self):
dataset = factories.Dataset()
resource = factories.Resource(package_id=dataset["id"])
Expand Down
28 changes: 14 additions & 14 deletions ckanext/datastore/tests/test_dump.py
Expand Up @@ -244,23 +244,23 @@ def test_dump_json(self, app):
)
)

content = json.loads(res.data)
content = json.loads(six.ensure_text(res.data))
expected_content = {
'fields': [
{'id': '_id', 'type': 'int'},
{'id': 'bük', 'type': 'text'},
{'id': 'author', 'type': 'text'},
{'id': 'published', 'type': 'timestamp'},
{'id': 'characters', 'type': '_text'},
{'id': 'random_letters', 'type': '_text'},
{'id': 'nested', 'type': 'json'}
u'fields': [
{u'id': u'_id', u'type': u'int'},
{u'id': u'bük', u'type': u'text'},
{u'id': u'author', u'type': u'text'},
{u'id': u'published', u'type': u'timestamp'},
{u'id': u'characters', u'type': u'_text'},
{u'id': u'random_letters', u'type': u'_text'},
{u'id': u'nested', u'type': u'json'}
],
'records': [
u'records': [
[
1, 'annakarenina', 'tolstoy', '2005-03-01T00:00:00',
['Princess Anna', 'Sergius'],
['a', 'e', 'x'],
['b', {'moo': 'moo'}]
1, u'annakarenina', u'tolstoy', u'2005-03-01T00:00:00',
[u'Princess Anna', u'Sergius'],
[u'a', u'e', u'x'],
[u'b', {u'moo': u'moo'}]
]
]
}
Expand Down

0 comments on commit 973bc41

Please sign in to comment.