class TestPublisherGroupPackages(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def _run_fail_test(self, username, action): pkg = model.Package.by_name(self.packagename) context = {'package': pkg, 'model': model, 'user': username} try: self.auth.check_access(action, context, {}) assert False, "The user should not have access" except NotAuthorized, e: pass
class TestPublisherGroupPackages(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def _run_fail_test( self, username, action): pkg = model.Package.by_name(self.packagename) context = { 'package': pkg, 'model': model, 'user': username } try: self.auth.check_access(action, context, {}) assert False, "The user should not have access" except NotAuthorized, e: pass
class TestPublisherPackageRelationships(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.package1name = u'testpkg' self.package2name = u'testpkg2' model.repo.new_revision() pkg1 = model.Package(name=self.package1name) pkg2 = model.Package(name=self.package2name) model.Session.add(pkg1) model.Session.add(pkg2) model.Session.flush() pkg1 = model.Package.by_name(self.package1name) pkg2 = model.Package.by_name(self.package2name) self.rel = model.PackageRelationship(name="test", type='depends_on') self.rel.subject = pkg1 self.rel.object = pkg2 model.Session.add(self.rel) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_create_fail_user(self): p1 = model.Package.by_name(self.package1name) p2 = model.Package.by_name(self.package2name) context = {'model': model, 'user': '******'} try: self.auth.check_access('package_relationship_create', context, { 'id': p1.id, 'id2': p2.id }) assert False, "The user should not have access." except NotAuthorized, e: pass
class TestPublisherShow(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_package_show_deleted_success(self): userobj = model.User.get('russianfan') grp = model.Group.by_name(self.groupname) pkg = model.Package.by_name(self.packagename) pkg.state = 'deleted' f = model.User.get_groups g = model.Package.get_groups def gg(*args, **kwargs): return [grp] model.User.get_groups = gg model.Package.get_groups = gg context = {'package': pkg, 'model': model, 'user': userobj.name} try: self.auth.check_access('package_show', context, {}) except NotAuthorized, e: assert False, "The user should have %s access: %r." % (action, e.extra_msg) model.User.get_groups = f model.Package.get_groups = g pkg.state = "active"
class TestPublisherPackageRelationships(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.package1name = u'testpkg' self.package2name = u'testpkg2' model.repo.new_revision() pkg1 = model.Package(name=self.package1name) pkg2 = model.Package(name=self.package2name) model.Session.add( pkg1 ) model.Session.add( pkg2 ) model.Session.flush() pkg1 = model.Package.by_name(self.package1name) pkg2 = model.Package.by_name(self.package2name) self.rel = model.PackageRelationship(name="test", type='depends_on') self.rel.subject = pkg1 self.rel.object = pkg2 model.Session.add( self.rel ) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_create_fail_user( self): p1 = model.Package.by_name( self.package1name ) p2 = model.Package.by_name( self.package2name ) context = { 'model': model, 'user': '******' } try: self.auth.check_access('package_relationship_create', context, {'id': p1.id, 'id2': p2.id}) assert False, "The user should not have access." except NotAuthorized, e: pass
class TestPublisherShow(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_package_show_deleted_success(self): userobj = model.User.get('russianfan') grp = model.Group.by_name(self.groupname) pkg = model.Package.by_name(self.packagename) pkg.state = 'deleted' f = model.User.get_groups g = model.Package.get_groups def gg(*args, **kwargs): return [grp] model.User.get_groups = gg model.Package.get_groups = gg context = { 'package': pkg, 'model': model, 'user': userobj.name } try: self.auth.check_access('package_show', context, {}) except NotAuthorized, e: assert False, "The user should have %s access: %r." % (action, e.extra_msg) model.User.get_groups = f model.Package.get_groups = g pkg.state = "active"
class TestPublisherEdit(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_0_not_authz(self): from pylons import config from nose.exc import SkipTest if config.get('ckan.auth.profile', '') != 'publisher': raise SkipTest('Publisher auth profile not enabled') offset = url_for(controller='group', action='edit', id=self.groupname) # 401 gets caught by repoze.who and turned into redirect res = self.app.get(offset, status=[302, 401]) res = res.follow() assert res.request.url.startswith('/user/login') def test_2_edit(self): from pylons import config from nose.exc import SkipTest if config.get('ckan.auth.profile', '') != 'publisher': raise SkipTest('Publisher auth profile not enabled') group = model.Group.by_name(self.groupname) offset = url_for(controller='group', action='edit', id=self.groupname) user = model.User.get('russianfan') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Edit: %s' % group.title in res, res form = res.forms['group-edit'] titlefn = 'title' descfn = 'description' newtitle = 'xxxxxxx' newdesc = '''### Lots of stuff here Ho ho ho ''' form[titlefn] = newtitle form[descfn] = newdesc pkg = model.Package.by_name(self.packagename) form['packages__2__name'] = pkg.name res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) # should be read page # assert 'Groups - %s' % self.groupname in res, res model.Session.remove() group = model.Group.by_name(self.groupname) assert group.title == newtitle, group assert group.description == newdesc, group # now look at datasets assert len(group.active_packages().all()) == 3 def test_3_edit_form_has_new_package(self): # check for dataset in autocomplete offset = url_for(controller='package', action='autocomplete', q='an') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'annakarenina' in res, res assert not 'newone' in res, res model.repo.new_revision() pkg = model.Package(name=u'anewone') model.Session.add(pkg) model.repo.commit_and_remove() model.repo.new_revision() pkg = model.Package.by_name(u'anewone') user = model.User.by_name(u'russianfan') model.setup_default_user_roles(pkg, [user]) model.repo.commit_and_remove() res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'annakarenina' in res, res assert 'newone' in res def test_4_new_duplicate_package(self): prefix = '' # Create group group_name = u'testgrp4' CreateTestData.create_groups([{'name': group_name, 'packages': [self.packagename]}], admin_user_name='russianfan') # Add same package again offset = url_for(controller='group', action='edit', id=group_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) fv = res.forms['group-edit'] fv['packages__1__name'] = self.packagename res = fv.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) res = res.follow() assert group_name in res, res model.Session.remove() # check package only added to the group once group = model.Group.by_name(group_name) pkg_names = [pkg.name for pkg in group.active_packages().all()] assert_equal(pkg_names, [self.packagename]) def test_edit_plugin_hook(self): plugin = MockGroupControllerPlugin() plugins.load(plugin) offset = url_for(controller='group', action='edit', id=self.groupname) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) form = res.forms['group-edit'] group = model.Group.by_name(self.groupname) form['title'] = "huhuhu" res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert plugin.calls['edit'] == 1, plugin.calls plugins.unload(plugin) def test_edit_non_auth(self): offset = url_for(controller='group', action='edit', id=self.groupname) res = self.app.get(offset, status=[302,401], extra_environ={'REMOTE_USER': '******'}) def test_edit_fail_auth(self): context = { 'group': model.Group.by_name(self.groupname), 'model': model, 'user': '******' } try: if self.auth.check_access('group_update',context, {}): assert False, "Check access said we were allowed but we shouldn't really" except NotAuthorized, e: pass # Do nothing as this is what we expected
class TestPublisherEdit(FunctionalTestCase): @classmethod def setup_class(self): from ckan.tests.mock_publisher_auth import MockPublisherAuth self.auth = MockPublisherAuth() model.Session.remove() CreateTestData.create(auth_profile='publisher') self.groupname = u'david' self.packagename = u'testpkg' model.repo.new_revision() model.Session.add(model.Package(name=self.packagename)) model.repo.commit_and_remove() @classmethod def teardown_class(self): model.Session.remove() model.repo.rebuild_db() model.Session.remove() def test_0_not_authz(self): from pylons import config from nose.exc import SkipTest if config.get('ckan.auth.profile', '') != 'publisher': raise SkipTest('Publisher auth profile not enabled') offset = url_for(controller='group', action='edit', id=self.groupname) # 401 gets caught by repoze.who and turned into redirect res = self.app.get(offset, status=[302, 401]) res = res.follow() assert res.request.url.startswith('/user/login') def test_2_edit(self): from pylons import config from nose.exc import SkipTest if config.get('ckan.auth.profile', '') != 'publisher': raise SkipTest('Publisher auth profile not enabled') group = model.Group.by_name(self.groupname) offset = url_for(controller='group', action='edit', id=self.groupname) user = model.User.get('russianfan') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'Edit: %s' % group.title in res, res form = res.forms['group-edit'] titlefn = 'title' descfn = 'description' newtitle = 'xxxxxxx' newdesc = '''### Lots of stuff here Ho ho ho ''' form[titlefn] = newtitle form[descfn] = newdesc pkg = model.Package.by_name(self.packagename) form['packages__2__name'] = pkg.name res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) # should be read page # assert 'Groups - %s' % self.groupname in res, res model.Session.remove() group = model.Group.by_name(self.groupname) assert group.title == newtitle, group assert group.description == newdesc, group # now look at datasets assert len(group.active_packages().all()) == 3 def test_3_edit_form_has_new_package(self): # check for dataset in autocomplete offset = url_for(controller='package', action='autocomplete', q='an') res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'annakarenina' in res, res assert not 'newone' in res, res model.repo.new_revision() pkg = model.Package(name=u'anewone') model.Session.add(pkg) model.repo.commit_and_remove() model.repo.new_revision() pkg = model.Package.by_name(u'anewone') user = model.User.by_name(u'russianfan') model.setup_default_user_roles(pkg, [user]) model.repo.commit_and_remove() res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) assert 'annakarenina' in res, res assert 'newone' in res def test_4_new_duplicate_package(self): prefix = '' # Create group group_name = u'testgrp4' CreateTestData.create_groups([{ 'name': group_name, 'packages': [self.packagename] }], admin_user_name='russianfan') # Add same package again offset = url_for(controller='group', action='edit', id=group_name) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) fv = res.forms['group-edit'] fv['packages__1__name'] = self.packagename res = fv.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) res = res.follow() assert group_name in res, res model.Session.remove() # check package only added to the group once group = model.Group.by_name(group_name) pkg_names = [pkg.name for pkg in group.active_packages().all()] assert_equal(pkg_names, [self.packagename]) def test_edit_plugin_hook(self): plugin = MockGroupControllerPlugin() plugins.load(plugin) offset = url_for(controller='group', action='edit', id=self.groupname) res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': '******'}) form = res.forms['group-edit'] group = model.Group.by_name(self.groupname) form['title'] = "huhuhu" res = form.submit('save', status=302, extra_environ={'REMOTE_USER': '******'}) assert plugin.calls['edit'] == 1, plugin.calls plugins.unload(plugin) def test_edit_non_auth(self): offset = url_for(controller='group', action='edit', id=self.groupname) res = self.app.get(offset, status=[302, 401], extra_environ={'REMOTE_USER': '******'}) def test_edit_fail_auth(self): context = { 'group': model.Group.by_name(self.groupname), 'model': model, 'user': '******' } try: if self.auth.check_access('group_update', context, {}): assert False, "Check access said we were allowed but we shouldn't really" except NotAuthorized, e: pass # Do nothing as this is what we expected