def _bulk_action_setup(self): metadata_schema = ckanext_factories.MetadataSchema() self.workflow_transition_1 = ckanext_factories.WorkflowTransition( from_state_id='') self.workflow_transition_2 = ckanext_factories.WorkflowTransition( from_state_id=self.workflow_transition_1['to_state_id']) self.metadata_collection = ckanext_factories.MetadataCollection() self.metadata_records = [ ckanext_factories.MetadataRecord( owner_org=self.metadata_collection['organization_id'], metadata_collection_id=self.metadata_collection['id'], metadata_standard_id=metadata_schema['metadata_standard_id']) for _ in range(3) ] # modify the middle record; this should not affect processing of the surrounding records self.test_action('metadata_record_validate', id=self.metadata_records[1]['id']) self.test_action( 'metadata_record_workflow_state_override', id=self.metadata_records[1]['id'], workflow_state_id=self.workflow_transition_1['to_state_id']) assert_package_has_extra(self.metadata_records[1]['id'], 'validated', True) assert_package_has_extra(self.metadata_records[1]['id'], 'workflow_state_id', self.workflow_transition_1['to_state_id'])
def test_update_metadata_records_private_cascade_2(self): """ Test that the visibility of a metadata record is determined by that of its workflow state. """ metadata_record = ckanext_factories.MetadataRecord() workflow_state = ckanext_factories.WorkflowState( metadata_records_private=True) call_action('metadata_record_workflow_state_override', context={'user': self.normal_user['name']}, id=metadata_record['id'], workflow_state_id=workflow_state['id']) assert_package_has_extra(metadata_record['id'], 'workflow_state_id', workflow_state['id']) assert_package_has_attribute(metadata_record['id'], 'private', True) input_dict = { 'id': workflow_state['id'], 'revert_state_id': '', 'metadata_records_private': False, 'workflow_rules_json': '{}', } result, obj = self.test_action('workflow_state_update', **input_dict) assert_object_matches_dict(obj, input_dict) assert_package_has_extra(metadata_record['id'], 'workflow_state_id', workflow_state['id']) assert_package_has_attribute(metadata_record['id'], 'private', False)
def _generate_and_validate_metadata_record_using_schema( self, metadata_schema): """ Generate a metadata record with the same standard and organization/infrastructure of the given schema, and validate it using this schema. :return: metadata record dict """ metadata_collection = ckanext_factories.MetadataCollection( organization_id=metadata_schema['organization_id'] if metadata_schema['organization_id'] else ckan_factories.Organization()['id'], infrastructures=[{ 'id': metadata_schema['infrastructure_id'] }] if metadata_schema['infrastructure_id'] else []) metadata_record = ckanext_factories.MetadataRecord( metadata_standard_id=metadata_schema['metadata_standard_id'], owner_org=metadata_collection['organization_id'], metadata_collection_id=metadata_collection['id'], ) assert_metadata_record_has_validation_schemas(metadata_record['id'], metadata_schema['name']) self._validate_metadata_record(metadata_record) self.assert_validate_activity_logged(metadata_record['id'], metadata_schema) return metadata_record
def test_delete_with_dependencies(self): organization = self._generate_organization() metadata_collection = self._generate_metadata_collection( organization_id=organization['id']) metadata_schema = ckanext_factories.MetadataSchema( organization_id=organization['id']) metadata_record = ckanext_factories.MetadataRecord( owner_org=organization['id'], metadata_collection_id=metadata_collection['id']) result, obj = self.test_action('organization_delete', should_error=True, id=organization['id']) assert_error(result, 'message', 'Organization has dependent metadata records') assert ckan_model.Group.get( metadata_collection['id']).state == 'active' assert ckanext_model.MetadataSchema.get( metadata_schema['id']).state == 'active' call_action('metadata_record_delete', id=metadata_record['id']) self.test_action('organization_delete', id=organization['id']) assert ckan_model.Group.get( metadata_collection['id']).state == 'deleted' assert ckanext_model.MetadataSchema.get( metadata_schema['id']).state == 'deleted'
def test_member_create_invalid(self): metadata_record = ckanext_factories.MetadataRecord() infrastructure = self._generate_infrastructure() result, obj = self.test_action('member_create', should_error=True, check_auth=True, exception_class=tk.NotAuthorized, id=infrastructure['id'], object=metadata_record['id'], object_type='package', capacity='public') assert_error(result, None, "This action may not be used to alter a metadata record's membership of metadata collections or infrastructures.")
def test_delete_with_dependencies(self): metadata_collection = ckanext_factories.MetadataCollection() metadata_record = ckanext_factories.MetadataRecord( owner_org=metadata_collection['organization_id'], metadata_collection_id=metadata_collection['id']) result, obj = self.test_action('metadata_collection_delete', should_error=True, id=metadata_collection['id']) assert_error(result, 'message', 'Metadata collection has dependent metadata records') call_action('metadata_record_delete', id=metadata_record['id']) self.test_action('metadata_collection_delete', id=metadata_collection['id'])
def test_member_delete_invalid(self): metadata_collection = ckanext_factories.MetadataCollection() metadata_record = ckanext_factories.MetadataRecord( owner_org=metadata_collection['organization_id'], metadata_collection_id=metadata_collection['id']) result, obj = self.test_action('member_delete', should_error=True, check_auth=True, exception_class=tk.NotAuthorized, id=metadata_collection['id'], object=metadata_record['id'], object_type='package') assert_error( result, None, "This action may not be used to alter a metadata record's membership of metadata collections or infrastructures." )
def test_delete_with_dependencies(self): metadata_record = ckanext_factories.MetadataRecord() workflow_state = ckanext_factories.WorkflowState() call_action('metadata_record_workflow_state_override', context={'user': self.normal_user['name']}, id=metadata_record['id'], workflow_state_id=workflow_state['id']) assert_package_has_extra(metadata_record['id'], 'workflow_state_id', workflow_state['id']) result, obj = self.test_action('workflow_state_delete', should_error=True, id=workflow_state['id']) assert_error(result, 'message', 'Workflow state has dependent metadata records') call_action('metadata_record_delete', id=metadata_record['id']) self.test_action('workflow_state_delete', id=workflow_state['id'])
def test_delete_with_dependencies(self): metadata_standard = ckanext_factories.MetadataStandard() metadata_schema = ckanext_factories.MetadataSchema( metadata_standard_id=metadata_standard['id']) metadata_record = ckanext_factories.MetadataRecord( metadata_standard_id=metadata_standard['id']) result, obj = self.test_action('metadata_standard_delete', should_error=True, id=metadata_standard['id']) assert_error(result, 'message', 'Metadata standard has dependent metadata records') assert ckanext_model.MetadataSchema.get( metadata_schema['id']).state == 'active' call_action('metadata_record_delete', id=metadata_record['id']) self.test_action('metadata_standard_delete', id=metadata_standard['id']) assert ckanext_model.MetadataSchema.get( metadata_schema['id']).state == 'deleted'
def _generate_and_validate_metadata_record( self, metadata_standard_id=None, add_infrastructure_to_collection=False, add_organization_to_schema=False, add_infrastructure_to_schema=False): """ Generate a metadata record and a metadata schema, and validate the record using the schema. :param metadata_standard_id: specify the metadata standard to use :param add_infrastructure_to_collection: assign an infrastructure to the record's metadata collection :param add_organization_to_schema: associate the record's organization with the schema :param add_infrastructure_to_schema: associate the record's infrastructure with the schema :return: tuple of new record, new collection, new schema """ organization = ckan_factories.Organization() metadata_collection = ckanext_factories.MetadataCollection( organization_id=organization['id'], infrastructures=[{ 'id': ckanext_factories.Infrastructure()['id'] }] if add_infrastructure_to_collection else []) metadata_record = ckanext_factories.MetadataRecord( metadata_standard_id=metadata_standard_id, metadata_collection_id=metadata_collection['id'], owner_org=organization['id'], ) metadata_schema = ckanext_factories.MetadataSchema( metadata_standard_id=metadata_record['metadata_standard_id'], organization_id=organization['id'] if add_organization_to_schema else '', infrastructure_id=metadata_collection['infrastructures'][0]['id'] if add_infrastructure_to_schema else '') assert_metadata_record_has_validation_schemas(metadata_record['id'], metadata_schema['name']) self._validate_metadata_record(metadata_record) self.assert_validate_activity_logged(metadata_record['id'], metadata_schema) return metadata_record, metadata_collection, metadata_schema