Skip to content

Commit

Permalink
Adds logic show and list, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Aug 23, 2012
1 parent 76fc46b commit d67f5fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -63,6 +63,14 @@ def related_list_dictize(related_list, context):

return sorted(result_list, key=lambda x: x["created"], reverse=True)

def organization_role_list_dictize(role_list, context):
result_list = []
for res in role_list:
role_dict = organization_role_dictize(res, context)
result_list.append(role_dict)

return sorted(result_list, key=lambda x: x["name"])


def extras_dict_dictize(extras_dict, context):
result_list = []
Expand Down
49 changes: 49 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -153,6 +153,55 @@ def package_revision_list(context, data_dict):
return revision_dicts


def organization_role_show(context, data_dict=None):
'''Return a single organization role.
:param id: the id or name of the related item to show
:type id: string
:rtype: dictionary
'''
model = context['model']
id = _get_or_bust(data_dict, 'id')

role = model.OrganisationRole.get(id)
context['role'] = role

if role is None:
raise NotFound

# _check_access('related_show',context, data_dict)

schema = ckan.logic.schema.default_organization_role_schema()
role_dict = model_dictize.organization_role_dictize(role, context)
role_dict, errors = _validate(role_dict, schema, context=context)

return role_dict

def organization_role_list(context, data_dict=None):
'''Return a list of organization roles.
:rtype: list of dictionaries
'''
model = context['model']
session = context['session']
user = context['user']

# Temporarily here because the auth is being replaced, and so no point
# it being in there for now.
from ckan.authz import Authorizer
from ckan.logic import NotAuthorized
if not Authorizer().is_sysadmin(unicode(user)):
msg = _('No valid API key provided.')
log.debug(msg)
raise NotAuthorized(msg)

role_list = model.Session.query(model.OrganisationRole)
return model_dictize.organization_role_list_dictize( role_list, context)


def related_show(context, data_dict=None):
'''Return a single related item.
Expand Down
26 changes: 22 additions & 4 deletions ckan/tests/functional/api/test_organisation_auth.py
Expand Up @@ -34,7 +34,6 @@ def test_role_create(self):
params=params, extra_environ=self.extra_environ).json
assert response['success'] is True


def test_role_delete_succeed(self):
params = _json.dumps({'name':'test_role', 'permissions': [{"name":"package.view"}]})
response = self.app.post('/api/action/organization_role_create',
Expand All @@ -46,11 +45,30 @@ def test_role_delete_succeed(self):
params=params, extra_environ=self.extra_environ).json
assert response['success'] is True


def test_role_delete_fail(self):
params = _json.dumps({'id':'non-existant'})

response = self.app.post('/api/action/organization_role_delete',
params=params, extra_environ=self.extra_environ, status=404)

def test_role_show_succeed(self):
params = _json.dumps({'name':'test_role', 'permissions': [{"name":"package.view"}]})
response = self.app.post('/api/action/organization_role_create',
params=params, extra_environ=self.extra_environ).json
assert response['success'] is True

params = _json.dumps({'id':'test_role'})
response = self.app.post('/api/action/organization_role_show',
params=params, extra_environ=self.extra_environ).json
assert response['success'] is False
assert response['success'] is True

def test_role_show_fail(self):
params = _json.dumps({'id':'non-existant'})
self.app.post('/api/action/organization_role_show',
params=params, extra_environ=self.extra_environ, status=404)

def test_role_list(self):
params = _json.dumps({})
response = self.app.post('/api/action/organization_role_list',
params=params, extra_environ=self.extra_environ, status=200).json
assert response['success'] == True
print response

0 comments on commit d67f5fd

Please sign in to comment.