Ejemplo n.º 1
0
def copy_resource_task(ori_res_id, new_res_id=None, request_username=None):
    try:
        new_res = None
        if not new_res_id:
            new_res = create_empty_resource(ori_res_id,
                                            request_username,
                                            action='copy')
            new_res_id = new_res.short_id
        utils.copy_resource_files_and_AVUs(ori_res_id, new_res_id)
        ori_res = utils.get_resource_by_shortkey(ori_res_id)
        if not new_res:
            new_res = utils.get_resource_by_shortkey(new_res_id)
        utils.copy_and_create_metadata(ori_res, new_res)

        hs_identifier = ori_res.metadata.identifiers.all().filter(
            name="hydroShareIdentifier")[0]
        if hs_identifier:
            new_res.metadata.create_element('source',
                                            derived_from=hs_identifier.url)

        if ori_res.resource_type.lower() == "collectionresource":
            # clone contained_res list of original collection and add to new collection
            # note that new collection will not contain "deleted resources"
            new_res.resources = ori_res.resources.all()

        # create bag for the new resource
        create_bag(new_res)
        return new_res.get_absolute_url()
    except Exception as ex:
        if new_res:
            new_res.delete()
        raise utils.ResourceCopyException(str(ex))
Ejemplo n.º 2
0
    def test_copy(self):

        # create 1 SupportedResTypes obj with required params
        resource.create_metadata_element(
            self.resWebApp.short_id,
            'SupportedResTypes',
            supported_res_types=['NetcdfResource'])
        self.assertEqual(SupportedResTypes.objects.all().count(), 1)

        # set url launching pattern for aggregations
        metadata = [{
            'requesturlbaseaggregation': {
                'value': 'https://www.google.com?agg_path=${HS_AGG_PATH}'
            }
        }]
        self.resWebApp.metadata.update(metadata, self.user)
        resource.create_metadata_element(
            self.resWebApp.short_id,
            'SupportedAggTypes',
            supported_agg_types=['GeoRasterLogicalFile'])

        self.assertEqual(1, SupportedAggTypes.objects.all().count())

        # make a new copy of web app
        new_web_app = create_empty_resource(self.resWebApp.short_id,
                                            self.user,
                                            action='copy')

        new_web_app = copy_resource(self.resWebApp, new_web_app)

        # test the new copy is a web app
        self.assertTrue(isinstance(new_web_app, ToolResource))
        # test that added types are copied
        self.assertEqual(2, SupportedResTypes.objects.all().count())
        self.assertEqual(2, SupportedAggTypes.objects.all().count())
    def test_copy(self):
        # no contained resource
        self.assertEqual(self.resCollection.resources.count(), 0)

        # add 3 resources to collection
        self.resCollection.resources.add(self.resGen1)
        self.resCollection.resources.add(self.resGeoFeature)
        self.resCollection.resources.add(self.resCollection_with_missing_metadata)
        self.assertEqual(self.resCollection.resources.count(), 3)

        # make a new copy of collection
        new_collection = create_empty_resource(self.resCollection.short_id, self.user1,
                                               action='copy')

        new_collection = copy_resource(self.resCollection, new_collection)

        # test the new copy is a collection
        self.assertTrue(isinstance(new_collection, CollectionResource))

        # new copy collection should have same contained res as its original does
        self.assertEqual(new_collection.resources.count(), self.resCollection.resources.count())
        for contained_res in new_collection.resources.all():
            self.assertIn(contained_res, self.resCollection.resources.all())

        # changes to old collection should not affect new copied collection
        self.resCollection.resources.clear()
        self.assertEqual(self.resCollection.resources.count(), 0)
        self.assertEqual(new_collection.resources.count(), 3)
Ejemplo n.º 4
0
    def test_copy(self):

        # create 1 SupportedResTypes obj with required params
        resource.create_metadata_element(self.resWebApp.short_id, 'SupportedResTypes',
                                         supported_res_types=['NetcdfResource'])
        self.assertEqual(SupportedResTypes.objects.all().count(), 1)

        # set url launching pattern for aggregations
        metadata = [{'requesturlbaseaggregation': {
            'value': 'https://www.google.com?agg_path=${HS_AGG_PATH}'}}]
        self.resWebApp.metadata.update(metadata, self.user)
        resource.create_metadata_element(self.resWebApp.short_id, 'SupportedAggTypes',
                                         supported_agg_types=['GeoRasterLogicalFile'])

        self.assertEqual(1, SupportedAggTypes.objects.all().count())

        # make a new copy of web app
        new_web_app = create_empty_resource(self.resWebApp.short_id, self.user,
                                               action='copy')

        new_web_app = copy_resource(self.resWebApp, new_web_app)

        # test the new copy is a web app
        self.assertTrue(isinstance(new_web_app, ToolResource))
        # test that added types are copied
        self.assertEqual(2, SupportedResTypes.objects.all().count())
        self.assertEqual(2, SupportedAggTypes.objects.all().count())
Ejemplo n.º 5
0
    def setUp(self):
        super(TestHideOldVersions, self).setUp()
        self.group, _ = Group.objects.get_or_create(name='Hydroshare Author')

        # create a user who is the owner of the resource to be versioned
        self.owner = hydroshare.create_account('*****@*****.**',
                                               username='******',
                                               first_name='owner_firstname',
                                               last_name='owner_lastname',
                                               superuser=False,
                                               groups=[])
        # create a generic resource
        self.version0 = hydroshare.create_resource(
            resource_type='CompositeResource',
            owner=self.owner,
            title='Test Composite Resource')
        test_file1 = open('test1.txt', 'w')
        test_file1.write("Test text file in test1.txt")
        test_file1.close()
        test_file2 = open('test2.txt', 'w')
        test_file2.write("Test text file in test2.txt")
        test_file2.close()
        self.test_file1 = open('test1.txt', 'rb')
        self.test_file2 = open('test2.txt', 'rb')
        hydroshare.add_resource_files(self.version0.short_id, self.test_file1,
                                      self.test_file2)

        # make one version
        self.version1 = hydroshare.create_empty_resource(
            self.version0.short_id, self.owner)
        self.version1 = hydroshare.create_new_version_resource(
            self.version0, self.version1, self.owner)

        # and then make a version of that
        self.version2 = hydroshare.create_empty_resource(
            self.version1.short_id, self.owner)
        self.version2 = hydroshare.create_new_version_resource(
            self.version1, self.version2, self.owner)
Ejemplo n.º 6
0
    def test_new_version_generic_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        # add key/value metadata to original resource
        self.res_generic.extra_metadata = {'variable': 'temp', 'units': 'deg F'}
        self.res_generic.save()

        new_res_generic = hydroshare.create_empty_resource(self.res_generic.short_id,
                                                           self.owner)
        # test to make sure the new versioned empty resource has no content files
        self.assertEqual(new_res_generic.files.all().count(), 0)

        new_res_generic = hydroshare.create_new_version_resource(self.res_generic, new_res_generic,
                                                                 self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_generic, GenericResource))

        # test the new versioned resource has the correct content file with correct path copied over
        self.assertEqual(new_res_generic.files.all().count(), 2)
        # add each file of resource to list
        new_res_file_list = []
        for f in new_res_generic.files.all():
            new_res_file_list.append(f.resource_file.name)
        for f in self.res_generic.files.all():
            ori_res_no_id_file_path = f.resource_file.name[len(self.res_generic.short_id):]
            new_res_file_path = new_res_generic.short_id + ori_res_no_id_file_path
            self.assertIn(new_res_file_path, new_res_file_list,
                          msg='resource content path is not created correctly '
                              'for new versioned resource')

        # test key/value metadata copied over
        self.assertEqual(new_res_generic.extra_metadata, self.res_generic.extra_metadata)
        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertEqual(new_res_generic.metadata.title.value,
                         self.res_generic.metadata.title.value,
                         msg='metadata title is not copied over to the new versioned resource')
        self.assertEqual(new_res_generic.creator, self.owner,
                         msg='creator is not copied over to the new versioned resource')

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(
            new_res_generic.short_id,
            msg='Unique identifier has not been created for new versioned resource.')
        self.assertNotEqual(new_res_generic.short_id, self.res_generic.short_id)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_generic.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_generic.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_generic.short_id)
        self.assertIn(id_url, [id.url for id in new_res_generic.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_generic.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in new_res_generic.metadata.relations.all()],
                      msg="The original resource identifier is not set as value for isVersionOf "
                          "for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_generic.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value for "
                          "isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_generic.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_generic.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource after "
                             "its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_generic.short_id)
Ejemplo n.º 7
0
    def test_new_version_raster_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        new_res_raster = hydroshare.create_empty_resource(self.res_raster.short_id,
                                                          self.owner)
        new_res_raster = hydroshare.create_new_version_resource(self.res_raster, new_res_raster,
                                                                self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_raster, RasterResource))

        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertTrue(new_res_raster.metadata.title.value == self.res_raster.metadata.title.value)
        self.assertTrue(new_res_raster.creator == self.owner)

        # test extended metadata elements are copied from the original resource to the new
        # versioned resource
        self.assertTrue(OriginalCoverage.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        self.assertEqual(new_res_raster.metadata.originalCoverage.value,
                         self.res_raster.metadata.originalCoverage.value,
                         msg="OriginalCoverage of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(CellInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newcell = new_res_raster.metadata.cellInformation
        oldcell = self.res_raster.metadata.cellInformation
        self.assertEqual(newcell.rows, oldcell.rows,
                         msg="Rows of new versioned resource is not equal to that of "
                             "the original resource")
        self.assertEqual(newcell.columns, oldcell.columns,
                         msg="Columns of new versioned resource is not equal to that of the "
                             "original resource")
        self.assertEqual(newcell.cellSizeXValue, oldcell.cellSizeXValue,
                         msg="CellSizeXValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellSizeYValue, oldcell.cellSizeYValue,
                         msg="CellSizeYValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellDataType, oldcell.cellDataType,
                         msg="CellDataType of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(BandInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newband = new_res_raster.metadata.bandInformations.first()
        oldband = self.res_raster.metadata.bandInformations.first()
        self.assertEqual(newband.name, oldband.name,
                         msg="Band name of new versioned resource is not equal to that of "
                             "the original resource")

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(new_res_raster.short_id, msg='Unique identifier has not been '
                                                          'created for new versioned resource.')
        self.assertNotEqual(new_res_raster.short_id, self.res_raster.short_id)

        # test to make sure the new versioned resource has 2 content file
        # since an additional vrt file is created
        self.assertEqual(new_res_raster.files.all().count(), 2)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_raster.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_raster.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_raster.short_id)
        self.assertIn(id_url, [id.url for id in new_res_raster.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_raster.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_raster.short_id)
        self.assertIn(version_value, [rel.value for rel in new_res_raster.metadata.relations.all()],
                      msg="The original resource identifier is not set as value "
                          "for isVersionOf for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_raster.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_raster.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value "
                          "for isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_raster.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_raster.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource "
                             "after its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_raster.short_id)
Ejemplo n.º 8
0
    def test_resource_operations_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(TestUserZoneIRODSFederation,
                     self).is_federated_irods_available():
            return
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True)

        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(
            zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        self.assertFalse(
            self.irods_storage.exists(user_path + self.file_to_be_deleted))

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(),
                         2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(
            self.file_two in file_list,
            msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(
            ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        resource.delete_resource(new_res.short_id)
        resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id,
                                                   self.user,
                                                   action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        resource.delete_resource(new_res.short_id)
        resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[
                fed_file1_full_path, fed_file2_full_path, fed_file3_full_path
            ],
            move=False)
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(),
                         3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone')
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(res.short_id,
                                      source_names=[fed_test_file1_full_path],
                                      move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone
        hydroshare.replicate_resource_bag_to_user_zone(self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id +
                                                  '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
    def test_new_version_generic_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        # add key/value metadata to original resource
        self.res_generic.extra_metadata = {'variable': 'temp', 'units': 'deg F'}
        self.res_generic.save()

        # print("res_generic.files are:")
        # for f in self.res_generic.files.all():
        #     print(f.storage_path)

        new_res_generic = hydroshare.create_empty_resource(self.res_generic.short_id,
                                                           self.owner)
        # test to make sure the new versioned empty resource has no content files
        self.assertEqual(new_res_generic.files.all().count(), 0)

        new_res_generic = hydroshare.create_new_version_resource(self.res_generic, new_res_generic,
                                                                 self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_generic, GenericResource))

        # test the new versioned resource has the correct content file with correct path copied over

        # print("new_res_generic.files are:")
        # for f in new_res_generic.files.all():
        #     print(f.storage_path)

        self.assertEqual(new_res_generic.files.all().count(), 2)

        # add each file of resource to list
        new_res_file_list = []
        # TODO: revise for new file handling
        for f in new_res_generic.files.all():
            new_res_file_list.append(f.resource_file.name)
        for f in self.res_generic.files.all():
            ori_res_no_id_file_path = f.resource_file.name[len(self.res_generic.short_id):]
            new_res_file_path = new_res_generic.short_id + ori_res_no_id_file_path
            self.assertIn(new_res_file_path, new_res_file_list,
                          msg='resource content path is not created correctly '
                              'for new versioned resource')

        # test key/value metadata copied over
        self.assertEqual(new_res_generic.extra_metadata, self.res_generic.extra_metadata)
        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertEqual(new_res_generic.metadata.title.value,
                         self.res_generic.metadata.title.value,
                         msg='metadata title is not copied over to the new versioned resource')
        self.assertEqual(new_res_generic.creator, self.owner,
                         msg='creator is not copied over to the new versioned resource')

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(
            new_res_generic.short_id,
            msg='Unique identifier has not been created for new versioned resource.')
        self.assertNotEqual(new_res_generic.short_id, self.res_generic.short_id)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_generic.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_generic.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_generic.short_id)
        self.assertIn(id_url, [id.url for id in new_res_generic.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_generic.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in new_res_generic.metadata.relations.all()],
                      msg="The original resource identifier is not set as value for isVersionOf "
                          "for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_generic.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value for "
                          "isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_generic.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_generic.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource after "
                             "its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_generic.short_id)
    def test_new_version_raster_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        new_res_raster = hydroshare.create_empty_resource(self.res_raster.short_id,
                                                          self.owner)
        new_res_raster = hydroshare.create_new_version_resource(self.res_raster, new_res_raster,
                                                                self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_raster, RasterResource))

        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertTrue(new_res_raster.metadata.title.value == self.res_raster.metadata.title.value)
        self.assertTrue(new_res_raster.creator == self.owner)

        # test extended metadata elements are copied from the original resource to the new
        # versioned resource
        self.assertTrue(OriginalCoverage.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        self.assertEqual(new_res_raster.metadata.originalCoverage.value,
                         self.res_raster.metadata.originalCoverage.value,
                         msg="OriginalCoverage of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(CellInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newcell = new_res_raster.metadata.cellInformation
        oldcell = self.res_raster.metadata.cellInformation
        self.assertEqual(newcell.rows, oldcell.rows,
                         msg="Rows of new versioned resource is not equal to that of "
                             "the original resource")
        self.assertEqual(newcell.columns, oldcell.columns,
                         msg="Columns of new versioned resource is not equal to that of the "
                             "original resource")
        self.assertEqual(newcell.cellSizeXValue, oldcell.cellSizeXValue,
                         msg="CellSizeXValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellSizeYValue, oldcell.cellSizeYValue,
                         msg="CellSizeYValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellDataType, oldcell.cellDataType,
                         msg="CellDataType of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(BandInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newband = new_res_raster.metadata.bandInformations.first()
        oldband = self.res_raster.metadata.bandInformations.first()
        self.assertEqual(newband.name, oldband.name,
                         msg="Band name of new versioned resource is not equal to that of "
                             "the original resource")

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(new_res_raster.short_id, msg='Unique identifier has not been '
                                                          'created for new versioned resource.')
        self.assertNotEqual(new_res_raster.short_id, self.res_raster.short_id)

        # test to make sure the new versioned resource has 2 content file
        # since an additional vrt file is created
        self.assertEqual(new_res_raster.files.all().count(), 2)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_raster.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_raster.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_raster.short_id)
        self.assertIn(id_url, [id.url for id in new_res_raster.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_raster.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_raster.short_id)
        self.assertIn(version_value, [rel.value for rel in new_res_raster.metadata.relations.all()],
                      msg="The original resource identifier is not set as value "
                          "for isVersionOf for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_raster.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_raster.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value "
                          "for isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_raster.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_raster.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource "
                             "after its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_raster.short_id)
    def test_copy_composite_resource(self):
        """Test that logical file type objects gets copied along with the metadata that each
        logical file type object contains. Here we are not testing resource level metadata copy
        as that has been tested in separate unit tests"""

        self.raster_obj = open(self.temp_raster_file, 'r')
        files = [UploadedFile(file=self.raster_obj, name='cea.tif')]
        self.composite_resource = hydroshare.create_resource(
            resource_type='CompositeResource',
            owner=self.owner,
            title='Test Composite Resource',
            files=files,
            auto_aggregate=False
        )

        # run the resource post creation signal
        utils.resource_post_create_actions(resource=self.composite_resource, user=self.owner,
                                           metadata=self.composite_resource.metadata)

        self.assertEqual(self.composite_resource.files.all().count(), 1)
        res_file = self.composite_resource.files.first()

        # check that the resource file is not associated with file type
        self.assertEqual(res_file.has_logical_file, False)

        # set the tif file to GeoRasterFile type
        GeoRasterLogicalFile.set_file_type(self.composite_resource, self.owner, res_file.id)

        # ensure a nonowner who does not have permission to view a resource cannot copy it
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.composite_resource.short_id,
                                             self.nonowner,
                                             action='copy')
        # give nonowner view privilege so nonowner can create a new copy of this resource
        self.owner.uaccess.share_resource_with_user(self.composite_resource, self.nonowner,
                                                    PrivilegeCodes.VIEW)

        orig_res_file = self.composite_resource.files.first()
        orig_geo_raster_lfo = orig_res_file.logical_file

        # add some key value metadata
        orig_geo_raster_lfo.metadata.extra_metadata = {'key-1': 'value-1', 'key-2': 'value-2'}

        # create a copy of the composite resource
        new_composite_resource = hydroshare.create_empty_resource(self.composite_resource.short_id,
                                                                  self.nonowner,
                                                                  action='copy')
        new_composite_resource = hydroshare.copy_resource(self.composite_resource,
                                                          new_composite_resource)
        # check that there is 2 GeoRasterLogicalFile objects
        self.assertEqual(GeoRasterLogicalFile.objects.count(), 2)

        # compare the 2 GeoRasterLogicalFile objects from the original resource and the new one
        orig_res_file = self.composite_resource.files.first()
        orig_geo_raster_lfo = orig_res_file.logical_file
        copy_res_file = new_composite_resource.files.first()
        copy_geo_raster_lfo = copy_res_file.logical_file

        # check that we put the 2 files in a new folder (cea)
        for res_file in self.composite_resource.files.all():
            file_path, base_file_name = res_file.full_path, res_file.file_name
            expected_file_path = "{}/data/contents/cea/{}"
            expected_file_path = expected_file_path.format(self.composite_resource.root_path,
                                                           base_file_name)
            self.assertEqual(file_path, expected_file_path)

        for res_file in new_composite_resource.files.all():
            file_path, base_file_name = res_file.full_path, res_file.file_name
            expected_file_path = "{}/data/contents/cea/{}"
            expected_file_path = expected_file_path.format(new_composite_resource.root_path,
                                                           base_file_name)
            self.assertEqual(file_path, expected_file_path)

        # both logical file objects should have 2 resource files
        self.assertEqual(orig_geo_raster_lfo.files.count(), copy_geo_raster_lfo.files.count())
        self.assertEqual(orig_geo_raster_lfo.files.count(), 2)

        # both logical file objects should have same dataset_name
        self.assertEqual(orig_geo_raster_lfo.dataset_name, copy_geo_raster_lfo.dataset_name)
        # both should have same key/value metadata
        self.assertEqual(orig_geo_raster_lfo.metadata.extra_metadata,
                         copy_geo_raster_lfo.metadata.extra_metadata)

        # both logical file objects should have same coverage metadata
        self.assertEqual(orig_geo_raster_lfo.metadata.coverages.count(),
                         copy_geo_raster_lfo.metadata.coverages.count())

        self.assertEqual(orig_geo_raster_lfo.metadata.coverages.count(), 1)
        org_spatial_coverage = orig_geo_raster_lfo.metadata.spatial_coverage
        copy_spatial_coverage = copy_geo_raster_lfo.metadata.spatial_coverage
        self.assertEqual(org_spatial_coverage.type, copy_spatial_coverage.type)
        self.assertEqual(org_spatial_coverage.type, 'box')
        self.assertEqual(org_spatial_coverage.value['projection'],
                         copy_spatial_coverage.value['projection'])
        self.assertEqual(org_spatial_coverage.value['units'],
                         copy_spatial_coverage.value['units'])
        self.assertEqual(org_spatial_coverage.value['northlimit'],
                         copy_spatial_coverage.value['northlimit'])
        self.assertEqual(org_spatial_coverage.value['eastlimit'],
                         copy_spatial_coverage.value['eastlimit'])
        self.assertEqual(org_spatial_coverage.value['southlimit'],
                         copy_spatial_coverage.value['southlimit'])
        self.assertEqual(org_spatial_coverage.value['westlimit'],
                         copy_spatial_coverage.value['westlimit'])

        # both logical file objects should have same original coverage
        org_orig_coverage = orig_geo_raster_lfo.metadata.originalCoverage
        copy_orig_coverage = copy_geo_raster_lfo.metadata.originalCoverage
        self.assertEqual(org_orig_coverage.value['projection'],
                         copy_orig_coverage.value['projection'])
        self.assertEqual(org_orig_coverage.value['units'],
                         copy_orig_coverage.value['units'])
        self.assertEqual(org_orig_coverage.value['northlimit'],
                         copy_orig_coverage.value['northlimit'])
        self.assertEqual(org_orig_coverage.value['eastlimit'],
                         copy_orig_coverage.value['eastlimit'])
        self.assertEqual(org_orig_coverage.value['southlimit'],
                         copy_orig_coverage.value['southlimit'])
        self.assertEqual(org_orig_coverage.value['westlimit'],
                         copy_orig_coverage.value['westlimit'])

        # both logical file objects should have same cell information metadata
        orig_cell_info = orig_geo_raster_lfo.metadata.cellInformation
        copy_cell_info = copy_geo_raster_lfo.metadata.cellInformation
        self.assertEqual(orig_cell_info.rows, copy_cell_info.rows)
        self.assertEqual(orig_cell_info.columns, copy_cell_info.columns)
        self.assertEqual(orig_cell_info.cellSizeXValue, copy_cell_info.cellSizeXValue)
        self.assertEqual(orig_cell_info.cellSizeYValue, copy_cell_info.cellSizeYValue)
        self.assertEqual(orig_cell_info.cellDataType, copy_cell_info.cellDataType)

        # both logical file objects should have same band information metadata
        self.assertEqual(orig_geo_raster_lfo.metadata.bandInformations.count(), 1)
        self.assertEqual(orig_geo_raster_lfo.metadata.bandInformations.count(),
                         copy_geo_raster_lfo.metadata.bandInformations.count())
        orig_band_info = orig_geo_raster_lfo.metadata.bandInformations.first()
        copy_band_info = copy_geo_raster_lfo.metadata.bandInformations.first()
        self.assertEqual(orig_band_info.noDataValue, copy_band_info.noDataValue)
        self.assertEqual(orig_band_info.maximumValue, copy_band_info.maximumValue)
        self.assertEqual(orig_band_info.minimumValue, copy_band_info.minimumValue)

        # make sure to clean up all created resources to clean up iRODS storage
        if self.composite_resource:
            self.composite_resource.delete()
        if new_composite_resource:
            new_composite_resource.delete()
    def test_copy_raster_resource(self):
        # ensure a nonowner who does not have permission to view a resource cannot copy it
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id,
                                             self.nonowner,
                                             action='copy')
        # give nonowner view privilege so nonowner can create a new copy of this resource
        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.VIEW)

        new_res_raster = hydroshare.create_empty_resource(self.res_raster.short_id,
                                                          self.nonowner,
                                                          action='copy')
        new_res_raster = hydroshare.copy_resource(self.res_raster, new_res_raster)

        # test the new copied resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_raster, RasterResource))

        # test science metadata elements are copied from the original resource to the new copied
        # resource
        self.assertTrue(new_res_raster.metadata.title.value == self.res_raster.metadata.title.value)
        self.assertTrue(new_res_raster.creator == self.nonowner)

        # test extended metadata elements are copied from the original resource to the new
        # copied resource
        self.assertTrue(OriginalCoverage.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        self.assertEqual(new_res_raster.metadata.originalCoverage.value,
                         self.res_raster.metadata.originalCoverage.value,
                         msg="OriginalCoverage of new copied resource is not equal to "
                             "that of the original resource")

        self.assertTrue(CellInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newcell = new_res_raster.metadata.cellInformation
        oldcell = self.res_raster.metadata.cellInformation
        self.assertEqual(newcell.rows, oldcell.rows,
                         msg="Rows of new copied resource is not equal to that of "
                             "the original resource")
        self.assertEqual(newcell.columns, oldcell.columns,
                         msg="Columns of new copied resource is not equal to that of the "
                             "original resource")
        self.assertEqual(newcell.cellSizeXValue, oldcell.cellSizeXValue,
                         msg="CellSizeXValue of new copied resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellSizeYValue, oldcell.cellSizeYValue,
                         msg="CellSizeYValue of new copied resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellDataType, oldcell.cellDataType,
                         msg="CellDataType of new copied resource is not equal to "
                             "that of the original resource")

        self.assertTrue(BandInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newband = new_res_raster.metadata.bandInformations.first()
        oldband = self.res_raster.metadata.bandInformations.first()
        self.assertEqual(newband.name, oldband.name,
                         msg="Band name of new copied resource is not equal to that of "
                             "the original resource")

        # test to make sure a new unique identifier has been created for the new copied resource
        self.assertIsNotNone(new_res_raster.short_id, msg='Unique identifier has not been '
                                                          'created for new copied resource.')
        self.assertNotEqual(new_res_raster.short_id, self.res_raster.short_id)

        # test to make sure the new copied resource has 2 content file
        # since an additional vrt file is created
        self.assertEqual(new_res_raster.files.all().count(), 2)

        # test to make sure the new copied resource has the correct identifier
        self.assertEqual(new_res_raster.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_raster.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new copied resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_raster.short_id)
        self.assertIn(id_url, [id.url for id in new_res_raster.metadata.identifiers.all()],
                      msg="Identifier url was not found for new copied resource.")

        # test to make sure the new copied resource is linked with the original resource via
        # isDerivedFrom Source metadata element
        self.assertEqual(new_res_raster.metadata.sources.all().count(), 1,
                         msg="New copied resource does not has source element.")

        derived_from_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                     self.res_raster.short_id)
        self.assertIn(derived_from_value,
                      [src.derived_from for src in new_res_raster.metadata.sources.all()],
                      msg="The original resource identifier is not set in isDerivedFrom Source "
                          "metadata element of the new copied resource")
        # make sure to clean up resource so that irods storage can be cleaned up
        if new_res_raster:
            new_res_raster.delete()
    def test_copy_generic_resource(self):
        # ensure a nonowner who does not have permission to view a resource cannot copy it
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id,
                                             self.nonowner,
                                             action='copy')
        # ensure resource cannot be copied if the license does not allow derivation by a non-owner
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic_lic_nd.short_id,
                                             self.nonowner,
                                             action='copy')

        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic_lic_nc_nd.short_id,
                                             self.nonowner,
                                             action='copy')

        # add key/value metadata to original resource
        self.res_generic.extra_metadata = {'variable': 'temp', 'units': 'deg F'}
        self.res_generic.save()

        # give nonowner view privilege so nonowner can create a new copy of this resource
        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        new_res_generic = hydroshare.create_empty_resource(self.res_generic.short_id,
                                                           self.nonowner,
                                                           action='copy')
        # test to make sure the new copied empty resource has no content files
        self.assertEqual(new_res_generic.files.all().count(), 0)

        new_res_generic = hydroshare.copy_resource(self.res_generic, new_res_generic)

        # test the new copied resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_generic, GenericResource))

        # test the new copied resource has the correct content file with correct path copied over
        self.assertEqual(new_res_generic.files.all().count(), 2)
        # add each file of resource to list
        new_res_file_list = []
        for f in new_res_generic.files.all():
            new_res_file_list.append(f.resource_file.name)
        for f in self.res_generic.files.all():
            ori_res_no_id_file_path = f.resource_file.name[len(self.res_generic.short_id):]
            new_res_file_path = new_res_generic.short_id + ori_res_no_id_file_path
            self.assertIn(new_res_file_path, new_res_file_list,
                          msg='resource content path is not created correctly '
                              'for new copied resource')

        # test key/value metadata copied over
        self.assertEqual(new_res_generic.extra_metadata, self.res_generic.extra_metadata)
        # test science metadata elements are copied from the original resource to the new copied
        # resource
        self.assertEqual(new_res_generic.metadata.title.value,
                         self.res_generic.metadata.title.value,
                         msg='metadata title is not copied over to the new copied resource')
        self.assertEqual(new_res_generic.creator, self.nonowner,
                         msg='creator is not copied over to the new copied resource')

        # test to make sure a new unique identifier has been created for the new copied resource
        self.assertIsNotNone(
            new_res_generic.short_id,
            msg='Unique identifier has not been created for new copied resource.')
        self.assertNotEqual(new_res_generic.short_id, self.res_generic.short_id)

        # test to make sure the new copied resource has the correct identifier
        self.assertEqual(new_res_generic.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_generic.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new copied resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_generic.short_id)
        self.assertIn(id_url, [id.url for id in new_res_generic.metadata.identifiers.all()],
                      msg="Identifier url was not found for new copied resource.")

        # test to make sure the new copied resource is linked with the original resource via
        # isDerivedFrom Source metadata element
        self.assertGreater(new_res_generic.metadata.sources.all().count(), 0,
                           msg="New copied resource does not has source element.")

        derived_from_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                     self.res_generic.short_id)
        self.assertIn(derived_from_value,
                      [src.derived_from for src in new_res_generic.metadata.sources.all()],
                      msg="The original resource identifier is not set in isDerivedFrom Source "
                          "metadata element of the new copied resource")
        # make sure to clean up resource so that irods storage can be cleaned up
        if new_res_generic:
            new_res_generic.delete()
Ejemplo n.º 14
0
def create_new_version_resource_task(ori_res_id, username, new_res_id=None):
    """
    Task for creating a new version of a resource
    Args:
        ori_res_id: the original resource id that is to be versioned.
        new_res_id: the new versioned resource id from the original resource. If None, a
        new resource will be created.
        username: the requesting user's username
    Returns:
        the new versioned resource url as the payload
    """
    try:
        new_res = None
        if not new_res_id:
            new_res = create_empty_resource(ori_res_id, username)
            new_res_id = new_res.short_id
        utils.copy_resource_files_and_AVUs(ori_res_id, new_res_id)

        # copy metadata from source resource to target new-versioned resource except three elements
        ori_res = utils.get_resource_by_shortkey(ori_res_id)
        if not new_res:
            new_res = utils.get_resource_by_shortkey(new_res_id)
        utils.copy_and_create_metadata(ori_res, new_res)

        # add or update Relation element to link source and target resources
        hs_identifier = new_res.metadata.identifiers.all().filter(
            name="hydroShareIdentifier")[0]
        ori_res.metadata.create_element('relation',
                                        type='isReplacedBy',
                                        value=hs_identifier.url)

        if new_res.metadata.relations.all().filter(
                type='isVersionOf').exists():
            # the original resource is already a versioned resource, and its isVersionOf relation
            # element is copied over to this new version resource, needs to delete this element so
            # it can be created to link to its original resource correctly
            eid = new_res.metadata.relations.all().filter(
                type='isVersionOf').first().id
            new_res.metadata.delete_element('relation', eid)

        hs_identifier = ori_res.metadata.identifiers.all().filter(
            name="hydroShareIdentifier")[0]
        new_res.metadata.create_element('relation',
                                        type='isVersionOf',
                                        value=hs_identifier.url)

        if ori_res.resource_type.lower() == "collectionresource":
            # clone contained_res list of original collection and add to new collection
            # note that new version collection will not contain "deleted resources"
            new_res.resources = ori_res.resources.all()

        # create bag for the new resource
        create_bag(new_res)

        # since an isReplaceBy relation element is added to original resource, needs to call
        # resource_modified() for original resource
        utils.resource_modified(ori_res, by_user=username, overwrite_bag=False)
        # if everything goes well up to this point, set original resource to be immutable so that
        # obsoleted resources cannot be modified from REST API
        ori_res.raccess.immutable = True
        ori_res.raccess.save()
        ori_res.save()
        return new_res.get_absolute_url()
    except Exception as ex:
        if new_res:
            new_res.delete()
        raise utils.ResourceVersioningException(str(ex))
    finally:
        # release the lock regardless
        ori_res.locked_time = None
        ori_res.save()
    def test_resource_operations_in_user_zone(self):
        super(TestUserZoneIRODSFederation,
              self).assert_federated_irods_available()
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True)

        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(
            zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        file_path_name = user_path + self.file_to_be_deleted
        self.assertFalse(self.irods_storage.exists(file_path_name))

        # test django_irods CopyFiles() with an iRODS resource name being passed in
        # as input parameter to verify the file gets copied to the pass-in iRODS resource
        istorage = res.get_irods_storage()
        src_path = os.path.join(res.root_path, 'data', 'contents',
                                self.file_to_be_deleted)
        dest_path = file_path_name
        istorage.copyFiles(src_path, dest_path,
                           settings.HS_IRODS_LOCAL_ZONE_DEF_RES)
        # assert file did get copied over
        self.assertTrue(self.irods_storage.exists(file_path_name))
        stdout = self.irods_storage.session.run("ils", None, "-l",
                                                file_path_name)[0].split()
        # assert copied file gets written to the iRODS resource being passed into copyFiles() call
        self.assertEqual(stdout[2], settings.HS_IRODS_LOCAL_ZONE_DEF_RES)

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(),
                         2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(
            self.file_two in file_list,
            msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(
            ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id,
                                                   self.user,
                                                   action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[
                fed_file1_full_path, fed_file2_full_path, fed_file3_full_path
            ],
            move=False)
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(),
                         3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        hydroshare.resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone')
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(res.short_id,
                                      source_names=[fed_test_file1_full_path],
                                      move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone even if the bag_modified AVU for this
        # resource is wrongly set to False when the bag for this resource does not exist and
        # need to be recreated
        res.setAVU('bag_modified', 'false')
        hydroshare.resource.replicate_resource_bag_to_user_zone(
            self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id +
                                                  '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
    def test_resource_operations_in_user_zone(self):
        super(TestUserZoneIRODSFederation, self).assert_federated_irods_available()
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True
        )

        self.assertEqual(res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        file_path_name = user_path + self.file_to_be_deleted
        self.assertFalse(self.irods_storage.exists(file_path_name))

        # test django_irods CopyFiles() with an iRODS resource name being passed in
        # as input parameter to verify the file gets copied to the pass-in iRODS resource
        istorage = res.get_irods_storage()
        src_path = os.path.join(res.root_path, 'data', 'contents', self.file_to_be_deleted)
        dest_path = file_path_name
        istorage.copyFiles(src_path, dest_path, settings.HS_IRODS_LOCAL_ZONE_DEF_RES)
        # assert file did get copied over
        self.assertTrue(self.irods_storage.exists(file_path_name))
        stdout = self.irods_storage.session.run("ils", None, "-l", file_path_name)[0].split()
        # assert copied file gets written to the iRODS resource being passed into copyFiles() call
        self.assertEqual(stdout[2], settings.HS_IRODS_LOCAL_ZONE_DEF_RES)

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(), 0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(), 2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(self.file_one in file_list,
                        msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(self.file_two in file_list,
                        msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(), 0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False
        )
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path, new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(), new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False
        )
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user, action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path, new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(), new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_file1_full_path, fed_file2_full_path, fed_file3_full_path],
            move=False
        )
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(), 3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        hydroshare.resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone'
        )
        self.assertEqual(res.files.all().count(), 0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path],
            move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(self.file_one in file_list,
                        msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone even if the bag_modified AVU for this
        # resource is wrongly set to False when the bag for this resource does not exist and
        # need to be recreated
        res.setAVU('bag_modified', 'false')
        hydroshare.resource.replicate_resource_bag_to_user_zone(self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id + '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(), 0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))