Skip to content

Commit

Permalink
[#707] Refactor the group_purge tests
Browse files Browse the repository at this point in the history
Make them more like the organization_purge tests
  • Loading branch information
Sean Hammond committed Jun 21, 2013
1 parent ef1403c commit 3bd0c59
Showing 1 changed file with 155 additions and 150 deletions.
305 changes: 155 additions & 150 deletions ckan/tests/functional/api/model/test_group.py
Expand Up @@ -4,6 +4,8 @@
from ckan.lib.create_test_data import CreateTestData

from nose.tools import assert_equal
import paste
import pylons.test

from ckan.tests.functional.api.base import BaseModelApiTestCase
from ckan.tests.functional.api.base import Api1TestCase as Version1TestCase
Expand Down Expand Up @@ -223,98 +225,179 @@ def test_13_delete_group_404(self):
extra_environ=self.extra_environ)
self.assert_json_response(res, 'not found')

def test_group_purge_by_name(self):
'''A sysadmin should be able to purge a group by passing its name.'''

sysadmin = model.User.get('testsysadmin')
class TestGroupsVersion1(Version1TestCase, GroupsTestCase): pass
class TestGroupsVersion2(Version2TestCase, GroupsTestCase): pass

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan'}],
)

# Purge the group.
result = tests.call_action_api(self.app, 'group_purge',
apikey=sysadmin.apikey,
id='group-to-be-purged',
)
class TestGroupPurging(object):
'''Tests for the group_purge API.
assert result is None
'''
@classmethod
def setup_class(cls):
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)

# Now trying to show the group should give a 404.
result = tests.call_action_api(self.app, 'group_show',
id='group-to-be-purged',
status=404,
)
assert result == {'__type': 'Not Found Error', 'message': 'Not found'}
# Make a sysadmin user.
cls.sysadmin = model.User(name='test_sysadmin', sysadmin=True)
model.Session.add(cls.sysadmin)
model.Session.commit()
model.Session.remove()

assert group['name'] not in tests.call_action_api(self.app,
'group_list')
warandpeace = tests.call_action_api(self.app, 'package_show',
id='warandpeace')
assert group['name'] not in [group_['name'] for group_ in
warandpeace['groups']]
# TODO: Also want to assert that user is not in group anymore, but
# how to get a user's groups?
# A package that will be added to our test groups.
cls.package = tests.call_action_api(cls.app, 'package_create',
name='group_package',
apikey=cls.sysadmin.apikey)

# A user who will not be a member of our test groups.
cls.group_visitor = tests.call_action_api(cls.app, 'user_create',
name='non_member',
email='blah',
password='farm',
apikey=cls.sysadmin.apikey)

# A user who will become a member of our test groups.
cls.group_member = tests.call_action_api(cls.app, 'user_create',
name='member',
email='blah',
password='farm',
apikey=cls.sysadmin.apikey)

# A user who will become an editor of our test groups.
cls.group_editor = tests.call_action_api(cls.app, 'user_create',
name='editor',
email='blah',
password='farm',
apikey=cls.sysadmin.apikey)

# A user who will become an admin of our test groups.
cls.group_admin = tests.call_action_api(cls.app, 'user_create',
name='admin',
email='blah',
password='farm',
apikey=cls.sysadmin.apikey)

def test_group_purge_by_id(self):
'''A sysadmin should be able to purge a group by passing its id.'''
@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

sysadmin = model.User.get('testsysadmin')
def _group_create(self, group_name):
'''Make a group with a user and a dataset.'''

# Make a group with a dataset and a user.
# Make a group with a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan'}],
apikey=self.sysadmin.apikey,
name=group_name,
users=[
{'name': self.group_member['name'],
'capacity': 'member',
},
{'name': self.group_editor['name'],
'capacity': 'editor',
},
{'name': self.group_admin['name'],
'capacity': 'admin',
}],
packages=[
{'id': self.package['name']}],
)

# Purge the group.
result = tests.call_action_api(self.app, 'group_purge',
apikey=sysadmin.apikey,
id=group['id'],
)
# Let's just make sure that worked.
package = tests.call_action_api(self.app, 'package_show',
id=self.package['id'])
assert group['name'] in [group_['name']
for group_ in package['groups']]
group = tests.call_action_api(self.app, 'group_show', id=group['id'])
assert self.package['name'] in [package_['name'] for package_ in
group['packages']]
assert self.group_visitor['name'] not in [user['name']
for user in group['users']]
assert self.group_member['name'] in [user['name']
for user in group['users']
if user['capacity'] == 'member']
assert self.group_editor['name'] in [user['name']
for user in group['users']
if user['capacity'] == 'editor']
assert self.group_admin['name'] in [user['name']
for user in group['users']
if user['capacity'] == 'admin']

return group

def _test_group_purge(self, group_name, by_id):
'''Create a group with the given name, and test purging it.
:param name: the name of the group to create and purge
:param by_id: if True, pass the group's id to group_purge,
otherwise pass its name
:type by_id: boolean
'''
group = self._group_create(group_name)

# Purge the group.
if by_id:
result = tests.call_action_api(self.app, 'group_purge',
apikey=self.sysadmin.apikey,
id=group['id'],
)
else:
result = tests.call_action_api(self.app, 'group_purge',
apikey=self.sysadmin.apikey,
id=group['name'],
)
assert result is None

# Now trying to show the group should give a 404.
result = tests.call_action_api(self.app, 'group_show',
id='group-to-be-purged',
status=404,
)
assert result == {'__type': 'Not Found Error',
'message': 'Not found'}
id=group_name, status=404)
assert result == {'__type': 'Not Found Error', 'message': 'Not found'}

assert group['name'] not in tests.call_action_api(self.app,
'group_list')
warandpeace = tests.call_action_api(self.app, 'package_show',
id='warandpeace')
assert group['name'] not in [group_['name'] for group_ in
warandpeace['groups']]
# The group should not appear in group_list.
assert group_name not in tests.call_action_api(self.app, 'group_list')

def test_group_purge_with_invalid_id(self):
# The package should no longer belong to the group.
package = tests.call_action_api(self.app, 'package_show',
id=self.package['name'])
assert group['name'] not in [group_['name'] for group_
in package['groups']]

# TODO: Also want to assert that user is not in group anymore,
# but how to get a user's groups?

# It should be possible to create a new group with the same name as the
# purged one (you would not be able to do this if you had merely
# deleted the original group).
new_group = tests.call_action_api(self.app, 'group_create',
name=group_name,
apikey=self.sysadmin.apikey,
)
assert new_group['name'] == group_name

sysadmin = model.User.get('testsysadmin')
# TODO: Should we do a model-level check, to check that the group is
# really purged?

def test_group_purge_by_name(self):
self._test_group_purge('group-to-be-purged', by_id=False)

def test_group_purge_by_id(self):
self._test_group_purge('group-to-be-purged-2', by_id=True)

def test_group_purge_with_invalid_id(self):

for name in ('foo', 'invalid name', None, ''):
# Try to purge a group, but pass an invalid name.
result = tests.call_action_api(self.app, 'group_purge',
apikey=sysadmin.apikey,
apikey=self.sysadmin.apikey,
id=name,
status=404,
)
assert result == {'__type': 'Not Found Error',
'message': 'Not found: Group was not found'}

def test_group_purge_with_missing_id(self):
sysadmin = model.User.get('testsysadmin')
result = tests.call_action_api(self.app, 'group_purge',
apikey=sysadmin.apikey,
apikey=self.sysadmin.apikey,
status=409,
)
assert result == {'__type': 'Validation Error',
Expand All @@ -325,15 +408,7 @@ def test_visitors_cannot_purge_groups(self):
groups.
'''
sysadmin = model.User.get('testsysadmin')

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged-2',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan'}],
)
group = self._group_create('group-to-be-purged-3')

# Try to purge the group without an API key.
result = tests.call_action_api(self.app, 'group_purge',
Expand All @@ -348,27 +423,12 @@ def test_users_cannot_purge_groups(self):
purge the group.
'''
sysadmin = model.User.get('testsysadmin')

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged-3',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan'}],
)

# Get a user who is not a member of the group.
annafan = tests.call_action_api(self.app, 'user_show',
id='annafan', apikey=sysadmin.apikey)
# Since we did user_show as a sysadmin we should have got the user's
# api key.
assert 'apikey' in annafan
group = self._group_create('group-to-be-purged-4')

# Try to purge the group with annafan's API key.
# Try to purge the group with a non-member's API key.
result = tests.call_action_api(self.app, 'group_purge',
id=group['id'],
apikey=annafan['apikey'],
apikey=self.group_visitor['apikey'],
status=403,
)
assert result == {'__type': 'Authorization Error',
Expand All @@ -378,29 +438,12 @@ def test_members_cannot_purge_groups(self):
'''Members of a group should not be authorized to purge the group.
'''
sysadmin = model.User.get('testsysadmin')
group = self._group_create('group-to-be-purged-5')

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged-4',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan',
'capacity': 'member'}],
)

# Get a user who is a member of the group.
russianfan = tests.call_action_api(self.app, 'user_show',
id='russianfan',
apikey=sysadmin.apikey)
# Since we did user_show as a sysadmin we should have got the user's
# api key.
assert 'apikey' in russianfan

# Try to purge the group with russianfan's API key.
# Try to purge the group with a group member's API key.
result = tests.call_action_api(self.app, 'group_purge',
id=group['id'],
apikey=russianfan['apikey'],
apikey=self.group_member['apikey'],
status=403,
)
assert result == {'__type': 'Authorization Error',
Expand All @@ -410,29 +453,12 @@ def test_editors_cannot_purge_groups(self):
'''Editors of a group should not be authorized to purge the group.
'''
sysadmin = model.User.get('testsysadmin')

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged-5',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan',
'capacity': 'editor'}],
)

# Get a user who is an editor of the group.
russianfan = tests.call_action_api(self.app, 'user_show',
id='russianfan',
apikey=sysadmin.apikey)
# Since we did user_show as a sysadmin we should have got the user's
# api key.
assert 'apikey' in russianfan
group = self._group_create('group-to-be-purged-6')

# Try to purge the group with russianfan's API key.
# Try to purge the group with an editor's API key.
result = tests.call_action_api(self.app, 'group_purge',
id=group['id'],
apikey=russianfan['apikey'],
apikey=self.group_editor['apikey'],
status=403,
)
assert result == {'__type': 'Authorization Error',
Expand All @@ -442,34 +468,13 @@ def test_admins_cannot_purge_groups(self):
'''Admins of a group should not be authorized to purge the group.
'''
sysadmin = model.User.get('testsysadmin')
group = self._group_create('group-to-be-purged-7')

# Make a group with a dataset and a user.
group = tests.call_action_api(self.app, 'group_create',
apikey=sysadmin.apikey,
name='group-to-be-purged-6',
packages=[{'id': 'warandpeace'}],
users=[{'name': 'russianfan',
'capacity': 'admin'}],
)

# Get a user who is an admin of the group.
russianfan = tests.call_action_api(self.app, 'user_show',
id='russianfan',
apikey=sysadmin.apikey)
# Since we did user_show as a sysadmin we should have got the user's
# api key.
assert 'apikey' in russianfan

# Try to purge the group with russianfan's API key.
# Try to purge the group with an admin's API key.
result = tests.call_action_api(self.app, 'group_purge',
id=group['id'],
apikey=russianfan['apikey'],
apikey=self.group_admin['apikey'],
status=403,
)
assert result == {'__type': 'Authorization Error',
'message': 'Access denied'}


class TestGroupsVersion1(Version1TestCase, GroupsTestCase): pass
class TestGroupsVersion2(Version2TestCase, GroupsTestCase): pass

0 comments on commit 3bd0c59

Please sign in to comment.