Skip to content

Commit

Permalink
provide request context
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 2, 2020
1 parent 3e979fc commit c0e540f
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 100 deletions.
5 changes: 2 additions & 3 deletions ckan/logic/action/get.py
Expand Up @@ -1741,9 +1741,8 @@ def package_search(context, data_dict):
for package in query.results:
if isinstance(package, text_type):
package = {result_fl[0]: package}
if package.get('extras'):
package.update(package['extras'] )
package.pop('extras')
extras = package.pop('extras', {})
package.update(extras)
results.append(package)
else:
for package in query.results:
Expand Down
5 changes: 2 additions & 3 deletions ckan/logic/validators.py
Expand Up @@ -86,7 +86,7 @@ def int_validator(value, context):
except TypeError:
try:
return int(value)
except ValueError:
except (TypeError, ValueError):
pass
else:
if not part:
Expand Down Expand Up @@ -503,7 +503,6 @@ def ignore_not_sysadmin(key, data, errors, context):

user = context.get('user')
ignore_auth = context.get('ignore_auth')

if ignore_auth or (user and authz.is_sysadmin(user)):
return

Expand Down Expand Up @@ -698,7 +697,7 @@ def url_validator(key, data, errors, context):
try:
pieces = urlparse(url)
if all([pieces.scheme, pieces.netloc]) and \
set(pieces.netloc) <= set(string.letters + string.digits + '-.') and \
set(pieces.netloc) <= set(string.ascii_letters + string.digits + '-.') and \
pieces.scheme in ['http', 'https']:
return
except ValueError:
Expand Down
3 changes: 3 additions & 0 deletions ckan/model/domain_object.py
Expand Up @@ -99,6 +99,9 @@ def as_dict(self):
_dict[col.name] = val
return _dict

def __lt__(self, other):
return self.name < other.name

def __str__(self):
return repr(self)

Expand Down
1 change: 1 addition & 0 deletions ckan/tests/legacy/models/test_package.py
Expand Up @@ -27,6 +27,7 @@ def initial_data(self, clean_db):
model.Session.commit()
model.Session.remove()

@pytest.mark.usefixtures("with_request_context")
def test_as_dict(self):
pkg = model.Package.by_name(self.name)
out = pkg.as_dict()
Expand Down
44 changes: 23 additions & 21 deletions ckan/tests/logic/action/test_create.py
Expand Up @@ -5,6 +5,7 @@
import cgi
import mock
import pytest
import six

import ckan
import ckan.logic as logic
Expand All @@ -14,7 +15,7 @@
import ckan.tests.helpers as helpers
from ckan.common import config

from six import string_types, StringIO
from six import string_types, StringIO, BytesIO

from pyfakefs import fake_filesystem

Expand Down Expand Up @@ -43,7 +44,7 @@ def mock_open_if_open_fails(*args, **kwargs):
return fake_open(*args, **kwargs)


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestUserInvite(object):
@mock.patch("ckan.lib.mailer.send_invite")
def test_invited_user_is_created_as_pending(self, _):
Expand Down Expand Up @@ -477,8 +478,8 @@ def test_mimetype_by_upload_by_filename(self, monkeypatch):
If there's no url or the mimetype can't be guessed by the url, mimetype will be guessed by the extension in the filename
"""

test_file = StringIO()
test_file.write(
test_file = BytesIO()
test_file.write(six.ensure_binary(
"""
"info": {
"title": "BC Data Catalogue API",
Expand All @@ -496,7 +497,7 @@ def test_mimetype_by_upload_by_filename(self, monkeypatch):
"version": "3.0.0"
}
"""
)
))
test_resource = FakeFileStorage(test_file, "test.json")

context = {}
Expand Down Expand Up @@ -530,15 +531,15 @@ def test_mimetype_by_upload_by_file(self, monkeypatch):
If the mimetype can't be guessed by the url or filename, mimetype will be guessed by the contents inside the file
"""

test_file = StringIO()
test_file.write(
test_file = BytesIO()
test_file.write(six.ensure_binary(
"""
Snow Course Name, Number, Elev. metres, Date of Survey, Snow Depth cm, Water Equiv. mm, Survey Code, % of Normal, Density %, Survey Period, Normal mm
SKINS LAKE,1B05,890,2015/12/30,34,53,,98,16,JAN-01,54
MCGILLIVRAY PASS,1C05,1725,2015/12/31,88,239,,87,27,JAN-01,274
NAZKO,1C08,1070,2016/01/05,20,31,,76,16,JAN-01,41
"""
)
))
test_resource = FakeFileStorage(test_file, "")

context = {}
Expand Down Expand Up @@ -566,15 +567,15 @@ def test_size_of_resource_by_upload(self, monkeypatch):
The size of the resource determined by the uploaded file
"""

test_file = StringIO()
test_file.write(
test_file = BytesIO()
test_file.write(six.ensure_binary(
"""
Snow Course Name, Number, Elev. metres, Date of Survey, Snow Depth cm, Water Equiv. mm, Survey Code, % of Normal, Density %, Survey Period, Normal mm
SKINS LAKE,1B05,890,2015/12/30,34,53,,98,16,JAN-01,54
MCGILLIVRAY PASS,1C05,1725,2015/12/31,88,239,,87,27,JAN-01,274
NAZKO,1C08,1070,2016/01/05,20,31,,76,16,JAN-01,41
"""
)
))
test_resource = FakeFileStorage(test_file, "test.csv")

context = {}
Expand Down Expand Up @@ -612,6 +613,7 @@ def test_size_of_resource_by_user(self):
size = int(result.pop("size"))
assert size == 500

@pytest.mark.usefixtures("with_request_context")
def test_extras(self):
user = factories.User()
dataset = factories.Dataset(user=user)
Expand All @@ -636,7 +638,7 @@ def test_extras(self):
assert "someotherkey" not in resource


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestMemberCreate(object):
def test_group_member_creation(self):
user = factories.User()
Expand Down Expand Up @@ -721,7 +723,7 @@ def test_org_member_creation_raises_validation_error_if_role_missing(self):
)


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestDatasetCreate(object):
def test_normal_user_cant_set_id(self):
user = factories.User()
Expand Down Expand Up @@ -927,7 +929,7 @@ def test_return_id_only(self):
assert isinstance(dataset, string_types)


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestGroupCreate(object):
def test_create_group(self):
user = factories.User()
Expand Down Expand Up @@ -986,7 +988,7 @@ def test_create_matches_show(self):
assert created[k] == shown[k], k


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestOrganizationCreate(object):
def test_create_organization(self):
user = factories.User()
Expand Down Expand Up @@ -1063,7 +1065,7 @@ def test_create_organization_custom_type(self):
assert org["type"] == custom_org_type


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestUserCreate(object):
def test_user_create_with_password_hash(self):
sysadmin = factories.Sysadmin()
Expand All @@ -1082,7 +1084,7 @@ def test_user_create_with_password_hash(self):

def test_user_create_password_hash_not_for_normal_users(self):
normal_user = factories.User()
context = {"user": normal_user["name"]}
context = {"user": normal_user["name"], "ignore_auth": False}

user = helpers.call_action(
"user_create",
Expand All @@ -1105,7 +1107,7 @@ def _clear_activities():
model.Session.flush()


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestFollowDataset(object):
def test_no_activity(self, app):

Expand All @@ -1122,7 +1124,7 @@ def test_no_activity(self, app):
# https://github.com/ckan/ckan/pull/317


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestFollowGroup(object):
def test_no_activity(self, app):
user = factories.User()
Expand All @@ -1138,7 +1140,7 @@ def test_no_activity(self, app):
# https://github.com/ckan/ckan/pull/317


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestFollowOrganization(object):
def test_no_activity(self, app):
user = factories.User()
Expand All @@ -1154,7 +1156,7 @@ def test_no_activity(self, app):
# https://github.com/ckan/ckan/pull/317


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestFollowUser(object):
def test_no_activity(self, app):

Expand Down
12 changes: 6 additions & 6 deletions ckan/tests/logic/action/test_delete.py
Expand Up @@ -14,7 +14,7 @@
import ckan.tests.helpers as helpers


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestDelete:
def test_resource_delete(self):
user = factories.User()
Expand All @@ -36,7 +36,7 @@ def test_resource_delete(self):


@pytest.mark.ckan_config("ckan.plugins", "image_view")
@pytest.mark.usefixtures("clean_db", "with_plugins")
@pytest.mark.usefixtures("clean_db", "with_plugins", "with_request_context")
class TestDeleteResourceViews(object):
def test_resource_view_delete(self):
resource_view = factories.ResourceView()
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_tag_delete_with_unicode_returns_unicode_error(self):
assert 0, "Should have raised NotFound"


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestGroupPurge(object):
def test_a_non_sysadmin_cant_purge_group(self):
user = factories.User()
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_bad_id_returns_404(self):
helpers.call_action("group_purge", id="123")


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestOrganizationPurge(object):
def test_a_non_sysadmin_cant_purge_org(self):
user = factories.User()
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_bad_id_returns_404(self):
helpers.call_action("organization_purge", id="123")


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestDatasetPurge(object):
def test_a_non_sysadmin_cant_purge_dataset(self):
user = factories.User()
Expand Down Expand Up @@ -439,7 +439,7 @@ def test_bad_id_returns_404(self):
helpers.call_action("dataset_purge", id="123")


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("clean_db", "with_request_context")
class TestUserDelete(object):
def test_user_delete(self):
user = factories.User()
Expand Down

0 comments on commit c0e540f

Please sign in to comment.