Skip to content

Commit

Permalink
[1607] Fixes to tests that were failing because of schema changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Mar 8, 2012
1 parent eb173a3 commit 0efe8a7
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 92 deletions.
44 changes: 22 additions & 22 deletions ckan/lib/create_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CreateTestData(cli.CkanCommand):
tag_names = []
group_names = set()
user_refs = []

pkg_core_fields = ['name', 'title', 'version', 'url', 'notes',
'author', 'author_email',
'maintainer', 'maintainer_email',
Expand Down Expand Up @@ -89,7 +89,7 @@ def create_test_user(cls):

@classmethod
def create_arbitrary(cls, package_dicts, relationships=[],
extra_user_names=[], extra_group_names=[],
extra_user_names=[], extra_group_names=[],
admins=[]):
'''Creates packages and a few extra objects as well at the
same time if required.
Expand All @@ -101,7 +101,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
@param extra_group_names - a list of group names to create. No
properties get set though.
@param admins - a list of user names to make admins of all the
packages created.
packages created.
'''
assert isinstance(relationships, (list, tuple))
assert isinstance(extra_user_names, (list, tuple))
Expand All @@ -111,11 +111,11 @@ def create_arbitrary(cls, package_dicts, relationships=[],
new_user_names = extra_user_names
new_group_names = set()
new_groups = {}
rev = model.repo.new_revision()

rev = model.repo.new_revision()
rev.author = cls.author
rev.message = u'Creating test packages.'

admins_list = defaultdict(list) # package_name: admin_names
if package_dicts:
if isinstance(package_dicts, dict):
Expand All @@ -131,7 +131,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
if isinstance(val, str):
val = unicode(val)
if attr=='name':
continue
continue
if attr in cls.pkg_core_fields:
pass
elif attr == 'download_url':
Expand Down Expand Up @@ -160,7 +160,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
if not tag:
tag = model.Tag(name=tag_name)
cls.tag_names.append(tag_name)
model.Session.add(tag)
model.Session.add(tag)
pkg.tags.append(tag)
model.Session.flush()
elif attr == 'groups':
Expand Down Expand Up @@ -208,8 +208,8 @@ def create_arbitrary(cls, package_dicts, relationships=[],
model.repo.commit_and_remove()

needs_commit = False
rev = model.repo.new_revision()

rev = model.repo.new_revision()
for group_name in extra_group_names:
group = model.Group(name=unicode(group_name))
model.Session.add(group)
Expand Down Expand Up @@ -258,7 +258,7 @@ def create_arbitrary(cls, package_dicts, relationships=[],
needs_commit = False

if relationships:
rev = model.repo.new_revision()
rev = model.repo.new_revision()
rev.author = cls.author
rev.message = u'Creating package relationships.'

Expand All @@ -270,7 +270,7 @@ def pkg(pkg_name):
needs_commit = True

model.repo.commit_and_remove()


@classmethod
def create_groups(cls, group_dicts, admin_user_name=None, auth_profile=""):
Expand Down Expand Up @@ -324,7 +324,7 @@ def create(cls, auth_profile=""):

cls.pkg_names = [u'annakarenina', u'warandpeace']
pkg1 = model.Package(name=cls.pkg_names[0])
if auth_profile == "publisher":
if auth_profile == "publisher":
pkg1.group = publisher_group
model.Session.add(pkg1)
pkg1.title = u'A Novel By Tolstoy'
Expand Down Expand Up @@ -368,7 +368,7 @@ def create(cls, auth_profile=""):
u with umlaut \xfc
66-style quote \u201c
foreign word: th\xfcmb
Needs escaping:
left arrow <
Expand All @@ -379,7 +379,7 @@ def create(cls, auth_profile=""):
tag1 = model.Tag(name=u'russian')
tag2 = model.Tag(name=u'tolstoy')

if auth_profile == "publisher":
if auth_profile == "publisher":
pkg2.group = publisher_group

# Flexible tag, allows spaces, upper-case,
Expand Down Expand Up @@ -407,12 +407,12 @@ def create(cls, auth_profile=""):
type=auth_profile or 'group')
for obj in [david, roger]:
model.Session.add(obj)

cls.group_names.add(u'david')
cls.group_names.add(u'roger')

model.Session.flush()

model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=david))
model.Session.add(model.Member(table_id=pkg2.id, table_name='package', group=david))
model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=roger))
Expand Down Expand Up @@ -447,7 +447,7 @@ def create(cls, auth_profile=""):

# Create a couple of authorization groups
for ag_name in [u'anauthzgroup', u'anotherauthzgroup']:
ag=model.AuthorizationGroup.by_name(ag_name)
ag=model.AuthorizationGroup.by_name(ag_name)
if not ag: #may already exist, if not create
ag=model.AuthorizationGroup(name=ag_name)
model.Session.add(ag)
Expand Down Expand Up @@ -559,7 +559,7 @@ def get_all_data(cls):
'groups':'ukgov test1 test2 penguin',
'license':'odc-by',
'notes':u'''From <http://www.gpoaccess.gov/gils/about.html>
> The Government Information Locator Service (GILS) is an effort to identify, locate, and describe publicly available Federal
> Because this collection is decentralized, the GPO
Expand Down Expand Up @@ -604,7 +604,7 @@ def get_all_data(cls):
{'name':'uk-government-expenditure',
'title':'UK Government Expenditure',
'tags':'workshop-20081101,uk,gov,expenditure,finance,public,funding,penguin'.split(','),
'groups':'ukgov penguin',
'groups':'ukgov penguin',
'notes':'''Discussed at [Workshop on Public Information, 2008-11-02](http://okfn.org/wiki/PublicInformation).
Overview is available in Red Book, or Financial Statement and Budget Report (FSBR), [published by the Treasury](http://www.hm-treasury.gov.uk/budget.htm).''',
Expand All @@ -613,7 +613,7 @@ def get_all_data(cls):
{'name':'se-publications',
'title':'Sweden - Government Offices of Sweden - Publications',
'url':'http://www.sweden.gov.se/sb/d/574',
'groups':'penguin',
'groups':'penguin',
'tags':u'country-sweden,format-pdf,access-www,documents,publications,government,eutransparency,penguin,CAPITALS,surprise.,greek omega \u03a9,japanese katakana \u30a1'.split(','),
'license':'',
'notes':'''### About
Expand All @@ -627,7 +627,7 @@ def get_all_data(cls):
},
{'name':'se-opengov',
'title':'Opengov.se',
'groups':'penguin',
'groups':'penguin',
'url':'http://www.opengov.se/',
'download_url':'http://www.opengov.se/data/open/',
'tags':'country-sweden,government,data,penguin'.split(','),
Expand Down
1 change: 1 addition & 0 deletions ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def default_group_schema():
'packages': {
"id": [not_empty, unicode, package_id_or_name_exists],
"title":[ignore_missing, unicode],
"name":[ignore_missing, unicode],
"__extras": [ignore]
},
'groups': {
Expand Down
48 changes: 26 additions & 22 deletions ckan/tests/functional/test_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AuthzTestBase(object):
ENTITY_CLASS_MAP = {'dataset': model.Package,
'group': model.Group,
'package_relationship': model.PackageRelationship}

@classmethod
def setup_class(self):
setup_test_search_index()
Expand Down Expand Up @@ -111,10 +111,14 @@ def _test_via_wui(self, action, user, entity_name, entity='dataset'):
str_required_in_response = '<select id="state"'
else:
raise NotImplementedError

res = self.app.get(offset, extra_environ={'REMOTE_USER': user.name.encode('utf8')}, expect_errors=True)
tests = {}
tests['str_required (%s)' % str_required_in_response] = bool(str_required_in_response in res)
tests['error string'] = bool('error' not in res)

r = res.body
r = r.replace('form_errors', '')
tests['error string'] = bool('error' not in r)
tests['status'] = bool(res.status in (200, 201))
tests['0 packages found'] = bool(u'<strong>0</strong> packages found' not in res)
is_ok = False not in tests.values()
Expand Down Expand Up @@ -144,7 +148,7 @@ def _test_via_api(self, action, user, entity_name, entity_type='dataset'):
func = self.app.get
elif action == 'create':
offset = '/api/rest/%s' % (entity_type)
postparams = '%s=1' % json.dumps({'name': unicode(entity_name),
postparams = '%s=1' % json.dumps({'name': unicode(entity_name),
'title': u'newtitle'},
encoding='utf8')
func = self.app.post
Expand Down Expand Up @@ -189,7 +193,7 @@ def _test_via_api(self, action, user, entity_name, entity_type='dataset'):
else:
offset = '/api/rest/dataset/%(entity1)s/%(type)s/%(entity2)s' % entity_properties
str_required_in_response = '"object": "%(entity2)s", "type": "%(type)s", "subject": "%(entity1)s"' % entity_properties

if user.name == 'visitor':
environ = {}
else:
Expand All @@ -209,7 +213,7 @@ class TestUsage(TestController, AuthzTestBase):
'''Use case: role defaults (e.g. like ckan.net operates)
* reader can read only
* editor can edit most properties of a package
'''
@classmethod
def _create_test_data(cls):
Expand Down Expand Up @@ -307,7 +311,7 @@ def _create_test_data(cls):
model.add_user_to_role(visitor, model.Role.READER, entity)
model.add_user_to_role(mrloggedin, model.Role.READER, entity)
model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, entity)

model.repo.commit_and_remove()

assert model.Package.by_name(u'deleted').state == model.State.DELETED
Expand All @@ -327,13 +331,13 @@ def _create_test_data(cls):

def test_14_visitor_reads_stopped(self):
self._test_cant('read', self.visitor, ['xx', 'rx', 'wx'])
def test_01_visitor_reads(self):
def test_01_visitor_reads(self):
self._test_can('read', self.visitor, ['rr', 'wr', 'ww'])

def test_12_visitor_edits_stopped(self):
self._test_cant('edit', self.visitor, ['xx', 'rx', 'wx', 'rr', 'wr', 'ww'], interfaces=['rest'])
self._test_cant('edit', self.visitor, ['xx', 'rx', 'wx', 'rr', 'wr'], interfaces=['wui'])

def test_02_visitor_edits(self):
self._test_can('edit', self.visitor, ['ww'], interfaces=['wui'])
self._test_can('edit', self.visitor, [], interfaces=['rest'])
Expand All @@ -356,7 +360,7 @@ def test_04_user_edits(self):

def test_user_creates(self):
self._test_can('create', self.mrloggedin, [])

def test_list(self):
# NB there is no listing of package in wui interface any more
# NB under the new model all active packages are always visible in listings by default
Expand All @@ -380,10 +384,10 @@ def test_search_deleted(self):
# TODO: Discuss authorized queries for packages and resolve this issue.
# self._test_cant('search', self.mrloggedin, ['deleted', 'xx'], entity_types=['dataset'])
self._test_cant('search', self.mrloggedin, ['deleted'], entity_types=['dataset'])

def test_05_author_is_new_package_admin(self):
user = self.mrloggedin

# make new package
assert not model.Package.by_name(u'annakarenina')
offset = url_for(controller='package', action='new')
Expand All @@ -404,16 +408,16 @@ def test_05_author_is_new_package_admin(self):

def test_sysadmin_can_read_anything(self):
self._test_can('read', self.testsysadmin, ['xx', 'rx', 'wx', 'rr', 'wr', 'ww', 'deleted'])

def test_sysadmin_can_edit_anything(self):
self._test_can('edit', self.testsysadmin, ['xx', 'rx', 'wx', 'rr', 'wr', 'ww', 'deleted'])

def test_sysadmin_can_create_anything(self):
self._test_can('create', self.testsysadmin, [])

def test_sysadmin_can_search_anything(self):
self._test_can('search', self.testsysadmin, ['xx', 'rx', 'wx', 'rr', 'wr', 'ww'], entity_types=['dataset'])

def test_visitor_deletes(self):
self._test_cant('delete', self.visitor, ['gets_filled'], interfaces=['wui'])

Expand All @@ -437,7 +441,7 @@ def test_admin_purges(self):

def test_sysadmin_purges(self):
self._test_can('purge', self.testsysadmin, ['gets_filled'], interfaces=['rest'], entity_types=['dataset'])

def test_sysadmin_relationships(self):
opts = {'interfaces': ['rest'],
'entity_types': ['package_relationship']}
Expand Down Expand Up @@ -466,7 +470,7 @@ def test_user_relationships(self):
self._test_can('edit', self.mrloggedin, [('ww', 'links_to', 'wr')], **opts)
self._test_cant('edit', self.mrloggedin, [('ww', 'depends_on', 'xx')], **opts)
#TODO self._test_cant('delete', self.mrloggedin, [('ww', 'links_to', 'wr')], **opts)

def test_visitor_relationships(self):
opts = {'interfaces': ['rest'],
'entity_types': ['package_relationship']}
Expand Down Expand Up @@ -577,7 +581,7 @@ def test_outcast_search(self):
self._test_cant('search', self.outcast, self.ENTITY_NAME, entity_types=['dataset']) # cannot search groups
self._test_cant('read', self.outcast, self.ENTITY_NAME, entity_types=['tag'])


class TestLockedDownViaRoles(TestController):
'''Use case:
* 'Visitor' has no edit rights
Expand All @@ -593,17 +597,17 @@ def setup_class(self):
.filter(model.UserObjectRole.user==model.User.by_name(u"visitor"))
for role in q:
model.Session.delete(role)

q = model.Session.query(model.RoleAction).filter(model.RoleAction.role==model.Role.READER)
for role_action in q:
model.Session.delete(role_action)

model.repo.commit_and_remove()
setup_test_search_index()
TestUsage._create_test_data()
model.Session.remove()
self.user_name = TestUsage.mrloggedin.name.encode('utf-8')

def _check_logged_in_users_authorized_only(self, offset):
'''Checks the offset is accessible to logged in users only.'''
res = self.app.get(offset, extra_environ={})
Expand All @@ -613,7 +617,7 @@ def _check_logged_in_users_authorized_only(self, offset):

def test_home(self):
self._check_logged_in_users_authorized_only('/')

def test_tags_pages(self):
self._check_logged_in_users_authorized_only('/tag')
self._check_logged_in_users_authorized_only('/tag/test')
Expand All @@ -626,7 +630,7 @@ def test_user_pages(self):
self._check_logged_in_users_authorized_only('/user/' + self.user_name)
res = self.app.get('/user/login', extra_environ={})
assert res.status in [200], res.status

def test_new_package(self):
offset = url_for(controller='package', action='new')
self._check_logged_in_users_authorized_only(offset)
Expand Down

0 comments on commit 0efe8a7

Please sign in to comment.