Skip to content

Commit

Permalink
[#1117] Move call_auth() into test helpers
Browse files Browse the repository at this point in the history
Where other auth test modules can use it.

Also add a complete docstring.
  • Loading branch information
Sean Hammond committed Aug 2, 2013
1 parent 4f34165 commit 081e126
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
40 changes: 40 additions & 0 deletions ckan/new_tests/helpers.py
Expand Up @@ -52,3 +52,43 @@ def call_action(action_name, context=None, **kwargs):
context.setdefault('user', '127.0.0.1')
context.setdefault('ignore_auth', True)
return logic.get_action(action_name)(context=context, data_dict=kwargs)


def call_auth(auth_name, context, **kwargs):
'''Call a ckan.logic.auth function and return the result.
This is just a convenience function for tests in
ckan.new_tests.logic.auth to use.
Usage:
result = self._call_auth('user_update', context=context,
id='some_user_id',
name='updated_user_name')
:param auth_name: the name of the auth function to call, e.g.
``'user_update'``
:type auth_name: string
:param context: the context dict to pass to the auth function, must
contain 'user' and 'model' keys,
e.g. ``{'user': 'fred', 'model': my_mock_model_object}``
:type context: dict
:param kwargs: any arguments to be passed to the auth function, these
will be wrapped in a dict and passed to the auth function as its
``data_dict`` argument
:type kwargs: keyword arguments
'''
import ckan.logic.auth.update

assert 'user' in context, ('Test methods must put a user name in the '
'context dict')
assert 'model' in context, ('Test methods must put a model in the '
'context dict')

# FIXME: Do we want to go through check_access() here?
auth_function = ckan.logic.auth.update.__getattribute__(auth_name)
return auth_function(context=context, data_dict=kwargs)
26 changes: 6 additions & 20 deletions ckan/new_tests/logic/auth/test_update.py
Expand Up @@ -3,24 +3,10 @@
'''
import mock

import ckan.new_tests.helpers as helpers

class TestUpdate(object):

# TODO: Probably all auth function tests want this? Move to helpers.py?
def _call_auth(self, auth_name, context=None, **kwargs):
'''Call a ckan.logic.auth function more conveniently for testing.
'''
import ckan.logic.auth.update

assert 'user' in context, ('Test methods must put a user name in the '
'context dict')
assert 'model' in context, ('Test methods must put a model in the '
'context dict')

# FIXME: Do we want to go through check_access() here?
auth_function = ckan.logic.auth.update.__getattribute__(auth_name)
return auth_function(context=context, data_dict=kwargs)
class TestUpdate(object):

def test_user_update_visitor_cannot_update_user(self):
'''Visitors should not be able to update users' accounts.'''
Expand Down Expand Up @@ -49,7 +35,7 @@ def test_user_update_visitor_cannot_update_user(self):
'id': fred.id,
'name': 'updated_user_name',
}
result = self._call_auth('user_update', context=context, **params)
result = helpers.call_auth('user_update', context=context, **params)

assert result['success'] is False

Expand Down Expand Up @@ -80,7 +66,7 @@ def test_user_update_user_cannot_update_another_user(self):
'id': fred.id,
'name': 'updated_user_name',
}
result = self._call_auth('user_update', context=context, **params)
result = helpers.call_auth('user_update', context=context, **params)

assert result['success'] is False

Expand Down Expand Up @@ -113,7 +99,7 @@ def test_user_update_user_can_update_herself(self):
'id': fred.id,
'name': 'updated_user_name',
}
result = self._call_auth('user_update', context=context, **params)
result = helpers.call_auth('user_update', context=context, **params)

assert result['success'] is True

Expand Down Expand Up @@ -142,7 +128,7 @@ def test_user_update_with_no_user_in_context(self):
'id': mock_user.id,
'name': 'updated_user_name',
}
result = self._call_auth('user_update', context=context, **params)
result = helpers.call_auth('user_update', context=context, **params)

assert result['success'] is False

Expand Down

0 comments on commit 081e126

Please sign in to comment.