def test_resource_with_0_views(self): app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) resource = self._create_resource(app, package, apikey) # The package_show() API should return 0 recent views and 0 total # views for the unviewed resource. package = tests.call_action_api(app, 'package_show', id=package['name']) assert len(package['resources']) == 1 resource = package['resources'][0] tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 0, ("A resource that has not " "been viewed should have 0 " "recent views") assert tracking_summary['total'] == 0, ("A resource that has not " "been viewed should have 0 " "total views") # The resource_show() API should return 0 recent views and 0 total # views for the unviewed resource. resource = tests.call_action_api(app, 'resource_show', id=resource['id']) tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 0, ("A resource that has not " "been viewed should have 0 " "recent views") assert tracking_summary['total'] == 0, ("A resource that has not " "been viewed should have 0 " "total views")
def test_create_public_dataset_by_nonmember(self): ''' Anyone can create a public dataset to an organization ''' data_dict = copy.deepcopy(self.TEST_DATADICT) data_dict['private'] = False call_action_api(self.app, 'package_create', apikey=self.user_anna.apikey, status=200, **data_dict)
def test_api_create_dataset(self): tests.call_action_api(self.app, 'package_create', status=409, name='test-name-1', title="test-title-1", content_type="test1,test2", license_id="other", notes="test notes", tag_string="tag1,tag2", apikey=self.sysadmin.apikey) tests.call_action_api(self.app, 'package_create', status=200, name='test-name-2', title="test-title-2", content_type="test1,test2", license_id="other", notes="test notes", tag_string="tag1,tag2", collection_type="Open Data", apikey=self.sysadmin.apikey) test_dataset = Package.get('test-name-2') self.assert_equal(test_dataset.maintainer, "") self.assert_equal(test_dataset.maintainer_email, "") if not asbool(config.get('ckanext.ytp.auto_author', False)): self.assert_equal(test_dataset.author, "") self.assert_equal(test_dataset.author_email, "")
def test_update_by_data_pid_fail(self): '''Try to update a dataset with wrong PIDs''' data_dict = copy.deepcopy(self.TEST_DATADICT) data_dict['pids'] = [{'id': unicode(x), 'type': 'data'} for x in range(1, 9)] call_action_api(self.app, 'package_update', apikey=self.user_normal.apikey, status=409, **data_dict)
def test_request_membership(self): tests.call_action_api(self.app, 'hdx_send_request_membership', display_name='User Name', name='username', email='*****@*****.**', organization='Org Name', message='Some message', admins=[], status=403)
def test_create_dataset_and_resources(self): ''' Add a dataset and 20 resources and read dataset through API ''' print 'Create dataset' output = call_action_api(self.app, 'package_create', apikey=self.user_normal.apikey, status=200, **self.TEST_DATADICT) if '__type' in output: assert output['__type'] != 'Validation Error' assert 'id' in output new_res = copy.deepcopy(TEST_RESOURCE) new_res['package_id'] = output['id'] for res_num in range(20): print 'Adding resource %r' % (res_num + 1) output = call_action_api(self.app, 'resource_create', apikey=self.user_normal.apikey, status=200, **new_res) if '__type' in output: assert output['__type'] != 'Validation Error' assert output print 'Read dataset' output = call_action_api(self.app, 'package_show', apikey=self.user_normal.apikey, status=200, id=new_res['package_id']) assert 'id' in output # Check that some metadata value is correct. assert output['checksum'] == self.TEST_DATADICT['checksum']
def test_create_and_read_resource(self): ''' Create and read resource data through API and test that 'url' matches. Availability 'through_provider'. ''' data_dict = copy.deepcopy(self.TEST_DATADICT) data_dict['availability'] = 'through_provider' data_dict['through_provider_URL'] = 'http://www.tdata.fi/' data_dict.pop('direct_download_URL') output = call_action_api(self.app, 'package_create', apikey=self.user_normal.apikey, status=200, **data_dict) assert 'id' in output new_res = copy.deepcopy(TEST_RESOURCE) new_res['package_id'] = output['id'] output = call_action_api(self.app, 'resource_create', apikey=self.user_normal.apikey, status=200, **new_res) assert output output = call_action_api(self.app, 'package_show', apikey=self.user_normal.apikey, status=200, id=new_res['package_id']) assert 'id' in output resources = output.get('resources') assert len(resources) == 2 assert resources[0]['url'] == TEST_RESOURCE['url'] or \ resources[1]['url'] == TEST_RESOURCE['url'], resources[0]['url'] + ' --- ' + resources[1]['url']
def test_create_update_delete_dataset(self): ''' Add, modify and delete a dataset through API ''' print 'Create dataset' output = call_action_api(self.app, 'package_create', apikey=self.user_normal.apikey, status=200, **self.TEST_DATADICT) if '__type' in output: assert output['__type'] != 'Validation Error' assert 'id' in output data_dict = copy.deepcopy(self.TEST_DATADICT) data_dict['id'] = output['id'] print 'Update dataset' output = call_action_api(self.app, 'package_update', apikey=self.user_normal.apikey, status=200, **data_dict) if '__type' in output: assert output['__type'] != 'Validation Error' assert output print 'Update dataset' output = call_action_api(self.app, 'package_update', apikey=self.user_normal.apikey, status=200, **data_dict) if '__type' in output: assert output['__type'] != 'Validation Error' assert output print 'Delete dataset' output = call_action_api(self.app, 'package_delete', apikey=self.user_normal.apikey, status=200, id=data_dict['id'])
def test_resource_with_one_download(self): app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) resource = self._create_resource(app, package, apikey) self._post_to_tracking(app, resource['url'], type_='resource') self._update_tracking_summary() package = tests.call_action_api(app, 'package_show', id=package['id']) assert len(package['resources']) == 1 resource = package['resources'][0] assert package['tracking_summary']['recent'] == 0, ( "Downloading a resource should not increase the package's recent " "views") assert package['tracking_summary']['total'] == 0, ( "Downloading a resource should not increase the package's total " "views") assert resource['tracking_summary']['recent'] == 1, ( "Downloading a resource should increase the resource's recent " "views") assert resource['tracking_summary']['total'] == 1, ( "Downloading a resource should increase the resource's total " "views") # The resource_show() API should return the same result. resource = tests.call_action_api(app, 'resource_show', id=resource['id']) tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 1, ( "Downloading a resource should increase the resource's recent " "views") assert tracking_summary['total'] == 1, ( "Downloading a resource should increase the resource's total " "views")
def setup(self): '''Nose runs this method before each test method in our test class.''' # Access CKAN's model directly (bad) to create a sysadmin user and save # it against self for all test methods to access. self.sysadmin = model.User(name='test_sysadmin', sysadmin=True) model.Session.add(self.sysadmin) model.Session.commit() model.Session.remove() self.org_dict = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin.apikey, name='my_org_000') self.package_dict = tests.call_action_api(self.app, 'package_create', apikey=self.sysadmin.apikey, name='my_package_000', title='my package', notes='my package notes', tag_string='my_package', modified='2014-04-04', publisher='GSA', contact_name='john doe', contact_email='*****@*****.**', unique_id='000', public_access_level='public', bureau_code='001:40', program_code='015:010', access_level_comment='Access level commemnt', parent_dataset = 'true', ower_org = self.org_dict['id'] )
def setup(self): # Create a sysadmin user self.sysadmin = model.User(name='test_sysadmin', sysadmin=True) model.Session.add(self.sysadmin) model.Session.commit() model.Session.remove() # Create three users: test_member_1, test_member_2 and test_editor self.users = {} for name in ['test_member_1', 'test_member_2', 'test_editor']: self.users[name] = tests.call_action_api(self.app, 'user_create', apikey=self.sysadmin.apikey, name=name, email='email', password='******') # Create the organization test_org_1 of which test_member_1 and test_member_2 are # members, and test_editor is an editor users = [ {'name': 'test_member_1', 'capacity': 'member'}, {'name': 'test_member_2', 'capacity': 'member'}, {'name': 'test_editor', 'capacity': 'editor'}, ] self.organizations = {} self.organizations['test_org_1'] = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin.apikey, name='test_org_1', users=users) # Create the organization test_org_2 with no members. self.organizations['test_org_2'] = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin.apikey, name='test_org_2')
def test_check_observable_datastore_by_normal_user(self): resource = model.Package.get('annakarenina').resources[0] # is observable? tests.call_action_api(self.app, 'realtime_check_observable_datastore', resource_id=resource.id, apikey=self.normal_user.apikey, status=409)
def test_providing_res_with_url_calls_datapusher_correctly(self): pylons.config['datapusher.url'] = 'http://datapusher.ckan.org' httpretty.HTTPretty.register_uri(httpretty.HTTPretty.POST, 'http://datapusher.ckan.org/job', content_type='application/json', body=json.dumps({ 'job_id': 'foo', 'job_key': 'bar' })) package = model.Package.get('annakarenina') tests.call_action_api(self.app, 'datastore_create', apikey=self.sysadmin_user.apikey, resource=dict(package_id=package.id, url='demo.ckan.org')) assert len(package.resources) == 4, len(package.resources) resource = package.resources[3] data = json.loads(httpretty.last_request().body) assert data['metadata']['resource_id'] == resource.id, data assert not data['metadata'].get('ignore_hash'), data assert data['result_url'].endswith('/action/datapusher_hook'), data assert data['result_url'].startswith('http://'), data
def test_invalid_wrong_configuration(self): source_dict = copy.deepcopy(self.default_source_dict) source_dict['config'] = 'not_json' result = tests.call_action_api( self.app, self.action, apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'config' in result assert u'Error parsing the configuration options: No JSON object could be decoded' in result[ 'config'][0] source_dict['config'] = json.dumps({'custom_option': 'not_a_list'}) result = tests.call_action_api( self.app, self.action, apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'config' in result assert u'Error parsing the configuration options: custom_option must be a list' in result[ 'config'][0]
def _organization_create(self, organization_name): '''Return an organization with some users and a dataset.''' # Make an organization with some users. users = [{ 'name': self.member['name'], 'capacity': 'member' }, { 'name': self.editor['name'], 'capacity': 'editor' }, { 'name': self.admin['name'], 'capacity': 'admin' }] organization = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin.apikey, name=organization_name, users=users) # Add a dataset to the organization (have to do this separately # because the packages param of organization_create doesn't work). tests.call_action_api(self.app, 'package_update', name=self.package['name'], owner_org=organization['name'], apikey=self.sysadmin.apikey) return organization
def test_visitor_cannot_create_group_when_config_is_True(self): app = self._get_app(users_can_create_groups=True) tests.call_action_api(app, 'group_create', name='test-group', status=403)
def test_01_no_email_notifications_after_registration(self): '''A new user who isn't following anything shouldn't get any emails.''' # Clear any emails already sent due to CreateTestData.create(). tests.call_action_api(self.app, 'send_email_notifications', apikey=self.testsysadmin['apikey']) self.clear_smtp_messages() # Register a new user. sara = tests.call_action_api(self.app, 'user_create', apikey=self.testsysadmin['apikey'], name='sara', email='*****@*****.**', password='******', fullname='Sara Rollins', activity_streams_email_notifications=True) # Save the user for later tests to use. TestEmailNotifications.sara = sara # No notification emails should be sent to anyone at this point. tests.call_action_api(self.app, 'send_email_notifications', apikey=self.testsysadmin['apikey']) assert len(self.get_smtp_messages()) == 0
def setup_class(self): print ("") self.oMetadataPlugin = metadataPlugin.MetadataPlugin() # Make the Paste TestApp that we'll use to simulate HTTP requests to CKAN. self.app = paste.fixture.TestApp(pylons.test.pylonsapp) # Access CKAN's model directly (bad) to create a sysadmin user and save # it against self for all test methods to access. self.sysadmin_user = model.User(name='test_sysadmin', sysadmin=True) model.Session.add(self.sysadmin_user) model.Session.commit() model.Session.remove() #Create organization organization = {'name': 'test_org', 'title': 'Africa - Maroc', 'description': 'Maroc in north Africa.'} self.organization = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin_user.apikey, **organization) #Create Dataset and tied it to created org dataset = {'name': 'test_org_dataset_mapsearch', 'title': 'Africa - Maroc: Beautiful country for tourist', 'owner_org': organization['name'], 'md_package': ""} self.dataset = tests.call_action_api(self.app, 'package_create', apikey=self.sysadmin_user.apikey, **dataset) #Create Resource and tied it to created dataset resource = {'package_id': self.dataset['id'], 'url': 'http://teste.teste', 'md_resource': ""} self.resource = tests.call_action_api(self.app, 'resource_create', apikey=self.sysadmin_user.apikey, **resource)
def test_broadcast_event_by_admin(self): resource = model.Package.get('annakarenina').resources[0] tests.call_action_api(self.app, 'realtime_broadcast_event', resource_id=resource.id, event_type='datastore_update', apikey=self.sysadmin_user.apikey)
def test_create_and_delete_resources(self): ''' Add a dataset and add and delete a resource through API ''' print 'Create dataset' output = call_action_api(self.app, 'package_create', apikey=self.user_normal.apikey, status=200, **self.TEST_DATADICT) if '__type' in output: assert output['__type'] != 'Validation Error' assert 'id' in output print 'Add resource #1' new_res = copy.deepcopy(TEST_RESOURCE) new_res['package_id'] = output['id'] output = call_action_api(self.app, 'resource_create', apikey=self.user_normal.apikey, status=200, **new_res) if '__type' in output: assert output['__type'] != 'Validation Error' assert output res_id = output['id'] print 'Delete resource #1' # For some reason this is forbidden for the user that created the resource output = call_action_api(self.app, 'resource_delete', apikey=self.user_sysadmin.apikey, status=200, id=res_id) if output is not None and '__type' in output: assert output['__type'] != 'Validation Error'
def test_invalid_wrong_configuration(self): source_dict = copy.deepcopy(self.default_source_dict) source_dict['config'] = 'not_json' result = tests.call_action_api(self.app, self.action, apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'config' in result assert u'Error parsing the configuration options: No JSON object could be decoded' in result[ 'config'][0] source_dict['config'] = json.dumps({'custom_option': 'not_a_list'}) result = tests.call_action_api(self.app, self.action, apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'config' in result assert u'Error parsing the configuration options: custom_option must be a list' in result[ 'config'][0]
def test_dataset_search_is_cached(self): app = self._get_app(users_can_create_groups=False) sysadmin = factories.Sysadmin() tests.call_action_api(app, 'group_create', name='test-group', apikey=sysadmin['apikey']) pass
def test_create(self): source_dict = self.default_source_dict result = tests.call_action_api(self.app, 'harvest_source_create', apikey=self.sysadmin['apikey'], **source_dict) for key in source_dict.keys(): assert source_dict[key] == result[key] # Check that source was actually created source = harvest_model.HarvestSource.get(result['id']) assert source.url == source_dict['url'] assert source.type == source_dict['source_type'] # Trying to create a source with the same URL fails source_dict = copy.deepcopy(self.default_source_dict) source_dict['name'] = 'test-source-action-new' result = tests.call_action_api(self.app, 'harvest_source_create', apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'url' in result assert u'There already is a Harvest Source for this URL' in result['url'][0]
def _make_curators_group(self): '''This is a helper method for test methods to call when they want the 'curators' group to be created. ''' # Create a user who will *not* be a member of the curators group. noncurator = tests.call_action_api(self.app, 'user_create', apikey=self.sysadmin.apikey, name='noncurator', email='email', password='******') # Create a user who will be a member of the curators group. curator = tests.call_action_api(self.app, 'user_create', apikey=self.sysadmin.apikey, name='curator', email='email', password='******') # Create the curators group, with the 'curator' user as a member. users = [{'name': curator['name'], 'capacity': 'member'}] curators_group = tests.call_action_api(self.app, 'group_create', apikey=self.sysadmin.apikey, name='curators', users=users) return (noncurator, curator, curators_group)
def setup(self): '''Nose runs this method before each test method in our test class.''' self.sysadmin = factories.Sysadmin() self.org_dict = tests.call_action_api( self.app, 'organization_create', apikey=self.sysadmin.get('apikey'), name='my_org_000') self.package_dict = tests.call_action_api( self.app, 'package_create', apikey=self.sysadmin.get('apikey'), name='my_package_000', title='my package', notes='my package notes', tag_string='my_package', modified='2014-04-04', publisher='GSA', contact_name='john doe', contact_email='*****@*****.**', unique_id='000', public_access_level='public', bureau_code='001:40', program_code='015:010', access_level_comment='Access level commemnt', parent_dataset='true', ower_org=self.org_dict['id'])
def test_create(self): source_dict = self.default_source_dict result = tests.call_action_api(self.app, 'harvest_source_create', apikey=self.sysadmin['apikey'], **source_dict) for key in source_dict.keys(): assert source_dict[key] == result[key] # Check that source was actually created source = harvest_model.HarvestSource.get(result['id']) assert source.url == source_dict['url'] assert source.type == source_dict['source_type'] # Trying to create a source with the same URL fails source_dict = copy.deepcopy(self.default_source_dict) source_dict['name'] = 'test-source-action-new' result = tests.call_action_api(self.app, 'harvest_source_create', apikey=self.sysadmin['apikey'], status=409, **source_dict) assert 'url' in result assert u'There already is a Harvest Source for this URL' in result[ 'url'][0]
def test_resource_create(self): package_dict = tests.call_action_api(self.app, 'package_create', apikey=self.sysadmin.apikey, name='my_package', title='my package', notes='my package notes', tag_string='my_package', modified='2014-04-04', publisher='GSA', publisher_1='OCSIT', contact_name='john doe', contact_email='*****@*****.**', unique_id='001', public_access_level='public', bureau_code='001:40', program_code='015:010', access_level_comment='Access level commemnt', license_id='http://creativecommons.org/publicdomain/zero/1.0/', license_new='http://creativecommons.org/publicdomain/zero/1.0/', spatial='Lincoln, Nebraska', temporal='2000-01-15T00:45:00Z/2010-01-15T00:06:00Z', category=["vegetables","produce"], data_dictionary='www.google.com', data_dictionary_type='tex/csv', data_quality='true', publishing_status='open', accrual_periodicity='annual', conforms_to='www.google.com', homepage_url='www.google.com', language='us-EN', primary_it_investment_uii='021-123456789', related_documents='www.google.com', release_date='2014-01-02', system_of_records='www.google.com', is_parent='true', accessURL='www.google.com', webService='www.gooogle.com', format='text/csv', formatReadable='text/csv', resources=[ { 'name':'my_resource', 'url':'www.google.com', 'description':'description'}, { 'name':'my_resource_1', 'url':'www.google.com', 'description':'description_2'}, ] ) assert package_dict['name'] == 'my_package' assert package_dict['resources'][0]['name'] == 'my_resource' resource_dict = tests.call_action_api(self.app, 'resource_create', apikey=self.sysadmin.apikey, package_id = package_dict['id'], name='my_resource_2', url='www.google.com', description='description_3' ) assert resource_dict['name'] == 'my_resource_2'
def test_sysadmin_can_create_group_when_config_is_True(self): app = self._get_app(users_can_create_groups=True) sysadmin = factories.Sysadmin() tests.call_action_api(app, 'group_create', name='test-group', apikey=sysadmin['apikey'])
def test_hdx_solr_additions(self): testsysadmin = model.User.by_name('testsysadmin') tests.call_action_api(self.app, 'group_create', name="col",title="Colombia",apikey=testsysadmin.apikey, status=200) p = tests.call_action_api(self.app, 'package_create', package_creator="test function", name="test_activity_12",dataset_source= "World Bank",notes="This is a test activity",title= "Test Activity 1",indicator= 1,groups= [{"name": "col"}],apikey=testsysadmin.apikey, status=200) context = {'ignore_auth': True, 'model': model, 'session': model.Session, 'user': '******'} s = self._get_action('package_show')(context, {"id":p["id"]}) assert json.loads(s['solr_additions'])['countries'] == ['Colombia']
def test_create_public_package_with_no_organization(self): '''Test that public packages with no organization can be created.''' testsysadmin = model.User.by_name('testsysadmin') tests.call_action_api(self.app, 'package_create', name='test', private=False, apikey=testsysadmin.apikey)
def test_make_observable_by_normal_user(self): # should it really be 409? resource = model.Package.get('annakarenina').resources[0] tests.call_action_api(self.app, 'datastore_make_observable', resource_id=resource.id, apikey=self.normal_user.apikey, status=409)
def test_user_can_create_group_when_config_is_True(self): app = self._get_app(users_can_create_groups=True) user = factories.User() tests.call_action_api(app, 'group_create', name='test-group', apikey=user['apikey'])
def test_00_send_email_notifications_not_authorized(self): '''Unauthorized users shouldn't be able to send email notifications. ''' tests.call_action_api(self.app, 'send_email_notifications', apikey=self.annafan['apikey'], status=403)
def test_auth_user_cannot_create_article(self): user = factories.User() article = _make_article(apikey=user['apikey']) # app = helpers._get_test_app() app = paste.fixture.TestApp(pylons.test.pylonsapp) ckan.plugins.load(*plugins_to_load) tests.call_action_api(app, 'package_create', status=403, **article)
def test_delete_country(self): testsysadmin = model.User.by_name('testsysadmin') user = model.User.by_name('tester') create_result = tests.call_action_api(self.app, 'group_create', name='test_group_c', title='Test Group C', apikey=testsysadmin.apikey, status=200) tests.call_action_api(self.app, 'group_delete', id=create_result['id'], apikey=user.apikey, status=403) assert True, 'user should not be allowed to delete the group'
def test_create_country_member(self): testsysadmin = model.User.by_name('testsysadmin') create_result = tests.call_action_api(self.app, 'group_create', name='test_group_d', title='Test Group D', apikey=testsysadmin.apikey, status=200) tests.call_action_api(self.app, 'group_member_create', id=create_result['id'], username='******', role='editor', apikey=testsysadmin.apikey, status=403) assert True, 'Country members shouldn\'t be allowed'
def test_delete_org(self): testsysadmin = model.User.by_name('testsysadmin') user = model.User.by_name('tester') create_result = tests.call_action_api(self.app, 'organization_create', name='test_org_c', title='Test Org C', apikey=testsysadmin.apikey, status=200) tests.call_action_api(self.app, 'organization_delete', id=create_result['id'], apikey=user.apikey, status=403) assert True, 'user should not be allowed to delete the org'
def test_broadcast_event_by_normal_user(self): # should it really be 409? resource = model.Package.get('annakarenina').resources[0] tests.call_action_api(self.app, 'realtime_broadcast_event', resource_id=resource.id, event_type='datastore_update', apikey=self.normal_user.apikey, status=409)
def test_ytp_tasks_add(self): """ Test adding organization source via API. """ self.assert_raises(Exception, tests.call_action_api, self.app, 'ytp_tasks_add') self.assert_raises(KeyError, tests.call_action_api, self.app, 'ytp_tasks_add', apikey=self.sysadmin.apikey) data = {'id': 'test1', 'task': 'test-task', 'data': 'test-data', 'frequency': "HOURLY"} tests.call_action_api(self.app, 'ytp_tasks_add', apikey=self.sysadmin.apikey, **data) self.assert_raises(Exception, tests.call_action_api, self.app, 'ytp_tasks_add', **data)
def setup_class(self): print ("") # get config options config = ConfigParser.RawConfigParser({ 'ckan_web_map_service_url': '', }) config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests_config.cfg')) self.serviceUrl = config.get('tests', 'ckan_web_map_service_url') if not self.serviceUrl: raise Exception('You must provide WebMapServer URL to the tests ' ' configuration file') self.actions = ngdsClientAction # Make the Paste TestApp that we'll use to simulate HTTP requests to CKAN. self.app = paste.fixture.TestApp(pylons.test.pylonsapp) # Access CKAN's model directly (bad) to create a sysadmin user and save # it against self for all test methods to access. self.sysadmin_user = model.User(name='test_sysadmin', sysadmin=True) model.Session.add(self.sysadmin_user) model.Session.commit() model.Session.remove() #Create organization organization = {'name': 'test_org', 'title': 'Maroc', 'description': 'Roger likes these books.'} resultOrg = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin_user.apikey, **organization) self.orgID = resultOrg['id'] #Create Dataset and tied it to created org dataset = {'name': 'test_org_dataset', 'title': 'A Novel By Tolstoy', 'owner_org': organization['name']} resultDataset = tests.call_action_api(self.app, 'package_create', apikey=self.sysadmin_user.apikey, **dataset) self.datasetID = resultDataset['id'] #Create Resource and tied it to created dataset resource = {'package_id': resultDataset['id'], 'url': self.serviceUrl} resultResource = tests.call_action_api(self.app, 'resource_create', apikey=self.sysadmin_user.apikey, **resource) #save resource id self.resourceID = resultResource['id']
def test_create_org_member(self): testsysadmin = model.User.by_name('testsysadmin') user = model.User.by_name('tester') create_result = tests.call_action_api(self.app, 'organization_create', name='test_org_d', title='Test Org D', apikey=testsysadmin.apikey, status=200) tests.call_action_api(self.app, 'organization_member_create', id=create_result['id'], username='******', role='editor', apikey=user.apikey, status=403) assert True, 'user shoudn\'t be allowed to add himself as a member'
def test_04_no_repeat_email_notifications(self): '''Test that a user does not get a second email notification for the same new activity. ''' # TODO: Assert that Sara has some new activities and has already had # an email about them. tests.call_action_api(self.app, 'send_email_notifications', apikey=self.testsysadmin['apikey']) assert len(self.get_smtp_messages()) == 0
def test_group_create_with_no_curators_group(self): '''Test that group_create doesn't crash when there's no curators group. ''' # Make sure there's no curators group. assert 'curators' not in tests.call_action_api(self.app, 'group_list') # Make our sysadmin user create a group. CKAN should not crash. tests.call_action_api(self.app, 'group_create', name='test-group', apikey=self.sysadmin.apikey)
def test_datapusher_hook(self): package = model.Package.get('annakarenina') resource = package.resources[0] context = {'user': self.sysadmin_user.name} p.toolkit.get_action('task_status_update')( context, { 'entity_id': resource.id, 'entity_type': 'resource', 'task_type': 'datapusher', 'key': 'job_id', 'value': 'my_id', 'last_updated': str(datetime.datetime.now()), 'state': 'pending' }) p.toolkit.get_action('task_status_update')( context, { 'entity_id': resource.id, 'entity_type': 'resource', 'task_type': 'datapusher', 'key': 'job_key', 'value': 'my_key', 'last_updated': str(datetime.datetime.now()), 'state': 'pending' }) data = {'status': 'success', 'metadata': {'resource_id': resource.id}} postparams = '%s=1' % json.dumps(data) auth = {'Authorization': str(self.sysadmin_user.apikey)} res = self.app.post('/api/action/datapusher_hook', params=postparams, extra_environ=auth, status=200) print res.body res_dict = json.loads(res.body) assert res_dict['success'] is True task = tests.call_action_api(self.app, 'task_status_show', entity_id=resource.id, task_type='datapusher', key='job_id') assert task['state'] == 'success', task task = tests.call_action_api(self.app, 'task_status_show', entity_id=resource.id, task_type='datapusher', key='job_key') assert task['state'] == 'success', task
def _create_datasets(self): sysadmin = factories.User() self.ed_dataset1 = tests.call_action_api(self.app, 'package_create', apikey=sysadmin['apikey'], name='ed_dataset1', extras=[{ 'key': 'Domain', 'value': 'Education' }, { 'key': 'Subdomain', 'value': 'TestSubtopic1' }]) self.ed_dataset2 = tests.call_action_api(self.app, 'package_create', apikey=sysadmin['apikey'], name='ed_dataset2', extras=[{ 'key': 'Domain', 'value': 'Education' }, { 'key': 'Subdomain', 'value': 'TestSubtopic1' }]) self.ed_dataset3 = tests.call_action_api(self.app, 'package_create', apikey=sysadmin['apikey'], name='ed_dataset3', extras=[{ 'key': 'Domain', 'value': 'Education' }, { 'key': 'Subdomain', 'value': 'TestSubtopic2' }]) self.ed_dataset4 = tests.call_action_api(self.app, 'package_create', apikey=sysadmin['apikey'], name='ed_dataset4', extras=[{ 'key': 'Domain', 'value': 'Health' }, { 'key': 'Subdomain', 'value': 'TestSubtopic3' }])
def test_make_package_with_no_organization_private(self): '''Test that private packages with no organization cannot be created by package_update. ''' testsysadmin = model.User.by_name('testsysadmin') package = tests.call_action_api(self.app, 'package_create', name='test_2', private=False, apikey=testsysadmin.apikey) package['private'] = True result = tests.call_action_api(self.app, 'package_update', apikey=testsysadmin.apikey, status=409, **package) assert result == {'__type': 'Validation Error', 'private': ["Datasets with no organization can't be private."]}
def test_create_unique_organizations(self): """ Test duplicate title name """ tests.call_action_api(self.app, 'organization_create', name='test-name-1', title="test-title", apikey=self.sysadmin.apikey) tests.call_action_api(self.app, 'organization_create', status=409, name='test-name-2', title="test-title", apikey=self.sysadmin.apikey)
def test_group_create_with_no_curators_group(self): '''Test that group_create doesn't crash when there's no curators group. ''' sysadmin = factories.Sysadmin() # Make sure there's no curators group. assert 'curators' not in tests.call_action_api(self.app, 'group_list') # Make our sysadmin user create a group. CKAN should not crash. tests.call_action_api(self.app, 'group_create', name='test-group', apikey=sysadmin['apikey'])
def test_package_update(self): package_dict = tests.call_action_api( self.app, 'package_create', apikey=self.sysadmin.get('apikey'), name='my_package', title='my package', notes='my package notes', tag_string='my_package', modified='2014-04-04', publisher='GSA', contact_name='john doe', contact_email='*****@*****.**', unique_id='001', public_access_level='public', bureau_code='001:40', program_code='015:010', access_level_comment='Access level commemnt') assert package_dict['name'] == 'my_package' package_dict_update = tests.call_action_api( self.app, 'package_update', apikey=self.sysadmin.get('apikey'), name='my_package', title='my package update', notes='my package notes update', tag_string='my_package', modified='2014-04-05', publisher='GSA', contact_name='john doe jr', contact_email='*****@*****.**', unique_id='002', public_access_level='public', bureau_code='001:41', program_code='015:011', access_level_comment='Access level commemnt update') assert package_dict_update['title'] == 'my package update' assert package_dict_update['extras'][0][ 'value'] == 'Access level commemnt update' assert package_dict_update['extras'][1]['value'] == '001:41' assert package_dict_update['extras'][2]['value'] == '*****@*****.**' assert package_dict_update['extras'][3]['value'] == 'john doe jr' assert package_dict_update['extras'][4]['value'] == '2014-04-05' assert package_dict_update['extras'][5]['value'] == '015:011' assert package_dict_update['extras'][6]['value'] == 'public' assert package_dict_update['extras'][7]['value'] == 'GSA' assert package_dict_update['extras'][8]['value'] == '002'
def test_package_parent_dataset(self): org_dict = tests.call_action_api(self.app, 'organization_create', apikey=self.sysadmin.get('apikey'), name='my_org') package_dict = tests.call_action_api( self.app, 'package_create', apikey=self.sysadmin.get('apikey'), name='my_package', title='my package', notes='my package notes', tag_string='my_package', modified='2014-04-04', publisher='GSA', contact_name='john doe', contact_email='*****@*****.**', unique_id='001', public_access_level='public', bureau_code='001:40', program_code='015:010', access_level_comment='Access level commemnt', parent_dataset='true', ower_org=org_dict['id']) assert package_dict['name'] == 'my_package' title = db_utils.get_organization_title(package_dict['id']) assert title == 'my package' class Config: def __init__(self, **kwds): self.__dict__.update(kwds) class Userobj: def __init__(self, **kwds): self.__dict__.update(kwds) def get_group_ids(self): return [org_dict['id']] config = Config(userobj=Userobj(sysadmin=True)) items = db_utils.get_parent_organizations(config) assert org_dict['id'] not in items config = Config(userobj=Userobj(sysadmin=False)) items = db_utils.get_parent_organizations(config) assert org_dict['id'] not in items
def test_package_with_one_view(self): app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) self._create_resource(app, package, apikey) url = routes.url_for(controller='package', action='read', id=package['name']) self._post_to_tracking(app, url) self._update_tracking_summary() package = tests.call_action_api(app, 'package_show', id=package['id']) tracking_summary = package['tracking_summary'] assert tracking_summary['recent'] == 1, ("A package that has been " "viewed once should have 1 " "recent view.") assert tracking_summary['total'] == 1, ("A package that has been " "viewed once should have 1 " "total view") assert len(package['resources']) == 1 resource = package['resources'][0] tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 0, ("Viewing a package should " "not increase the recent " "views of the package's " "resources") assert tracking_summary['total'] == 0, ("Viewing a package should " "not increase the total views " "of the package's resources")
def test_package_with_many_views(self): app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) self._create_resource(app, package, apikey) url = routes.url_for(controller='package', action='read', id=package['name']) # View the package three times from different IPs. self._post_to_tracking(app, url, ip='111.222.333.44') self._post_to_tracking(app, url, ip='111.222.333.55') self._post_to_tracking(app, url, ip='111.222.333.66') self._update_tracking_summary() package = tests.call_action_api(app, 'package_show', id=package['id']) tracking_summary = package['tracking_summary'] assert tracking_summary['recent'] == 3, ( "A package that has been viewed 3 times recently should have 3 " "recent views") assert tracking_summary['total'] == 3, ( "A package that has been viewed 3 times should have 3 total views") assert len(package['resources']) == 1 resource = package['resources'][0] tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 0, ( "Viewing a package should not increase the recent views of the " "package's resources") assert tracking_summary['total'] == 0, ( "Viewing a package should not increase the total views of the " "package's resources")
def test_resource_with_many_downloads(self): app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) resource = self._create_resource(app, package, apikey) url = resource['url'] # Download the resource three times from different IPs. self._post_to_tracking(app, url, type_='resource', ip='111.222.333.44') self._post_to_tracking(app, url, type_='resource', ip='111.222.333.55') self._post_to_tracking(app, url, type_='resource', ip='111.222.333.66') self._update_tracking_summary() package = tests.call_action_api(app, 'package_show', id=package['id']) assert len(package['resources']) == 1 resource = package['resources'][0] tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 3, ( "A resource that has been downloaded 3 times recently should have " "3 recent downloads") assert tracking_summary['total'] == 3, ( "A resource that has been downloaded 3 times should have 3 total " "downloads") tracking_summary = package['tracking_summary'] assert tracking_summary['recent'] == 0, ( "Downloading a resource should not increase the resource's " "package's recent views") assert tracking_summary['total'] == 0, ( "Downloading a resource should not increase the resource's " "package's total views")
def _create_package(self, app, apikey, name='look_to_windward'): '''Create a package via the action api.''' return tests.call_action_api(app, 'package_create', apikey=apikey, name=name)
def test_dataset_view_count_throttling(self): '''If the same user visits the same dataset multiple times on the same day, only one view should get counted. ''' app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) self._create_resource(app, package, apikey) url = routes.url_for(controller='package', action='read', id=package['name']) # Visit the dataset three times from the same IP. self._post_to_tracking(app, url) self._post_to_tracking(app, url) self._post_to_tracking(app, url) self._update_tracking_summary() package = tests.call_action_api(app, 'package_show', id=package['id']) tracking_summary = package['tracking_summary'] assert tracking_summary['recent'] == 1, ("Repeat dataset views should " "not add to recent views " "count") assert tracking_summary['total'] == 1, ("Repeat dataset views should " "not add to total views count")
def test_resource_download_count_throttling(self): '''If the same user downloads the same resource multiple times on the same day, only one view should get counted. ''' app = self._get_app() sysadmin_user, apikey = self._create_sysadmin(app) package = self._create_package(app, apikey) resource = self._create_resource(app, package, apikey) # Download the resource three times from the same IP. self._post_to_tracking(app, resource['url'], type_='resource') self._post_to_tracking(app, resource['url'], type_='resource') self._post_to_tracking(app, resource['url'], type_='resource') self._update_tracking_summary() resource = tests.call_action_api(app, 'resource_show', id=resource['id']) tracking_summary = resource['tracking_summary'] assert tracking_summary['recent'] == 1, ( "Repeat resource downloads should not add to recent views count") assert tracking_summary['total'] == 1, ( "Repeat resource downloads should not add to total views count")