Skip to content

Commit

Permalink
Datastore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Dec 26, 2019
1 parent bde93da commit 0d0c1ed
Show file tree
Hide file tree
Showing 26 changed files with 380 additions and 420 deletions.
2 changes: 1 addition & 1 deletion ckan/authz.py
Expand Up @@ -385,7 +385,7 @@ def get_user_id_for_username(user_name, allow_none=False):
try:
if c.userobj and c.userobj.name == user_name:
return c.userobj.id
except TypeError:
except (TypeError, AttributeError):
# c is not available
pass
user = model.User.get(user_name)
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/navl/validators.py
Expand Up @@ -183,7 +183,7 @@ def limit_to_configured_maximum(config_option, default_limit):
'''
def callable(key, data, errors, context):

value = data.get(key)
value = convert_int(data.get(key), context)
limit = int(config.get(config_option, default_limit))
if value > limit:
data[key] = limit
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/__init__.py
Expand Up @@ -35,7 +35,7 @@ def __init__(self, message=''):
super(ActionError, self).__init__(message)

def __str__(self):
return self.message
return str(self.message)


class NotFound(ActionError):
Expand Down
3 changes: 2 additions & 1 deletion ckan/model/domain_object.py
@@ -1,6 +1,7 @@
# encoding: utf-8

import datetime
import six
from collections import OrderedDict

import sqlalchemy as sa
Expand Down Expand Up @@ -114,4 +115,4 @@ def __unicode__(self):
return repr

def __repr__(self):
return self.__unicode__().encode('utf-8')
return six.ensure_str(self.__unicode__())
12 changes: 6 additions & 6 deletions ckan/tests/controllers/test_package.py
Expand Up @@ -465,7 +465,7 @@ def test_edit_a_dataset_that_does_not_exist_404s(self, app):
response = app.get(
url_for("dataset.edit", id="does-not-exist"),
extra_environ=env,
expect_errors=True,

)
assert 404 == response.status_int

Expand Down Expand Up @@ -637,7 +637,7 @@ def test_owner_delete(self, app):
def test_delete_on_non_existing_dataset(self, app):
response = app.post(
url_for("dataset.delete", id="schrodingersdatset"),
expect_errors=True,

)
assert 404 == response.status_int

Expand Down Expand Up @@ -684,7 +684,7 @@ def test_logged_in_user_cannot_delete_owned_dataset(self, app):
response = app.post(
url_for("dataset.delete", id=dataset["name"]),
extra_environ=env,
expect_errors=True,

)
assert 403 == response.status_int
response.mustcontain("Unauthorized to delete package")
Expand Down Expand Up @@ -752,7 +752,7 @@ def test_404_on_manage_dataset_resource_listing_page_that_does_not_exist(
response = app.get(
url_for("dataset.resources", id="does-not-exist"),
extra_environ=env,
expect_errors=True,

)
assert 404 == response.status_int

Expand Down Expand Up @@ -1165,7 +1165,7 @@ def test_deleting_non_existing_resource_404s(self, app):
resource_id="doesnotexist",
),
extra_environ=env,
expect_errors=True,

)
assert 404 == response.status_int

Expand Down Expand Up @@ -1208,7 +1208,7 @@ def test_logged_in_users_cannot_delete_resources_they_do_not_own(
resource_id=resource["id"],
),
extra_environ=env,
expect_errors=True,

)
assert 403 == response.status_int
response.mustcontain("Unauthorized to delete package")
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/controllers/test_user.py
Expand Up @@ -181,8 +181,8 @@ def test_non_root_user_logout_url_redirect(self, app):
logout_url = url_for("user.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)
assert logout_response.status_int == 302
logout_response = app.get(logout_url)
assert logout_response.status_code == 302
assert "/my/prefix/user/logout" in logout_response.location

def test_not_logged_in_dashboard(self, app):
Expand Down
23 changes: 19 additions & 4 deletions ckan/tests/helpers.py
Expand Up @@ -200,19 +200,34 @@ def test_client(self, use_cookies=True):
return self.flask_app.test_client()

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

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


class CKANTestClient(FlaskClient):
def open(self, *args, **kwargs):
status = kwargs.pop('status', None)
extra_environ = kwargs.pop('extra_environ', None)
if extra_environ:
kwargs['environ_overrides'] = extra_environ
# params = kwargs.pop('params', None)
# if params:
# kwargs['query_string'] = params


if args and isinstance(args[0], six.string_types):
kwargs.setdefault('follow_redirects', True)
kwargs.setdefault('base_url', config['ckan.site_url'])
result = super(CKANTestClient, self).open(*args, **kwargs)
return result
res = super(CKANTestClient, self).open(*args, **kwargs)

if status:
assert res.status_code == status, (res.status_code, status)

return res


def _get_test_app():
Expand Down
3 changes: 1 addition & 2 deletions ckan/tests/legacy/__init__.py
Expand Up @@ -420,10 +420,9 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs):
:rtype: dictionary
"""
params = json.dumps(kwargs)
response = app.post(
"/api/action/{0}".format(action),
params=params,
json=kwargs,
extra_environ={"Authorization": str(apikey)},
status=status,
)
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/legacy/functional/api/base.py
Expand Up @@ -344,7 +344,7 @@ def http_request(
if extra_environ:
environ.update(extra_environ)
self.app._set_headers({}, environ)
req = TestRequest(offset, environ, expect_errors=False)
req = TestRequest(offset, environ)
return self.app.do_request(req, status=status)

def set_env(self, extra_environ):
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/legacy/functional/api/test_api.py
Expand Up @@ -43,7 +43,7 @@ def test_sideeffect_action_is_not_get_able(self, app):
offset = self.offset("/action/package_create")
data_dict = {"type": "dataset", "name": "a-name"}
res = app.get(
offset, params=data_dict, status=[400], expect_errors=True
offset, json=data_dict, status=400
)
assert body_contains(
res,
Expand Down
9 changes: 4 additions & 5 deletions ckan/tests/legacy/functional/api/test_user.py
Expand Up @@ -66,9 +66,8 @@ def test_user_create_api_enabled_sysadmin(self, app):
}
res = app.post(
"/api/3/action/user_create",
json.dumps(params),
data=json.dumps(params),
extra_environ={"Authorization": str(self.sysadmin_user.apikey)},
expect_errors=True,
)
res_dict = res.json
assert res_dict["success"] is True
Expand All @@ -80,7 +79,7 @@ def test_user_create_api_disabled_anon(self, app):
"password": "TestPassword1",
}
res = app.post(
"/api/3/action/user_create", json.dumps(params), expect_errors=True
"/api/3/action/user_create", json.dumps(params)
)
res_dict = res.json
assert res_dict["success"] is False
Expand Down Expand Up @@ -140,7 +139,7 @@ def test_user_create_api_disabled(self, app):
"password": "TestPassword1",
}
res = app.post(
"/api/3/action/user_create", json.dumps(params), expect_errors=True
"/api/3/action/user_create", json.dumps(params)
)
res_dict = res.json
assert res_dict["success"] is False
Expand All @@ -164,7 +163,7 @@ def test_user_create_api_disabled(self, app):
"password": "TestPassword1",
}
res = app.post(
"/api/3/action/user_create", json.dumps(params), expect_errors=True
"/api/3/action/user_create", json.dumps(params)
)
res_dict = res.json
assert res_dict["success"] is False
Expand Down
4 changes: 2 additions & 2 deletions ckanext/datastore/backend/postgres.py
Expand Up @@ -741,7 +741,7 @@ def convert(data, type_name):
sub_type = type_name[1:]
return [convert(item, sub_type) for item in data]
if type_name == 'tsvector':
return text_type(data, 'utf-8')
return six.ensure_text(data)
if isinstance(data, datetime.datetime):
return data.isoformat()
if isinstance(data, (int, float)):
Expand Down Expand Up @@ -1209,7 +1209,7 @@ def validate(context, data_dict):
elif isinstance(values, (list, tuple)):
value = values[0]
elif isinstance(values, dict):
value = values.keys()[0]
value = list(values.keys())[0]
else:
value = values

Expand Down
3 changes: 2 additions & 1 deletion ckanext/datastore/helpers.py
Expand Up @@ -5,6 +5,7 @@

import ckan.common as converters
import sqlparse
import six

from six import string_types

Expand Down Expand Up @@ -93,7 +94,7 @@ def get_table_names_from_sql(context, sql):
sql = queries.pop()
result = context['connection'].execute(
'EXPLAIN (VERBOSE, FORMAT JSON) {0}'.format(
sql.encode('utf-8'))).fetchone()
six.ensure_str(sql))).fetchone()

try:
query_plan = json.loads(result['QUERY PLAN'])
Expand Down
1 change: 0 additions & 1 deletion ckanext/datastore/logic/auth.py
Expand Up @@ -10,7 +10,6 @@ def datastore_auth(context, data_dict, privilege='resource_update'):
user = context.get('user')

authorized = p.toolkit.check_access(privilege, context, data_dict)

if not authorized:
return {
'success': False,
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/plugin.py
Expand Up @@ -168,7 +168,7 @@ def datastore_validate(self, context, data_dict, fields_types):
del data_dict['q']
column_names.append(u'rank')
elif isinstance(q, dict):
for key in q.keys():
for key in list(q.keys()):
if key in fields_types and isinstance(q[key],
string_types):
column_names.append(u'rank ' + key)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/tests/sample_datastore_plugin.py
Expand Up @@ -11,7 +11,7 @@ class SampleDataStorePlugin(p.SingletonPlugin):
def datastore_validate(self, context, data_dict, column_names):
valid_filters = ("age_between", "age_not_between", "insecure_filter")
filters = data_dict.get("filters", {})
for key in filters.keys():
for key in list(filters.keys()):
if key in valid_filters:
del filters[key]

Expand Down

0 comments on commit 0d0c1ed

Please sign in to comment.