Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 2.7, 3.9 ]
python-version: [ 3.9, "3.10" ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install requirements
Expand All @@ -25,18 +25,16 @@ jobs:
fail-fast: false
matrix:
include:
- ckan-container-version: "2.9-py2"
ckan-postgres-version: "2.9"
ckan-solr-version: "2.9-solr8"
- ckan-container-version: "2.9"
ckan-postgres-version: "2.9"
ckan-solr-version: "2.9-solr8"
- ckan-container-version: "2.10"
- ckan-version: "ckan-dev:2.10-py3.10"
ckan-postgres-version: "2.10"
ckan-solr-version: "2.10"
- ckan-version: "ckan-dev:2.11-py3.10"
ckan-postgres-version: "2.11"
ckan-solr-version: "2.11-solr9"

container:
image: openknowledge/ckan-dev:${{ matrix.ckan-container-version }}
image: ckan/${{ matrix.ckan-version }}
options: --user root
services:
solr:
image: ckan/ckan-solr:${{ matrix.ckan-solr-version }}
Expand All @@ -57,7 +55,7 @@ jobs:
CKAN_REDIS_URL: redis://redis:6379/1

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v6
- name: Install requirements
run: |
pip install -r requirements.txt
Expand Down
10 changes: 8 additions & 2 deletions ckanext/versions/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

from ckanext.versions.model import Version

# CKAN 2.10+ moved Activity to a separate module
try:
from ckan.model.activity import Activity
except ImportError:
Activity = core_model.Activity

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -107,9 +113,9 @@ def resource_version_create(context, data_dict):
site_id = toolkit.config.get('ckan.site_id', 'ckan_site_user')
creator_user_id = model.User.get(site_id).id

activity = model.Session.query(model.Activity). \
activity = model.Session.query(Activity). \
filter_by(object_id=resource.package_id). \
order_by(model.Activity.timestamp.desc()). \
order_by(Activity.timestamp.desc()). \
first()

if not activity:
Expand Down
12 changes: 9 additions & 3 deletions ckanext/versions/logic/dataset_version_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from ckanext.versions.logic.action import version_show
from ckanext.versions.model import Version

# CKAN 2.10+ moved Activity to a separate module
try:
from ckan.model.activity import Activity
except ImportError:
Activity = core_model.Activity

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -48,11 +54,11 @@ def dataset_version_create(context, data_dict):
creator_user_id = context['auth_user_obj'].id

if activity_id:
activity = model.Activity.get(activity_id)
activity = Activity.get(activity_id)
else:
activity = model.Session.query(model.Activity). \
activity = model.Session.query(Activity). \
filter_by(object_id=dataset_id). \
order_by(model.Activity.timestamp.desc()). \
order_by(Activity.timestamp.desc()). \
first()
if not activity:
raise toolkit.ObjectNotFound('Activity not found')
Expand Down
12 changes: 10 additions & 2 deletions ckanext/versions/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ def as_dict(self):


def create_tables():
Version.__table__.create()
from ckan.model import meta
Version.__table__.create(bind=meta.engine, checkfirst=True)


def tables_exist():
return Version.__table__.exists()
from ckan.model import meta
try:
engine = meta.engine
if engine is None:
return False
return Version.__table__.exists(bind=engine)
except (AttributeError, TypeError):
return False
130 changes: 79 additions & 51 deletions ckanext/versions/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,61 @@
from ckan.tests import factories, helpers


@pytest.fixture
def org_admin():
return factories.User()


@pytest.fixture
def org_editor():
return factories.User()


@pytest.fixture
def org_member():
return factories.User()


@pytest.fixture
def other_org_admin():
return factories.User()


@pytest.fixture
def admin_user():
return factories.Sysadmin()


@pytest.fixture
def org(org_admin, org_editor, org_member):
return factories.Organization(
users=[
{'name': org_admin['name'], 'capacity': 'admin'},
{'name': org_editor['name'], 'capacity': 'editor'},
{'name': org_member['name'], 'capacity': 'member'},
]
)


@pytest.fixture
def other_org(other_org_admin):
return factories.Organization(
users=[
{'name': other_org_admin['name'], 'capacity': 'admin'},
]
)


@pytest.fixture
def private_dataset(org):
return factories.Dataset(owner_org=org['id'], private=True)


@pytest.fixture
def public_dataset(org):
return factories.Dataset(owner_org=org['id'], private=False)


@pytest.mark.usefixtures("clean_db", "versions_setup")
class TestVersionsAuth(object):

Expand All @@ -13,33 +68,6 @@ def _get_context(self, user):
'user': user if isinstance(user, str) else user['name']
}

def setup(self):
# TODO: Refactor to a new pytest approach
self.org_admin = factories.User()
self.org_editor = factories.User()
self.org_member = factories.User()
self.other_org_admin = factories.User()
self.admin_user = factories.Sysadmin()

self.org = factories.Organization(
users=[
{'name': self.org_admin['name'], 'capacity': 'admin'},
{'name': self.org_editor['name'], 'capacity': 'editor'},
{'name': self.org_member['name'], 'capacity': 'member'},
]
)

self.other_org = factories.Organization(
users=[
{'name': self.other_org_admin['name'], 'capacity': 'admin'},
]
)

self.private_dataset = factories.Dataset(owner_org=self.org['id'],
private=True)
self.public_dataset = factories.Dataset(owner_org=self.org['id'],
private=False)

@pytest.mark.parametrize("user_type, dataset_type", [
('org_admin', 'private_dataset'),
('org_admin', 'public_dataset'),
Expand All @@ -48,11 +76,11 @@ def setup(self):
('admin_user', 'private_dataset'),
('admin_user', 'public_dataset'),
])
def test_create_is_authorized(self, user_type, dataset_type):
def test_create_is_authorized(self, user_type, dataset_type, request):
"""Test that authorized users can create versions on a given dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
assert helpers.call_auth('version_create',
context=context,
Expand All @@ -64,12 +92,12 @@ def test_create_is_authorized(self, user_type, dataset_type):
('other_org_admin', 'private_dataset'),
('other_org_admin', 'public_dataset'),
])
def test_create_is_unauthorized(self, user_type, dataset_type):
def test_create_is_unauthorized(self, user_type, dataset_type, request):
"""Test that unauthorized users cannot create versions on a given
dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
with pytest.raises(toolkit.NotAuthorized):
helpers.call_auth(
Expand All @@ -85,11 +113,11 @@ def test_create_is_unauthorized(self, user_type, dataset_type):
('admin_user', 'private_dataset'),
('admin_user', 'public_dataset'),
])
def test_delete_is_authorized(self, user_type, dataset_type):
def test_delete_is_authorized(self, user_type, dataset_type, request):
"""Test that authorized users can delete versions on a given dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
assert helpers.call_auth('version_delete',
context=context,
Expand All @@ -101,12 +129,12 @@ def test_delete_is_authorized(self, user_type, dataset_type):
('other_org_admin', 'private_dataset'),
('other_org_admin', 'public_dataset'),
])
def test_delete_is_unauthorized(self, user_type, dataset_type):
def test_delete_is_unauthorized(self, user_type, dataset_type, request):
"""Test that unauthorized users cannot delete versions on a given
dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
with pytest.raises(toolkit.NotAuthorized):
helpers.call_auth(
Expand All @@ -125,11 +153,11 @@ def test_delete_is_unauthorized(self, user_type, dataset_type):
('admin_user', 'public_dataset'),
('other_org_admin', 'public_dataset'),
])
def test_list_is_authorized(self, user_type, dataset_type):
def test_list_is_authorized(self, user_type, dataset_type, request):
"""Test that authorized users can list versions of a given dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
assert helpers.call_auth('version_list',
context=context,
Expand All @@ -138,12 +166,12 @@ def test_list_is_authorized(self, user_type, dataset_type):
@pytest.mark.parametrize("user_type, dataset_type", [
('other_org_admin', 'private_dataset'),
])
def test_list_is_unauthorized(self, user_type, dataset_type):
def test_list_is_unauthorized(self, user_type, dataset_type, request):
"""Test that unauthorized users cannot list versions on a given
dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
with pytest.raises(toolkit.NotAuthorized):
helpers.call_auth(
Expand All @@ -162,11 +190,11 @@ def test_list_is_unauthorized(self, user_type, dataset_type):
('admin_user', 'public_dataset'),
('other_org_admin', 'public_dataset'),
])
def test_show_is_authorized(self, user_type, dataset_type):
def test_show_is_authorized(self, user_type, dataset_type, request):
"""Test that authorized users can view versions of a given dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
assert helpers.call_auth('version_show',
context=context,
Expand All @@ -175,12 +203,12 @@ def test_show_is_authorized(self, user_type, dataset_type):
@pytest.mark.parametrize("user_type, dataset_type", [
('other_org_admin', 'private_dataset'),
])
def test_show_is_unauthorized(self, user_type, dataset_type):
def test_show_is_unauthorized(self, user_type, dataset_type, request):
"""Test that unauthorized users cannot view versions on a given
dataset
"""
user = getattr(self, user_type)
dataset = getattr(self, dataset_type)
user = request.getfixturevalue(user_type)
dataset = request.getfixturevalue(dataset_type)
context = self._get_context(user)
with pytest.raises(toolkit.NotAuthorized):
helpers.call_auth(
Expand Down
2 changes: 1 addition & 1 deletion ckanext/versions/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock
from unittest import mock
import pytest

from ckan.plugins import toolkit
Expand Down
2 changes: 1 addition & 1 deletion test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use = config:../ckan/test-core.ini
# Insert any custom config settings to be used when running your extension's
# tests here.

ckan.plugins = versions image_view
ckan.plugins = activity versions image_view

# Logging configuration
[loggers]
Expand Down
Loading