def test_update_invalid_duplicate_name(self): metadata_collection1 = ckanext_factories.MetadataCollection() metadata_collection2 = ckanext_factories.MetadataCollection() input_dict = { 'id': metadata_collection1['id'], 'name': metadata_collection2['name'], } result, obj = self.test_action('metadata_collection_update', should_error=True, **input_dict) assert_error(result, 'name', 'Group name already exists in database')
def test_update_invalid_hierarchy_not_allowed(self): metadata_collection1 = ckanext_factories.MetadataCollection() metadata_collection2 = ckanext_factories.MetadataCollection() input_dict = { 'id': metadata_collection1['id'], 'groups': [{ 'name': metadata_collection2['name'] }], } result, obj = self.test_action('metadata_collection_update', should_error=True, **input_dict) assert_error(result, '__junk', 'The input field .*groups.* was not expected.')
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 _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_member_delete_invalid(self): infrastructure = self._generate_infrastructure() metadata_collection = ckanext_factories.MetadataCollection(infrastructures=[{'id': infrastructure['id']}]) result, obj = self.test_action('member_delete', should_error=True, check_auth=True, exception_class=tk.NotAuthorized, id=infrastructure['id'], object=metadata_collection['id'], object_type='group') assert_error(result, None, "This action may not be used to alter a metadata collection's membership of infrastructures.")
def test_group_delete_invalid(self): metadata_collection = ckanext_factories.MetadataCollection() result, obj = self.test_action('group_delete', should_error=True, check_auth=True, exception_class=tk.NotAuthorized, id=metadata_collection['id']) assert_error( result, None, "This action may not be used for metadata_collection type objects." )
def test_update_invalid_cannot_change_organization(self): metadata_collection = ckanext_factories.MetadataCollection() organization = self._generate_organization() input_dict = { 'id': metadata_collection['id'], 'organization_id': organization['id'], } result, obj = self.test_action('metadata_collection_update', should_error=True, **input_dict) assert_error(result, 'organization_id', 'Organization cannot be changed')
def test_update_invalid_deleted_references(self): metadata_collection = ckanext_factories.MetadataCollection() infrastructure = self._generate_infrastructure() call_action('infrastructure_delete', id=infrastructure['id']) result, obj = self.test_action('metadata_collection_update', should_error=True, id=metadata_collection['id'], infrastructures=[{ 'id': infrastructure['id'] }]) assert_error(result, 'infrastructures/0/id', 'Not found: Infrastructure')
def test_update_valid(self): infrastructure1 = self._generate_infrastructure() infrastructure2 = self._generate_infrastructure() metadata_collection = ckanext_factories.MetadataCollection( infrastructures=[{ 'id': infrastructure1['id'] }, { 'id': infrastructure2['id'] }]) new_infrastructure = self._generate_infrastructure() input_dict = { 'id': metadata_collection['id'], 'name': 'updated-test-metadata-collection', 'title': 'Updated Test Metadata Collection', 'description': 'Updated test metadata collection', 'organization_id': metadata_collection['organization_id'], 'doi_collection': 'updated.test.doi', 'infrastructures': [ { 'id': infrastructure2['name'] }, { 'id': new_infrastructure['name'] }, ], } result, obj = self.test_action('metadata_collection_update', **input_dict) assert obj.type == 'metadata_collection' assert obj.is_organization == False del input_dict['organization_id'] del input_dict['doi_collection'] del input_dict['infrastructures'] assert_object_matches_dict(obj, input_dict) assert_group_has_extra(obj.id, 'organization_id', metadata_collection['organization_id']) assert_group_has_extra(obj.id, 'doi_collection', 'UPDATED.TEST.DOI') assert_group_has_member(infrastructure1['id'], obj.id, 'group', state='deleted') assert_group_has_member(infrastructure2['id'], obj.id, 'group') assert_group_has_member(new_infrastructure['id'], obj.id, 'group')
def test_delete_with_dependencies(self): infrastructure = self._generate_infrastructure() metadata_schema = ckanext_factories.MetadataSchema(infrastructure_id=infrastructure['id']) metadata_collection = ckanext_factories.MetadataCollection(infrastructures=[{'id': infrastructure['id']}]) result, obj = self.test_action('infrastructure_delete', should_error=True, id=infrastructure['id']) assert_error(result, 'message', 'Project has dependent metadata collections') assert ckanext_model.MetadataSchema.get(metadata_schema['id']).state == 'active' call_action('metadata_collection_delete', id=metadata_collection['id']) self.test_action('infrastructure_delete', id=infrastructure['id']) assert ckanext_model.MetadataSchema.get(metadata_schema['id']).state == 'deleted'
def test_member_create_invalid(self): metadata_record = ckanext_factories.MetadataRecord() metadata_collection = ckanext_factories.MetadataCollection() result, obj = self.test_action('member_create', should_error=True, check_auth=True, exception_class=tk.NotAuthorized, id=metadata_collection['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_update_valid_partial(self): metadata_collection = ckanext_factories.MetadataCollection() input_dict = { 'id': metadata_collection['id'], 'title': 'Updated Test Metadata Collection', 'organization_id': metadata_collection['organization_id'], 'doi_collection': 'updated.test.doi', } result, obj = self.test_action('metadata_collection_update', **input_dict) assert obj.type == 'metadata_collection' assert obj.is_organization == False assert obj.title == input_dict['title'] assert obj.name == metadata_collection['name'] assert obj.description == metadata_collection['description'] assert_group_has_extra(obj.id, 'organization_id', metadata_collection['organization_id']) assert_group_has_extra(obj.id, 'doi_collection', 'UPDATED.TEST.DOI')
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
def test_delete_valid(self): metadata_collection = ckanext_factories.MetadataCollection() self.test_action('metadata_collection_delete', id=metadata_collection['id'])
def test_create_invalid_duplicate_name(self): metadata_collection = ckanext_factories.MetadataCollection() result, obj = self.test_action('metadata_collection_create', should_error=True, name=metadata_collection['name']) assert_error(result, 'name', 'Group name already exists in database')
def _generate_metadata_collection(self, **kwargs): return ckanext_factories.MetadataCollection(user=self.normal_user, **kwargs)