diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 3a00ee34e07..4f877de7160 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1107,7 +1107,6 @@ def resource_read(self, id, resource_id): c.datastore_api = '%s/api/action' % \ config.get('ckan.site_url', '').rstrip('/') - c.resource['can_be_previewed'] = self._resource_preview( {'resource': c.resource, 'package': c.package}) @@ -1310,8 +1309,8 @@ def activity(self, id): c.pkg_dict = get_action('package_show')(context, data_dict) c.pkg = context['package'] c.package_activity_stream = get_action( - 'package_activity_list_html')(context, - {'id': c.pkg_dict['id']}) + 'package_activity_list_html')( + context, {'id': c.pkg_dict['id']}) dataset_type = c.pkg_dict['type'] or 'dataset' except NotFound: abort(404, _('Dataset not found')) diff --git a/ckan/tests/legacy/functional/test_home.py b/ckan/tests/legacy/functional/test_home.py deleted file mode 100644 index 2e3add427dd..00000000000 --- a/ckan/tests/legacy/functional/test_home.py +++ /dev/null @@ -1,138 +0,0 @@ -from pylons.i18n import set_lang - -from ckan.lib.create_test_data import CreateTestData -from ckan.controllers.home import HomeController -import ckan.model as model - -from ckan.tests.legacy import * -from ckan.tests.legacy.html_check import HtmlCheckMethods -from ckan.tests.legacy.pylons_controller import PylonsTestCase -from ckan.tests.legacy import setup_test_search_index - -from ckan.common import c, session - -class TestHomeController(TestController, PylonsTestCase, HtmlCheckMethods): - @classmethod - def setup_class(cls): - setup_test_search_index() - PylonsTestCase.setup_class() - model.repo.init_db() - CreateTestData.create() - - @classmethod - def teardown_class(self): - model.repo.rebuild_db() - - def test_template_head_end(self): - offset = url_for('home') - res = self.app.get(offset) - assert 'ckan.template_head_end = ' - - def test_template_footer_end(self): - offset = url_for('home') - res = self.app.get(offset) - assert 'TEST TEMPLATE_FOOTER_END TEST' - - - def test_update_profile_notice(self): - edit_url = url_for(controller='user', action='edit') - email_notice = 'Please update your profile' \ - ' and add your email address.' % (edit_url) - fullname_notice = 'Please update your profile' \ - ' and add your full name' % (edit_url) - email_and_fullname_notice ='Please update your' \ - ' profile and add your email address and your full name.' \ - % (edit_url) - url = url_for('home') - - # No update profile notices should be flashed if no one is logged in. - response = self.app.get(url) - assert email_notice not in response - assert fullname_notice not in response - assert email_and_fullname_notice not in response - - # Make some test users. - user1 = model.user.User(name='user1', fullname="user 1's full name", - email='user1@testusers.org') - user2 = model.user.User(name='user2', fullname="user 2's full name") - user3 = model.user.User(name='user3', email='user3@testusers.org') - user4 = model.user.User(name='user4') - - # Some test users with Google OpenIDs. - user5 = model.user.User( - name='https://www.google.com/accounts/o8/id/id=ACyQatixLeL' - 'ODscWvwqsCXWQ2sa3RRaBhaKTkcsvUElI6tNHIQ1_egX_wt1x3fA' - 'Y983DpW4UQV_U', - fullname="user 5's full name", email="user5@testusers.org") - user6 = model.user.User( - name='https://www.google.com/accounts/o8/id/id=ACyQatixLeL' - 'ODscWvwqsCXWQ2sa3RRaBhaKTkcsvUElI6tNHIQ1_egX_wt1x3fA' - 'Y983DpW4UQV_J', - fullname="user 6's full name") - user7 = model.user.User( - name='https://www.google.com/accounts/o8/id/id=AItOawl27F2' - 'M92ry4jTdjiVx06tuFNA', - email='user7@testusers.org') - user8 = model.user.User( - name='https://www.google.com/accounts/o8/id/id=AItOawl27F2' - 'M92ry4jTdjiVx06tuFNs' - ) - - users = (user1, user2, user3, user4, user5, user6, user7, user8) - google_users = (user5, user6, user7, user8) - - for user in users: - model.repo.new_revision() - model.Session.add(user) - model.Session.commit() - - response = self.app.get(url, extra_environ={'REMOTE_USER': - user.name.encode('utf-8')}) - - model.repo.new_revision() - model.Session.add(user) - - if user in google_users: - # Users with Google OpenIDs are asked to give their email if - # they don't have one and to enter a full name if they don't - # have one. - if not user.email and not user.fullname: - assert email_and_fullname_notice in response - assert email_notice not in response - assert fullname_notice not in response - elif user.email and not user.fullname: - assert email_notice not in response - assert fullname_notice in response, response - assert email_and_fullname_notice not in response - elif not user.email and user.fullname: - assert email_notice in response - assert fullname_notice not in response - assert email_and_fullname_notice not in response - elif user.email and user.fullname: - assert email_notice not in response - assert fullname_notice not in response - assert email_and_fullname_notice not in response - else: - # Users without Google OpenIDs are just asked to give their - # email if they don't have one. - if not user.email: - assert email_notice in response - assert email_and_fullname_notice not in response - assert fullname_notice not in response - elif user.email: - assert email_notice not in response - assert fullname_notice not in response - assert email_and_fullname_notice not in response - - if not user.email: - user.email = "mr_tusks@tusk_family.org" - if not user.fullname: - user.fullname = "Mr. Tusks" - model.Session.commit() - - response = self.app.get(url, extra_environ={'REMOTE_USER': - user.name.encode('utf-8')}) - assert email_notice not in response - assert fullname_notice not in response - assert email_and_fullname_notice not in response - diff --git a/ckan/tests/legacy/functional/test_related.py b/ckan/tests/legacy/functional/test_related.py deleted file mode 100644 index aa636a8957d..00000000000 --- a/ckan/tests/legacy/functional/test_related.py +++ /dev/null @@ -1,482 +0,0 @@ -import json - -from nose.tools import assert_equal, assert_raises - -import ckan.tests.legacy as tests -import ckan.model as model -import ckan.logic as logic -import ckan.lib.helpers as h -import ckan.tests.legacy.functional.base as base -import ckan.tests.legacy.functional.api.base as apibase - - -class TestRelated: - - @classmethod - def setup_class(self): - model.Session.remove() - tests.CreateTestData.create() - - @classmethod - def teardown_class(self): - model.repo.rebuild_db() - - def test_create(self): - p = model.Package.get('warandpeace') - r = model.Related() - p.related.append(r) - - assert len(p.related) == 1, p.related - assert len(r.datasets) == 1, r.datasets - - model.Session.add(p) - model.Session.add(r) - model.Session.commit() - - # To get the RelatedDataset objects (for state change) - assert p.related_count == 1, p.related_count - assert len(model.Related.get_for_dataset(p)) == 1 - assert len(model.Related.get_for_dataset(p,status='inactive')) == 0 - p.related.remove(r) - model.Session.delete(r) - model.Session.commit() - - assert len(p.related) == 0 - assert p.related_count == 0, p.related_count - - - def test_inactive_related(self): - p = model.Package.get('warandpeace') - r = model.Related() - p.related.append(r) - assert len(p.related) == 1, p.related - model.Session.add(r) - model.Session.commit() - - # To get the RelatedDataset objects (for state change) - assert p.related_count == 1, p.related_count - assert len(model.Related.get_for_dataset(p,status='active')) == 1 - assert len(model.Related.get_for_dataset(p,status='inactive')) == 0 - r.deactivate( p ) - r.deactivate( p ) # Does nothing. - model.Session.refresh(p) - assert p.related_count == 0, p.related_count - assert len(model.Related.get_for_dataset(p,status='active')) == 0 - assert len(model.Related.get_for_dataset(p,status='inactive')) == 1 - - model.Session.refresh(p) # Would like to get rid of the need for this - assert len(p.related) == 0, p.related # not sure inactive item ... - model.Session.delete(r) - - - def _related_create(self, title, description, type, url, image_url): - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = dict(title=title,description=description, - url=url,image_url=image_url,type=type) - return logic.get_action("related_create")( context, data_dict ) - - def test_related_create(self): - rel = self._related_create("Title", "Description", - "visualization", - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - assert rel['title'] == "Title", rel - assert rel['description'] == "Description", rel - assert rel['type'] == "visualization", rel - assert rel['url'] == "http://ckan.org", rel - assert rel['image_url'] == "http://ckan.org/files/2012/03/ckanlogored.png", rel - - def test_related_create_fail(self): - try: - rel = self._related_create("Title", "Description", - None, - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - assert False, "Create succeeded with missing field" - except logic.ValidationError, e: - assert 'type' in e.error_dict and e.error_dict['type'] == [u'Missing value'] - - def test_related_create_featured_as_sysadmin(self): - '''Sysadmin can create featured related items''' - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - - context = { - 'model': model, - 'user': usr['name'], - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - 'featured': 1, - } - - result = logic.get_action("related_create")(context, data_dict) - - assert_equal(result['featured'], 1) - - def test_related_create_featured_as_non_sysadmin_fails(self): - '''Non-sysadmin users should not be able to create featured relateds''' - - context = { - 'model': model, - 'user': 'annafan', - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - 'featured': 1, - } - - assert_raises( - logic.NotAuthorized, - logic.get_action('related_create'), - context, - data_dict) - - def test_related_create_not_featured_as_non_sysadmin_succeeds(self): - '''Non-sysadmins can set featured to false''' - - context = { - 'model': model, - 'user': 'annafan', - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - 'featured': 0, - } - - result = logic.get_action("related_create")(context, data_dict) - - assert_equal(result['featured'], 0) - - def test_related_create_featured_empty_as_non_sysadmin_succeeds(self): - '''Non-sysadmins can leave featured empty.''' - - context = { - 'model': model, - 'user': 'annafan', - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - } - - result = logic.get_action("related_create")(context, data_dict) - - assert_equal(result['featured'], 0) - - def test_related_delete(self): - rel = self._related_create("Title", "Description", - "visualization", - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = dict(id=rel['id']) - logic.get_action('related_delete')(context, data_dict) - - r = model.Related.get(rel['id']) - assert r is None, r # Ensure it doesn't exist - - def test_related_update(self): - rel = self._related_create("Title", "Description", - "visualization", - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = rel - data_dict['title'] = "New Title" - result = logic.get_action('related_update')(context,data_dict) - assert result['title'] == 'New Title' - - def test_sysadmin_changes_related_items_featured_field(self): - '''Sysadmins can change featured field''' - rel = self._related_create( - "Title", - "Description", - "visualization", - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = { - 'model': model, - 'user': usr['name'], - 'session': model.Session - } - - data_dict = rel - data_dict['title'] = "New Title" - data_dict['featured'] = 1 - result = logic.get_action('related_update')(context,data_dict) - assert_equal(result['title'], 'New Title') - assert_equal(result['featured'], 1) - - def test_non_sysadmin_changes_related_items_featured_field_fails(self): - '''Non-sysadmins cannot change featured field''' - - context = { - 'model': model, - 'user': 'annafan', - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - } - - # Create the related item as annafan - result = logic.get_action('related_create')(context, data_dict) - - # Try to change it to a featured item - result['featured'] = 1 - - try: - logic.get_action('related_update')(context, result) - except logic.NotAuthorized, e: - # Check it's the correct authorization error - assert 'featured' in str(e) - - def test_non_sysadmin_can_update_related_item(self): - '''Non-sysadmins can change related item. - - If they don't change the featured field. - ''' - - context = { - 'model': model, - 'user': 'annafan', - 'session': model.Session - } - - data_dict = { - 'title': 'Title', - 'description': 'Description', - 'type': 'visualization', - 'url': 'http://ckan.org', - 'image_url': 'http://ckan.org/files/2012/03/ckanlogored.png', - } - - # Create the related item as annafan - result = logic.get_action('related_create')(context, data_dict) - - # Try to change it to a featured item - result['title'] = 'New Title' - - result = logic.get_action('related_update')(context, result) - assert_equal(result['title'], 'New Title') - - def test_update_related_item_check_owner_status(self): - '''After edit of a related item by a sysadmin, check that the owner id is unchanged - ''' - offset = h.url_for(controller='related', - action='new', id='warandpeace') - data = { - "title": "testing_create", - "url": u"http://ckan.org/feed/", - } - user = model.User.by_name('tester') - admin = model.User.by_name('testsysadmin') - - #create related item - context = dict(model=model, user=user.name, session=model.Session) - data_dict = dict(title="testing_create",description="description", - url="http://ckan.org/feed/",image_url="",type="visualization") - res = logic.get_action("related_create")( context, data_dict ) - - #edit related item - data_dict = dict(id=res['id'],title="testing_update",description="description", - url="http://ckan.org/feed/",image_url="",type="visualization") - - context = dict(model=model, user=admin.name, session=model.Session) - result = logic.get_action('related_update')(context,data_dict) - #Confirm related item owner status - assert result['owner_id'] == user.id - - def test_related_show(self): - rel = self._related_create("Title", "Description", - "visualization", - "http://ckan.org", - "http://ckan.org/files/2012/03/ckanlogored.png") - - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = {'id': rel['id']} - - result = logic.get_action('related_show')(context,data_dict) - assert rel['id'] == result['id'], result - assert rel['title'] == result['title'], result - assert rel['description'] == result['description'], result - assert rel['description'] == result['description'], result - - def test_related_list_missing_id_and_name(self): - p = model.Package.get('warandpeace') - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = {} - related_list = logic.get_action('related_list')(context, data_dict) - assert len(related_list) == 8 - related_keys = set(['view_count', 'description', 'title', 'url', - 'created', 'featured', 'image_url', 'type', 'id', 'owner_id']) - for related in related_list: - assert set(related.keys()) == related_keys - - - def test_related_list(self): - p = model.Package.get('warandpeace') - r = model.Related(title="Title", type="idea") - p.related.append(r) - r = model.Related(title="Title 2", type="idea") - p.related.append(r) - model.Session.add(r) - model.Session.commit() - - assert len(p.related) == 2 - assert p.related_count == 2, p.related_count - - usr = logic.get_action('get_site_user')({'model':model,'ignore_auth': True},{}) - context = dict(model=model, user=usr['name'], session=model.Session) - data_dict = {'id': p.id} - - result = logic.get_action('related_list')(context,data_dict) - assert len(result) == len(p.related) - -class TestRelatedActionAPI(apibase.BaseModelApiTestCase): - - @classmethod - def setup_class(cls): - model.Session.remove() - tests.CreateTestData.create() - cls.user_name = u'russianfan' # created in CreateTestData - cls.init_extra_environ(cls.user_name) - - @classmethod - def teardown_class(self): - model.repo.rebuild_db() - - def test_api_create_invalid(self): - res = self.app.post("/api/3/action/related_create", params="{}=1", - status=self.STATUS_409_CONFLICT, - extra_environ=self.extra_environ) - r = json.loads(res.body) - assert r['success'] == False, r - - - def _create(self, rtype="visualization", title="Test related item"): - r = { - "type": rtype, - "title": title - } - postparams = '%s=1' % json.dumps(r) - res = self.app.post("/api/3/action/related_create", params=postparams, - status=self.STATUS_200_OK, - extra_environ=self.extra_environ) - r = json.loads(res.body) - return r - - def test_api_create_valid(self): - r = self._create() - assert r['success'] == True, r - assert r['result']['type'] == "visualization" - assert r['result']['title'] == "Test related item" - - def test_api_show(self): - existing = self._create() - - r = { - "id": existing["result"]["id"] - } - postparams = '%s=1' % json.dumps(r) - res = self.app.post("/api/3/action/related_show", params=postparams, - status=self.STATUS_200_OK, - extra_environ=self.extra_environ) - r = json.loads(res.body) - assert r['success'] == True, r - assert r['result']['type'] == "visualization" - assert r['result']['title'] == "Test related item" - - - def test_api_list(self): - p = model.Package.get('warandpeace') - one = model.Related(type="idea", title="one") - two = model.Related(type="idea", title="two") - p.related.append(one) - p.related.append(two) - model.Session.commit() - - r = { - "id": p.id - } - postparams = '%s=1' % json.dumps(r) - res = self.app.post("/api/3/action/related_list", params=postparams, - status=self.STATUS_200_OK, - extra_environ=self.extra_environ) - r = json.loads(res.body) - assert r['success'] == True, r - assert r['result'][0]['type'] == "idea" - assert r['result'][0]['title'] == "two", r - - p.related.remove(one) - p.related.remove(two) - model.Session.delete(one) - model.Session.delete(two) - - def test_api_delete(self): - existing = self._create() - - r = { - "id": existing["result"]["id"] - } - postparams = '%s=1' % json.dumps(r) - res = self.app.post("/api/3/action/related_delete", params=postparams, - status=self.STATUS_200_OK, - extra_environ=self.extra_environ) - r = json.loads(res.body) - assert r['success'] == True, r - assert r['result'] is None, r - - def test_api_delete_fail(self): - existing = self._create() - r = { - "id": existing["result"]["id"] - } - - usr = model.User.by_name("annafan") - extra={'Authorization' : str(usr.apikey)} - - postparams = '%s=1' % json.dumps(r) - res = self.app.post("/api/3/action/related_delete", params=postparams, - status=self.STATUS_403_ACCESS_DENIED, - extra_environ=extra) - r = json.loads(res.body) - assert r['success'] == False, r - assert r[u'error'][u'__type'] == "Authorization Error", r diff --git a/ckan/tests/legacy/logic/test_action.py b/ckan/tests/legacy/logic/test_action.py index 164eb42d72a..2c109002bea 100644 --- a/ckan/tests/legacy/logic/test_action.py +++ b/ckan/tests/legacy/logic/test_action.py @@ -1362,72 +1362,3 @@ def _assert_we_can_add_user_to_group(self, user_id, group_id): group_ids = [g.id for g in groups] assert res['success'] is True, res assert group.id in group_ids, (group, user_groups) -<<<<<<< HEAD -======= - - -class TestRelatedAction(WsgiAppCase): - - sysadmin_user = None - - normal_user = None - - @classmethod - def setup_class(cls): - search.clear_all() - CreateTestData.create() - cls.sysadmin_user = model.User.get('testsysadmin') - - @classmethod - def teardown_class(cls): - model.repo.rebuild_db() - - def _add_basic_package(self, package_name=u'test_package', **kwargs): - package = { - 'name': package_name, - 'title': u'A Novel By Tolstoy', - 'resources': [{ - 'description': u'Full text.', - 'format': u'plain text', - 'url': u'http://datahub.io/download/' - }] - } - package.update(kwargs) - - postparams = '%s=1' % json.dumps(package) - res = self.app.post('/api/action/package_create', params=postparams, - extra_environ={'Authorization': 'tester'}) - return json.loads(res.body)['result'] - - def test_update_add_related_item(self): - package = self._add_basic_package() - related_item = { - "description": "Testing a Description", - "url": "http://example.com/image.png", - "title": "Testing", - "featured": 0, - "image_url": "http://example.com/image.png", - "type": "idea", - "dataset_id": package['id'], - } - related_item_json = json.dumps(related_item) - res_create = self.app.post('/api/action/related_create', - params=related_item_json, - extra_environ={'Authorization': 'tester'}) - assert res_create.json['success'] - - related_update = res_create.json['result'] - related_update = {'id': related_update['id'], 'title': 'Updated'} - related_update_json = json.dumps(related_update) - res_update = self.app.post('/api/action/related_update', - params=related_update_json, - extra_environ={'Authorization': 'tester'}) - assert res_update.json['success'] - res_update_json = res_update.json['result'] - assert res_update_json['title'] == related_update['title'] - - related_item.pop('title') - related_item.pop('dataset_id') - for field in related_item: - assert related_item[field] == res_update_json[field] ->>>>>>> master