def test_resource_list_by_keyword(self): gen_res_one = resource.create_resource('GenericResource', self.user, 'Resource 1') gen_res_two = resource.create_resource('GenericResource', self.user, 'Resource 2') gen_res_three = resource.create_resource('GenericResource', self.user, 'Resource 3') gen_res_four = resource.create_resource('GenericResource', self.user, 'Resource 2') self.resources_to_delete.append(gen_res_one.short_id) self.resources_to_delete.append(gen_res_two.short_id) self.resources_to_delete.append(gen_res_three.short_id) self.resources_to_delete.append(gen_res_four.short_id) gen_res_one.metadata.create_element("subject", value="one") gen_res_two.metadata.create_element("subject", value="other") gen_res_three.metadata.create_element("subject", value="One") gen_res_four.metadata.create_element("subject", value="Other") response = self.client.get('/hsapi/resource/', {'subject': 'one'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2) response = self.client.get('/hsapi/resource/', {'subject': 'other'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2) response = self.client.get('/hsapi/resource/', {'subject': 'one,other'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 4) response = self.client.get('/hsapi/resource/', {'subject': 'oth'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2)
def setUp(self): super(TestPublicResourceFlagsEndpoint, self).setUp() self.tmp_dir = tempfile.mkdtemp() self.rtype = 'GenericResource' self.title = 'My Test resource' res = resource.create_resource(self.rtype, self.user, self.title) metadata_dict = [ {'description': {'abstract': 'My test abstract'}}, {'subject': {'value': 'sub-1'}} ] file_one = "test1.txt" open(file_one, "w").close() self.file_one = open(file_one, "r") self.txt_file_path = os.path.join(self.tmp_dir, 'text.txt') txt = open(self.txt_file_path, 'w') txt.write("Hello World\n") txt.close() self.rtype = 'GenericResource' self.title = 'My Test resource' res_two = resource.create_resource(self.rtype, self.user, self.title, files=(self.file_one,), metadata=metadata_dict) self.pid = res.short_id self.pid_two = res_two.short_id self.resources_to_delete.append(self.pid) self.resources_to_delete.append(self.pid_two)
def test_resource_list_by_user(self): # HSRESTTestCase is forcing authentication of user, so we'll just test that user gen_res_one = resource.create_resource('GenericResource', self.user, 'Resource 1') gen_res_two = resource.create_resource('GenericResource', self.user, 'Resource 2') gen_res_three = resource.create_resource('GenericResource', self.user, 'Resource 3') self.resources_to_delete.append(gen_res_one.short_id) self.resources_to_delete.append(gen_res_two.short_id) self.resources_to_delete.append(gen_res_three.short_id) # resources by owner username response = self.client.get('/hsapi/resource/', {'owner': self.user.username}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # resources by owner email response = self.client.get('/hsapi/resource/', {'owner': self.user.email}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # resources by creator username response = self.client.get('/hsapi/resource/', {'creator': self.user.username}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # resources by creator email response = self.client.get('/hsapi/resource/', {'creator': self.user.email}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # resources by user username response = self.client.get('/hsapi/resource/', {'user': self.user.username}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # resources by user email response = self.client.get('/hsapi/resource/', {'user': self.user.email}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3)
def test_resource_list_by_bounding_box(self): metadata_dict_one = [{'coverage': {'type': 'box', 'value': {'northlimit': '80', 'eastlimit': '40', 'southlimit': '60', 'westlimit': '20', 'units': 'decimal deg'}}}] gen_res_one = resource.create_resource('GenericResource', self.user, 'Resource 1', metadata=metadata_dict_one) metadata_dict_two = [{'coverage': {'type': 'box', 'value': {'northlimit': '60', 'eastlimit': '110', 'southlimit': '50', 'westlimit': '90', 'units': 'decimal deg'}}}] gen_res_two = resource.create_resource('GenericResource', self.user, 'Resource 2', metadata=metadata_dict_two) metadata_dict_two = [{'coverage': {'type': 'point', 'value': {'north': '70', 'east': '70', 'units': 'decimal deg'}}}] gen_res_three = resource.create_resource('GenericResource', self.user, 'Resource 2', metadata=metadata_dict_two) self.resources_to_delete.append(gen_res_one.short_id) self.resources_to_delete.append(gen_res_two.short_id) self.resources_to_delete.append(gen_res_three.short_id) response = self.client.get('/hsapi/resource/', {'coverage_type': 'box', 'north': '70', 'east': '50', 'south': '50', 'west': '30'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 1) response = self.client.get('/hsapi/resource/', {'coverage_type': 'box', 'north': '70', 'east': '120', 'south': '40', 'west': '100'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 1) response = self.client.get('/hsapi/resource/', {'coverage_type': 'box', 'north': '90', 'east': '140', 'south': '30', 'west': '0'}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) # Bad coverage has no effect response = self.client.get('/hsapi/resource/', {'coverage_type': 'bad', 'nonsensical': '90', 'params': '140'}, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_resource_with_metadata_for_publisher(self): # trying to create a resource with metadata for publisher should fail due to the fact that the # resource is not yet published metadata_dict = [{'publisher': {'name': 'HydroShare', 'url': 'https://hydroshare.org'}}, ] with self.assertRaises(Exception): resource.create_resource(resource_type='GenericResource', owner=self.user, title='My Test Resource', metadata=metadata_dict )
def test_create_resource_with_content_files(self): new_res = resource.create_resource('GenericResource', self.user, 'My Test Resource', files=(self.file_one, )) # test resource has one file self.assertEqual(new_res.files.all().count(), 1) # test the mime_type of the content file res_file = new_res.files.all().first() self.assertEqual(res_file.mime_type, "text/plain") # test the extension of the content file self.assertEqual(res_file.extension, ".txt") self.assertEqual(new_res.resource_type, 'GenericResource') self.assertTrue(isinstance(new_res, GenericResource), type(new_res)) self.assertTrue(new_res.metadata.title.value == 'My Test Resource') self.assertTrue( new_res.created.strftime('%m/%d/%Y') == dtime.datetime.today().strftime('%m/%d/%Y')) self.assertTrue(new_res.creator == self.user) self.assertTrue(new_res.short_id is not None, 'Short ID has not been created!') self.assertEqual(new_res.files.all().count(), 1, msg="Number of content files is not equal to 1") self.assertEqual( new_res.get_quota_holder(), self.user, msg= "The quota holder of the newly created resource is not the creator" ) if new_res: new_res.delete() # test creating resource with multiple files new_res = resource.create_resource('GenericResource', self.user, 'My Test Resource', files=(self.file_one, self.file_two)) # test resource has 2 files self.assertEquals(new_res.files.all().count(), 2, msg="Number of content files is not equal to 2") if new_res: new_res.delete()
def test_resource_list_by_group(self): group_one = self.user.uaccess.create_group(title='Group One', description='This is a great group', purpose='To have fun', auto_approve=True) group_two = self.user.uaccess.create_group(title='Group Two', description='This is another great group', purpose='To have fun', auto_approve=True) gen_res_one = resource.create_resource('GenericResource', self.user, 'Resource 1', edit_groups=[group_one, group_two], view_groups=None) gen_res_two = resource.create_resource('GenericResource', self.user, 'Resource 2', edit_groups=None, view_groups=[group_one, group_two]) gen_res_three = resource.create_resource('GenericResource', self.user, 'Resource 3', edit_groups=[group_one], view_groups=None) gen_res_four = resource.create_resource('GenericResource', self.user, 'Resource 4', edit_groups=None, view_groups=None) self.groups_to_delete.append(group_one) self.groups_to_delete.append(group_two) self.resources_to_delete.append(gen_res_one.short_id) self.resources_to_delete.append(gen_res_two.short_id) self.resources_to_delete.append(gen_res_three.short_id) self.resources_to_delete.append(gen_res_four.short_id) # resources by group id response = self.client.get('/hsapi/resource/', {'group': str(group_one.pk)}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) response = self.client.get('/hsapi/resource/', {'group': str(group_two.pk)}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2) # resources by group name response = self.client.get('/hsapi/resource/', {'group': str(group_one.name)}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 3) response = self.client.get('/hsapi/resource/', {'group': str(group_two.name)}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2)
def setUp(self): super(TestCreateResource, self).setUp() self.tmp_dir = tempfile.mkdtemp() self.hs_group, _ = Group.objects.get_or_create( name='Hydroshare Author') # create a user self.user = hydroshare.create_account('*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False, groups=[self.hs_group]) # create files file_one = os.path.join(self.tmp_dir, "test1.txt") open(file_one, "w").close() # open files for read and upload self.file_one = open(file_one, "r") self.res = resource.create_resource('GenericResource', self.user, 'My Test Resource', files=(self.file_one, ))
def test_add_files(self): # create a resource res = create_resource( resource_type='GenericResource', owner=self.user, title='Test Resource', metadata=[], ) # delete all resource files for created resource res.files.all().delete() # add files - this is the api we are testing add_resource_files(res.short_id, self.myfile1, self.myfile2, self.myfile3) # resource should have 3 files self.assertEquals(res.files.all().count(), 3) # add each file of resource to list file_list = [] for f in res.files.all(): file_list.append(f.resource_file.name.split('/')[-1]) # check if the file name is in the list of files self.assertTrue(self.n1 in file_list, "file 1 has not been added") self.assertTrue(self.n2 in file_list, "file 2 has not been added") self.assertTrue(self.n3 in file_list, "file 3 has not been added") res.delete()
def test_create_resource_with_two_files(self): raster = MyTemporaryUploadedFile(open(self.raster_file_path, 'rb'), name=self.raster_file_path, content_type='image/tiff', size=os.stat( self.raster_file_path).st_size) text = MyTemporaryUploadedFile(open(self.txt_file_path, 'r'), name=self.txt_file_path, content_type='text/plain', size=os.stat( self.txt_file_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(raster, text)) pid = res.short_id # get the resource by pid res = get_resource_by_shortkey(pid) self.assertEqual(res.resource_type, 'GenericResource') self.assertTrue(isinstance(res, GenericResource), type(res)) self.assertEqual(res.metadata.title.value, 'My Test resource') self.assertEquals(res.files.all().count(), 2) if res: res.delete()
def setUp(self): # create an admin user self.admin_user = users.create_account( '*****@*****.**', username='******', first_name='Super', last_name='User', superuser=True, groups=[]) # create a test user self.test_user = users.create_account( '*****@*****.**', username='******', first_name='Ima', last_name='Testuser', superuser=False, groups=[]) self.new_res = resource.create_resource( 'GenericResource', self.admin_user, 'My Test Resource' ) # get the user's id #self.userid = User.objects.get(username=self.user).pk self.test_group = users.create_group( 'MyTestGroup', members=[self.admin_user], owners=[self.admin_user] )
def setUp(self): super(TestCreateResource, self).setUp() self.tmp_dir = tempfile.mkdtemp() self.hs_group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user self.user = hydroshare.create_account( '*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False, groups=[self.hs_group] ) # create files file_one = os.path.join(self.tmp_dir, "test1.txt") open(file_one, "w").close() # open files for read and upload self.file_one = open(file_one, "r") self.res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource', files=(self.file_one,) )
def test_set_and_delete_user_resource_access(self): rtype = 'GenericResource' title = 'My Test resource' keywords = ('foo', 'bar') abstract = 'This is a resource used for testing /hsapi/resource/{id}/access/' new_res = resource.create_resource(rtype, self.user, title, keywords=keywords) new_res.metadata.create_element('description', abstract=abstract) res_id = new_res.short_id self.resources_to_delete.append(res_id) access_url = "/hsapi/resource/{res_id}/access/".format(res_id=res_id) put_response = self.client.put(access_url, { "privilege": PrivilegeCodes.VIEW, "user_id": self.secondUser.id }, format='json') self.assertEqual("Resource access privileges added.", put_response.data['success']) get_response = self.client.get(access_url) self.assertEqual(2, len(get_response.data['users'])) self.assertEqual(0, len(get_response.data['groups'])) delete_response = self.client.delete(access_url + "?user_id=" + str(self.secondUser.id)) self.assertEqual("Resource access privileges removed.", delete_response.data['success']) get_response = self.client.get(access_url) self.assertEqual(1, len(get_response.data['users']))
def test_DEPRECATED_set_access_rules(self): rtype = 'GenericResource' title = 'My Test resource' keywords = ('foo', 'bar') abstract = 'This is a resource used for testing /hsapi/accessRules' new_res = resource.create_resource(rtype, self.user, title, keywords=keywords) new_res.metadata.create_element('description', abstract=abstract) res_id = new_res.short_id self.resources_to_delete.append(res_id) sysmeta_url = "/hsapi/sysmeta/{res_id}/".format(res_id=res_id) response = self.client.get(sysmeta_url) self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['resource_type'], rtype) self.assertEqual(content['resource_title'], title) self.assertFalse(content['public']) access_url = "/hsapi/resource/accessRules/{res_id}/".format( res_id=res_id) response = self.client.put(access_url, {'public': True}) # this test resource does not have content file, so it cannot be made public self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) response = self.client.get(sysmeta_url) self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertFalse(content['public'])
def setUp(self): super(TestFolderDownloadZip, self).setUp() self.output_path = "zips/rand/foo.zip" self.group, _ = Group.objects.get_or_create(name='Hydroshare Author') self.user = create_account( '*****@*****.**', username='******', first_name='Shaun', last_name='Livingston', superuser=False, groups=[] ) # create files self.n1 = "test1.txt" test_file = open(self.n1, 'w') test_file.write("Test text file in test1.txt") test_file.close() test_file = open(self.n1, "r") self.res = create_resource(resource_type='GenericResource', owner=self.user, title='Test Resource', metadata=[], ) ResourceFile.create_folder(self.res, 'foo') # add one file to the resource add_resource_files(self.res.short_id, test_file, folder='foo')
def setUp(self): super(TestResourceFile, self).setUp() self.tmp_dir = tempfile.mkdtemp() # Make a text file self.txt_file_name = 'text.txt' self.txt_file_path = os.path.join(self.tmp_dir, self.txt_file_name) txt = open(self.txt_file_path, 'w') txt.write("Hello World\n") txt.close() self.raster_file_name = 'cea.tif' self.raster_file_path = 'hs_core/tests/data/cea.tif' # Make a zip file zip_path = os.path.join(self.tmp_dir, 'test.zip') with zipfile.ZipFile(zip_path, 'w') as zfile: zfile.write(self.raster_file_path) zfile.write(self.txt_file_path) # Create a resource with zipfile, do not un-pack payload = MyTemporaryUploadedFile(open(zip_path, 'rb'), name=zip_path, content_type='application/zip', size=os.stat(zip_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(payload, ), unpack_file=True) self.pid = res.short_id self.resources_to_delete.append(self.pid)
def setUp(self): super(TestSetDownloadAgreement, self).setUp() self.hs_group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user self.user_owner = hydroshare.create_account( '*****@*****.**', username='******', first_name='some_first_name_owner', last_name='some_last_name_owner', superuser=False, groups=[self.hs_group] ) self.user_non_owner = hydroshare.create_account( '*****@*****.**', username='******', first_name='some_first_name_non_owner', last_name='some_last_name_non_owner', superuser=False, groups=[self.hs_group] ) self.res = resource.create_resource( 'GenericResource', self.user_owner, 'My Test Resource' )
def test_resource_list_obsolete(self): gen_res_one = resource.create_resource('GenericResource', self.user, 'Resource 1') # make a new version of gen_res_one to make gen_res_one obsolete new_ver_gen_res_one = resource.create_empty_resource(gen_res_one.short_id, self.user) new_ver_gen_res_one = resource.create_new_version_resource(gen_res_one, new_ver_gen_res_one, self.user) self.resources_to_delete.append(new_ver_gen_res_one.short_id) self.resources_to_delete.append(gen_res_one.short_id) # the default for include_obsolete is False which should NOT return obsoleted resources response = self.client.get('/hsapi/resource/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 1) self.assertEqual(content['results'][0]['resource_id'], new_ver_gen_res_one.short_id) # set include_obsolete to True, which should return all resources including obsoleted ones response = self.client.get('/hsapi/resource/', {'include_obsolete': True}, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 2) result_res_id_list = [] result_res_id_list.append(content['results'][0]['resource_id']) result_res_id_list.append(content['results'][1]['resource_id']) self.assertIn(new_ver_gen_res_one.short_id, result_res_id_list, msg='new versioned resource id is not included in returned resource list') self.assertIn(gen_res_one.short_id, result_res_id_list, msg='obsoleted resource id is not included in returned resource list')
def setUp(self): # create an admin user self.admin_user = users.create_account('*****@*****.**', username='******', first_name='Super', last_name='User', superuser=True, groups=[]) # create a test user self.test_user = users.create_account('*****@*****.**', username='******', first_name='Ima', last_name='Testuser', superuser=False, groups=[]) self.new_res = resource.create_resource('GenericResource', self.admin_user, 'My Test Resource') # get the user's id #self.userid = User.objects.get(username=self.user).pk self.test_group = users.create_group('MyTestGroup', members=[self.admin_user], owners=[self.admin_user])
def test_add_files(self): user = User.objects.create_user('shaun', '*****@*****.**', 'shaun6745') #create user #create files n1 = "test.txt" n2 = "test2.txt" n3 = "test3.txt" open(n1,"w").close() #files are created open(n2,"w").close() open(n3,"w").close() myfile = open(n1,"r") #files are opened as 'read-only' myfile1 = open(n2,"r") myfile2 = open(n3,"r") res1 = create_resource('GenericResource',user,'res1') #create resource #delete all resource files for created resource res1.files.all().delete() #add files add_resource_files(res1.short_id,myfile,myfile1,myfile2) #add each file of resource to list res1 = get_resource_by_shortkey(res1.short_id) l=[] for f in res1.files.all(): l.append(f.resource_file.name.split('/')[-1]) #check if the file name is in the list of files self.assertTrue(n1 in l, "file 1 has not been added") self.assertTrue(n2 in l, "file 2 has not been added") self.assertTrue(n3 in l, "file 3 has not been added")
def setUp(self): super(TestResourceMetadata, self).setUp() self.rtype = 'GenericResource' self.title = 'My Test resource' res = resource.create_resource(self.rtype, self.user, self.title) self.pid = res.short_id self.resources_to_delete.append(self.pid)
def setUp(self): super(TestCreateResourceVersion, self).setUp() self.rtype = 'GenericResource' self.title = 'My Test resource' res = resource.create_resource(self.rtype, self.user, self.title) self.pid = res.short_id
def setUp(self): super(ModelInstanceSciMetaTestCase, self).setUp() self.rtype_prog = 'ModelProgramResource' self.title_prog = 'Some program' res = resource.create_resource(self.rtype_prog, self.user, self.title_prog) self.pid_prog = res.short_id self.resources_to_delete.append(self.pid_prog)
def setUp(self): super(TestPublicCopyResourceEndpoint, self).setUp() self.rtype = 'GenericResource' self.title = 'My Test resource' res = resource.create_resource(self.rtype, self.user, self.title) self.pid = res.short_id self.resources_to_delete.append(self.pid)
def test_bag_ingestion(self): from hs_core.views.utils import unzip_file def normalize_metadata(metadata_str): """Prepares metadata string to match resource id and hydroshare url of original""" return metadata_str\ .replace(current_site_url(), "http://www.hydroshare.org")\ .replace(res.short_id, "97523bdb7b174901b3fc2d89813458f1") # create empty resource res = resource.create_resource( 'CompositeResource', self.user, 'My Test Resource' ) full_paths = {} files_to_upload = [UploadedFile(file=open('hs_core/tests/data/test_resource_metadata_files.zip', 'rb'), name="test_resource_metadata_files.zip")] add_resource_files(res.short_id, *files_to_upload, full_paths=full_paths) unzip_file(self.user, res.short_id, "data/contents/test_resource_metadata_files.zip", True, overwrite=True, auto_aggregate=True, ingest_metadata=True) def compare_metadatas(new_metadata_str, original_metadata_file): original_graph = Graph() with open(os.path.join(self.extracted_directory, original_metadata_file), "r") as f: original_graph = original_graph.parse(data=f.read()) new_graph = Graph() new_graph = new_graph.parse(data=normalize_metadata(new_metadata_str)) # remove modified date, they'll never match subject = new_graph.value(predicate=RDF.type, object=DCTERMS.modified) new_graph.remove((subject, None, None)) subject = original_graph.value(predicate=RDF.type, object=DCTERMS.modified) original_graph.remove((subject, None, None)) for (new_triple, original_triple) in _squashed_graphs_triples(new_graph, original_graph): self.assertEquals(new_triple, original_triple, "Ingested resource metadata does not match original") res.refresh_from_db() compare_metadatas(res.metadata.get_xml(), "resourcemetadata.xml") compare_metadatas(res.get_logical_files(GenericLogicalFile.type_name())[0].metadata.get_xml(), "test_meta.xml") compare_metadatas(res.get_logical_files(FileSetLogicalFile.type_name())[0].metadata.get_xml(), "asdf/asdf_meta.xml") compare_metadatas(res.get_logical_files(GeoFeatureLogicalFile.type_name())[0].metadata.get_xml(), "watersheds_meta.xml") compare_metadatas(res.get_logical_files(GeoRasterLogicalFile.type_name())[0].metadata.get_xml(), "logan_meta.xml") compare_metadatas(res.get_logical_files(NetCDFLogicalFile.type_name())[0].metadata.get_xml(), "SWE_time_meta.xml") compare_metadatas(res.get_logical_files(RefTimeseriesLogicalFile.type_name())[0].metadata.get_xml(), "msf_version.refts_meta.xml") compare_metadatas(res.get_logical_files(TimeSeriesLogicalFile.type_name())[0].metadata.get_xml(), "ODM2_Multi_Site_One_Variable_meta.xml")
def setUp(self): super(TestResourceMap, self).setUp() self.rtype = 'GenericResource' self.title = 'My Test resource' self.res = resource.create_resource(self.rtype, self.user, self.title) self.pid = self.res.short_id self.resources_to_delete.append(self.pid) self.tmp_dir = tempfile.mkdtemp()
def test_create_resource_with_content_files(self): new_res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource', files=(self.file_one,) ) # test resource has one file self.assertEqual(new_res.files.all().count(), 1) # test the mime_type of the content file res_file = new_res.files.all().first() self.assertEqual(res_file.mime_type, "text/plain") # test the extension of the content file self.assertEqual(res_file.extension, ".txt") self.assertEqual(new_res.resource_type, 'GenericResource') self.assertTrue(isinstance(new_res, GenericResource), type(new_res)) self.assertTrue(new_res.metadata.title.value == 'My Test Resource') self.assertTrue(new_res.created.strftime('%m/%d/%Y') == dtime.datetime.today().strftime('%m/%d/%Y')) self.assertTrue(new_res.creator == self.user) self.assertTrue(new_res.short_id is not None, 'Short ID has not been created!') self.assertEqual(new_res.files.all().count(), 1, msg="Number of content files is not equal to 1") self.assertEqual(new_res.get_quota_holder(), self.user, msg="The quota holder of the newly created resource is not the creator") if new_res: new_res.delete() # test creating resource with multiple files new_res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource', files=(self.file_one, self.file_two) ) # test resource has 2 files self.assertEquals(new_res.files.all().count(), 2, msg="Number of content files is not equal to 2") if new_res: new_res.delete()
def setUp(self): super(TestSetFileTypeEndPoint, self).setUp() self.raster_file_name = 'cea.tif' self.raster_file_path = 'hs_core/tests/data/cea.tif' self.rtype = 'CompositeResource' self.title = 'My Test resource' self.resource = resource.create_resource(self.rtype, self.user, self.title) self.resources_to_delete.append(self.resource.short_id)
def test_resource_list(self): new_res = resource.create_resource('GenericResource', self.user, 'My Test Resource') pid = new_res.short_id self.resources_to_delete.append(pid) response = self.client.get('/hsapi/resource/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) content = json.loads(response.content) self.assertEqual(content['count'], 1) self.assertEqual(content['results'][0]['resource_id'], pid)
def test_create_resource_with_metadata_for_date(self): # trying to create a resource with metadata for 'date' element of type 'created' or 'modified' should ignore # the provided date metadata as date of type created and modified are system generated based on resource # creation time. # trying to create a resource with metadata for 'date' element of type 'published' should ignore the provided # metadata as date of type published is created when the resource is published # trying to create a resource with metadata for 'date' element of type 'available' should ignore the provided # metadata as date of type available is created when the resource is made public. # the only date element that can be created at the time of resource creation by specifying necessary data is # of the type 'valid' metadata_dict = [{'date': {'type': 'created', 'start_date': parser.parse('01/16/2016')}}, {'date': {'type': 'modified', 'start_date': parser.parse('01/16/2016')}}, {'date': {'type': 'published', 'start_date': parser.parse('01/16/2016')}}, {'date': {'type': 'available', 'start_date': parser.parse('01/16/2016')}}, {'date': {'type': 'valid', 'start_date': parser.parse('01/20/2016'), 'end_date': parser.parse('02/20/2016')}}] res = resource.create_resource( resource_type='GenericResource', owner=self.user, title='My Test Resource', metadata=metadata_dict ) self.assertIn('created', [dt.type for dt in res.metadata.dates.all()], msg="Date element type 'Created' does not exist") self.assertIn('modified', [dt.type for dt in res.metadata.dates.all()], msg="Date element type 'Modified' does not exist") # skipped dates are created, modified, published, and available skipped_date = timezone.make_aware(dtime.datetime.strptime('01/16/2016', "%m/%d/%Y"), timezone.get_default_timezone()) self.assertNotIn(skipped_date, [dat.start_date for dat in res.metadata.dates.all()], msg="Matching date value was found") self.assertEquals(res.metadata.dates.filter(type='publisher').count(), 0, msg="Publisher date was found.") self.assertEquals(res.metadata.dates.filter(type='available').count(), 0, msg="Available date was found.") # valid date should have been created self.assertEquals(res.metadata.dates.filter(type='valid').count(), 1) valid_start_date = timezone.make_aware(dtime.datetime.strptime('01/20/2016', "%m/%d/%Y"), timezone.get_default_timezone()) valid_end_date = timezone.make_aware(dtime.datetime.strptime('02/20/2016', "%m/%d/%Y"), timezone.get_default_timezone()) self.assertIn(valid_start_date, [dt.start_date for dt in res.metadata.dates.all()], msg="Matching date value was not found") self.assertIn(valid_end_date, [dt.end_date for dt in res.metadata.dates.all()], msg="Matching date value was not found") if res: res.delete()
def test_create_resource_with_metadata_for_format(self): # trying to create a resource with metadata for format element should ignore the provided format element data # as format elements are system generated based on resource content files metadata_dict = [{'format': {'value': 'plain/text'}}, {'format': {'value': 'image/tiff'}}] res = resource.create_resource( resource_type='GenericResource', owner=self.user, title='My Test Resource', metadata=metadata_dict ) self.assertEqual(res.metadata.formats.all().count(), 0, msg="Number of format elements not equal to 0.") if res: res.delete()
def setUp(self): super(TestHStore, self).setUp() self.group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user self.user = users.create_account('*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False, groups=[]) self.res = resource.create_resource('GenericResource', self.user, 'My Test Resource')
def test_create_resource_with_zipfile(self): # Make a zip file zip_path = os.path.join(self.tmp_dir, 'test.zip') with zipfile.ZipFile(zip_path, 'w') as zfile: zfile.write(self.raster_file_path) zfile.write(self.txt_file_path) # Create a resource with zipfile, do not un-pack payload = MyTemporaryUploadedFile(open(zip_path, 'rb'), name=zip_path, content_type='application/zip', size=os.stat(zip_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(payload, )) pid = res.short_id # get the resource by pid res = get_resource_by_shortkey(pid) self.assertEquals(res.files.all().count(), 1) # Create a resource with zipfile, un-pack payload2 = MyTemporaryUploadedFile(open(zip_path, 'rb'), name=zip_path, content_type='application/zip', size=os.stat(zip_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(payload2, ), unpack_file=True) pid = res.short_id res = get_resource_by_shortkey(pid) self.assertEquals(res.files.all().count(), 2) if res: res.delete()
def setUp(self): super(TestPublicResourceFlagsEndpoint, self).setUp() self.tmp_dir = tempfile.mkdtemp() self.rtype = 'GenericResource' self.title = 'My Test resource' res = resource.create_resource(self.rtype, self.user, self.title) metadata_dict = [{ 'description': { 'abstract': 'My test abstract' } }, { 'subject': { 'value': 'sub-1' } }] file_one = "test1.txt" open(file_one, "w").close() self.file_one = open(file_one, "rb") self.txt_file_path = os.path.join(self.tmp_dir, 'text.txt') txt = open(self.txt_file_path, 'w') txt.write("Hello World\n") txt.close() self.rtype = 'GenericResource' self.title = 'My Test resource' res_two = resource.create_resource(self.rtype, self.user, self.title, files=(self.file_one, ), metadata=metadata_dict) self.pid = res.short_id self.pid_two = res_two.short_id self.resources_to_delete.append(self.pid) self.resources_to_delete.append(self.pid_two)
def test_change_quota_holder(self): res = resource.create_resource( 'GenericResource', self.user1, 'My Test Resource' ) self.assertTrue(res.creator == self.user1) self.assertTrue(res.get_quota_holder() == self.user1) self.assertFalse(res.raccess.public) self.assertFalse(res.raccess.discoverable) with self.assertRaises(PermissionDenied): res.set_quota_holder(self.user1, self.user2) # test to make sure one owner can transfer quota holder to another owner self.user1.uaccess.share_resource_with_user(res, self.user2, PrivilegeCodes.OWNER) res.set_quota_holder(self.user1, self.user2) self.assertTrue(res.get_quota_holder() == self.user2) self.assertFalse(res.get_quota_holder() == self.user1) # test to make sure quota holder cannot be removed from ownership with self.assertRaises(PermissionDenied): self.user1.uaccess.unshare_resource_with_user(res, self.user2) # test to make sure quota holder cannot be changed to an owner who is over-quota uquota = self.user1.quotas.first() # make user1's quota over hard limit 125% uquota.used_value = uquota.allocated_value * 1.3 uquota.save() if not QuotaMessage.objects.exists(): QuotaMessage.objects.create() qmsg = QuotaMessage.objects.first() qmsg.enforce_quota = True qmsg.save() # QuotaException should be raised when attempting to change quota holder to user1 when # quota is enforced with self.assertRaises(QuotaException): res.set_quota_holder(self.user2, self.user1) qmsg.enforce_quota = False qmsg.save() # QuotaException should NOT be raised now that quota is not enforced res.set_quota_holder(self.user2, self.user1) if res: res.delete()
def test_delete_resource(self): new_res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource' ) # there should be one resource at this point self.assertEquals(GenericResource.objects.all().count(), 1, msg="Number of resources not equal to 1") # delete the resource - this is the api we are testing resource.delete_resource(new_res.short_id) # there should be no resource at this point self.assertEquals(GenericResource.objects.all().count(), 0, msg="Number of resources not equal to 0")
def test_create_resource_with_zipfile(self): # Make a zip file zip_path = os.path.join(self.tmp_dir, 'test.zip') with zipfile.ZipFile(zip_path, 'w') as zfile: zfile.write(self.raster_file_path) zfile.write(self.txt_file_path) # Create a resource with zipfile, do not un-pack payload = MyTemporaryUploadedFile(open(zip_path, 'rb'), name=zip_path, content_type='application/zip', size=os.stat(zip_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(payload,)) pid = res.short_id # get the resource by pid res = get_resource_by_shortkey(pid) self.assertEquals(res.files.all().count(), 1) # Create a resource with zipfile, un-pack payload2 = MyTemporaryUploadedFile(open(zip_path, 'rb'), name=zip_path, content_type='application/zip', size=os.stat(zip_path).st_size) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(payload2,), unpack_file=True) pid = res.short_id res = get_resource_by_shortkey(pid) self.assertEquals(res.files.all().count(), 2) if res: res.delete()
def test_create_resource_with_metadata_for_type(self): # trying to create a resource with metadata for type element should ignore the provided type element data # and create the system generated type element metadata_dict = [{'type': {'url': 'https://hydroshare.org/GenericResource'}}, ] res = resource.create_resource( resource_type='GenericResource', owner=self.user, title='My Test Resource', metadata=metadata_dict ) type_url = '{0}/terms/{1}'.format(hydroshare.utils.current_site_url(), 'GenericResource') self.assertEqual(res.metadata.type.url, type_url, msg='type element url is wrong') if res: res.delete()
def test_delete_resource(self): new_res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource' ) # there should be one resource at this point self.assertEqual(GenericResource.objects.all().count(), 1, msg="Number of resources not equal to 1") # delete the resource - this is the api we are testing resource.delete_resource(new_res.short_id) # there should be no resource at this point self.assertEqual(GenericResource.objects.all().count(), 0, msg="Number of resources not equal to 0")
def test_create_resource_without_content_files(self): res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource' ) self.assertEqual(res.resource_type, 'GenericResource') self.assertTrue(isinstance(res, GenericResource)) self.assertTrue(res.metadata.title.value == 'My Test Resource') self.assertTrue(res.created.strftime('%m/%d/%Y') == dtime.datetime.today().strftime('%m/%d/%Y')) self.assertTrue(res.creator == self.user) self.assertTrue(res.short_id is not None, 'Short ID has not been created!') self.assertEqual(res.files.all().count(), 0, 'Resource has content files') if res: res.delete()
def test_create_resource_with_file(self): raster = open(self.raster_file_path) res = resource.create_resource('GenericResource', self.user, 'My Test resource', files=(raster,)) pid = res.short_id # get the resource by pid res = get_resource_by_shortkey(pid) self.assertEqual(res.resource_type, 'GenericResource') self.assertTrue(isinstance(res, GenericResource), type(res)) self.assertEqual(res.metadata.title.value, 'My Test resource') self.assertEquals(res.files.all().count(), 1) if res: res.delete()
def setUp(self): self.hydroshare_author_group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user self.user = users.create_account( '*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False, groups=[]) self.res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource' )
def setUp(self): super(TestStorage, self).setUp() self.group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user self.user = users.create_account( '*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False) self.rtype = 'CompositeResource' self.title = 'My Test resource' self.res = resource.create_resource(self.rtype, self.user, self.title) self.pid = self.res.short_id
def setUp(self): super(TestAuthorize, self).setUp() self.group, _ = Group.objects.get_or_create(name='Hydroshare Author') # create a user - resource owner self.user = users.create_account( '*****@*****.**', username='******', first_name='some_first_name', last_name='some_last_name', superuser=False, groups=[]) self.res = resource.create_resource( 'GenericResource', self.user, 'My Test Resource' ) self.request = RequestFactory().request()