Skip to content

Commit

Permalink
Add tests for chained functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zharktas committed Feb 2, 2021
1 parent 4cc07a3 commit 74f5c96
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Empty file.
45 changes: 45 additions & 0 deletions ckanext/chained_functions/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
import ckan.plugins as p
from ckan.plugins.toolkit import (auth_allow_anonymous_access,
chained_auth_function,
chained_action,
side_effect_free,
chained_helper
)


class ChainedFunctionsPlugin(p.SingletonPlugin):
p.implements(p.IAuthFunctions)
p.implements(p.IActions)
p.implements(p.ITemplateHelpers)

def get_auth_functions(self):
return {
"user_show": user_show
}

def get_actions(self):
return {
"package_search": package_search
}

def get_helpers(self):
return {
"ckan_version": ckan_version
}

@chained_auth_function
@auth_allow_anonymous_access
def user_show(next_auth, context, data_dict=None):
return next_auth(context, data_dict)

@chained_action
@side_effect_free
def package_search(original_action, context, data_dict):
return original_action(context, data_dict)

@chained_helper
def ckan_version(next_func, **kw):
return next_func(**kw)

setattr(ckan_version, "some_attribute", "some_value")
Empty file.
21 changes: 21 additions & 0 deletions ckanext/chained_functions/tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

import pytest

@pytest.mark.ckan_config(u"ckan.plugins", u"chained_functions")
@pytest.mark.usefixtures(u"with_plugins")
class TestChainedFunctionsPlugin(object):
def test_auth_attributes_are_retained(self):
from ckan.authz import _AuthFunctions
user_show = _AuthFunctions.get("user_show")
assert hasattr(user_show, 'auth_allow_anonymous_access') == True

def test_action_attributes_are_retained(self):
from ckan.plugins.toolkit import get_action
package_search = get_action('package_search')
assert hasattr(package_search, 'side_effect_free') == True

def test_helper_attributes_are_retained(self):
from ckan.lib.helpers import helper_functions
ckan_version = helper_functions.get('ckan_version')
assert hasattr(ckan_version, "some_attribute") == True
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'organizations = ckanext.organizations.forms:OrganizationForm',
'organizations_dataset = ckanext.organizations.forms:OrganizationDatasetForm',
'expire_api_token = ckanext.expire_api_token.plugin:ExpireApiTokenPlugin',
'chained_functions = ckanext.chained_functions.plugin:ChainedFunctionsPlugin',
'datastore = ckanext.datastore.plugin:DatastorePlugin',
'datapusher=ckanext.datapusher.plugin:DatapusherPlugin',
'test_tag_vocab_plugin = ckanext.test_tag_vocab_plugin:MockVocabTagsPlugin',
Expand Down

0 comments on commit 74f5c96

Please sign in to comment.