Skip to content

Commit

Permalink
[#1736] Change test factories to use 'user' kwarg
Browse files Browse the repository at this point in the history
Use an explicit keyword argument for the authorized user, instead of
silently using the site_user.
  • Loading branch information
seanh committed Jun 2, 2014
1 parent 973d982 commit 0f3b56f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 65 deletions.
136 changes: 88 additions & 48 deletions ckan/new_tests/factories.py
Expand Up @@ -44,24 +44,20 @@
import ckan.new_tests.helpers as helpers


def _get_action_user_name(kwargs):
'''Return the name of the user in kwargs, defaulting to the site user
def _context_with_user(kwargs_dict):
'''Pop 'user' from kwargs and return a context dict with the user in it.
It can be overriden by explictly setting {'user': None} in the keyword
arguments. In that case, this method will return None.
'''

if 'user' in kwargs:
user = kwargs['user']
else:
user = helpers.call_action('get_site_user')
Some action functions require a context with a user in it even when we are
skipping authorizaiton. For example when creating a group or organization a
user is needed to be the first admin of that group or organization.
if user is None:
user_name = None
else:
user_name = user['name']

return user_name
'''
context = {}
if kwargs_dict and 'user' in kwargs_dict:
user = kwargs_dict.pop('user')
if user and 'name' in user:
context['user'] = user['name']
return context


def _generate_email(user):
Expand Down Expand Up @@ -126,8 +122,18 @@ def _create(cls, target_class, *args, **kwargs):


class Resource(factory.Factory):
'''A factory class for creating CKAN resources.'''
'''A factory class for creating CKAN resources.
This factory accepts an additional keyword argument `user` - the user who
will create the resource. This param is passed to the resource_create()
action function in the context dict not in the data_dict with any other
params.
Example:
resource_dict = factories.Resource(user=factories.User())
'''
FACTORY_FOR = ckan.model.Resource

name = factory.Sequence(lambda n: 'test_resource_{n}'.format(n=n))
Expand All @@ -145,11 +151,9 @@ def _create(cls, target_class, *args, **kwargs):
if args:
assert False, "Positional args aren't supported, use keyword args."

context = {'user': _get_action_user_name(kwargs)}

resource_dict = helpers.call_action('resource_create', context=context,
**kwargs)
return resource_dict
return helpers.call_action('resource_create',
context=_context_with_user(kwargs),
**kwargs)


class Sysadmin(factory.Factory):
Expand Down Expand Up @@ -192,8 +196,20 @@ def _create(cls, target_class, *args, **kwargs):


class Group(factory.Factory):
'''A factory class for creating CKAN groups.'''
'''A factory class for creating CKAN groups.
This factory accepts an additional keyword argument `user` - the user who
will create the group and become its first admin. This param is passed to
the group_create() action function in the context dict not in the
data_dict with any other params.
Example:
group_dict = factories.Group(user=factories.User())
The user param is required - group_create() will crash without it.
'''
FACTORY_FOR = ckan.model.Group

name = factory.Sequence(lambda n: 'test_group_{n}'.format(n=n))
Expand All @@ -212,17 +228,26 @@ def _create(cls, target_class, *args, **kwargs):
if args:
assert False, "Positional args aren't supported, use keyword args."

context = {'user': _get_action_user_name(kwargs)}

group_dict = helpers.call_action('group_create',
context=context,
**kwargs)
return group_dict
return helpers.call_action('group_create',
context=_context_with_user(kwargs),
**kwargs)


class Organization(factory.Factory):
'''A factory class for creating CKAN organizations.'''
'''A factory class for creating CKAN organizations.
This factory accepts an additional keyword argument `user` - the user who
will create the organization and become its first admin. This param is
passed to the organization_create() action function in the context dict not
in the data_dict with any other params.
Example:
organization_dict = factories.Organization(user=factories.User())
The user param is required - organization_create() will crash without it.
'''
# This is the class that OrganizationFactory will create and return
# instances of.
FACTORY_FOR = ckan.model.Group
Expand All @@ -248,17 +273,26 @@ def _create(cls, target_class, *args, **kwargs):
if args:
assert False, "Positional args aren't supported, use keyword args."

context = {'user': _get_action_user_name(kwargs)}

group_dict = helpers.call_action('organization_create',
context=context,
**kwargs)
return group_dict
return helpers.call_action('organization_create',
context=_context_with_user(kwargs),
**kwargs)


class Related(factory.Factory):
'''A factory class for creating related items.'''
'''A factory class for creating related items.
This factory accepts an additional keyword argument `user` - the user who
will create the related item. This param is passed to the related_create()
action function in the context dict not in the data_dict with any other
params.
Example:
related_dict = factories.Related(user=factories.User())
The user param is required - related_create() will crash without it.
'''
FACTORY_FOR = ckan.model.Related

type = 'idea'
Expand All @@ -276,15 +310,24 @@ def _create(cls, target_class, *args, **kwargs):
if args:
assert False, "Positional args aren't supported, use keyword args."

context = {'user': _get_action_user_name(kwargs)}
related_dict = helpers.call_action('related_create', context=context,
**kwargs)
return related_dict
return helpers.call_action('related_create',
context=_context_with_user(kwargs),
**kwargs)


class Dataset(factory.Factory):
'''A factory class for creating CKAN datasets.'''
'''A factory class for creating CKAN datasets.
This factory accepts an additional keyword argument `user` - the user who
will create the dataset. This param is passed to the package_create()
action function in the context dict not in the data_dict with any other
params.
Example:
dataset_dict = factories.Dataset(user=factories.User())
'''
FACTORY_FOR = ckan.model.Package

# These are the default params that will be used to create new groups.
Expand All @@ -303,12 +346,9 @@ def _create(cls, target_class, *args, **kwargs):
if args:
assert False, "Positional args aren't supported, use keyword args."

context = {'user': _get_action_user_name(kwargs)}

dataset_dict = helpers.call_action('package_create',
context=context,
**kwargs)
return dataset_dict
return helpers.call_action('package_create',
context=_context_with_user(kwargs),
**kwargs)


class MockUser(factory.Factory):
Expand Down
21 changes: 10 additions & 11 deletions ckan/new_tests/logic/action/test_get.py
Expand Up @@ -90,8 +90,9 @@ def test_group_show_no_packages_returned(self):

def test_organization_list(self):

org1 = factories.Organization()
org2 = factories.Organization()
user = factories.User()
org1 = factories.Organization(user=user)
org2 = factories.Organization(user=user)

org_list = helpers.call_action('organization_list')

Expand All @@ -100,7 +101,7 @@ def test_organization_list(self):

def test_organization_show(self):

org = factories.Organization()
org = factories.Organization(user=factories.User())

org_dict = helpers.call_action('organization_show', id=org['id'])

Expand All @@ -110,9 +111,8 @@ def test_organization_show(self):

def test_organization_show_packages_returned(self):

user_name = helpers.call_action('get_site_user')['name']

org = factories.Organization()
user = factories.User()
org = factories.Organization(user=user)

datasets = [
{'name': 'dataset_1', 'owner_org': org['name']},
Expand All @@ -121,7 +121,7 @@ def test_organization_show_packages_returned(self):

for dataset in datasets:
helpers.call_action('package_create',
context={'user': user_name},
context={'user': user['name']},
**dataset)

org_dict = helpers.call_action('organization_show', id=org['id'])
Expand All @@ -131,9 +131,8 @@ def test_organization_show_packages_returned(self):

def test_organization_show_private_packages_not_returned(self):

user_name = helpers.call_action('get_site_user')['name']

org = factories.Organization()
user = factories.User()
org = factories.Organization(user=user)

datasets = [
{'name': 'dataset_1', 'owner_org': org['name']},
Expand All @@ -142,7 +141,7 @@ def test_organization_show_private_packages_not_returned(self):

for dataset in datasets:
helpers.call_action('package_create',
context={'user': user_name},
context={'user': user['name']},
**dataset)

org_dict = helpers.call_action('organization_show', id=org['id'])
Expand Down
15 changes: 9 additions & 6 deletions ckan/new_tests/test_factories.py
Expand Up @@ -32,18 +32,21 @@ def test_sysadmin_factory(self):
assert_not_equals(sysadmin1['id'], sysadmin2['id'])

def test_group_factory(self):
group1 = factories.Group()
group2 = factories.Group()
user = factories.User()
group1 = factories.Group(user=user)
group2 = factories.Group(user=user)
assert_not_equals(group1['id'], group2['id'])

def test_organization_factory(self):
organization1 = factories.Organization()
organization2 = factories.Organization()
user = factories.User()
organization1 = factories.Organization(user=user)
organization2 = factories.Organization(user=user)
assert_not_equals(organization1['id'], organization2['id'])

def test_related_factory(self):
related1 = factories.Related()
related2 = factories.Related()
user = factories.User()
related1 = factories.Related(user=user)
related2 = factories.Related(user=user)
assert_not_equals(related1['id'], related2['id'])

def test_dataset_factory(self):
Expand Down

0 comments on commit 0f3b56f

Please sign in to comment.