def _test_delete_optional_file(self, file_extension):
        # test that deleting the optional file with the specified extension *file_extension*
        # deletes only that file

        self.resGeoFeature = hydroshare.create_resource(
            resource_type='GeographicFeatureResource',
            owner=self.user,
            title='Test Geographic Feature (shapefiles)'
        )
        # check that the resource has no files
        self.assertEqual(self.resGeoFeature.files.count(), 0)

        # add files first
        files = []
        target = 'hs_geographic_feature_resource/tests/gis.osm_adminareas_v06_all_files.zip'
        files.append(UploadedFile(file=open(target, 'r'),
                                  name='gis.osm_adminareas_v06_all_files.zip'))
        hydroshare.utils.resource_file_add_process(self.resGeoFeature, files, self.user, )

        # check that the resource has 15 files
        self.assertEqual(self.resGeoFeature.files.count(), 15)

        # find the .shp file and delete it
        for f in self.resGeoFeature.files.all():
            if f.extension == file_extension:
                hydroshare.delete_resource_file(self.resGeoFeature.short_id, f.id, self.user)
                break
        # resource should have 14 files
        self.assertEqual(self.resGeoFeature.files.count(), 14)
        self.resGeoFeature.delete()
Example #2
0
    def delete(self, request, pk, filename):
        resource, _, user = view_utils.authorize(request, pk, edit=True, full=True)
        try:
            hydroshare.delete_resource_file(pk, filename, user)
        except ObjectDoesNotExist as ex:  # matching file not found
            raise NotFound(detail=ex.message)

        # prepare response data
        response_data = {"resource_id": pk, "file_name": filename}
        return Response(data=response_data, status=status.HTTP_200_OK)
    def test_metadata_on_content_file_delete(self):
        # test that some of the metadata is not deleted on content file deletion
        files = [UploadedFile(file=self.raster_tif_file_obj, name=self.raster_tif_file_name)]
        utils.resource_file_add_pre_process(resource=self.resRaster, files=files, user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resRaster, files=files, user=self.user,
                                        extract_metadata=True)
        # there should be 2 content files
        self.assertEqual(self.resRaster.files.all().count(), 2)

        # there should be 2 format elements
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 2)
        self.assertEqual(self.resRaster.metadata.formats.all().filter(
            value='application/vrt').count(), 1)

        self.assertEqual(self.resRaster.metadata.formats.all().filter(
            value='image/tiff').count(), 1)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resRaster.short_id, self.raster_tif_file_name,
                                        self.user)

        # there should no content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # there should be a title element
        self.assertNotEqual(self.resRaster.metadata.title, None)

        # there should be no abstract element
        self.assertEqual(self.resRaster.metadata.description, None)

        # there should be 1 creator element
        self.assertEqual(self.resRaster.metadata.creators.all().count(), 1)

        # there should be no contributor element
        self.assertEqual(self.resRaster.metadata.contributors.all().count(), 0)

        # there should be no coverage element
        self.assertEqual(self.resRaster.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 0)

        # there should be no subject element
        self.assertEqual(self.resRaster.metadata.subjects.all().count(), 0)

        # testing extended metadata elements - there should not be any resource specific metadata
        self.assertEqual(self.resRaster.metadata.originalCoverage, None)

        self.assertEqual(self.resRaster.metadata.cellInformation, None)
        self.assertEqual(self.resRaster.metadata.bandInformations.count(), 0)

        self.resRaster.delete()
    def test_delete_prj_file(self):
        # deleting .prj file should set attributes (datum, unit, and projection_name) of
        # the orginalcoverage element to 'unknown' and delete the spatial coverage at the resource
        # level
        self.assertEqual(self.resGeoFeature.files.count(), 0)

        # add files first
        files = []
        target = 'hs_geographic_feature_resource/tests/gis.osm_adminareas_v06_all_files.zip'
        files.append(UploadedFile(file=open(target, 'r'),
                                  name='gis.osm_adminareas_v06_all_files.zip'))
        hydroshare.utils.resource_file_add_process(self.resGeoFeature, files, self.user, )

        # check that the resource has 15 files
        self.assertEqual(self.resGeoFeature.files.count(), 15)
        self.assertTrue(self.resGeoFeature.metadata.coverages.filter(type='box').exists())
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum, 'WGS_1984')
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.eastlimit -
                            3.4520493) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.northlimit -
                            45.0466382) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.southlimit -
                            42.5732416) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.westlimit -
                            (-0.3263017)) < self.allowance)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'Degree')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name,
                         'GCS_WGS_1984')
        self.assertGreater(len(self.resGeoFeature.metadata.originalcoverage.projection_string), 0)

        # find the .prj file and delete it
        for f in self.resGeoFeature.files.all():
            if f.extension == '.prj':
                hydroshare.delete_resource_file(self.resGeoFeature.short_id, f.id, self.user)
                break

        # resource should have 14 files
        self.assertEqual(self.resGeoFeature.files.count(), 14)
        # resource level spatial coverage should have been deleted
        self.assertFalse(self.resGeoFeature.metadata.coverages.filter(type='box').exists())
        # test original coverage
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum, 'unknown')
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.eastlimit -
                            3.4520493) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.northlimit -
                            45.0466382) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.southlimit -
                            42.5732416) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.westlimit -
                            (-0.3263017)) < self.allowance)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name, 'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_string, 'unknown')
        self.resGeoFeature.delete()
def undo_migrate_tif_file(apps, schema_editor):
    # delete vrt file from each of the raster resources
    for res in RasterResource.objects.all():
        try:
            for res_file in res.files.all():
                file_ext = os.path.splitext(res_file.resource_file.name)[1]
                if file_ext == '.vrt':
                    file_name = os.path.basename(res_file.resource_file.name)
                    hydroshare.delete_resource_file(res.short_id, file_name, res.creator)
        except:
            pass
    def test_metadata_on_content_file_delete(self):
        files = [UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)]
        utils.resource_file_add_pre_process(resource=self.resModelProgram, files=files,
                                            user=self.user, extract_metadata=False)

        utils.resource_file_add_process(resource=self.resModelProgram, files=files,
                                        user=self.user, extract_metadata=False)

        self.resModelProgram.metadata.create_element('Description', abstract="test abstract")
        self.resModelProgram.metadata.create_element('Subject', value="test subject")
        release_date = '2016-10-24T21:05:00.315907+00:00'
        self.resModelProgram.metadata.create_element('MpMetadata',
                                                     modelVersion='5.1.011',
                                                     modelProgramLanguage='Fortran',
                                                     modelOperatingSystem='Windows',
                                                     modelReleaseDate=release_date,
                                                     modelWebsite='http://www.hydroshare.org',
                                                     modelCodeRepository='http://www.github.com',
                                                     modelReleaseNotes='releaseNote.pdf',
                                                     modelDocumentation='manual.pdf',
                                                     modelSoftware='utilities.exe',
                                                     modelEngine='sourceCode.zip')

        # there should one content file
        self.assertEquals(self.resModelProgram.files.all().count(), 1)

        # there should be one format element
        self.assertEquals(self.resModelProgram.metadata.formats.all().count(), 1)

        # the short path should just consist of the file name.
        self.assertEquals(self.resModelProgram.files.all()[0].short_path, self.file_name)

        # delete content file that we added above; note that file name is a short_path
        hydroshare.delete_resource_file(self.resModelProgram.short_id, self.file_name, self.user)

        # there should no content file
        self.assertEquals(self.resModelProgram.files.all().count(), 0)

        # test the core metadata at this point
        self.assertNotEquals(self.resModelProgram.metadata.title, None)

        # there should be an abstract element
        self.assertNotEquals(self.resModelProgram.metadata.description, None)

        # there should be one creator element
        self.assertEquals(self.resModelProgram.metadata.creators.all().count(), 1)

        # testing extended metadata elements
        self.assertNotEqual(self.resModelProgram.metadata.program, None)
    def _test_file_metadata_on_file_delete(self, ext):

        self.create_composite_resource(self.netcdf_file)
        res_file = self.composite_resource.files.first()

        # extract metadata from the tif file
        NetCDFLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id)

        # test that we have one logical file of type NetCDFLogicalFile
        self.assertEqual(NetCDFLogicalFile.objects.count(), 1)
        self.assertEqual(NetCDFFileMetaData.objects.count(), 1)

        res_file = self.composite_resource.files.first()
        logical_file = res_file.logical_file
        # there should be 2 coverage elements - one spatial and the other one temporal
        self.assertEqual(logical_file.metadata.coverages.all().count(), 2)
        self.assertNotEqual(logical_file.metadata.spatial_coverage, None)
        self.assertNotEqual(logical_file.metadata.temporal_coverage, None)

        # there should be one original coverage
        self.assertNotEqual(logical_file.metadata.originalCoverage, None)
        # testing extended metadata element: variables
        self.assertEqual(logical_file.metadata.variables.all().count(), 5)

        # there should be 4 coverage objects - 2 at the resource level
        # and the other 2 at the file type level
        self.assertEqual(Coverage.objects.count(), 4)
        self.assertEqual(OriginalCoverage.objects.count(), 1)
        self.assertEqual(Variable.objects.count(), 5)

        # delete content file specified by extension (ext parameter)
        res_file = hydroshare.utils.get_resource_files_by_extension(
            self.composite_resource, ext)[0]
        hydroshare.delete_resource_file(self.composite_resource.short_id,
                                        res_file.id,
                                        self.user)
        # test that we don't have logical file of type NetCDFLogicalFile Type
        self.assertEqual(NetCDFLogicalFile.objects.count(), 0)
        self.assertEqual(NetCDFFileMetaData.objects.count(), 0)

        # test that all metadata deleted - there should be still 2 resource level coverages
        self.assertEqual(self.composite_resource.metadata.coverages.all().count(), 2)
        self.assertEqual(Coverage.objects.count(), 2)
        self.assertEqual(OriginalCoverage.objects.count(), 0)
        self.assertEqual(Variable.objects.count(), 0)

        self.composite_resource.delete()
    def test_metadata_on_content_file_delete(self):
        # test that some of the metadata is not deleted on content file deletion
        # adding a valid netcdf file should generate some core metadata and all extended metadata
        files = [UploadedFile(file=self.netcdf_file_obj, name=self.netcdf_file_name)]
        utils.resource_file_add_pre_process(resource=self.resNetcdf, files=files, user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resNetcdf, files=files, user=self.user,
                                        extract_metadata=True)

        # there should be 1 content files
        self.assertEqual(self.resNetcdf.files.all().count(), 2)

        # there should be 1 format elements
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 2)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resNetcdf.short_id, self.netcdf_file_name, self.user)

        # there should no content file
        self.assertEqual(self.resNetcdf.files.all().count(), 0)

        # there should be a title element
        self.assertNotEquals(self.resNetcdf.metadata.title, None)

        # there should be abstract element
        self.assertNotEquals(self.resNetcdf.metadata.description, None)

        # there should be 2 creator element
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 2)

        # there should be 1 contributor element
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 1)

        # there should be no coverage element
        self.assertEqual(self.resNetcdf.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 0)

        # there should be subject element
        self.assertNotEquals(self.resNetcdf.metadata.subjects.all().count(), 0)

        # testing extended metadata elements
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)
    def test_delete_file(self):
        # test if the test file is added to the resource
        resource_file_objects = ResourceFile.objects.filter(object_id=self.res.pk)
        self.assertIn(
            self.file.name,
            [os.path.basename(rf.resource_file.name) for rf in resource_file_objects],
            msg='the test file is not added to the resource'
        )


        # test if the added test file is deleted
        hydroshare.delete_resource_file(self.res.short_id, self.file.name)
        resource_file_objects = ResourceFile.objects.filter(object_id=self.res.pk)
        self.assertNotIn(
            self.file.name,
            [os.path.basename(rf.resource_file.name) for rf in resource_file_objects],
            msg='the added test file is not deleted from the resource'
        )
    def _test_file_metadata_on_file_delete(self, ext):
        self.raster_file_obj = open(self.raster_file, 'r')
        self._create_composite_resource()
        res_file = self.composite_resource.files.first()

        # extract metadata from the tif file
        GeoRasterLogicalFile.set_file_type(self.composite_resource,
                                           res_file.id, self.user)

        # test that we have one logical file of type GeoRasterFileType
        self.assertEqual(GeoRasterLogicalFile.objects.count(), 1)
        self.assertEqual(GeoRasterFileMetaData.objects.count(), 1)

        res_file = self.composite_resource.files.first()
        logical_file = res_file.logical_file
        # there should be 1 coverage element of type spatial
        self.assertEqual(logical_file.metadata.coverages.all().count(), 1)
        self.assertNotEqual(logical_file.metadata.spatial_coverage, None)
        self.assertNotEqual(logical_file.metadata.originalCoverage, None)
        self.assertNotEqual(logical_file.metadata.cellInformation, None)
        self.assertNotEqual(logical_file.metadata.bandInformations, None)

        # there should be 2 coverage objects - one at the resource level
        # and the other one at the file type level
        self.assertEqual(Coverage.objects.count(), 2)
        self.assertEqual(OriginalCoverage.objects.count(), 1)
        self.assertEqual(CellInformation.objects.count(), 1)
        self.assertEqual(BandInformation.objects.count(), 1)

        # delete content file specified by extension (ext parameter)
        res_file_tif = hydroshare.utils.get_resource_files_by_extension(
            self.composite_resource, ext)[0]
        hydroshare.delete_resource_file(self.composite_resource.short_id,
                                        res_file_tif.id, self.user)
        # test that we don't have logical file of type GeoRasterFileType
        self.assertEqual(GeoRasterLogicalFile.objects.count(), 0)
        self.assertEqual(GeoRasterFileMetaData.objects.count(), 0)

        # test that all metadata deleted
        self.assertEqual(Coverage.objects.count(), 0)
        self.assertEqual(OriginalCoverage.objects.count(), 0)
        self.assertEqual(CellInformation.objects.count(), 0)
        self.assertEqual(BandInformation.objects.count(), 0)
    def test_delete_file(self):
        # test if the test file is added to the resource
        resource_file_objects = ResourceFile.objects.filter(object_id=self.res.pk)
        self.assertIn(
            self.file.name,
            [os.path.basename(rf.resource_file.name) for rf in resource_file_objects],
            msg='the test file is not added to the resource'
        )
        self.assertEqual(27, self.res.size)

        # delete the resource file - this is the api we are testing
        hydroshare.delete_resource_file(self.res.short_id, self.file.name, self.user)
        # test if the added test file is deleted
        resource_file_objects = ResourceFile.objects.filter(object_id=self.res.pk)
        self.assertNotIn(
            self.file.name,
            [os.path.basename(rf.resource_file.name) for rf in resource_file_objects],
            msg='the added test file is not deleted from the resource'
        )
        self.assertEqual(0, self.res.size)
Example #12
0
    def test_aggregation_metadata_on_file_delete(self):
        # test that when a file that's part of the GenericLogicalFile object
        # is deleted all metadata associated with the file type also get deleted

        self.create_composite_resource(self.generic_file)
        res_file = self.composite_resource.files.first()
        # set the file to generic logical file
        GenericLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id)
        res_file = self.composite_resource.files.first()
        gen_logical_file = res_file.logical_file
        self.assertEqual(GenericLogicalFile.objects.count(), 1)
        self.assertEqual(GenericFileMetaData.objects.count(), 1)
        # at this point there should not be any coverage elements associated with
        # logical file
        self.assertEqual(gen_logical_file.metadata.coverages.count(), 0)
        # at this point there should not be any key/value metadata associated with
        # logical file
        self.assertEqual(gen_logical_file.metadata.extra_metadata, {})
        # add temporal coverage
        value_dict = {'name': 'Name for period coverage', 'start': '1/1/2000', 'end': '12/12/2012'}
        gen_logical_file.metadata.create_element('coverage', type='period', value=value_dict)
        # add spatial coverage
        value_dict = {'east': '56.45678', 'north': '12.6789', 'units': 'Decimal degree'}
        gen_logical_file.metadata.create_element('coverage', type='point', value=value_dict)
        # at this point there should be 2 coverage elements associated with
        # logical file
        self.assertEqual(gen_logical_file.metadata.coverages.count(), 2)
        # at this point we should have 4 coverage elements (2 resource level
        # and 2 file type level
        self.assertEqual(Coverage.objects.count(), 4)
        # add key/value metadata
        gen_logical_file.metadata.extra_metadata = {'key1': 'value 1', 'key2': 'value 2'}
        gen_logical_file.metadata.save()
        hydroshare.delete_resource_file(self.composite_resource.short_id,
                                        res_file.id,
                                        self.user)
        # test that we don't have logical file of type GenericLogicalFile
        self.assertEqual(GenericLogicalFile.objects.count(), 0)
        self.assertEqual(GenericFileMetaData.objects.count(), 0)
        # test that resource level coverage element exist - not got deleted
        self.assertEqual(Coverage.objects.count(), 2)
Example #13
0
    def test_add_files(self):
        # here we are testing add_files_to_resource view function

        # There should be no files for the resource now
        self.assertEqual(self.gen_res.files.count(), 0)

        # add a file
        post_data = {'files': (self.txt_file_name_1, open(self.txt_file_path_1), 'text/plain')}
        url_params = {'shortkey': self.gen_res.short_id}

        url = reverse('add_files_to_resource', kwargs=url_params)
        request = self.factory.post(url, data=post_data)
        request.user = self.user
        # make it a ajax request
        request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
        self.set_request_message_attributes(request)
        response = add_files_to_resource(request, shortkey=self.gen_res.short_id)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # there should be 1 file
        self.assertEqual(self.gen_res.files.count(), 1)

        # test adding a file to a deep folder
        res_file = self.gen_res.files.first()
        hydroshare.delete_resource_file(self.gen_res.short_id, res_file.id, self.user)
        # there should be no file
        self.assertEqual(self.gen_res.files.count(), 0)
        post_data = {'files': (self.txt_file_name_1, open(self.txt_file_path_1), 'text/plain'),
                     'file_folder': 'data/contents/foo'}
        url_params = {'shortkey': self.gen_res.short_id}

        url = reverse('add_files_to_resource', kwargs=url_params)
        request = self.factory.post(url, data=post_data)
        request.user = self.user
        # make it a ajax request
        request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
        self.set_request_message_attributes(request)
        response = add_files_to_resource(request, shortkey=self.gen_res.short_id)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # there should be 1 file
        self.assertEqual(self.gen_res.files.count(), 1)
        hydroshare.delete_resource(self.gen_res.short_id)
    def _content_file_delete(self, ext):
        # test that when any file that is part of an instance GeoRasterFileType is deleted
        # all files associated with GeoRasterFileType is deleted
        self.raster_file_obj = open(self.raster_file, 'r')
        self._create_composite_resource()
        res_file = self.composite_resource.files.first()
        self.assertEqual(res_file.logical_file_type_name, "GenericLogicalFile")
        # extract metadata from the tif file
        GeoRasterLogicalFile.set_file_type(self.composite_resource,
                                           res_file.id, self.user)
        self.assertEqual(self.composite_resource.files.all().count(), 2)
        self.assertEqual(GeoRasterLogicalFile.objects.count(), 1)

        # delete the content file specified by the ext (file extension param)
        res_file_tif = hydroshare.utils.get_resource_files_by_extension(
            self.composite_resource, ext)[0]
        hydroshare.delete_resource_file(self.composite_resource.short_id,
                                        res_file_tif.id, self.user)

        self.assertEqual(self.composite_resource.files.all().count(), 0)
        self.assertEqual(GeoRasterLogicalFile.objects.count(), 0)
    def delete(self, request, pk, pathname):
        resource, _, user = view_utils.authorize(
            request, pk, needed_permission=ACTION_TO_AUTHORIZE.EDIT_RESOURCE)

        if not resource.supports_folders and '/' in pathname:
            return Response("Resource type does not support folders", status.HTTP_403_FORBIDDEN)

        try:
            view_utils.irods_path_is_allowed(pathname)  # check for hacking attempts
        except (ValidationError, SuspiciousFileOperation) as ex:
            return Response(ex.message, status=status.HTTP_400_BAD_REQUEST)

        try:
            hydroshare.delete_resource_file(pk, pathname, user)
        except ObjectDoesNotExist as ex:    # matching file not found
            raise NotFound(detail=ex.message)

        # prepare response data
        response_data = {'resource_id': pk, 'file_name': pathname}
        resource_modified(resource, request.user, overwrite_bag=False)
        return Response(data=response_data, status=status.HTTP_200_OK)
    def test_file_delete_composite_resource(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(CompositeResourceTest,
                     self).is_federated_irods_available():
            return

        self._create_composite_resource()
        # set the logical file - which get sets as part of the post resource creation signal
        resource_post_create_actions(resource=self.composite_resource,
                                     user=self.user,
                                     metadata=self.composite_resource.metadata)

        # there should be one GenericLogicalFile object at this point
        self.assertEqual(GenericLogicalFile.objects.count(), 1)
        self.assertEqual(self.composite_resource.files.all().count(), 1)
        res_file = self.composite_resource.files.first()
        hydroshare.delete_resource_file(self.composite_resource.short_id,
                                        res_file.id, self.user)
        self.assertEqual(self.composite_resource.files.all().count(), 0)
        # there should not be any GenericLogicalFile object at this point
        self.assertEqual(GenericLogicalFile.objects.count(), 0)
    def test_metadata_on_content_file_delete(self):
        files = [UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)]
        utils.resource_file_add_pre_process(resource=self.resModelInstance, files=files, 
                                            user=self.user, extract_metadata=False)

        utils.resource_file_add_process(resource=self.resModelInstance, files=files, 
                                        user=self.user, extract_metadata=False)

        self.resModelInstance.metadata.create_element('Description', abstract="test abstract")
        self.resModelInstance.metadata.create_element('Subject', value="test subject")
        self.resModelInstance.metadata.create_element('ModelOutput', includes_output=True)
        self.resModelInstance.metadata.create_element('ExecutedBy', model_name=self.resModelProgram.short_id)

        # there should one content file
        self.assertEquals(self.resModelInstance.files.all().count(), 1)

        # there should be one format element
        self.assertEquals(self.resModelInstance.metadata.formats.all().count(), 1)

        # created file should have a short path equal to the declared name
        self.assertEquals(self.resModelInstance.files.all()[0].short_path, self.file_name) 

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resModelInstance.short_id, self.file_name, self.user)
        # there should no content file
        self.assertEquals(self.resModelInstance.files.all().count(), 0)

        # test the core metadata at this point
        self.assertNotEquals(self.resModelInstance.metadata.title, None)

        # there should be an abstract element
        self.assertNotEquals(self.resModelInstance.metadata.description, None)

        # there should be one creator element
        self.assertEquals(self.resModelInstance.metadata.creators.all().count(), 1)

        # testing extended metadata elements
        self.assertNotEqual(self.resModelInstance.metadata.model_output, None)
        self.assertNotEqual(self.resModelInstance.metadata.executed_by, None)
Example #18
0
    def test_metadata_on_content_file_delete(self):
        files = [UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)]
        utils.resource_file_add_pre_process(resource=self.resModelInstance, files=files, 
                                            user=self.user, extract_metadata=False)

        utils.resource_file_add_process(resource=self.resModelInstance, files=files, 
                                        user=self.user, extract_metadata=False)

        self.resModelInstance.metadata.create_element('Description', abstract="test abstract")
        self.resModelInstance.metadata.create_element('Subject', value="test subject")
        self.resModelInstance.metadata.create_element('ModelOutput', includes_output=True)
        self.resModelInstance.metadata.create_element('ExecutedBy', model_name=self.resModelProgram.short_id)

        # there should one content file
        self.assertEqual(self.resModelInstance.files.all().count(), 1)

        # there should be one format element
        self.assertEqual(self.resModelInstance.metadata.formats.all().count(), 1)

        # created file should have a short path equal to the declared name
        self.assertEqual(self.resModelInstance.files.all()[0].short_path, self.file_name) 

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resModelInstance.short_id, self.file_name, self.user)
        # there should no content file
        self.assertEqual(self.resModelInstance.files.all().count(), 0)

        # test the core metadata at this point
        self.assertNotEqual(self.resModelInstance.metadata.title, None)

        # there should be an abstract element
        self.assertNotEqual(self.resModelInstance.metadata.description, None)

        # there should be one creator element
        self.assertEqual(self.resModelInstance.metadata.creators.all().count(), 1)

        # testing extended metadata elements
        self.assertNotEqual(self.resModelInstance.metadata.model_output, None)
        self.assertNotEqual(self.resModelInstance.metadata.executed_by, None)
Example #19
0
    def delete(self, request, pk, pathname):
        resource, _, user = view_utils.authorize(
            request, pk, needed_permission=ACTION_TO_AUTHORIZE.EDIT_RESOURCE)

        if not resource.supports_folders and '/' in pathname:
            return Response("Resource type does not support folders",
                            status.HTTP_403_FORBIDDEN)

        try:
            view_utils.irods_path_is_allowed(
                pathname)  # check for hacking attempts
        except (ValidationError, SuspiciousFileOperation) as ex:
            return Response(ex.message, status=status.HTTP_400_BAD_REQUEST)

        try:
            hydroshare.delete_resource_file(pk, pathname, user)
        except ObjectDoesNotExist as ex:  # matching file not found
            raise NotFound(detail=ex.message)

        # prepare response data
        response_data = {'resource_id': pk, 'file_name': pathname}
        resource_modified(resource, request.user, overwrite_bag=False)
        return Response(data=response_data, status=status.HTTP_200_OK)
Example #20
0
    def _test_delete_file(self, file_extension):
        # test that deleting the file with the specified extension *file_extension*
        # deletes all files

        # check that the resource has no files
        self.assertEqual(self.resGeoFeature.files.count(), 0)

        # add files first
        files = []
        target = 'hs_geographic_feature_resource/tests/gis.osm_adminareas_v06_all_files.zip'
        files.append(UploadedFile(file=open(target, 'r'),
                                  name='gis.osm_adminareas_v06_all_files.zip'))
        hydroshare.utils.resource_file_add_process(self.resGeoFeature, files, self.user, )

        # check that the resource has 15 files
        self.assertEqual(self.resGeoFeature.files.count(), 15)

        # find the .shp file and delete it
        for f in self.resGeoFeature.files.all():
            if f.extension == file_extension:
                hydroshare.delete_resource_file(self.resGeoFeature.short_id, f.id, self.user)
                break
        # resource should have no files
        self.assertEqual(self.resGeoFeature.files.count(), 0)
    def test_metadata_in_user_zone(self):
        super(TestRasterMetaData, self).assert_federated_irods_available()
        # test metadata extraction with resource creation with tif file coming from user zone space
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.raster_tif_file_name)
        res_upload_files = []
        _, _, metadata, fed_res_path = utils.resource_pre_create_actions(
            resource_type='RasterResource',
            resource_title='My Test Raster Resource',
            page_redirect_url_key=None,
            files=res_upload_files,
            source_names=[fed_test_file_full_path])

        self.resRaster = hydroshare.create_resource(
            'RasterResource',
            self.user,
            'My Test Raster Resource',
            files=res_upload_files,
            source_names=[fed_test_file_full_path],
            fed_res_path=fed_res_path[0] if len(fed_res_path) == 1 else '',
            move=False,
            metadata=metadata)

        # raster file validation and metadata extraction in post resource creation signal handler
        utils.resource_post_create_actions(resource=self.resRaster, user=self.user,
                                           metadata=[])
        super(TestRasterMetaData, self).raster_metadata_extraction()

        # test metadata is deleted after content file is deleted in user zone space
        # there should be 2 content file: tif file and vrt file at this point
        self.assertEqual(self.resRaster.files.all().count(), 2)

        # there should be 2 format elements
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 2)
        self.assertEqual(self.resRaster.metadata.formats.all().filter(
            value='application/vrt').count(), 1)
        self.assertEqual(self.resRaster.metadata.formats.all().filter(
            value='image/tiff').count(), 1)

        # delete content file now
        hydroshare.delete_resource_file(self.resRaster.short_id, self.raster_tif_file_name,
                                        self.user)

        # there should be no content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # there should be a title element
        self.assertNotEqual(self.resRaster.metadata.title, None)

        # there should be no abstract element
        self.assertEqual(self.resRaster.metadata.description, None)

        # there should be 1 creator element
        self.assertEqual(self.resRaster.metadata.creators.all().count(), 1)

        # there should be no contributor element
        self.assertEqual(self.resRaster.metadata.contributors.all().count(), 0)

        # there should be no coverage element
        self.assertEqual(self.resRaster.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 0)

        # there should be no subject element
        self.assertEqual(self.resRaster.metadata.subjects.all().count(), 0)

        # testing extended metadata elements - there should be no extended metadata elements
        # at this point
        self.assertEqual(self.resRaster.metadata.originalCoverage, None)
        self.assertEqual(self.resRaster.metadata.cellInformation, None)
        self.assertEqual(self.resRaster.metadata.bandInformations.count(), 0)

        # test metadata extraction with a valid tif file being added coming from user zone space
        res_add_files = []

        # now necessary in order to test add_process
        utils.resource_file_add_pre_process(resource=self.resRaster,
                                            files=res_add_files,
                                            user=self.user,
                                            source_names=[fed_test_file_full_path])

        # file validation and metadata extraction happen during post file add signal handler
        utils.resource_file_add_process(resource=self.resRaster,
                                        files=res_add_files,
                                        user=self.user,
                                        source_names=[fed_test_file_full_path])

        super(TestRasterMetaData, self).raster_metadata_extraction()

        # test metadata deletion when deleting a resource in user zone space
        self.assertEqual(CoreMetaData.objects.all().count(), 1)

        # delete resource
        hydroshare.delete_resource(self.resRaster.short_id)

        # resource core metadata is deleted after resource deletion
        self.assertEqual(CoreMetaData.objects.all().count(), 0)

        # test adding file from user zone to existing empty resource in hydroshare zone
        # even there is no file uploaded to resource initially, there are default extended
        # automatically metadata created
        self.resRaster = hydroshare.create_resource(
            resource_type='RasterResource',
            owner=self.user,
            title='My Test Raster Resource'
        )
        # test metadata extraction with a valid tif file being added coming from user zone space
        # file validation and metadata extraction happen during post file add signal handler
        utils.resource_file_add_process(resource=self.resRaster,
                                        files=[],
                                        user=self.user,
                                        source_names=[fed_test_file_full_path])
        super(TestRasterMetaData, self).raster_metadata_extraction()

        # there should be 2 content file: tif file and vrt file at this point
        self.assertEqual(self.resRaster.files.all().count(), 2)
        # delete resource
        hydroshare.delete_resource(self.resRaster.short_id)
        # resource core metadata is deleted after resource deletion
        self.assertEqual(CoreMetaData.objects.all().count(), 0)
Example #22
0
    def test_allowed_file_types(self):
        # test allowed file type is '.tif, .zip'
        self.assertIn('.tif', RasterResource.get_supported_upload_file_types())
        self.assertIn('.zip', RasterResource.get_supported_upload_file_types())
        self.assertEqual(len(RasterResource.get_supported_upload_file_types()),
                         2)

        # there should not be any content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # trying to add a text file to this resource should raise exception
        files = [
            UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)
        ]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster,
                                                files=files,
                                                user=self.user,
                                                extract_metadata=False)
            utils.resource_file_add_process(resource=self.resRaster,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)

        # trying to add bad .tif file should raise file validation error
        files = [
            UploadedFile(file=self.raster_bad_tif_file_obj,
                         name=self.raster_bad_tif_file_name)
        ]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster,
                                                files=files,
                                                user=self.user,
                                                extract_metadata=False)
            utils.resource_file_add_process(resource=self.resRaster,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)

        # trying to add good .tif file should pass the file check
        files = [
            UploadedFile(file=self.raster_tif_file_obj,
                         name=self.raster_tif_file_name)
        ]
        utils.resource_file_add_pre_process(resource=self.resRaster,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)
        utils.resource_file_add_process(resource=self.resRaster,
                                        files=files,
                                        user=self.user,
                                        extract_metadata=False)

        # there should be 2 content file: with .vrt file created by system
        self.assertEqual(self.resRaster.files.all().count(), 2)
        file_names = [
            os.path.basename(f.resource_file.name)
            for f in self.resRaster.files.all()
        ]
        self.assertIn('raster_tif_valid.vrt', file_names)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resRaster.short_id,
                                        self.raster_tif_file_name, self.user)

        # there should be no content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # trying to add good .zip file should pass the file check
        files = [
            UploadedFile(file=self.raster_zip_file_obj,
                         name=self.raster_zip_file_name)
        ]
        utils.resource_file_add_pre_process(resource=self.resRaster,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)
        utils.resource_file_add_process(resource=self.resRaster,
                                        files=files,
                                        user=self.user,
                                        extract_metadata=False)

        # there should be 10 content file:
        self.assertEqual(self.resRaster.files.all().count(), 10)

        # file pre add process should raise validation error if we try to add a 2nd file when
        # the resource has already content files
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster,
                                                files=files,
                                                user=self.user,
                                                extract_metadata=False)
Example #23
0
    def test_get_revisions(self):
        # create a user to be used for creating the resource
        user_creator = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Creator_FirstName',
            last_name='Creator_LastName',
            superuser=False,
            groups=[]
        )
        resource_changed_by = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='Pabitra',
            last_name='Dash',
            superuser=False,
            groups=[]
        )

        # create a resource
        resource = hydroshare.create_resource('GenericResource', user_creator, 'My resource')

        # test that we have only one revision at this point - this is the api call we are testing
        res_revisions = hydroshare.get_revisions(resource.short_id)
        self.assertEqual(len(res_revisions), 1)

        # set the resource last changed by a different user - to a create another revision of the resource
        hydroshare.utils.resource_modified(resource, resource_changed_by)
        res_revisions = hydroshare.get_revisions(resource.short_id)

        # test that we now have 2 revisions
        self.assertEqual(len(res_revisions), 2)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)

        # test that each resource revision has the same resource id
        for bags in res_revisions:
            self.assertEqual(resource.id, bags.object_id)

        # add a file to the resource to generate another revision of the resource
        # create a file
        original_file_name = 'original.txt'
        original_file = open(original_file_name, 'w')
        original_file.write("original text")
        original_file.close()

        original_file = open(original_file_name, 'r')
        # add the file to the resource
        hydroshare.add_resource_files(resource.short_id, original_file)
        res_revisions = hydroshare.get_revisions(resource.short_id)
        # test that we now have 3 revisions
        self.assertEqual(len(res_revisions), 3)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[2].timestamp)
        self.assertNotEqual(res_revisions[1].timestamp, res_revisions[2].timestamp)

        # delete the file in the resource to create another revision of the resource
        hydroshare.delete_resource_file(resource.short_id, original_file_name)
        res_revisions = hydroshare.get_revisions(resource.short_id)
        # test that we now have 4 revisions
        self.assertEqual(len(res_revisions), 4)
Example #24
0
    def test_metadata_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(TestTimeSeriesMetaData,
                     self).is_federated_irods_available():
            return

        # test metadata extraction with resource creation with file coming from user zone space
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            username=self.user.username,
            fname=self.odm2_sqlite_file_name)
        self.resTimeSeries = hydroshare.create_resource(
            resource_type='TimeSeriesResource',
            owner=self.user,
            title='My Test TimeSeries Resource',
            source_names=[fed_test_file_full_path],
            move=False)
        utils.resource_post_create_actions(resource=self.resTimeSeries,
                                           user=self.user,
                                           metadata=[])

        super(TestTimeSeriesMetaData, self).timeseries_metadata_extraction()

        # test that metadata is NOT deleted (except format element) on content file deletion
        # there should be one content file at this point
        self.assertEqual(self.resTimeSeries.files.all().count(), 1)
        # there should be one format element
        self.assertEqual(self.resTimeSeries.metadata.formats.all().count(), 1)

        hydroshare.delete_resource_file(self.resTimeSeries.short_id,
                                        self.odm2_sqlite_file_name, self.user)
        # there should be no content file
        self.assertEqual(self.resTimeSeries.files.all().count(), 0)

        # test the core metadata at this point
        self.assertNotEqual(self.resTimeSeries.metadata.title, None)

        # there should be an abstract element
        self.assertNotEqual(self.resTimeSeries.metadata.description, None)

        # there should be one creator element
        self.assertEqual(self.resTimeSeries.metadata.creators.all().count(), 1)

        # there should be one contributor element
        self.assertEqual(
            self.resTimeSeries.metadata.contributors.all().count(), 1)

        # there should be 2 coverage element -  point type and period type
        self.assertEqual(self.resTimeSeries.metadata.coverages.all().count(),
                         2)
        self.assertEqual(
            self.resTimeSeries.metadata.coverages.all().filter(
                type='box').count(), 1)
        self.assertEqual(
            self.resTimeSeries.metadata.coverages.all().filter(
                type='period').count(), 1)
        # there should be no format element
        self.assertEqual(self.resTimeSeries.metadata.formats.all().count(), 0)
        # there should be one subject element
        self.assertEqual(self.resTimeSeries.metadata.subjects.all().count(), 1)

        # testing extended metadata elements
        self.assertNotEqual(self.resTimeSeries.metadata.sites.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.variables.all().count(), 0)
        self.assertNotEqual(self.resTimeSeries.metadata.methods.all().count(),
                            0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.processing_levels.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.time_series_results.all().count(), 0)

        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_variable_names.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_variable_types.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_speciations.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_site_types.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_elevation_datums.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_method_types.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_statuses.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_mediums.all().count(), 0)
        self.assertNotEqual(
            self.resTimeSeries.metadata.cv_aggregation_statistics.all().count(
            ), 0)

        # test metadata extraction with a valid ODM2 sqlite file being added coming from user zone
        # space
        res_add_files = []
        utils.resource_file_add_pre_process(
            resource=self.resTimeSeries,
            files=res_add_files,
            user=self.user,
            extract_metadata=False,
            source_names=[fed_test_file_full_path])

        utils.resource_file_add_process(resource=self.resTimeSeries,
                                        files=res_add_files,
                                        user=self.user,
                                        extract_metadata=True,
                                        source_names=[fed_test_file_full_path])

        super(TestTimeSeriesMetaData, self).timeseries_metadata_extraction()

        # test metadata deletion when deleting a resource in user zone space
        # all metadata should get deleted when the resource get deleted
        self.assertEqual(CoreMetaData.objects.all().count(), 1)
        # delete resource
        hydroshare.delete_resource(self.resTimeSeries.short_id)
        self.assertEqual(CoreMetaData.objects.all().count(), 0)
    def test_metadata_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        super(TestNetcdfMetaData, self).assert_federated_irods_available()

        # test metadata extraction with resource creation with nc file coming from user zone space
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.netcdf_file_name)
        res_upload_files = []
        _, _, metadata, fed_res_path = utils.resource_pre_create_actions(
            resource_type='NetcdfResource',
            resource_title='Snow water equivalent estimation at TWDEF site from Oct 2009 to June '
                           '2010',
            page_redirect_url_key=None,
            files=res_upload_files,
            source_names=[fed_test_file_full_path])
        self.resNetcdf = hydroshare.create_resource(
            'NetcdfResource',
            self.user,
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010',
            files=res_upload_files,
            source_names=[fed_test_file_full_path],
            fed_res_path=fed_res_path[0] if len(fed_res_path) == 1 else '',
            move=False,
            metadata=metadata)
        utils.resource_post_create_actions(self.resNetcdf, self.user, metadata)
        super(TestNetcdfMetaData, self).netcdf_metadata_extraction()

        # test metadata is deleted after content file is deleted in user zone space
        # there should be 2 content files at this point
        self.assertEqual(self.resNetcdf.files.all().count(), 2)

        # there should be 2 format elements
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 2)

        # delete content file now
        hydroshare.delete_resource_file(self.resNetcdf.short_id, self.netcdf_file_name, self.user)

        # there should no content file
        self.assertEqual(self.resNetcdf.files.all().count(), 0)

        # there should be a title element
        self.assertNotEqual(self.resNetcdf.metadata.title, None)

        # there should be abstract element
        self.assertNotEqual(self.resNetcdf.metadata.description, None)

        # there should be 1 creator element (based on the extracted metadata)
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 1)

        # there should be 1 contributor element
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 1)

        # there should be no coverage element
        self.assertEqual(self.resNetcdf.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 0)

        # there should be subject element
        self.assertNotEqual(self.resNetcdf.metadata.subjects.all().count(), 0)

        # testing extended metadata elements
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)

        # test metadata extraction with a valid nc file being added coming from user zone space
        res_add_files = []
        utils.resource_file_add_pre_process(resource=self.resNetcdf,
                                            files=res_add_files,
                                            user=self.user,
                                            source_names=[fed_test_file_full_path])
        utils.resource_file_add_process(resource=self.resNetcdf,
                                        files=res_add_files,
                                        user=self.user,
                                        source_names=[fed_test_file_full_path])

        super(TestNetcdfMetaData, self).netcdf_metadata_extraction()
        self.assertEqual(CoreMetaData.objects.all().count(), 1)
        # delete resource
        hydroshare.delete_resource(self.resNetcdf.short_id)
        self.assertEqual(CoreMetaData.objects.all().count(), 0)
Example #26
0
 def delete_resource_file(self, pk, filename):
     authorize(self.request, pk, edit=True)
     f = hydroshare.delete_resource_file(pk, filename)
     return HttpResponse(f, content_type='text/plain')
    def test_metadata_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(TestGeoFeature, self).is_federated_irods_available():
            return
        # test metadata extraction with resource creation with file coming from user zone space
        resource_type = "GeographicFeatureResource"
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.valid_file_name)
        res_upload_files = []
        res_title = "test title"

        self.resGeoFeature = hydroshare.create_resource(
            resource_type=resource_type,
            owner=self.user,
            title=res_title,
            files=res_upload_files,
            source_names=[fed_test_file_full_path],
            fed_res_path='',
            move=False,
            metadata=[])

        # uploaded file validation and metadata extraction happens in post resource
        # creation handler
        hydroshare.utils.resource_post_create_actions(resource=self.resGeoFeature, user=self.user,
                                                      metadata=[])

        # check that the resource has 3 files
        self.assertEqual(self.resGeoFeature.files.count(), 3)

        # test extracted metadata

        # there should not be any resource level coverage
        self.assertEqual(self.resGeoFeature.metadata.coverages.count(), 0)
        self.assertNotEqual(self.resGeoFeature.metadata.geometryinformation, None)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.featureCount, 51)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.geometryType,
                         "MULTIPOLYGON")

        self.assertNotEqual(self.resGeoFeature.metadata.originalcoverage, None)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum,
                         'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name,
                         'unknown')
        self.assertGreater(len(self.resGeoFeature.metadata.originalcoverage.projection_string), 0)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.eastlimit, -66.9692712587578)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.northlimit, 71.406235393967)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.southlimit, 18.921786345087)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.westlimit,
                         -178.217598362366)

        # test that all files get deleted and extracted metadata gets deleted when one of the
        # required files get deleted
        shp_res_file = [f for f in self.resGeoFeature.files.all() if f.extension == '.shp'][0]
        hydroshare.delete_resource_file(self.resGeoFeature.short_id, shp_res_file.id,
                                        self.user)
        # check that the resource has no files
        self.assertEqual(self.resGeoFeature.files.count(), 0)

        # test metadata extraction with a valid file being added coming from user zone space
        res_add_files = []
        fed_test_add_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.valid_file_name2)

        hydroshare.utils.resource_file_add_process(resource=self.resGeoFeature,
                                                   files=res_add_files,
                                                   user=self.user,
                                                   source_names=[fed_test_add_file_full_path])

        # check that the resource has 15 files
        self.assertEqual(self.resGeoFeature.files.count(), 15)

        # test extracted metadata
        self.assertEqual(self.resGeoFeature.metadata.fieldinformations.all().count(), 7)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.featureCount, 87)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.geometryType, "POLYGON")
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum, 'WGS_1984')
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.eastlimit -
                            3.4520493) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.northlimit -
                            45.0466382) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.southlimit -
                            42.5732416) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.westlimit -
                            (-0.3263017)) < self.allowance)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'Degree')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name,
                         'GCS_WGS_1984')

        self.resGeoFeature.delete()
    def test_get_revisions(self):
        url = 'hsapi/revisions/{0}/'.format(self.res.short_id)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)
        
        self.assertValidJSONResponse(resp)
        self.assertEqual(len(res_revisions), 1)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id), res_revisions)

        resource_changed_by = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='User1_FirstName',
            last_name='User1_LastName'
        )
        hydroshare.utils.resource_modified(self.res, resource_changed_by)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        self.assertEqual(len(res_revisions), 2)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id), res_revisions)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)

        # test that each resource revision has the same resource id
        for bags in res_revisions:
            self.assertEqual(self.res.id, bags.object_id)

        # add a file to the resource to generate another revision of the resource
        # create a file
        original_file_name = 'original.txt'
        original_file = open(original_file_name, 'w')
        original_file.write("original text")
        original_file.close()

        original_file = open(original_file_name, 'r')
        # add the file to the resource
        hydroshare.add_resource_files(self.res.short_id, original_file)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        # test that we now have 3 revisions
        self.assertEqual(len(res_revisions), 3)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id), res_revisions)
        
        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[2].timestamp)
        self.assertNotEqual(res_revisions[1].timestamp, res_revisions[2].timestamp)

        # delete the file in the resource to create another revision of the resource
        hydroshare.delete_resource_file(self.res.short_id, original_file_name)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id), res_revisions)
        # test that we now have 4 revisions
        self.assertEqual(len(res_revisions), 4)
Example #29
0
    def test_metadata_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(TestNetcdfMetaData, self).is_federated_irods_available():
            return

        # test metadata extraction with resource creation with nc file coming from user zone space
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            username=self.user.username,
            fname=self.netcdf_file_name)
        res_upload_files = []
        _, _, metadata, fed_res_path = utils.resource_pre_create_actions(
            resource_type='NetcdfResource',
            resource_title=
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June '
            '2010',
            page_redirect_url_key=None,
            files=res_upload_files,
            source_names=[fed_test_file_full_path])
        self.resNetcdf = hydroshare.create_resource(
            'NetcdfResource',
            self.user,
            'Snow water equivalent estimation at TWDEF site from Oct 2009 to June 2010',
            files=res_upload_files,
            source_names=[fed_test_file_full_path],
            fed_res_path=fed_res_path[0] if len(fed_res_path) == 1 else '',
            move=False,
            metadata=metadata)
        utils.resource_post_create_actions(self.resNetcdf, self.user, metadata)
        super(TestNetcdfMetaData, self).netcdf_metadata_extraction()

        # test metadata is deleted after content file is deleted in user zone space
        # there should be 2 content files at this point
        self.assertEqual(self.resNetcdf.files.all().count(), 2)

        # there should be 2 format elements
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 2)

        # delete content file now
        hydroshare.delete_resource_file(self.resNetcdf.short_id,
                                        self.netcdf_file_name, self.user)

        # there should no content file
        self.assertEqual(self.resNetcdf.files.all().count(), 0)

        # there should be a title element
        self.assertNotEqual(self.resNetcdf.metadata.title, None)

        # there should be abstract element
        self.assertNotEqual(self.resNetcdf.metadata.description, None)

        # there should be 1 creator element (based on the extracted metadata)
        self.assertEqual(self.resNetcdf.metadata.creators.all().count(), 1)

        # there should be 1 contributor element
        self.assertEqual(self.resNetcdf.metadata.contributors.all().count(), 1)

        # there should be no coverage element
        self.assertEqual(self.resNetcdf.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resNetcdf.metadata.formats.all().count(), 0)

        # there should be subject element
        self.assertNotEqual(self.resNetcdf.metadata.subjects.all().count(), 0)

        # testing extended metadata elements
        self.assertEqual(self.resNetcdf.metadata.ori_coverage.all().count(), 0)
        self.assertEqual(self.resNetcdf.metadata.variables.all().count(), 0)

        # test metadata extraction with a valid nc file being added coming from user zone space
        res_add_files = []
        utils.resource_file_add_pre_process(
            resource=self.resNetcdf,
            files=res_add_files,
            user=self.user,
            source_names=[fed_test_file_full_path])
        utils.resource_file_add_process(resource=self.resNetcdf,
                                        files=res_add_files,
                                        user=self.user,
                                        source_names=[fed_test_file_full_path])

        super(TestNetcdfMetaData, self).netcdf_metadata_extraction()
        self.assertEqual(CoreMetaData.objects.all().count(), 1)
        # delete resource
        hydroshare.delete_resource(self.resNetcdf.short_id)
        self.assertEqual(CoreMetaData.objects.all().count(), 0)
Example #30
0
    def test_metadata_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(TestGeoFeature, self).is_federated_irods_available():
            return
        # test metadata extraction with resource creation with file coming from user zone space
        resource_type = "GeographicFeatureResource"
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            username=self.user.username,
            fname=self.valid_file_name)
        res_upload_files = []
        res_title = "test title"
        url_key = "page_redirect_url"
        page_url_dict, res_title, metadata, fed_res_path = \
            hydroshare.utils.resource_pre_create_actions(
                resource_type=resource_type, files=res_upload_files, resource_title=res_title,
                page_redirect_url_key=url_key, fed_res_file_names=[fed_test_file_full_path])

        for item_dict in metadata:
            self.assertEqual(len(item_dict.keys()), 1)
            key = item_dict.keys()[0]
            if key == "OriginalFileInfo":
                self.assertEqual(item_dict["OriginalFileInfo"]["baseFilename"],
                                 "states")
                self.assertEqual(item_dict["OriginalFileInfo"]["fileCount"], 7)
                self.assertEqual(item_dict["OriginalFileInfo"]["fileType"],
                                 "ZSHP")
            elif key == "field_info_array":
                self.assertEqual(len(item_dict["field_info_array"]), 5)
            elif key == "geometryinformation":
                self.assertEqual(
                    item_dict["geometryinformation"]["featureCount"], 51)
                self.assertEqual(
                    item_dict["geometryinformation"]["geometryType"],
                    "MULTIPOLYGON")
            elif key == "originalcoverage":
                self.assertEqual(item_dict["originalcoverage"]['datum'],
                                 'North_American_Datum_1983')
                self.assertEqual(item_dict["originalcoverage"]['eastlimit'],
                                 -66.96927125875777)
                self.assertEqual(item_dict["originalcoverage"]['northlimit'],
                                 71.40623539396698)
                self.assertEqual(item_dict["originalcoverage"]['southlimit'],
                                 18.92178634508703)
                self.assertEqual(item_dict["originalcoverage"]['westlimit'],
                                 -178.21759836236586)
                self.assertEqual(item_dict["originalcoverage"]['unit'],
                                 'Degree')
                self.assertEqual(
                    item_dict["originalcoverage"]['projection_name'],
                    'GCS_North_American_1983')

        self.resGeoFeature = hydroshare.create_resource(
            resource_type=resource_type,
            owner=self.user,
            title=res_title,
            files=res_upload_files,
            fed_res_file_names=[fed_test_file_full_path],
            fed_res_path=fed_res_path[0] if len(fed_res_path) == 1 else '',
            fed_copy_or_move='copy',
            metadata=metadata)

        # test metadata is deleted after content file is deleted in user zone space
        for res_f_obj in ResourceFile.objects.filter(
                object_id=self.resGeoFeature.id):
            try:
                hydroshare.delete_resource_file(self.resGeoFeature.short_id,
                                                res_f_obj.id, self.user)
            # deleting one file may delete other relevant files as well, so ObjectDoesNotExist
            # exception is expected
            except ObjectDoesNotExist:
                continue
        self.assertEqual(
            ResourceFile.objects.filter(
                object_id=self.resGeoFeature.id).count(), 0)
        self.assertEqual(
            self.resGeoFeature.metadata.geometryinformation.all().count(), 0)
        self.assertEqual(
            self.resGeoFeature.metadata.fieldinformation.all().count(), 0)
        self.assertEqual(
            self.resGeoFeature.metadata.originalcoverage.all().count(), 0)

        # test metadata extraction with a valid file being added coming from user zone space
        res_add_files = []
        fed_test_add_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            username=self.user.username,
            fname=self.valid_file_name2)
        hydroshare.utils.resource_file_add_pre_process(
            resource=self.resGeoFeature,
            files=res_add_files,
            user=self.user,
            fed_res_file_names=[fed_test_add_file_full_path])
        hydroshare.utils.resource_file_add_process(
            resource=self.resGeoFeature,
            files=res_add_files,
            user=self.user,
            fed_res_file_names=[fed_test_add_file_full_path])

        self.assertNotEqual(
            self.resGeoFeature.metadata.originalfileinfo.all().first(), None)
        self.assertEqual(
            self.resGeoFeature.metadata.originalfileinfo.all().first().
            baseFilename, "gis.osm_adminareas_v06")
        self.assertEqual(
            self.resGeoFeature.metadata.originalfileinfo.all().first().
            fileCount, 5)
        self.assertEqual(
            self.resGeoFeature.metadata.originalfileinfo.all().first().
            fileType, "ZSHP")
        self.assertEqual(
            self.resGeoFeature.metadata.fieldinformation.all().count(), 7)
        self.assertEqual(
            self.resGeoFeature.metadata.geometryinformation.all().first().
            featureCount, 87)
        self.assertEqual(
            self.resGeoFeature.metadata.geometryinformation.all().first().
            geometryType, "POLYGON")
        self.assertEqual(
            self.resGeoFeature.metadata.originalcoverage.all().first().datum,
            'WGS_1984')
        self.assertTrue(
            abs(self.resGeoFeature.metadata.originalcoverage.all().first().
                eastlimit - 3.4520493) < self.allowance)
        self.assertTrue(
            abs(self.resGeoFeature.metadata.originalcoverage.all().first().
                northlimit - 45.0466382) < self.allowance)
        self.assertTrue(
            abs(self.resGeoFeature.metadata.originalcoverage.all().first().
                southlimit - 42.5732416) < self.allowance)
        self.assertTrue(
            abs(self.resGeoFeature.metadata.originalcoverage.all().first().
                westlimit - (-0.3263017)) < self.allowance)
        self.assertEqual(
            self.resGeoFeature.metadata.originalcoverage.all().first().unit,
            'Degree')
        self.assertEqual(
            self.resGeoFeature.metadata.originalcoverage.all().first().
            projection_name, 'GCS_WGS_1984')
        self.assertEqual(CoreMetaData.objects.all().count(), 1)
        # delete resource
        hydroshare.delete_resource(self.resGeoFeature.short_id)
        self.assertEqual(CoreMetaData.objects.all().count(), 0)
Example #31
0
    def test_metadata_on_content_file_delete(self):
        # test that some of the metadata is not deleted on content file deletion
        files = [
            UploadedFile(file=self.raster_tif_file_obj,
                         name=self.raster_tif_file_name)
        ]
        utils.resource_file_add_pre_process(resource=self.resRaster,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resRaster,
                                        files=files,
                                        user=self.user,
                                        extract_metadata=True)
        # there should be 2 content files
        self.assertEqual(self.resRaster.files.all().count(), 2)

        # there should be 2 format elements
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 2)
        self.assertEqual(
            self.resRaster.metadata.formats.all().filter(
                value='application/vrt').count(), 1)

        self.assertEqual(
            self.resRaster.metadata.formats.all().filter(
                value='image/tiff').count(), 1)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resRaster.short_id,
                                        self.raster_tif_file_name, self.user)

        # there should no content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # there should be a title element
        self.assertNotEqual(self.resRaster.metadata.title, None)

        # there should be no abstract element
        self.assertEqual(self.resRaster.metadata.description, None)

        # there should be 1 creator element
        self.assertEqual(self.resRaster.metadata.creators.all().count(), 1)

        # there should be no contributor element
        self.assertEqual(self.resRaster.metadata.contributors.all().count(), 0)

        # there should be no coverage element
        self.assertEqual(self.resRaster.metadata.coverages.all().count(), 0)

        # there should be no format element
        self.assertEqual(self.resRaster.metadata.formats.all().count(), 0)

        # there should be no subject element
        self.assertEqual(self.resRaster.metadata.subjects.all().count(), 0)

        # testing extended metadata elements - there should not be any resource specific metadata
        self.assertEqual(self.resRaster.metadata.originalCoverage, None)

        self.assertEqual(self.resRaster.metadata.cellInformation, None)
        self.assertEqual(self.resRaster.metadata.bandInformations.count(), 0)

        self.resRaster.delete()
Example #32
0
 def delete_resource_file(self, pk, filename):
     authorize(self.request, pk, edit=True)
     f = hydroshare.delete_resource_file(pk, filename)
     return HttpResponse(f, content_type='text/plain')
    def test_allowed_file_types(self):
        # test allowed file types are: '.tiff', '.tif,', '.vrt' and '.zip'
        self.assertIn('.tiff', RasterResource.get_supported_upload_file_types())
        self.assertIn('.tif', RasterResource.get_supported_upload_file_types())
        self.assertIn('.vrt', RasterResource.get_supported_upload_file_types())
        self.assertIn('.zip', RasterResource.get_supported_upload_file_types())
        self.assertEqual(len(RasterResource.get_supported_upload_file_types()), 4)

        # there should not be any content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # trying to add a text file to this resource should raise exception
        files = [UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster, files=files,
                                                user=self.user, extract_metadata=False)
            utils.resource_file_add_process(resource=self.resRaster, files=files,
                                            user=self.user,
                                            extract_metadata=False)

        # trying to add bad .tif file should raise file validation error
        files = [UploadedFile(file=self.raster_bad_tif_file_obj,
                              name=self.raster_bad_tif_file_name)]
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster, files=files,
                                                user=self.user, extract_metadata=False)
            utils.resource_file_add_process(resource=self.resRaster, files=files,
                                            user=self.user, extract_metadata=False)

        # trying to add good .tif file should pass the file check
        files = [UploadedFile(file=self.raster_tif_file_obj, name=self.raster_tif_file_name)]
        utils.resource_file_add_pre_process(resource=self.resRaster, files=files,
                                            user=self.user, extract_metadata=False)
        utils.resource_file_add_process(resource=self.resRaster, files=files, user=self.user,
                                        extract_metadata=False)

        # there should be 2 content file: with .vrt file created by system
        self.assertEqual(self.resRaster.files.all().count(), 2)
        file_names = [os.path.basename(f.resource_file.name) for f in self.resRaster.files.all()]
        self.assertIn('raster_tif_valid.vrt', file_names)

        # delete content file that we added above
        hydroshare.delete_resource_file(self.resRaster.short_id, self.raster_tif_file_name,
                                        self.user)

        # there should be no content file
        self.assertEqual(self.resRaster.files.all().count(), 0)

        # trying to add good .zip file should pass the file check
        files = [UploadedFile(file=self.raster_zip_file_obj, name=self.raster_zip_file_name)]
        utils.resource_file_add_pre_process(resource=self.resRaster, files=files,
                                            user=self.user, extract_metadata=False)
        utils.resource_file_add_process(resource=self.resRaster, files=files,
                                        user=self.user, extract_metadata=False)

        # there should be 10 content file:
        self.assertEqual(self.resRaster.files.all().count(), 10)

        # file pre add process should raise validation error if we try to add a 2nd file when
        # the resource has already content files
        with self.assertRaises(utils.ResourceFileValidationException):
            utils.resource_file_add_pre_process(resource=self.resRaster, files=files,
                                                user=self.user, extract_metadata=False)
Example #34
0
def delete_file(request, shortkey, f, *args, **kwargs):
    res, _, user = authorize(request, shortkey, edit=True, full=True, superuser=True)
    hydroshare.delete_resource_file(shortkey, f, user)

    return HttpResponseRedirect(request.META['HTTP_REFERER'])
Example #35
0
    def test_get_revisions(self):
        # create a user to be used for creating the resource
        user_creator = hydroshare.create_account(
            "*****@*****.**",
            username="******",
            first_name="Creator_FirstName",
            last_name="Creator_LastName",
            superuser=False,
            groups=[],
        )
        resource_changed_by = hydroshare.create_account(
            "*****@*****.**",
            username="******",
            first_name="Pabitra",
            last_name="Dash",
            superuser=False,
            groups=[],
        )

        # create a resource
        resource = hydroshare.create_resource("GenericResource", user_creator, "My resource")

        # test that we have only one revision at this point - this is the api call we are testing
        res_revisions = hydroshare.get_revisions(resource.short_id)
        self.assertEqual(len(res_revisions), 1)

        # set the resource last changed by a different user - to a create another revision of the resource
        hydroshare.utils.resource_modified(resource, resource_changed_by)
        res_revisions = hydroshare.get_revisions(resource.short_id)

        # test that we now have 2 revisions
        self.assertEqual(len(res_revisions), 2)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)

        # test that each resource revision has the same resource id
        for bags in res_revisions:
            self.assertEqual(resource.id, bags.object_id)

        # add a file to the resource to generate another revision of the resource
        # create a file
        original_file_name = "original.txt"
        original_file = open(original_file_name, "w")
        original_file.write("original text")
        original_file.close()

        original_file = open(original_file_name, "r")
        # add the file to the resource
        hydroshare.add_resource_files(resource.short_id, original_file)
        res_revisions = hydroshare.get_revisions(resource.short_id)
        # test that we now have 3 revisions
        self.assertEqual(len(res_revisions), 3)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[1].timestamp)
        self.assertNotEqual(res_revisions[0].timestamp, res_revisions[2].timestamp)
        self.assertNotEqual(res_revisions[1].timestamp, res_revisions[2].timestamp)

        # delete the file in the resource to create another revision of the resource
        hydroshare.delete_resource_file(resource.short_id, original_file_name)
        res_revisions = hydroshare.get_revisions(resource.short_id)
        # test that we now have 4 revisions
        self.assertEqual(len(res_revisions), 4)
Example #36
0
    def test_metadata_on_content_file_delete(self):
        files = [
            UploadedFile(file=self.text_file_obj, name=self.text_file_obj.name)
        ]
        utils.resource_file_add_pre_process(resource=self.resModelProgram,
                                            files=files,
                                            user=self.user,
                                            extract_metadata=False)

        utils.resource_file_add_process(resource=self.resModelProgram,
                                        files=files,
                                        user=self.user,
                                        extract_metadata=False)

        self.resModelProgram.metadata.create_element('Description',
                                                     abstract="test abstract")
        self.resModelProgram.metadata.create_element('Subject',
                                                     value="test subject")
        release_date = '2016-10-24T21:05:00.315907+00:00'
        self.resModelProgram.metadata.create_element(
            'MpMetadata',
            modelVersion='5.1.011',
            modelProgramLanguage='Fortran',
            modelOperatingSystem='Windows',
            modelReleaseDate=release_date,
            modelWebsite='http://www.hydroshare.org',
            modelCodeRepository='http://www.github.com',
            modelReleaseNotes='releaseNote.pdf',
            modelDocumentation='manual.pdf',
            modelSoftware='utilities.exe',
            modelEngine='sourceCode.zip')

        # there should one content file
        self.assertEqual(self.resModelProgram.files.all().count(), 1)

        # there should be one format element
        self.assertEqual(self.resModelProgram.metadata.formats.all().count(),
                         1)

        # the short path should just consist of the file name.
        self.assertEqual(self.resModelProgram.files.all()[0].short_path,
                         self.file_name)

        # delete content file that we added above; note that file name is a short_path
        hydroshare.delete_resource_file(self.resModelProgram.short_id,
                                        self.file_name, self.user)

        # there should no content file
        self.assertEqual(self.resModelProgram.files.all().count(), 0)

        # test the core metadata at this point
        self.assertNotEqual(self.resModelProgram.metadata.title, None)

        # there should be an abstract element
        self.assertNotEqual(self.resModelProgram.metadata.description, None)

        # there should be one creator element
        self.assertEqual(self.resModelProgram.metadata.creators.all().count(),
                         1)

        # testing extended metadata elements
        self.assertNotEqual(self.resModelProgram.metadata.program, None)
    def test_get_revisions(self):
        url = 'hsapi/revisions/{0}/'.format(self.res.short_id)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        self.assertEqual(len(res_revisions), 1)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id),
                         res_revisions)

        resource_changed_by = hydroshare.create_account(
            '*****@*****.**',
            username='******',
            first_name='User1_FirstName',
            last_name='User1_LastName')
        hydroshare.utils.resource_modified(self.res, resource_changed_by)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        self.assertEqual(len(res_revisions), 2)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id),
                         res_revisions)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp,
                            res_revisions[1].timestamp)

        # test that each resource revision has the same resource id
        for bags in res_revisions:
            self.assertEqual(self.res.id, bags.object_id)

        # add a file to the resource to generate another revision of the resource
        # create a file
        original_file_name = 'original.txt'
        original_file = open(original_file_name, 'w')
        original_file.write("original text")
        original_file.close()

        original_file = open(original_file_name, 'r')
        # add the file to the resource
        hydroshare.add_resource_files(self.res.short_id, original_file)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        # test that we now have 3 revisions
        self.assertEqual(len(res_revisions), 3)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id),
                         res_revisions)

        # test that each revision has a different time stamp
        self.assertNotEqual(res_revisions[0].timestamp,
                            res_revisions[1].timestamp)
        self.assertNotEqual(res_revisions[0].timestamp,
                            res_revisions[2].timestamp)
        self.assertNotEqual(res_revisions[1].timestamp,
                            res_revisions[2].timestamp)

        # delete the file in the resource to create another revision of the resource
        hydroshare.delete_resource_file(self.res.short_id, original_file_name)
        resp = self.api_client.get(url)
        res_revisions = self.deserialize(resp)

        self.assertValidJSONResponse(resp)
        self.assertEqual(hydroshare.get_revisions(self.res.short_id),
                         res_revisions)
        # test that we now have 4 revisions
        self.assertEqual(len(res_revisions), 4)
    def test_metadata_in_user_zone(self):
        super(TestGeoFeature, self).assert_federated_irods_available()
        # test metadata extraction with resource creation with file coming from user zone space
        resource_type = "GeographicFeatureResource"
        fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.valid_file_name)
        res_upload_files = []
        res_title = "test title"

        self.resGeoFeature = hydroshare.create_resource(
            resource_type=resource_type,
            owner=self.user,
            title=res_title,
            files=res_upload_files,
            source_names=[fed_test_file_full_path],
            fed_res_path='',
            move=False,
            metadata=[])

        # uploaded file validation and metadata extraction happens in post resource
        # creation handler
        hydroshare.utils.resource_post_create_actions(resource=self.resGeoFeature, user=self.user,
                                                      metadata=[])

        # check that the resource has 3 files
        self.assertEqual(self.resGeoFeature.files.count(), 3)

        # test extracted metadata

        # there should not be any resource level coverage
        self.assertEqual(self.resGeoFeature.metadata.coverages.count(), 0)
        self.assertNotEqual(self.resGeoFeature.metadata.geometryinformation, None)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.featureCount, 51)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.geometryType,
                         "MULTIPOLYGON")

        self.assertNotEqual(self.resGeoFeature.metadata.originalcoverage, None)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum,
                         'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name,
                         'unknown')
        self.assertGreater(len(self.resGeoFeature.metadata.originalcoverage.projection_string), 0)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'unknown')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.eastlimit, -66.9692712587578)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.northlimit, 71.406235393967)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.southlimit, 18.921786345087)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.westlimit,
                         -178.217598362366)

        # test that all files get deleted and extracted metadata gets deleted when one of the
        # required files get deleted
        shp_res_file = [f for f in self.resGeoFeature.files.all() if f.extension == '.shp'][0]
        hydroshare.delete_resource_file(self.resGeoFeature.short_id, shp_res_file.id,
                                        self.user)
        # check that the resource has no files
        self.assertEqual(self.resGeoFeature.files.count(), 0)

        # test metadata extraction with a valid file being added coming from user zone space
        res_add_files = []
        fed_test_add_file_full_path = '/{zone}/home/{username}/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, username=self.user.username,
            fname=self.valid_file_name2)

        hydroshare.utils.resource_file_add_process(resource=self.resGeoFeature,
                                                   files=res_add_files,
                                                   user=self.user,
                                                   source_names=[fed_test_add_file_full_path])

        # check that the resource has 15 files
        self.assertEqual(self.resGeoFeature.files.count(), 15)

        # test extracted metadata
        self.assertEqual(self.resGeoFeature.metadata.fieldinformations.all().count(), 7)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.featureCount, 87)
        self.assertEqual(self.resGeoFeature.metadata.geometryinformation.geometryType, "POLYGON")
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.datum, 'WGS_1984')
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.eastlimit -
                            3.4520493) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.northlimit -
                            45.0466382) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.southlimit -
                            42.5732416) < self.allowance)
        self.assertTrue(abs(self.resGeoFeature.metadata.originalcoverage.westlimit -
                            (-0.3263017)) < self.allowance)
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.unit, 'Degree')
        self.assertEqual(self.resGeoFeature.metadata.originalcoverage.projection_name,
                         'GCS_WGS_1984')

        self.resGeoFeature.delete()
Example #39
0
    def test_geofeature_pre_delete_file(self):
        # Example
        # ResourceFileObj.resource_file.file.name
        # '/tmp/tmp7rsGzV'
        # ResourceFileObj.resource_file.name
        # u'dab1f89d9b2a4082aae083c9d0937d15/data/contents/states.sbx'

        # test: del .shp file (all files will be removed)
        for f in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            f.resource_file.delete()
            f.delete()
        # add files first
        files = []
        target = 'hs_geographic_feature_resource/tests/gis.osm_adminareas_v06_with_folder.zip'
        files.append(UploadedFile(file=open(target, 'r'),
                                  name='gis.osm_adminareas_v06_with_folder.zip'))
        hydroshare.utils.resource_file_add_pre_process(self.resGeoFeature, files, self.user,)

        hydroshare.add_resource_files(self.resGeoFeature.short_id, *files)
        self.assertEqual(ResourceFile.objects.filter(object_id=self.resGeoFeature.id).count(), 5)

        for res_f_obj in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            del_f_fullname = res_f_obj.resource_file.name.lower()
            del_f_fullname = del_f_fullname[del_f_fullname.rfind('/')+1:]
            del_f_name, del_f_ext = os.path.splitext(del_f_fullname)
            if del_f_ext == ".shp":
                hydroshare.delete_resource_file(self.resGeoFeature.short_id,
                                                res_f_obj.id,
                                                self.user)
                self.assertEqual(ResourceFile.objects.filter
                                 (object_id=self.resGeoFeature.id).count(), 0)

        # test: del .prj file
        for f in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            f.resource_file.delete()
            f.delete()
        self.assertEqual(ResourceFile.objects.filter(object_id=self.resGeoFeature.id).count(), 0)

        files = []
        target = 'hs_geographic_feature_resource/tests/gis.osm_adminareas_v06_with_folder.zip'
        files.append(UploadedFile(file=open(target, 'r'),
                                  name='gis.osm_adminareas_v06_with_folder.zip'))
        hydroshare.utils.resource_file_add_pre_process(self.resGeoFeature, files, self.user,)
        hydroshare.add_resource_files(self.resGeoFeature.short_id, *files)
        self.assertEqual(ResourceFile.objects.filter(object_id=self.resGeoFeature.id).count(), 5)
        for res_f_obj in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            del_f_fullname = res_f_obj.resource_file.name.lower()
            del_f_fullname = del_f_fullname[del_f_fullname.rfind('/')+1:]
            del_f_name, del_f_ext = os.path.splitext(del_f_fullname)
            if del_f_ext == ".prj":
                hydroshare.delete_resource_file(self.resGeoFeature.short_id,
                                                res_f_obj.id, self.user)
                self.assertEqual(ResourceFile.objects.filter
                                 (object_id=self.resGeoFeature.id).count(), 4)
                for res_f_obj_2 in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
                    del_f_fullname = res_f_obj_2.resource_file.name.lower()
                    del_f_fullname = del_f_fullname[del_f_fullname.rfind('/')+1:]
                    del_f_name, del_f_ext = os.path.splitext(del_f_fullname)
                    self.assertNotEqual(del_f_ext, ".prj")
                    originalcoverage_obj = self.resGeoFeature.metadata.\
                        originalcoverage.all().first()
                    self.assertEqual(originalcoverage_obj.projection_string, UNKNOWN_STR)
                    self.assertEqual(self.resGeoFeature.metadata.coverages.all().count(), 0)

        # test: del .xml file
        for f in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            f.resource_file.delete()
            f.delete()
        self.assertEqual(ResourceFile.objects.filter(object_id=self.resGeoFeature.id).count(), 0)

        files = []
        target = 'hs_geographic_feature_resource/tests/states_shp_sample.zip'
        files.append(UploadedFile(file=open(target, 'r'), name='states_shp_sample.zip'))
        hydroshare.utils.resource_file_add_pre_process(self.resGeoFeature, files, self.user,)
        hydroshare.add_resource_files(self.resGeoFeature.short_id, *files)
        self.assertEqual(ResourceFile.objects.filter(object_id=self.resGeoFeature.id).count(), 7)
        for res_f_obj in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
            del_f_fullname = res_f_obj.resource_file.name.lower()
            del_f_fullname = del_f_fullname[del_f_fullname.rfind('/')+1:]
            del_f_name, del_f_ext = os.path.splitext(del_f_fullname)
            if del_f_ext == ".xml":
                hydroshare.delete_resource_file(self.resGeoFeature.short_id,
                                                res_f_obj.id, self.user)
                self.assertEqual(ResourceFile.objects.filter
                                 (object_id=self.resGeoFeature.id).count(), 6)
                for res_f_obj_2 in ResourceFile.objects.filter(object_id=self.resGeoFeature.id):
                    del_f_fullname = res_f_obj_2.resource_file.name.lower()
                    del_f_fullname = del_f_fullname[del_f_fullname.rfind('/')+1:]
                    del_f_name, del_f_ext = os.path.splitext(del_f_fullname)
                    self.assertNotEqual(del_f_ext, ".xml")