def test_unfederated_folder_path_setting(self): """ an unfederated file in a subfolder has the proper state after state changes """ # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") ResourceFile.create_folder(self.res, 'foo') # add one file to the resource hydroshare.add_resource_files(self.res.short_id, self.test_file_1, folder='foo') # resource should has only one file at this point self.assertEqual(self.res.files.all().count(), 1, msg="resource file count didn't match") # get the handle of the file created above resfile = self.res.files.all()[0] # determine where that file should live shortpath = os.path.join(self.res.short_id, "data", "contents", "foo", "file1.txt") self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) self.assertTrue(resfile.path_is_acceptable(shortpath)) # non-existent files should raise error otherpath = os.path.join(self.res.short_id, "data", "contents", "foo", "file2.txt") with self.assertRaises(ValidationError): resfile.path_is_acceptable(otherpath) # try setting to an unqualified name; should qualify it resfile.set_storage_path("foo/file1.txt") # should match computed path self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to what it is already resfile.set_storage_path(shortpath) # should match computed path self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to a good path to a non-existent object with self.assertRaises(ValidationError): resfile.set_storage_path(otherpath) # should not change self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) # TODO: how to eliminate this particular error. # dumbpath = 'x' + shortpath # dumbpath = self.res.short_id + "file1.txt" # clean up after folder test # ResourceFile.remove_folder(self.res, 'foo', self.user) # delete resources to clean up hydroshare.delete_resource(self.res.short_id)
def test_set_resource_flag_make_not_shareable(self): # here we are testing the set_resource_flag view function to make a resource not shareable # test that the resource is shareable self.assertEqual(self.gen_res_one.raccess.shareable, True) url_params = {'shortkey': self.gen_res_one.short_id} post_data = {'flag': 'make_not_shareable'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_one.short_id) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) flag_messages = get_messages(request) err_messages = [m for m in flag_messages if m.tags == 'error'] self.assertEqual(len(err_messages), 0) # check that the resource is not shareable now self.gen_res_one.raccess.refresh_from_db() self.assertEqual(self.gen_res_one.raccess.shareable, False) # clean up hydroshare.delete_resource(self.gen_res_one.short_id) hydroshare.delete_resource(self.gen_res_two.short_id)
def test_share_resource_with_user_bad_requests(self): # here we are testing the share_resource_with_user view function with bad requests bad_privilege = 'bad' url_params = {'shortkey': self.gen_res.short_id, 'privilege': bad_privilege, 'user_id': self.user.id} url = reverse('share_resource_with_user', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.owner # make it a ajax request request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' response = share_resource_with_user(request, shortkey=self.gen_res.short_id, privilege=bad_privilege, user_id=self.user.id) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) response_data = json.loads(response.content) self.assertEqual(response_data['status'], 'error') url_params = {'shortkey': self.gen_res.short_id, 'privilege': 'view', 'user_id': self.user.id} url = reverse('share_resource_with_user', kwargs=url_params) request = self.factory.post(url, data={}) # user does not have permission to grant himself access to resource request.user = self.user # make it a ajax request request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' with self.assertRaises(PermissionDenied): share_resource_with_user(request, shortkey=self.gen_res.short_id, privilege='view', user_id=self.user.id) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_create_resource_with_invalid_file(self): # here we are testing the create_resource view function self.assertEqual(BaseResource.objects.count(), 0) self.assertEqual(ResourceFile.objects.count(), 0) # test with bad sqlite file - this file should not be uploaded post_data = { 'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true', 'files': (self.odm2_sqlite_invalid_file_name, open(self.odm2_sqlite_invalid_file)) } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'error') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) # that bad sqlite file was not uploaded self.assertEqual(ResourceFile.objects.count(), 0) hydroshare.delete_resource(res_id)
def test_set_resource_flag_make_shareable(self): # here we are testing the set_resource_flag view function to make a resource shareable # test that the resource is shareable self.assertEqual(self.gen_res_one.raccess.shareable, True) # set it not shareable self.gen_res_one.raccess.shareable = False self.gen_res_one.raccess.save() url_params = {'shortkey': self.gen_res_one.short_id} post_data = {'flag': 'make_shareable'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_one.short_id) response_data = json.loads(response.content) self.assertEqual(response_data['status'], 'success') # check that the resource is shareable now self.gen_res_one.raccess.refresh_from_db() self.assertEqual(self.gen_res_one.raccess.shareable, True) # clean up hydroshare.delete_resource(self.gen_res_one.short_id) hydroshare.delete_resource(self.gen_res_two.short_id)
def test_file_metadata_on_resource_delete(self): # test that when the composite resource is deleted # all metadata associated with NetCDFFileType is deleted self.netcdf_file_obj = open(self.netcdf_file, 'r') self._create_composite_resource() res_file = self.composite_resource.files.first() # extract metadata from the tif file NetCDFLogicalFile.set_file_type(self.composite_resource, res_file.id, self.user) # test that we have one logical file of type NetCDFLogicalFile as a result # of metadata extraction self.assertEqual(NetCDFLogicalFile.objects.count(), 1) self.assertEqual(NetCDFFileMetaData.objects.count(), 1) # test that we have the metadata elements # 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 resource hydroshare.delete_resource(self.composite_resource.short_id) # test that we have no logical file of type NetCDFLogicalFile self.assertEqual(NetCDFLogicalFile.objects.count(), 0) self.assertEqual(NetCDFFileMetaData.objects.count(), 0) # test that all metadata deleted self.assertEqual(Coverage.objects.count(), 0) self.assertEqual(OriginalCoverage.objects.count(), 0) self.assertEqual(Variable.objects.count(), 0)
def test_create_resource_with_invalid_file(self): # here we are testing the create_resource view function self.assertEqual(BaseResource.objects.count(), 0) self.assertEqual(ResourceFile.objects.count(), 0) # test with bad sqlite file - this file should not be uploaded post_data = {'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true', 'files': (self.odm2_sqlite_invalid_file_name, open(self.odm2_sqlite_invalid_file)) } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'error') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) # that bad sqlite file was not uploaded self.assertEqual(ResourceFile.objects.count(), 0) hydroshare.delete_resource(res_id)
def test_share_resource_with_user(self): # here we are testing the share_resource_with_user view function # test share resource with self.user with view permission # test self.user has no view permission self.assertNotIn(self.user, self.gen_res.raccess.view_users) self._check_share_with_user(privilege='view') # test self.user has now view permission self.assertIn(self.user, self.gen_res.raccess.view_users) # test share resource with self.user with edit permission # test self.user has no edit permission self.assertNotIn(self.user, self.gen_res.raccess.edit_users) self._check_share_with_user(privilege='edit') # test self.user has now edit permission self.assertIn(self.user, self.gen_res.raccess.edit_users) # test share resource with self.user with owner permission # test self.user has no owner permission self.assertNotIn(self.user, self.gen_res.raccess.owners) self._check_share_with_user(privilege='owner') # test self.user has now owner permission self.assertIn(self.user, self.gen_res.raccess.owners) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_unshare_resource_with_group(self): # here we are testing the unshare_resource_with_group view function # test unshare resource with self.test_group # test self.test_group has no view permission self.assertNotIn(self.test_group, self.gen_res.raccess.view_groups) # grant view access to self.test_group self.owner.uaccess.share_resource_with_group(self.gen_res, self.test_group, PrivilegeCodes.VIEW) # test self.test_group has now view permission self.gen_res.raccess.refresh_from_db() self.assertIn(self.test_group, self.gen_res.raccess.view_groups) self._check_unshare_with_group() # test self.test_group has no view permission self.assertNotIn(self.test_group, self.gen_res.raccess.view_groups) # grant edit access to test_group self.owner.uaccess.share_resource_with_group(self.gen_res, self.test_group, PrivilegeCodes.CHANGE) # test test_group has now edit permission self.gen_res.raccess.refresh_from_db() self.assertIn(self.test_group, self.gen_res.raccess.edit_groups) self._check_unshare_with_group() # test test_group has no edit permission self.assertNotIn(self.test_group, self.gen_res.raccess.edit_groups) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_01_delete_resource(self): """Delete works for resources: privileges are deleted with resource""" verdi = self.verdi dog = self.dog self.assertTrue(dog.uaccess.can_delete_resource(verdi)) hydroshare.delete_resource(verdi.short_id) self.assertFalse(dog.uaccess.can_delete_resource(verdi))
def test_file_metadata_on_resource_delete(self): # test that when the composite resource is deleted # all metadata associated with GeoFeatureLogicalFile Type is deleted self.create_composite_resource(self.states_required_zip_file) res_file = self.composite_resource.files.first() # extract metadata from the tif file GeoFeatureLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # test that we have one logical file of type GeoFeatureLogicalFile as a result # of metadata extraction self.assertEqual(GeoFeatureLogicalFile.objects.count(), 1) self.assertEqual(GeoFeatureFileMetaData.objects.count(), 1) # test that we have the metadata elements # there should be no Coverage objects self.assertEqual(Coverage.objects.count(), 0) self.assertEqual(GeometryInformation.objects.count(), 1) self.assertEqual(OriginalCoverage.objects.count(), 1) self.assertEqual(FieldInformation.objects.count(), 5) # delete resource hydroshare.delete_resource(self.composite_resource.short_id) # test that we have no logical file of type GeoFeatureLogicalFileType self.assertEqual(GeoFeatureLogicalFile.objects.count(), 0) self.assertEqual(GeoFeatureFileMetaData.objects.count(), 0) # test that all metadata deleted self.assertEqual(Coverage.objects.count(), 0) self.assertEqual(GeometryInformation.objects.count(), 0) self.assertEqual(OriginalCoverage.objects.count(), 0) self.assertEqual(FieldInformation.objects.count(), 0)
def test_aggregation_metadata_on_resource_delete(self): # test that when the composite resource is deleted # all metadata associated with NetCDFLogicalFile Type is deleted 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 as a result # of metadata extraction self.assertEqual(NetCDFLogicalFile.objects.count(), 1) self.assertEqual(NetCDFFileMetaData.objects.count(), 1) # test that we have the metadata elements # 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 resource hydroshare.delete_resource(self.composite_resource.short_id) # test that we have no logical file of type NetCDFLogicalFile self.assertEqual(NetCDFLogicalFile.objects.count(), 0) self.assertEqual(NetCDFFileMetaData.objects.count(), 0) # test that all metadata deleted self.assertEqual(Coverage.objects.count(), 0) self.assertEqual(OriginalCoverage.objects.count(), 0) self.assertEqual(Variable.objects.count(), 0)
def test_delete_file(self): # here we are testing delete_file 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) add_files_to_resource(request, shortkey=self.gen_res.short_id) res_file = self.gen_res.files.first() # test the delete_file view function url_params = {'shortkey': self.gen_res.short_id, 'f': res_file.id} url = reverse('delete_file', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user request.META['HTTP_REFERER'] = 'some-url' self.set_request_message_attributes(request) self.add_session_to_request(request) response = delete_file(request, shortkey=self.gen_res.short_id, f=res_file.id) self.assertEqual(response.status_code, status.HTTP_302_FOUND) self.assertEqual(response['Location'], request.META['HTTP_REFERER']) self.assertEqual(self.gen_res.files.count(), 0) hydroshare.delete_resource(self.gen_res.short_id)
def test_09_retract(self): """Retracted resources cannot be accessed""" chewies = self.chewies resource_short_id = chewies.short_id hydroshare.delete_resource(chewies.short_id) with self.assertRaises(Http404): hydroshare.get_resource(resource_short_id)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = { 'resource-type': 'CompositeResource', 'title': 'Test Composite Resource Creation', 'irods_federated': 'false' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload # at this point there should not be any resource file self.assertEqual(ResourceFile.objects.count(), 0) # at this point there should not be any generic logical file self.assertEqual(GenericLogicalFile.objects.count(), 0) post_data = { 'resource-type': 'CompositeResource', 'title': 'Test Composite Resource Creation', 'irods_federated': 'false', 'files': (self.txt_file_name, open(self.txt_file_path), 'text/plain') } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) # at this point there should 1 resource file self.assertEqual(ResourceFile.objects.count(), 1) # at this point there should not be any generic logical file as we not more # by default making any uploaded file as part of generic logical file self.assertEqual(GenericLogicalFile.objects.count(), 0) hydroshare.delete_resource(res_id)
def delete(self, request, pk): # only resource owners are allowed to delete view_utils.authorize( request, pk, needed_permission=ACTION_TO_AUTHORIZE.DELETE_RESOURCE) hydroshare.delete_resource(pk) # spec says we need return the id of the resource that got deleted - otherwise would # have used status code 204 and not 200 return Response(data={'resource_id': pk}, status=status.HTTP_200_OK)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = { 'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload self.assertEqual(ResourceFile.objects.count(), 0) post_data = { 'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true', 'files': (self.odm2_sqlite_file_name, open(self.odm2_sqlite_file)) } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) self.assertEqual(ResourceFile.objects.count(), 1) ts_resource = BaseResource.objects.filter(short_id=res_id).first() # check that the resource title got updated due to metadata extraction as part of resource # creation self.assertEqual( ts_resource.metadata.title.value, "Water temperature data from the Little Bear River, UT") hydroshare.delete_resource(res_id)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = {'resource-type': 'CompositeResource', 'title': 'Test Composite Resource Creation', 'irods_federated': 'false' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload # at this point there should not be any resource file self.assertEqual(ResourceFile.objects.count(), 0) # at this point there should not be any generic logical file self.assertEqual(GenericLogicalFile.objects.count(), 0) post_data = {'resource-type': 'CompositeResource', 'title': 'Test Composite Resource Creation', 'irods_federated': 'false', 'files': (self.txt_file_name, open(self.txt_file_path), 'text/plain') } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) # at this point there should 1 resource file self.assertEqual(ResourceFile.objects.count(), 1) # at this point there should not be any generic logical file as we not more # by default making any uploaded file as part of generic logical file self.assertEqual(GenericLogicalFile.objects.count(), 0) hydroshare.delete_resource(res_id)
def test_delete_author(self): # testing the delete_author view function # add an author element and then delete it # the resource should have only the original author now self.assertEqual(self.gen_res.metadata.creators.count(), 1) url_params = { 'shortkey': self.gen_res.short_id, 'element_name': 'creator' } post_data = {'name': 'Smith, John', 'email': '*****@*****.**'} url = reverse('add_metadata_element', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user # make it an ajax request request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' self.set_request_message_attributes(request) response = add_metadata_element(request, shortkey=self.gen_res.short_id, element_name='creator') self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content.decode()) self.assertEqual(response_dict['status'], 'success') self.assertEqual(response_dict['element_name'], 'creator') self.gen_res.refresh_from_db() # there should be two authors now self.assertEqual(self.gen_res.metadata.creators.count(), 2) # delete the author we added above author = self.gen_res.metadata.creators.all()[1] url_params = { 'shortkey': self.gen_res.short_id, 'element_id': author.id } url = reverse('delete_author', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user request.META['HTTP_REFERER'] = 'some-url' self.set_request_message_attributes(request) self.add_session_to_request(request) response = delete_author(request, shortkey=self.gen_res.short_id, element_id=author.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.gen_res.refresh_from_db() # there should be only the original author now self.assertEqual(self.gen_res.metadata.creators.count(), 1) hydroshare.delete_resource(self.gen_res.short_id)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = { 'resource-type': 'GenericResource', 'title': 'Test Generic Resource Creation', 'irods_federated': 'true' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload self.assertEqual(ResourceFile.objects.count(), 0) post_data = { 'resource-type': 'GenericResource', 'title': 'Test Generic Resource Creation', 'irods_federated': 'true', 'files': (self.txt_file_name, open(self.txt_file_path), 'text/plain') } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual( BaseResource.objects.filter(short_id=res_id).exists(), True) self.assertEqual(ResourceFile.objects.count(), 1) hydroshare.delete_resource(res_id)
def test_change_quota_holder(self): # here we are testing the change_quota_holder view function url_params = {'shortkey': self.res.short_id} url = reverse('change_quota_holder', kwargs=url_params) request = self.factory.post(url, data={'new_holder_username': '******'}) request.user = self.user1 self.add_session_to_request(request) response = change_quota_holder(request, shortkey=self.res.short_id) self.assertTrue(self.res.get_quota_holder() == self.user2) self.assertEqual(response.status_code, status.HTTP_302_FOUND) # clean up hydroshare.delete_resource(self.res.short_id)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = {'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload self.assertEqual(ResourceFile.objects.count(), 0) post_data = {'resource-type': 'TimeSeriesResource', 'title': 'Test Time Series Resource Creation', 'irods_federated': 'true', 'files': (self.odm2_sqlite_file_name, open(self.odm2_sqlite_file)) } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) self.assertEqual(ResourceFile.objects.count(), 1) ts_resource = BaseResource.objects.filter(short_id=res_id).first() # check that the resource title got updated due to metadata extraction as part of resource # creation self.assertEqual(ts_resource.metadata.title.value, "Water temperature data from the Little Bear River, UT") hydroshare.delete_resource(res_id)
def test_set_owner(self): res = hydroshare.create_resource('GenericResource', self.user, 'res1') user2 = hydroshare.create_account('*****@*****.**', username='******', first_name='User2_FirstName', last_name='User2_LastName', password='******') post_data = {'user': user2.pk, 'api_key': self.user.api_key.key} url = '{0}{1}/'.format(self.url_base, res.short_id) resp = self.api_client.put(url, data=post_data) self.assertEqual(resp.body, res.short_id) hydroshare.delete_resource(res.short_id)
def test_set_resource_flag_make_public(self): # here we are testing the set_resource_flag view function to make a resource public # test that trying set the resource flag to public when there is no content file # or required metadata it should not change the resource flag # test that the resource is not public self.assertEqual(self.gen_res_one.raccess.public, False) url_params = {'shortkey': self.gen_res_one.short_id} post_data = {'flag': 'make_public'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) set_resource_flag(request, shortkey=self.gen_res_one.short_id) # check that the resource is still not public self.gen_res_one.raccess.refresh_from_db() self.assertEqual(self.gen_res_one.raccess.public, False) flag_messages = get_messages(request) err_messages = [m for m in flag_messages if m.tags == 'error'] self.assertNotEqual(len(err_messages), 0) # setting flag to public for 2nd resource should succeed # test that the resource is not public self.assertEqual(self.gen_res_two.raccess.public, False) url_params = {'shortkey': self.gen_res_two.short_id} post_data = {'flag': 'make_public'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_two.short_id) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) flag_messages = get_messages(request) err_messages = [m for m in flag_messages if m.tags == 'error'] self.assertEqual(len(err_messages), 0) # check that the resource is public now self.gen_res_two.raccess.refresh_from_db() self.assertEqual(self.gen_res_two.raccess.public, True) # clean up hydroshare.delete_resource(self.gen_res_one.short_id) hydroshare.delete_resource(self.gen_res_two.short_id)
def test_resource_file_folder_oprs(self): # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") # add the three files to the resource hydroshare.add_resource_files(self.res.short_id, self.test_file_1, self.test_file_2, self.test_file_3) # resource should has only three files at this point self.assertEqual(self.res.files.all().count(), 3, msg="resource file count didn't match") super(TestResourceFileFolderOprsAPI, self).resource_file_oprs() # delete resources to clean up hydroshare.delete_resource(self.res.short_id)
def test_set_owner(self): res = hydroshare.create_resource("GenericResource", self.user, "res1") user2 = hydroshare.create_account( "*****@*****.**", username="******", first_name="User2_FirstName", last_name="User2_LastName", password="******", ) post_data = {"user": user2.pk, "api_key": self.user.api_key.key} url = "{0}{1}/".format(self.url_base, res.short_id) resp = self.api_client.put(url, data=post_data) self.assertEqual(resp.body, res.short_id) hydroshare.delete_resource(res.short_id)
def test_change_quota_holder(self): # here we are testing the change_quota_holder view function url_params = {'shortkey': self.res.short_id} url = reverse('change_quota_holder', kwargs=url_params) request = self.factory.post(url, data={'new_holder_username': '******'}) request.user = self.user1 self.add_session_to_request(request) response = change_quota_holder(request, shortkey=self.res.short_id) response_data = json.loads(response.content.decode()) self.assertTrue(self.res.get_quota_holder() == self.user2) self.assertEqual(response_data['status'], 'success') # clean up hydroshare.delete_resource(self.res.short_id)
def test_replicate_bag(self): # here we are testing rep_res_bag_to_irods_user_zone view function super(TestReplicateBagToUserZone, self).assert_federated_irods_available() url_params = {'shortkey': self.gen_res.short_id} url = reverse('replicate_bag_user_zone', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user # make it a ajax request request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' response = rep_res_bag_to_irods_user_zone(request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = json.loads(response.content) self.assertIn('success', response_data) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_set_resource_flag_make_discoverable(self): # here we are testing the set_resource_flag view function to make a resource discoverable # test that trying set the resource discoverable when there is no content file # or required metadata it should not make the resource discoverable # test that the resource is not discoverable self.assertEqual(self.gen_res_one.raccess.discoverable, False) url_params = {'shortkey': self.gen_res_one.short_id} post_data = {'flag': 'make_discoverable'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_one.short_id) # check that the resource is still not discoverable self.gen_res_one.raccess.refresh_from_db() self.assertEqual(self.gen_res_one.raccess.discoverable, False) response_data = json.loads(response.content.decode()) self.assertEqual(response_data['status'], 'error') # setting flag to discoverable for 2nd resource should succeed # test that the resource is not discoverable self.assertEqual(self.gen_res_two.raccess.discoverable, False) url_params = {'shortkey': self.gen_res_two.short_id} post_data = {'flag': 'make_discoverable'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_two.short_id) response_data = json.loads(response.content.decode()) self.assertEqual(response_data['status'], 'success') # check that the resource is discoverable now self.gen_res_two.raccess.refresh_from_db() self.assertEqual(self.gen_res_two.raccess.discoverable, True) # clean up hydroshare.delete_resource(self.gen_res_one.short_id) hydroshare.delete_resource(self.gen_res_two.short_id)
def test_update_key_value_metadata(self): # here we are testing the update_key_value_metadata view function # no key/value metadata at this point self.assertEqual(self.gen_res.extra_metadata, {}) # create key/value metadata url_params = {'shortkey': self.gen_res.short_id} post_data = {'key1': 'key-1', 'value1': 'value-1'} url = reverse('update_key_value_metadata', 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 = update_key_value_metadata(request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content.decode()) self.assertEqual(response_dict['status'], 'success') self.gen_res.refresh_from_db() self.assertEqual(self.gen_res.extra_metadata, post_data) # update key/value metadata url_params = {'shortkey': self.gen_res.short_id} post_data = { 'key1A': 'key-1A', 'value1': 'value-1', 'key1B': 'key-1B', 'value1B': 'value-1B' } url = reverse('update_key_value_metadata', 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 = update_key_value_metadata(request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content.decode()) self.assertEqual(response_dict['status'], 'success') self.gen_res.refresh_from_db() self.assertEqual(self.gen_res.extra_metadata, post_data) hydroshare.delete_resource(self.gen_res.short_id)
def test_create_resource(self): # here we are testing the create_resource view function # test with no file upload post_data = {'resource-type': 'GenericResource', 'title': 'Test Generic Resource Creation', 'irods_federated': 'true' } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) hydroshare.delete_resource(res_id) self.assertEqual(BaseResource.objects.count(), 0) # test with file upload self.assertEqual(ResourceFile.objects.count(), 0) post_data = {'resource-type': 'GenericResource', 'title': 'Test Generic Resource Creation', 'irods_federated': 'true', 'files': (self.txt_file_name, open(self.txt_file_path), 'text/plain') } url = reverse('create_resource') request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = create_resource(request) self.assertEqual(response.status_code, status.HTTP_200_OK) json_content = json.loads(response.content) self.assertEqual(json_content['status'], 'success') self.assertEqual(json_content['file_upload_status'], 'success') res_id = json_content['resource_url'].split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) self.assertEqual(ResourceFile.objects.count(), 1) hydroshare.delete_resource(res_id)
def test_set_resource_flag_make_public(self): # here we are testing the set_resource_flag view function to make a resource public # test that trying set the resource flag to public when there is no content file # or required metadata it should not change the resource flag # test that the resource is not public self.assertEqual(self.gen_res_one.raccess.public, False) url_params = {'shortkey': self.gen_res_one.short_id} post_data = {'flag': 'make_public'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_one.short_id) # check that the resource is still not public self.gen_res_one.raccess.refresh_from_db() self.assertEqual(self.gen_res_one.raccess.public, False) response_data = json.loads(response.content) self.assertEqual(response_data['status'], 'error') # setting flag to public for 2nd resource should succeed # test that the resource is not public self.assertEqual(self.gen_res_two.raccess.public, False) url_params = {'shortkey': self.gen_res_two.short_id} post_data = {'flag': 'make_public'} url = reverse('set_resource_flag', kwargs=url_params) request = self.factory.post(url, data=post_data) request.user = self.user self.set_request_message_attributes(request) self.add_session_to_request(request) response = set_resource_flag(request, shortkey=self.gen_res_two.short_id) response_data = json.loads(response.content) self.assertEqual(response_data['status'], 'success') # check that the resource is public now self.gen_res_two.raccess.refresh_from_db() self.assertEqual(self.gen_res_two.raccess.public, True) # clean up hydroshare.delete_resource(self.gen_res_one.short_id) hydroshare.delete_resource(self.gen_res_two.short_id)
def test_replicate_bag(self): # here we are testing rep_res_bag_to_irods_user_zone view function super(TestReplicateBagToUserZone, self).assert_federated_irods_available() url_params = {'shortkey': self.gen_res.short_id} url = reverse('replicate_bag_user_zone', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user # make it a ajax request request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' response = rep_res_bag_to_irods_user_zone( request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = json.loads(response.content.decode()) self.assertIn('resource copy to user zone', response_data.values()) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_unshare_resource_with_user(self): # here we are testing the unshare_resource_with_user view function # test unshare resource with self.user # test self.user has no view permission self.assertNotIn(self.user, self.gen_res.raccess.view_users) # grant view access to self.user self.owner.uaccess.share_resource_with_user(self.gen_res, self.user, PrivilegeCodes.VIEW) # test self.user has now view permission self.gen_res.raccess.refresh_from_db() self.assertIn(self.user, self.gen_res.raccess.view_users) self._check_unshare_with_user() # test self.user has no view permission self.assertNotIn(self.user, self.gen_res.raccess.view_users) # grant edit access to self.user self.owner.uaccess.share_resource_with_user(self.gen_res, self.user, PrivilegeCodes.CHANGE) # test self.user has now edit permission self.gen_res.raccess.refresh_from_db() self.assertIn(self.user, self.gen_res.raccess.edit_users) self._check_unshare_with_user() # test self.user has no edit permission self.assertNotIn(self.user, self.gen_res.raccess.edit_users) # grant owner access to self.user self.owner.uaccess.share_resource_with_user(self.gen_res, self.user, PrivilegeCodes.OWNER) # test self.user has now owner permission self.gen_res.raccess.refresh_from_db() self.assertIn(self.user, self.gen_res.raccess.owners) self._check_unshare_with_user() # test self.user has no owner permission self.assertNotIn(self.user, self.gen_res.raccess.owners) # clean up hydroshare.delete_resource(self.gen_res.short_id)
def test_pdf_classification(self): """ files with recognized extensions are classified properly """ # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") # add one file to the resource test_file_handle = open(self.filenames[0], 'r') hydroshare.add_resource_files(self.res.short_id, test_file_handle) self.assertEqual(self.res.files.all().count(), 1, msg="resource file count didn't match") types = get_content_types(self.res) self.assertTrue(is_equal_to_as_set(types[0], ['Composite', self.content_types[0]])) self.assertTrue(is_equal_to_as_set(types[1], [])) # no left-over extensions # delete resources to clean up hydroshare.delete_resource(self.res.short_id)
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 test_create_resource_version(self): # here we are testing the create_new_version_resource view function # we should have 1 resource at this point self.assertEqual(BaseResource.objects.count(), 1) url_params = {'shortkey': self.gen_res.short_id} url = reverse('create_resource_version', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user self.add_session_to_request(request) response = create_new_version_resource(request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_302_FOUND) res_id = response.url.split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) # should have 2 resources now self.assertEqual(BaseResource.objects.count(), 2) # clean up hydroshare.delete_resource(res_id) hydroshare.delete_resource(self.gen_res.short_id)
def test_copy_resource(self): # here we are testing the copy_resource view function # we should have 1 resource at this point self.assertEqual(BaseResource.objects.count(), 1) url_params = {'shortkey': self.gen_res.short_id} url = reverse('copy_resource', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user self.add_session_to_request(request) response = copy_resource(request, shortkey=self.gen_res.short_id) self.assertEqual(response.status_code, status.HTTP_302_FOUND) res_id = response.url.split('/')[2] self.assertEqual(BaseResource.objects.filter(short_id=res_id).exists(), True) # should have 2 resources now self.assertEqual(BaseResource.objects.count(), 2) # clean up hydroshare.delete_resource(res_id) hydroshare.delete_resource(self.gen_res.short_id)
def test_share_resource_with_group(self): # here we are testing the share_resource_with_group view function # test share resource with self.test_group with view permission # test self.test_group has no view permission self.assertNotIn(self.test_group, self.gen_res.raccess.view_groups) self._check_share_with_group(privilege='view') self.gen_res.raccess.refresh_from_db() # test self.test_group has now view permission self.assertIn(self.test_group, self.gen_res.raccess.view_groups) # test share resource with self.test_group with edit permission # test self.test_group has no edit permission self.assertNotIn(self.test_group, self.gen_res.raccess.edit_groups) self._check_share_with_group(privilege='edit') self.gen_res.raccess.refresh_from_db() # test self.test_group has now edit permission self.assertIn(self.test_group, self.gen_res.raccess.edit_groups) # clean up hydroshare.delete_resource(self.gen_res.short_id)
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)
def delete(self, request, pk): # only resource owners are allowed to delete view_utils.authorize(request, pk, needed_permission=ACTION_TO_AUTHORIZE.DELETE_RESOURCE) hydroshare.delete_resource(pk) return Response(status=status.HTTP_204_NO_CONTENT)
def delete_resource(self, pk): authorize(self.request, pk, edit=True) hydroshare.delete_resource(pk) return HttpResponse(pk, content_type=None)
def test_CRUD_metadata(self): # here we are testing the add_metadata_element view function # There should be no keywords (subject element) now self.assertEqual(self.gen_res.metadata.subjects.count(), 0) # add keywords url_params = {'shortkey': self.gen_res.short_id, 'element_name': 'subject'} post_data = {'value': 'kw-1, kw 2, key word'} url = reverse('add_metadata_element', 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_metadata_element(request, shortkey=self.gen_res.short_id, element_name='subject') self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content) self.assertEqual(response_dict['status'], 'success') self.assertEqual(response_dict['element_name'], 'subject') self.gen_res.refresh_from_db() self.assertEqual(self.gen_res.metadata.subjects.count(), 3) # here we are testing the update_metadata_element view function # update title metadata self.assertEqual(self.gen_res.metadata.title.value, 'Generic Resource Key/Value Metadata Testing') title_element = self.gen_res.metadata.title url_params = {'shortkey': self.gen_res.short_id, 'element_name': 'title', 'element_id': title_element.id} post_data = {'value': 'Updated Resource Title'} url = reverse('update_metadata_element', 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 = update_metadata_element(request, shortkey=self.gen_res.short_id, element_name='title', element_id=title_element.id) self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content) self.assertEqual(response_dict['status'], 'success') self.gen_res.refresh_from_db() self.assertEqual(self.gen_res.metadata.title.value, 'Updated Resource Title') # here we are testing the delete_metadata_element view function # first create a contributor element and then delete it # there should be no contributors now self.assertEqual(self.gen_res.metadata.contributors.count(), 0) url_params = {'shortkey': self.gen_res.short_id, 'element_name': 'contributor'} post_data = {'name': 'John Smith', 'email': '*****@*****.**'} url = reverse('add_metadata_element', 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_metadata_element(request, shortkey=self.gen_res.short_id, element_name='contributor') self.assertEqual(response.status_code, status.HTTP_200_OK) response_dict = json.loads(response.content) self.assertEqual(response_dict['status'], 'success') self.assertEqual(response_dict['element_name'], 'contributor') self.gen_res.refresh_from_db() # there should be one contributor now self.assertEqual(self.gen_res.metadata.contributors.count(), 1) # now delete the contributor we created above contributor = self.gen_res.metadata.contributors.first() url_params = {'shortkey': self.gen_res.short_id, 'element_name': 'contributor', 'element_id': contributor.id} url = reverse('delete_metadata_element', kwargs=url_params) request = self.factory.post(url, data={}) request.user = self.user request.META['HTTP_REFERER'] = 'some-url' self.set_request_message_attributes(request) self.add_session_to_request(request) response = delete_metadata_element(request, shortkey=self.gen_res.short_id, element_name='contributor', element_id=contributor.id) self.assertEqual(response.status_code, status.HTTP_302_FOUND) self.assertEqual(response['Location'], request.META['HTTP_REFERER']) self.gen_res.refresh_from_db() # there should be no contributors self.assertEqual(self.gen_res.metadata.contributors.count(), 0) hydroshare.delete_resource(self.gen_res.short_id)
def test_unfederated_root_path_checks(self): """ an unfederated file in the root folder has the proper state after state changes """ # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") check_irods_files(self.res, stop_on_error=True) # add one file to the resource hydroshare.add_resource_files(self.res.short_id, self.test_file_1) # should succeed without errors check_irods_files(self.res, stop_on_error=True) # cleaning should not change anything check_irods_files(self.res, stop_on_error=True, log_errors=False, return_errors=True, clean_irods=True, clean_django=True, sync_ispublic=True) # resource should has only one file at this point self.assertEqual(self.res.files.all().count(), 1, msg="resource file count didn't match") # get the handle of the file created above resfile = self.res.files.all()[0] # determine where that file should live shortpath = os.path.join(self.res.short_id, "data", "contents", "file1.txt") self.assertEqual(resfile.file_folder, None) self.assertEqual(resfile.storage_path, shortpath) self.assertTrue(resfile.path_is_acceptable(shortpath)) # now try to intentionally corrupt it resfile.set_short_path("fuzz.txt") # should raise exception with self.assertRaises(ValidationError): check_irods_files(self.res, stop_on_error=True) # now don't raise exception and read error errors, ecount = check_irods_files(self.res, return_errors=True, log_errors=False) self.assertTrue(errors[0].endswith( 'data/contents/fuzz.txt does not exist in iRODS')) self.assertTrue(errors[1].endswith( 'data/contents/file1.txt in iRODs does not exist in Django')) self.assertTrue(errors[2].endswith( "type is GenericResource, title is 'My Test Resource'")) # now try to clean it up errors, ecount = check_irods_files(self.res, return_errors=True, log_errors=False, clean_irods=True, clean_django=True) self.assertTrue(errors[0].endswith( 'data/contents/fuzz.txt does not exist in iRODS (DELETED FROM DJANGO)')) self.assertTrue(errors[1].endswith( 'data/contents/file1.txt in iRODs does not exist in Django (DELETED FROM IRODS)')) self.assertTrue(errors[2].endswith( "type is GenericResource, title is 'My Test Resource'")) # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") # now check should succeed errors, ecount = check_irods_files(self.res, stop_on_error=True, log_errors=False) self.assertEqual(ecount, 0) # delete resources to clean up hydroshare.delete_resource(self.res.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 tearDown(self): User.objects.all().delete() hydroshare.delete_resource(self.res.short_id)
def test_federated_folder_path_logic(self): """ a federated file in a subfolder has the proper state after state changes """ # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") ResourceFile.create_folder(self.res, 'foo') # add one file to the resource hydroshare.add_resource_files(self.res.short_id, self.test_file_1, folder='foo') # resource should has only one file at this point self.assertEqual(self.res.files.all().count(), 1, msg="resource file count didn't match") # get the handle of the file created above resfile = self.res.files.all()[0] self.assertEqual(resfile.resource_file.name, os.path.join(self.res.short_id, "data", "contents", "foo", "file1.txt")) self.assertEqual(resfile.file_folder, "foo") # cheat: set a fake federated path to test path logic fedpath = "/myzone/home/myuser" self.res.resource_federation_path = fedpath self.res.save() resfile.content_object.refresh_from_db() resfile.set_storage_path('foo/file1.txt', test_exists=False) # determine where that file should live shortpath = os.path.join(fedpath, self.res.short_id, "data", "contents", "foo", "file1.txt") self.assertEqual(shortpath, resfile.storage_path) # this should result in an exact path resfile.set_storage_path(shortpath, test_exists=False) self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) self.assertTrue(resfile.path_is_acceptable(shortpath, test_exists=False)) # non-existent files should raise error otherpath = os.path.join(fedpath, self.res.short_id, "data", "contents", "foo", "file2.txt") resfile.path_is_acceptable(otherpath, test_exists=False) # This won't work because federation path is fake. # with self.assertRaises(ValidationError): # resfile.path_is_acceptable(otherpath, test_exists=True) # try setting to an unqualified name; should qualify it resfile.set_storage_path("foo/file1.txt", test_exists=False) # should match computed path self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to what it is already resfile.set_storage_path(shortpath, test_exists=False) # should match computed path self.assertEqual(resfile.file_folder, "foo") self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to a good path to a non-existent object resfile.set_storage_path(otherpath, test_exists=False) # conclusion: unfederate the resource self.res.resource_federation_path = "" self.res.save() resfile.content_object.refresh_from_db() resfile.set_storage_path("foo/file1.txt", test_exists=False) # delete resources to clean up hydroshare.delete_resource(self.res.short_id)
def test_metadata_delete_on_resource_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') self.resModelProgram.metadata.create_element('Contributor', name="user2") # before resource delete core_metadata_obj = self.resModelProgram.metadata self.assertEqual(CoreMetaData.objects.all().count(), 1) # there should be Creator metadata objects self.assertTrue(Creator.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Contributor metadata objects self.assertTrue(Contributor.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Identifier metadata objects self.assertTrue(Identifier.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Type metadata objects self.assertTrue(Type.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Source metadata objects self.assertFalse(Source.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Relation metadata objects self.assertFalse(Relation.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Publisher metadata objects self.assertFalse(Publisher.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Title metadata objects self.assertTrue(Title.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Description (Abstract) metadata objects self.assertTrue(Description.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Date metadata objects self.assertTrue(Date.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Subject metadata objects self.assertTrue(Subject.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Coverage metadata objects self.assertFalse(Coverage.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Format metadata objects self.assertTrue(Format.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Language metadata objects self.assertTrue(Language.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be Rights metadata objects self.assertTrue(Rights.objects.filter(object_id=core_metadata_obj.id).exists()) # resource specific metadata # there should be Model Program metadata objects self.assertTrue(MpMetadata.objects.filter(object_id=core_metadata_obj.id).exists()) # delete resource hydroshare.delete_resource(self.resModelProgram.short_id) self.assertEquals(CoreMetaData.objects.all().count(), 0) # there should be no Creator metadata objects self.assertFalse(Creator.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Contributor metadata objects self.assertFalse(Contributor.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Identifier metadata objects self.assertFalse(Identifier.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Type metadata objects self.assertFalse(Type.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Source metadata objects self.assertFalse(Source.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Relation metadata objects self.assertFalse(Relation.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Publisher metadata objects self.assertFalse(Publisher.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Title metadata objects self.assertFalse(Title.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Description (Abstract) metadata objects self.assertFalse(Description.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Date metadata objects self.assertFalse(Date.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Subject metadata objects self.assertFalse(Subject.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Coverage metadata objects self.assertFalse(Coverage.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Format metadata objects self.assertFalse(Format.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Language metadata objects self.assertFalse(Language.objects.filter(object_id=core_metadata_obj.id).exists()) # there should be no Rights metadata objects self.assertFalse(Rights.objects.filter(object_id=core_metadata_obj.id).exists()) # resource specific metadata # there should be no Model Output metadata objects self.assertFalse(MpMetadata.objects.filter(object_id=core_metadata_obj.id).exists())
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_metadata_delete_on_resource_delete(self): # adding a valid raster tif file should generate some core metadata and all extended # metadata files = [UploadedFile(file=self.raster_tif_file_obj, name=self.raster_tif_file_name)] utils.resource_file_add_process(resource=self.resRaster, files=files, user=self.user, extract_metadata=True) # before resource delete # resource core metadata raster_metadata_obj = self.resRaster.metadata self.assertEqual(CoreMetaData.objects.all().count(), 1) # there should be Creator metadata objects self.assertTrue(Creator.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Contributor metadata objects self.assertFalse(Contributor.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Identifier metadata objects self.assertTrue(Identifier.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Type metadata objects self.assertTrue(Type.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Source metadata objects self.assertFalse(Source.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Relation metadata objects self.assertFalse(Relation.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Publisher metadata objects self.assertFalse(Publisher.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Title metadata objects self.assertTrue(Title.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Description (Abstract) metadata objects self.assertFalse(Description.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Date metadata objects self.assertTrue(Date.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Subject metadata objects self.assertFalse(Subject.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Coverage metadata objects self.assertTrue(Coverage.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Format metadata objects self.assertTrue(Format.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Language metadata objects self.assertTrue(Language.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be Rights metadata objects self.assertTrue(Rights.objects.filter(object_id=raster_metadata_obj.id).exists()) # resource specific metadata # there should be original coverage metadata objects self.assertTrue(OriginalCoverage.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be CellInformation metadata objects self.assertTrue(CellInformation.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be BandInformation metadata objects self.assertTrue(BandInformation.objects.filter(object_id=raster_metadata_obj.id).exists()) # delete resource hydroshare.delete_resource(self.resRaster.short_id) self.assertEqual(CoreMetaData.objects.all().count(), 0) # there should be no Creator metadata objects self.assertFalse(Creator.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Contributor metadata objects self.assertFalse(Contributor.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Identifier metadata objects self.assertFalse(Identifier.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Type metadata objects self.assertFalse(Type.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Source metadata objects self.assertFalse(Source.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Relation metadata objects self.assertFalse(Relation.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Publisher metadata objects self.assertFalse(Publisher.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Title metadata objects self.assertFalse(Title.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Description (Abstract) metadata objects self.assertFalse(Description.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Date metadata objects self.assertFalse(Date.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Subject metadata objects self.assertFalse(Subject.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Coverage metadata objects self.assertFalse(Coverage.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Format metadata objects self.assertFalse(Format.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Language metadata objects self.assertFalse(Language.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be no Rights metadata objects self.assertFalse(Rights.objects.filter(object_id=raster_metadata_obj.id).exists()) # resource specific metadata # there should be no original coverage metadata objects self.assertFalse(OriginalCoverage.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be CellInformation metadata objects self.assertFalse(CellInformation.objects.filter(object_id=raster_metadata_obj.id).exists()) # there should be bandInformation metadata objects self.assertFalse(BandInformation.objects.filter(object_id=raster_metadata_obj.id).exists())
def test_federated_root_path_logic(self): """ a federated file path in the root folder has the proper state after state changes """ # resource should not have any files at this point self.assertEqual(self.res.files.all().count(), 0, msg="resource file count didn't match") # add one file to the resource hydroshare.add_resource_files(self.res.short_id, self.test_file_1) # resource should has only one file at this point self.assertEqual(self.res.files.all().count(), 1, msg="resource file count didn't match") # get the handle of the file created above resfile = self.res.files.all()[0] # cheat: set a fake federated path to test path logic oldfedpath = self.res.resource_federation_path oldpath = resfile.storage_path # intentionally break path logic by setting an unused federation path fedpath = "/myzone/home/myuser" self.res.resource_federation_path = fedpath self.res.save() # must load changes into resfile from self.res before setting storage path resfile.content_object.refresh_from_db() resfile.set_storage_path('file1.txt', test_exists=False) self.assertEqual(self.res.resource_federation_path, fedpath) self.assertEqual(resfile.storage_path, get_path(resfile, 'file1.txt')) # determine where that file should live; THIS IS FAKE shortpath = os.path.join(fedpath, self.res.short_id, "data", "contents", "file1.txt") # intentionally break the resource file path resfile.set_storage_path(shortpath, test_exists=False) self.assertEqual(shortpath, resfile.storage_path) self.assertEqual(resfile.file_folder, None) self.assertEqual(resfile.storage_path, shortpath) self.assertTrue(resfile.path_is_acceptable(shortpath, test_exists=False)) otherpath = os.path.join(fedpath, self.res.short_id, "data", "contents", "file2.txt") resfile.path_is_acceptable(otherpath, test_exists=False) # non-existent files should raise error # This won't work because federation path is fake # with self.assertRaises(ValidationError): # resfile.path_is_acceptable(otherpath, test_exists=True) # try setting to an unqualified name; should qualify it resfile.set_storage_path("file1.txt", test_exists=False) # should match computed path self.assertEqual(resfile.file_folder, None) self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to what it is already resfile.set_storage_path(shortpath, test_exists=False) # should match computed path self.assertEqual(resfile.file_folder, None) self.assertEqual(resfile.storage_path, shortpath) # now try to change that path to a good path to a non-existent object resfile.set_storage_path(otherpath, test_exists=False) # conclusion: strip off federation path self.res.resource_federation_path = oldfedpath self.res.save() # must load changes into resfile from self.res before setting storage path resfile.content_object.refresh_from_db() resfile.set_storage_path(oldpath, test_exists=False) # delete resources to clean up hydroshare.delete_resource(self.res.short_id)