def _get_experiment(self, entry, user): experimentId, title, public_access = \ self._get_experiment_details(entry, user) try: try: param_name = ParameterName.objects.\ get(name=self.PARAM_EXPERIMENT_ID, \ schema=AtomImportSchemas.get_schema(Schema.EXPERIMENT)) parameter = ExperimentParameter.objects.\ get(name=param_name, string_value=experimentId) except ExperimentParameter.DoesNotExist: raise Experiment.DoesNotExist return parameter.parameterset.experiment except Experiment.DoesNotExist: experiment = Experiment(title=title, created_by=user, public_access=public_access) experiment.save() self._create_experiment_id_parameter_set(experiment, experimentId) acl = ExperimentACL(experiment=experiment, pluginId=django_user, entityId=user.id, canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def _create_test_data(): user = User(username='******', first_name='Thomas', last_name='Atkins', email='*****@*****.**') user.save() user2 = User(username='******', email='*****@*****.**') user2.save() map(lambda u: UserProfile(user=u).save(), [user, user2]) license_ = License(name='Creative Commons Attribution-NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user2.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return (user, experiment)
def setUp(self): from django.contrib.auth.models import User from tardis.tardis_portal.models import Experiment from tardis.tardis_portal.models import ExperimentACL from tardis.tardis_portal.models import Dataset from tardis.apps.mrtardis.mrtask import MRtask user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.client = Client() self.client.login(username=user, password=pwd) self.experiment = Experiment(approved=True, title="Test Experiment", institution_name="Test Institution", created_by=self.user, public=False) self.experiment.save() acl = ExperimentACL(pluginId="django_user", entityId="1", experiment=self.experiment, canRead=True, canWrite=True, canDelete=True, isOwner=True) acl.save() self.test_dataset = Dataset(experiment=self.experiment, description="test dataset") self.test_dataset.save() self.test_mrtask = MRtask(dataset=self.test_dataset)
def _create_test_data(): user = User(username='******', first_name='Thomas', last_name='Atkins', email='*****@*****.**') user.save() UserProfile(user=user).save() license_ = License( name='Creative Commons Attribution-NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create( order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create( order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return (user, experiment)
def _create_test_data(): user = User(username='******', first_name='Thomas', last_name='Atkins', email='*****@*****.**') user.save() UserProfile(user=user).save() license_ = License(name='Creative Commons Attribution-NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create(order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create(order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return (user, experiment)
def _get_experiment(self, entry, user): experimentId, title, public_access = \ self._get_experiment_details(entry, user) try: try: param_name = ParameterName.objects.\ get(name=self.PARAM_EXPERIMENT_ID, \ schema=AtomImportSchemas.get_schema(Schema.EXPERIMENT)) parameter = ExperimentParameter.objects.\ get(name=param_name, string_value=experimentId) except ExperimentParameter.DoesNotExist: raise Experiment.DoesNotExist return parameter.parameterset.experiment except Experiment.DoesNotExist: experiment = Experiment(title=title, created_by=user, public_access=public_access) experiment.save() self._create_experiment_id_parameter_set(experiment, experimentId) acl = ExperimentACL(experiment=experiment, pluginId=django_user, entityId=user.id, canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def createhpcexperiment(request, user, dl): from django.contrib.auth.models import User from tardis.tardis_portal.views import _registerExperimentDocument import os import tempfile # TODO temp = tempfile.TemporaryFile() for chunk in dl.chunks(): temp.write(chunk) temp.seek(0) metadata = {} for line in temp: key, value = line.split("~") metadata[key] = value temp.close() author = metadata["Name"] title = metadata["Experiment"] ds_desc = metadata["Facility"] desc = metadata["Description"] fname = metadata["FolderName"] counter = metadata["Counter"] package = metadata["Package"] exp = Experiment( title=title, institution_name="RMIT University", description=desc, created_by=User.objects.get(id=user.pk) ) exp.save() eid = exp.id # Store the author for the dataset ae = models.Author_Experiment(experiment=exp, author=author, order="1") ae.save() auth_key = settings.DEFAULT_AUTH try: e = Experiment.objects.get(pk=eid) except Experiment.DoesNotExist: logger.exception("Experiment for eid %i in CreateHPCExperiment does not exist" % eid) acl = ExperimentACL( experiment=e, pluginId=django_user, entityId=str(user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() folder_desc = "%s.%s.%s.%s" % (ds_desc.strip(), package.strip(), fname.strip(), counter.strip()) logger.debug("folder_desc = %s" % folder_desc) return eid, exp, folder_desc
def setUp(self): from os import path, mkdir from tempfile import mkdtemp user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.test_dir = mkdtemp() Location.force_initialize() self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.experiment_path = path.join(settings.FILE_STORE_PATH, str(self.dataset.get_first_experiment().id)) self.dataset_path = path.join(self.experiment_path, str(self.dataset.id)) if not path.exists(self.experiment_path): mkdir(self.experiment_path) if not path.exists(self.dataset_path): mkdir(self.dataset_path) # write test file self.filename = 'testfile.txt' self.f1 = open(path.join(self.test_dir, self.filename), 'w') self.f1.write('Test file 1') self.f1.close() self.f1_size = path.getsize(path.join(self.test_dir, self.filename)) self.f1 = open(path.join(self.test_dir, self.filename), 'r')
def setUp(self): from os import path, mkdir from tempfile import mkdtemp user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.test_dir = mkdtemp() self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.experiment_path = path.join( settings.FILE_STORE_PATH, str(self.dataset.get_first_experiment().id)) self.dataset_path = path.join(self.experiment_path, str(self.dataset.id)) if not path.exists(self.experiment_path): mkdir(self.experiment_path) if not path.exists(self.dataset_path): mkdir(self.dataset_path) # write test file self.filename = 'testfile.txt' self.f1 = open(path.join(self.test_dir, self.filename), 'w') self.f1.write('Test file 1') self.f1.close() self.f1_size = path.getsize(path.join(self.test_dir, self.filename)) self.f1 = open(path.join(self.test_dir, self.filename), 'r')
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() Location.force_initialize() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId='django_user', entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() dataset = Dataset(description='dataset description...') dataset.save() dataset.experiments.add(experiment) dataset.save() def create_datafile(index): testfile = path.join(path.dirname(__file__), 'fixtures', 'middleware_test%d.txt' % index) size, sha512sum = get_size_and_sha512sum(testfile) datafile = Dataset_File(dataset=dataset, filename=path.basename(testfile), size=size, sha512sum=sha512sum) datafile.save() base_url = 'file://' + path.abspath(path.dirname(testfile)) location = Location.load_location({ 'name': 'test-middleware', 'url': base_url, 'type': 'external', 'priority': 10, 'transfer_provider': 'local'}) replica = Replica(datafile=datafile, url='file://'+path.abspath(testfile), protocol='file', location=location) if index == 1: replica.verify() replica.save() return Dataset_File.objects.get(pk=datafile.pk) self.dataset = dataset self.datafiles = [create_datafile(i) for i in (1,2)]
def _get_experiment(self, entry, user): ''' Finds the Experiment corresponding to this entry. If it doesn't exist, a new Experiment is created and given an appropriate ACL for the user. ''' experimentId, title, public_access = self._get_experiment_details(entry, user) if (experimentId, title) == (None, None): return None if experimentId: try: # Try and match ExperimentID if we have one. param_name = ParameterName.objects.\ get(name=IngestOptions.PARAM_EXPERIMENT_ID, \ schema=AtomImportSchemas.get_schema(Schema.EXPERIMENT)) parameter = ExperimentParameter.objects.\ get(name=param_name, string_value=experimentId) return parameter.parameterset.experiment except ExperimentParameter.DoesNotExist: pass # Failing that, match ExperimentTitle if possible if title: try: experiment = Experiment.objects.get(title=title, created_by=user) return experiment except Experiment.DoesNotExist: pass # No existing expriment - shall we create a new one? if not IngestOptions.ALLOW_EXPERIMENT_CREATION: logging.getLogger(__name__).info("Skipping dataset. ALLOW_EXPERIMENT_CREATION disabled, so ignoring (experimentId: {0}, title: {1}, user: {2})".format( experimentId, title, user)) return None experiment = Experiment(title=title, created_by=user, public_access=public_access) experiment.save() self._create_experiment_id_parameter_set(experiment, experimentId) logging.getLogger(__name__).info("Created experiment {0} (title: {1}, user: {2}, experimentId: {3})".format( experiment.id, experiment.title, experiment.created_by, experimentId)) acl = ExperimentACL(experiment=experiment, pluginId=django_user, entityId=user.id, canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def create_experiment(i): experiment = Experiment(title='Text Experiment #%d' % i, institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() return experiment
def create_experiment(i): experiment = Experiment(title='Text Experiment #%d' % i, institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() return experiment
def setUp(self): self.ns = { 'r': 'http://ands.org.au/standards/rif-cs/registryObjects', 'o': 'http://www.openarchives.org/OAI/2.0/' } user, client = _create_user_and_login() license_ = License( name='Creative Commons Attribution-NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=False, canRead=True, canWrite=True, canDelete=False, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() params = { 'type': 'website', 'identifier': 'https://www.google.com/', 'title': 'Google', 'notes': 'This is a note.' } response = client.post(\ reverse('tardis.apps.related_info.views.'\ +'list_or_create_related_info', args=[experiment.id]), data=json.dumps(params), content_type='application/json') # Check related info was created expect(response.status_code).to_equal(201) self.acl = acl self.client = client self.experiment = experiment self.params = params
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId='django_user', entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() dataset = Dataset(description='dataset description...') dataset.save() dataset.experiments.add(experiment) dataset.save() def create_datafile(index): testfile = path.join(path.dirname(__file__), 'fixtures', 'jeol_sem_test%d.txt' % index) size, sha512sum = get_size_and_sha512sum(testfile) datafile = Dataset_File(dataset=dataset, filename=path.basename(testfile), url='file://'+path.abspath(testfile), protocol='file', size=size, sha512sum=sha512sum) datafile.verify() datafile.save() return datafile self.dataset = dataset self.datafiles = [create_datafile(i) for i in (1,2)]
def test_publish(self): """ Create an experiment and publish it as RIF-CS using RMITANDSService""" login = self.client.login(username=self.username, password=self.pwd) self.assertTrue(login) # Create simple experiment exp = models.Experiment(title='test exp1', institution_name='rmit', created_by=self.user, public=True ) exp.save() acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.assertEqual(exp.title, 'test exp1') self.assertEqual(exp.url, None) self.assertEqual(exp.institution_name, 'rmit') self.assertEqual(exp.approved, False) self.assertEqual(exp.handle, None) self.assertEqual(exp.created_by, self.user) self.assertEqual(exp.public, True) self.assertEqual(exp.get_or_create_directory(), path.join(settings.FILE_STORE_PATH, str(exp.id))) self.experiment_path = path.join(settings.FILE_STORE_PATH, str(exp.id)) # publish data = {'legal':'on', 'profile':'default.xml'} response = self.client.post("/apps/hpctardis/publisher/1/", data) logger.debug("response=%s" % response) # check resulting rif-cs response = self.client.post("/apps/hpctardis/rif_cs/") self.assertTrue(_grep("test exp1",str(response))) self.assertTrue(_grep("<key>http://www.rmit.edu.au/HPC/1</key>",str(response))) self.assertTrue(_grep("""<addressPart type="text">rmit</addressPart>""",str(response))) self.assertFalse(_grep("<key>http://www.rmit.edu.au/HPC/2</key>",str(response))) logger.debug("response=%s" % response)
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Need UserAuthentication UserAuthentication(userProfile=profile, username=username, authenticationMethod='localdb').save() # Create staging dir from os import path, makedirs staging_dir = path.join(settings.STAGING_PATH, username) if not path.exists(staging_dir): makedirs(staging_dir) # Ensure that staging dir is set up properly expect(get_full_staging_path(username)).to_be_truthy() Location.force_initialize() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment,\ canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(experiment) self.dataset.save() self.username, self.password = (username, password)
def setUp(self): user, client = _create_user_and_login() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.save() acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=False, canRead=True, canWrite=False, canDelete=False, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() self.client = client self.experiment = experiment
def generate_experiment(datasets=[], users=[]): from tardis.tardis_portal.models import Experiment, ExperimentACL experiment = Experiment(created_by=users[0]) experiment.save() for ds in datasets: ds.experiments.add(experiment) ds.save() for user in users: acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def setUp(self): self.ns = {'r': 'http://ands.org.au/standards/rif-cs/registryObjects', 'o': 'http://www.openarchives.org/OAI/2.0/'} user, client = _create_user_and_login() license_ = License(name='Creative Commons Attribution-NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=False, canRead=True, canWrite=True, canDelete=False, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() params = {'type': 'website', 'identifier': 'https://www.google.com/', 'title': 'Google', 'notes': 'This is a note.'} response = client.post(\ reverse('tardis.apps.related_info.views.'\ +'list_or_create_related_info', args=[experiment.id]), data=json.dumps(params), content_type='application/json') # Check related info was created expect(response.status_code).to_equal(201) self.acl = acl self.client = client self.experiment = experiment self.params = params
def testRightsRequireValidOwner(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # Create client and login as user client = Client() login = client.login(username=username, password=password) self.assertTrue(login) # Get "Choose Rights" page, and check that we're forbidden rights_url = reverse('tardis.tardis_portal.views.choose_rights', args=[str(experiment.id)]) response = client.get(rights_url) expect(response.status_code).to_equal(403) # Fill in remaining details user.first_name = "Voltaire" # Mononymous persons are just fine user.save() # Get "Choose Rights" page, and check that we're now allowed access response = client.get(rights_url) expect(response.status_code).to_equal(200)
def setUp(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Need UserAuthentication UserAuthentication(userProfile=profile, username=username, authenticationMethod='localdb').save() # Create staging dir from os import path, makedirs staging_dir = path.join(settings.STAGING_PATH, username) if not path.exists(staging_dir): makedirs(staging_dir) # Ensure that staging dir is set up properly expect(get_full_staging_path(username)).to_be_truthy() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment,\ canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.dataset = \ Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(experiment) self.dataset.save() self.username, self.password = (username, password)
def testRightsRequireValidOwner(self): # Create test owner without enough details username, email, password = ('testuser', '*****@*****.**', 'password') user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, isDjangoAccount=True) profile.save() # Create test experiment and make user the owner of it experiment = Experiment(title='Text Experiment', institution_name='Test Uni', created_by=user) experiment.save() acl = ExperimentACL( pluginId=django_user, entityId=str(user.id), experiment=experiment, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # Create client and login as user client = Client() login = client.login(username=username, password=password) self.assertTrue(login) # Get "Choose Rights" page, and check that we're forbidden rights_url = reverse('tardis.tardis_portal.views.choose_rights', args=[str(experiment.id)]) response = client.get(rights_url) expect(response.status_code).to_equal(403) # Fill in remaining details user.first_name = "Voltaire" # Mononymous persons are just fine user.save() # Get "Choose Rights" page, and check that we're now allowed access response = client.get(rights_url) expect(response.status_code).to_equal(200)
def _metadata_extract(self,expname,files,ns,schname,results): """ Check that we can create an VASP experiment and extract metadata from it""" login = self.client.login(username=self.username, password=self.pwd) self.assertTrue(login) exp = models.Experiment(title=expname, institution_name='rmit', created_by=self.user, public=True ) exp.save() acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() self.assertEqual(exp.title, expname) self.assertEqual(exp.url, None) self.assertEqual(exp.institution_name, 'rmit') self.assertEqual(exp.approved, False) self.assertEqual(exp.handle, None) self.assertEqual(exp.created_by, self.user) self.assertEqual(exp.public, True) self.assertEqual(exp.get_or_create_directory(), path.join(settings.FILE_STORE_PATH, str(exp.id))) self.experiment_path = path.join(settings.FILE_STORE_PATH, str(exp.id)) dataset = models.Dataset(description="dataset description...", experiment=exp) dataset.save() for f in files: self._make_datafile(dataset, path.join(path.abspath(path.dirname(__file__)),f)) go() self._test_metadata(ns,schname,dataset,results)
def _create_test_experiment(user, license_): experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create( order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create( order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def testCantEditLockedExperiment(self): login = self.client3.login(username=self.user3.username, password='******') self.assertTrue(login) # user3 has acl to write to experiment3 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user3.id), experiment=self.experiment3, canRead=True, canWrite=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() response = self.client3.get('/experiment/edit/%i/' % (self.experiment3.id)) self.assertEqual(response.status_code, 403) response = self.client3.post( '/experiment/edit/%i/' % (self.experiment3.id), { 'anything': True, }) self.assertEqual(response.status_code, 403) acl.delete() self.client3.logout()
def _create_test_experiment(user, license_): experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create(order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create(order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return experiment
def testCantEditPublicExperiment(self): login = self.client3.login(username=self.user3.username, password='******') self.assertTrue(login) # user3 has acl to write to experiment3 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user3.id), experiment=self.experiment3, canRead=True, canWrite=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() response = self.client3.get('/experiment/edit/%i/' % (self.experiment3.id)) self.assertEqual(response.status_code, 403) response = self.client3.post('/experiment/edit/%i/' % (self.experiment3.id), {'anything': True, }) self.assertEqual(response.status_code, 403) acl.delete() self.client3.logout()
def _create_test_data(): """ Create Single experiment with two owners """ user1 = User(username="******", first_name="Thomas", last_name="Atkins", email="*****@*****.**") user1.save() UserProfile(user=user1).save() user2 = User(username="******", first_name="Joe", last_name="Bloggs", email="*****@*****.**") user2.save() UserProfile(user=user2).save() license_ = License( name="Creative Commons Attribution-NoDerivs 2.5 Australia", url="http://creativecommons.org/licenses/by-nd/2.5/au/", internal_description="CC BY 2.5 AU", allows_distribution=True, ) license_.save() experiment = Experiment(title="Norwegian Blue", description="Parrot + 40kV", created_by=user1) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create(order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create(order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl1 = ExperimentACL( experiment=experiment, pluginId="django_user", entityId=str(user1.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl1.save() acl2 = ExperimentACL( experiment=experiment, pluginId="django_user", entityId=str(user2.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl2.save() return experiment
def setUp(self): """ setting up essential objects, copied from tests above """ user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() self.acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) self.acl.save() self.dataset = Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.dataset_file = Dataset_File(dataset=self.dataset, size=42, filename="foo", md5sum="junk") self.dataset_file.save() self.testschema = Schema(namespace="http://test.com/test/schema", name="Test View", type=Schema.DATAFILE, hidden=True) self.testschema.save() self.dfps = DatafileParameterSet(dataset_file=self.dataset_file, schema=self.testschema) self.dfps.save()
def setUp(self): """ setting up essential objects, copied from tests above """ Location.force_initialize() self.location = Location.get_location('local') user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() self.acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) self.acl.save() self.dataset = Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.dataset_file = Dataset_File(dataset=self.dataset, size=42, filename="foo", md5sum="junk") self.dataset_file.save() self.replica = Replica(datafile=self.dataset_file, url="http://foo", location=self.location, verified=False) self.replica.save()
def _create_datafile(): user = User.objects.create_user('testuser', '*****@*****.**', 'pwd') user.save() UserProfile(user=user).save() full_access = Experiment.PUBLIC_ACCESS_FULL experiment = Experiment.objects.create(title="IIIF Test", created_by=user, public_access=full_access) experiment.save() ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED).save() dataset = Dataset() dataset.save() dataset.experiments.add(experiment) dataset.save() # Create new Datafile tempfile = TemporaryUploadedFile('iiif_stored_file', None, None, None) with Image(filename='magick:rose') as img: img.format = 'tiff' img.save(file=tempfile.file) tempfile.file.flush() datafile = Dataset_File(dataset=dataset) datafile.size = os.path.getsize(tempfile.file.name) #os.remove(tempfilename) datafile.filename = 'iiif_named_file' datafile.url = write_uploaded_file_to_dataset(dataset, tempfile) datafile.verify(allowEmptyChecksums=True) datafile.save() return datafile
def setUp(self): # create a couple of test users self.user1 = User.objects.create_user('testuser1', '', 'secret') self.user2 = User.objects.create_user('testuser2', '', 'secret') self.user3 = User.objects.create_user('testuser3', '', 'secret') self.user4 = User.objects.create_user('testuser4', '', 'secret') # with standard permissions for user in [self.user1, self.user2, self.user3, self.user4]: user.user_permissions.add(Permission.objects.get(codename='add_experiment')) user.user_permissions.add(Permission.objects.get(codename='change_experiment')) user.user_permissions.add(Permission.objects.get(codename='change_group')) user.user_permissions.add(Permission.objects.get(codename='change_userauthentication')) user.user_permissions.add(Permission.objects.get(codename='change_experimentacl')) self.userProfile1 = UserProfile(user=self.user1) self.userProfile2 = UserProfile(user=self.user2) self.userProfile3 = UserProfile(user=self.user3) self.userProfile4 = UserProfile(user=self.user4) # each user will have their own client self.client1 = Client() self.client2 = Client() self.client3 = Client() self.client4 = Client() # user1 will own experiment1 self.experiment1 = Experiment( title='Experiment1', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user1, ) self.experiment1.save() # user2 will own experiment2 self.experiment2 = Experiment( title='Experiment2', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user2, ) self.experiment2.save() # experiment3 is public self.experiment3 = Experiment( title='Experiment3', institution_name='Australian Synchrotron', approved=True, public=True, created_by=self.user3, ) self.experiment3.save() # experiment4 will be accessible based on location information self.experiment4 = Experiment( title='Experiment4', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user1, ) self.experiment4.save() # user1 owns experiment1 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user1.id), experiment=self.experiment1, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # user2 owns experiment2 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user2.id), experiment=self.experiment2, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # experiment4 is accessible via location acl = ExperimentACL( pluginId='ip_address', entityId='127.0.0.1', experiment=self.experiment4, canRead=True, aclOwnershipType=ExperimentACL.SYSTEM_OWNED, ) acl.save()
def _registerExperimentDocument(filename, created_by, expid=None, owners=[], username=None): ''' Register the experiment document and return the experiment id. :param filename: path of the document to parse (METS or notMETS) :type filename: string :param created_by: a User instance :type created_by: :py:class:`django.contrib.auth.models.User` :param expid: the experiment ID to use :type expid: int :param owners: a list of owners :type owner: list :param username: **UNUSED** :rtype: int ''' f = open(filename) firstline = f.readline() f.close() if firstline.startswith('<experiment'): logger.debug('processing simple xml') processExperiment = ProcessExperiment() eid = processExperiment.process_simple(filename, created_by, expid) else: logger.debug('processing METS') eid = parseMets(filename, created_by, expid) # Create a DatasetWrapper for each Dataset experiment = Experiment.objects.get(pk=eid) sample = Sample(experiment=experiment, name="Default Sample", description="A default sample for %s" % experiment.title) sample.save() _create_wrappers_for_datasets(sample, experiment) # Create a Project to wraps the experiment, then create a Sample that # points to the experiment project = Project(experiment=experiment) project.save() auth_key = '' try: auth_key = settings.DEFAULT_AUTH except AttributeError: logger.error('no default authentication for experiment ownership set (settings.DEFAULT_AUTH)') force_user_create = False try: force_user_create = settings.DEFAULT_AUTH_FORCE_USER_CREATE except AttributeError: pass if auth_key: for owner in owners: logger.debug('** Owner : %s' %owner) # for each PI if not owner: continue owner_username = None if '@' in owner: logger.debug('** Email as username **') owner_username = auth_service.getUsernameByEmail(auth_key, owner) if not owner_username: logger.debug('** No existing user!! **') owner_username = owner owner_user = auth_service.getUser(auth_key, owner_username, force_user_create=force_user_create) if owner_user: # if exist, create ACL logger.debug('registering owner: ' + owner) e = Experiment.objects.get(pk=eid) acl = ExperimentACL(experiment=e, pluginId=django_user, entityId=str(owner_user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() # Also update email if '@' in owner: owner_user.email = owner owner_user.save() return eid
def _registerExperimentDocument(filename, created_by, expid=None, owners=[], username=None): ''' Register the experiment document and return the experiment id. :param filename: path of the document to parse (METS or notMETS) :type filename: string :param created_by: a User instance :type created_by: :py:class:`django.contrib.auth.models.User` :param expid: the experiment ID to use :type expid: int :param owners: a list of owners :type owner: list :param username: **UNUSED** :rtype: int ''' f = open(filename) firstline = f.readline() f.close() sync_root = '' if firstline.startswith('<experiment'): logger.debug('processing simple xml') processExperiment = ProcessExperiment() eid, sync_root = processExperiment.process_simple( filename, created_by, expid) else: logger.debug('processing METS') eid, sync_root = parseMets(filename, created_by, expid) auth_key = '' try: auth_key = settings.DEFAULT_AUTH except AttributeError: logger.error('no default authentication for experiment' + ' ownership set (settings.DEFAULT_AUTH)') force_user_create = False try: force_user_create = settings.DEFAULT_AUTH_FORCE_USER_CREATE except AttributeError: pass if auth_key: for owner in owners: # for each PI if not owner: continue owner_username = None if '@' in owner: owner_username = auth_service.getUsernameByEmail( auth_key, owner) if not owner_username: owner_username = owner owner_user = auth_service.getUser( auth_key, owner_username, force_user_create=force_user_create) # if exist, create ACL if owner_user: #logger.debug('registering owner: ' + owner) e = Experiment.objects.get(pk=eid) acl = ExperimentACL(experiment=e, pluginId=django_user, entityId=str(owner_user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return (eid, sync_root)
def _create_test_data(): """ Create Single experiment with two owners """ user1 = User(username='******', first_name='Thomas', last_name='Atkins', email='*****@*****.**') user1.save() UserProfile(user=user1).save() user2 = User(username='******', first_name='Joe', last_name='Bloggs', email='*****@*****.**') user2.save() UserProfile(user=user2).save() license_ = License(name='Creative Commons Attribution-' + 'NoDerivs 2.5 Australia', url='http://creativecommons.org/licenses/by-nd/2.5/au/', internal_description='CC BY 2.5 AU', allows_distribution=True) license_.save() experiment = Experiment(title='Norwegian Blue', description='Parrot + 40kV', created_by=user1) experiment.public_access = Experiment.PUBLIC_ACCESS_FULL experiment.license = license_ experiment.save() experiment.author_experiment_set.create(order=0, author="John Cleese", url="http://nla.gov.au/nla.party-1") experiment.author_experiment_set.create(order=1, author="Michael Palin", url="http://nla.gov.au/nla.party-2") acl1 = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user1.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl1.save() acl2 = ExperimentACL(experiment=experiment, pluginId='django_user', entityId=str(user2.id), isOwner=True, canRead=True, canWrite=True, canDelete=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl2.save() sch, _ = Schema.objects.get_or_create(namespace=settings.KEY_NAMESPACE, name=settings.KEY_NAME, type=Schema.EXPERIMENT) pn, _ = ParameterName.objects.\ get_or_create(schema=sch, name=settings.KEY_NAME, data_type=ParameterName.STRING) return experiment
def _registerExperimentDocument(filename, created_by, expid=None, owners=[], username=None): ''' Register the experiment document and return the experiment id. :param filename: path of the document to parse (METS or notMETS) :type filename: string :param created_by: a User instance :type created_by: :py:class:`django.contrib.auth.models.User` :param expid: the experiment ID to use :type expid: int :param owners: a list of email addresses of users who 'own' the experiment, as registered in the local db :type owner: list :param username: **UNUSED** :rtype: int ''' global current_action, experiment current_action = "Ingest Processing" f = open(filename) firstline = f.readline() f.close() if firstline.startswith('<experiment'): logger.debug('processing simple xml') processExperiment = ProcessExperiment() eid = processExperiment.process_simple(filename, created_by, expid) else: logger.debug('processing METS') try: eid = parseMets(filename, created_by, expid) except SAXParseException: add_status(status=RegistrationStatus.ERROR, message="Processing METS failed: Document isn't XML, or well formed.<br> (%s)" % filename, exception=True) auth_key = '' try: auth_key = settings.DEFAULT_AUTH except AttributeError: add_status(status=RegistrationStatus.ERROR, message='No default authentication for experiment ownership set (settings.DEFAULT_AUTH)') if auth_key: for owner in owners: # for each PI if owner: user = auth_service.getUser({'pluginname': auth_key, 'id': owner}) # if exist, create ACL if user: logger.debug('registering owner: ' + owner) acl = ExperimentACL(experiment=experiment, pluginId=django_user, entityId=str(user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() else: # Make this a critical error add_status(status=RegistrationStatus.ERROR, message="Can't create ACL for experiment: no user found for owner '%s'. Auth_key: %s.: " % (owner, auth_key,)) return experiment.id # unneeded?
def _registerExperimentDocument(filename, created_by, expid=None, owners=[], username=None): ''' Register the experiment document and return the experiment id. :param filename: path of the document to parse (METS or notMETS) :type filename: string :param created_by: a User instance :type created_by: :py:class:`django.contrib.auth.models.User` :param expid: the experiment ID to use :type expid: int :param owners: a list of owners :type owner: list :param username: **UNUSED** :rtype: int ''' f = open(filename) firstline = f.readline() f.close() sync_root = '' if firstline.startswith('<experiment'): logger.debug('processing simple xml') processExperiment = ProcessExperiment() eid, sync_root = processExperiment.process_simple(filename, created_by, expid) else: logger.debug('processing METS') eid, sync_root = parseMets(filename, created_by, expid) auth_key = '' try: auth_key = settings.DEFAULT_AUTH except AttributeError: logger.error('no default authentication for experiment' + ' ownership set (settings.DEFAULT_AUTH)') force_user_create = False try: force_user_create = settings.DEFAULT_AUTH_FORCE_USER_CREATE except AttributeError: pass if auth_key: for owner in owners: # for each PI if not owner: continue owner_username = None if '@' in owner: owner_username = auth_service.getUsernameByEmail(auth_key, owner) if not owner_username: owner_username = owner owner_user = auth_service.getUser(auth_key, owner_username, force_user_create=force_user_create) # if exist, create ACL if owner_user: #logger.debug('registering owner: ' + owner) e = Experiment.objects.get(pk=eid) acl = ExperimentACL(experiment=e, pluginId=django_user, entityId=str(owner_user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() return (eid, sync_root)
def setUp(self): # create a couple of test users self.user1 = User.objects.create_user('testuser1', '', 'secret') self.user2 = User.objects.create_user('testuser2', '', 'secret') self.user3 = User.objects.create_user('testuser3', '', 'secret') self.user4 = User.objects.create_user('testuser4', '', 'secret') # each user will have their own client self.client1 = Client() self.client2 = Client() self.client3 = Client() self.client4 = Client() # user1 will own experiment1 self.experiment1 = Experiment( title='Experiment1', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user1, ) self.experiment1.save() # user2 will own experiment2 self.experiment2 = Experiment( title='Experiment2', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user2, ) self.experiment2.save() # experiment3 is public self.experiment3 = Experiment( title='Experiment3', institution_name='Australian Synchrotron', approved=True, public=True, created_by=self.user3, ) self.experiment3.save() # experiment4 will be accessible based on location information self.experiment4 = Experiment( title='Experiment4', institution_name='Australian Synchrotron', approved=True, public=False, created_by=self.user1, ) self.experiment4.save() # user1 owns experiment1 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user1.id), experiment=self.experiment1, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # user2 owns experiment2 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user2.id), experiment=self.experiment2, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # experiment4 is accessible via location acl = ExperimentACL( pluginId='ip_address', entityId='127.0.0.1', experiment=self.experiment4, canRead=True, aclOwnershipType=ExperimentACL.SYSTEM_OWNED, ) acl.save()
def setUp(self): # create a couple of test users self.user1 = User.objects.create_user('testuser1', '', 'secret') self.user2 = User.objects.create_user('testuser2', '', 'secret') self.user3 = User.objects.create_user('testuser3', '', 'secret') self.user4 = User.objects.create_user('testuser4', '', 'secret') # with standard permissions for user in [self.user1, self.user2, self.user3, self.user4]: user.user_permissions.add( Permission.objects.get(codename='add_experiment')) user.user_permissions.add( Permission.objects.get(codename='change_experiment')) user.user_permissions.add( Permission.objects.get(codename='change_group')) user.user_permissions.add( Permission.objects.get(codename='change_userauthentication')) user.user_permissions.add( Permission.objects.get(codename='change_experimentacl')) self.userProfile1 = UserProfile(user=self.user1) self.userProfile2 = UserProfile(user=self.user2) self.userProfile3 = UserProfile(user=self.user3) self.userProfile4 = UserProfile(user=self.user4) # each user will have their own client self.client1 = Client() self.client2 = Client() self.client3 = Client() self.client4 = Client() # user1 will own experiment1 self.experiment1 = Experiment( title='Experiment1', institution_name='Australian Synchrotron', approved=True, public_access=Experiment.PUBLIC_ACCESS_NONE, created_by=self.user1, ) self.experiment1.save() # user2 will own experiment2 self.experiment2 = Experiment( title='Experiment2', institution_name='Australian Synchrotron', approved=True, public_access=Experiment.PUBLIC_ACCESS_NONE, created_by=self.user2, ) self.experiment2.save() # experiment3 is public & locked self.experiment3 = Experiment( title='Experiment3', institution_name='Australian Synchrotron', approved=True, locked=True, public_access=Experiment.PUBLIC_ACCESS_FULL, created_by=self.user3, ) self.experiment3.save() # experiment4 will be accessible based on location information self.experiment4 = Experiment( title='Experiment4', institution_name='Australian Synchrotron', approved=True, public_access=Experiment.PUBLIC_ACCESS_NONE, created_by=self.user1, ) self.experiment4.save() # user1 owns experiment1 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user1.id), experiment=self.experiment1, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # user2 owns experiment2 acl = ExperimentACL( pluginId=django_user, entityId=str(self.user2.id), experiment=self.experiment2, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) acl.save() # experiment4 is accessible via location acl = ExperimentACL( pluginId='ip_address', entityId='127.0.0.1', experiment=self.experiment4, canRead=True, aclOwnershipType=ExperimentACL.SYSTEM_OWNED, ) acl.save()
def createhpcexperiment(request, user, dl): from django.contrib.auth.models import User from tardis.tardis_portal.views import _registerExperimentDocument import os import tempfile #TODO temp = tempfile.TemporaryFile() for chunk in dl.chunks(): temp.write(chunk) temp.seek(0) metadata = {} for line in temp: key, value = line.split('~') metadata[key] = value temp.close() author = metadata['Name'] title = metadata['Experiment'] ds_desc = metadata['Facility'] desc = metadata['Description'] fname = metadata['FolderName'] counter = metadata['Counter'] package = metadata['Package'] exp = Experiment( title=title, institution_name="RMIT University", description=desc, created_by=User.objects.get(id=user.pk), ) exp.save() eid = exp.id # Store the author for the dataset ae = models.Author_Experiment(experiment=exp, author=author, order='1') ae.save() auth_key = settings.DEFAULT_AUTH try: e = Experiment.objects.get(pk=eid) except Experiment.DoesNotExist: logger.exception( 'Experiment for eid %i in CreateHPCExperiment does not exist' % eid) acl = ExperimentACL(experiment=e, pluginId=django_user, entityId=str(user.id), canRead=True, canWrite=True, canDelete=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED) acl.save() folder_desc = "%s.%s.%s.%s" % (ds_desc.strip(), package.strip(), fname.strip(), counter.strip()) logger.debug('folder_desc = %s' % folder_desc) return eid, exp, folder_desc
class ContextualViewTest(TestCase): def setUp(self): """ setting up essential objects, copied from tests above """ user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() self.acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) self.acl.save() self.dataset = Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.dataset_file = Dataset_File(dataset=self.dataset, size=42, filename="foo", md5sum="junk") self.dataset_file.save() self.testschema = Schema(namespace="http://test.com/test/schema", name="Test View", type=Schema.DATAFILE, hidden=True) self.testschema.save() self.dfps = DatafileParameterSet(dataset_file=self.dataset_file, schema=self.testschema) self.dfps.save() def tearDown(self): self.user.delete() self.exp.delete() self.dataset.delete() self.dataset_file.delete() self.testschema.delete() self.dfps.delete() self.acl.delete() def testDetailsDisplay(self): """ test display of view for an existing schema and no display for an undefined one. """ from tardis.tardis_portal.views import display_datafile_details request = flexmock(user=self.user, groups=[("testgroup",flexmock())]) with self.settings(DATAFILE_VIEWS=[("http://test.com/test/schema", "/test/url"), ("http://does.not.exist", "/false/url")]): response = display_datafile_details(request, dataset_file_id=self.dataset_file.id) self.assertEqual(response.status_code, 200) self.assertTrue("/ajax/parameters/" in response.content) self.assertTrue("/test/url" in response.content) self.assertFalse("/false/url" in response.content)
class ViewTemplateContextsTest(TestCase): def setUp(self): """ setting up essential objects, copied from tests above """ Location.force_initialize() self.location = Location.get_location('local') user = '******' pwd = 'secret' email = '' self.user = User.objects.create_user(user, email, pwd) self.userProfile = UserProfile(user=self.user) self.exp = Experiment(title='test exp1', institution_name='monash', created_by=self.user) self.exp.save() self.acl = ExperimentACL( pluginId=django_user, entityId=str(self.user.id), experiment=self.exp, canRead=True, isOwner=True, aclOwnershipType=ExperimentACL.OWNER_OWNED, ) self.acl.save() self.dataset = Dataset(description='dataset description...') self.dataset.save() self.dataset.experiments.add(self.exp) self.dataset.save() self.dataset_file = Dataset_File(dataset=self.dataset, size=42, filename="foo", md5sum="junk") self.dataset_file.save() self.replica = Replica(datafile=self.dataset_file, url="http://foo", location=self.location, verified=False) self.replica.save() def tearDown(self): self.user.delete() self.exp.delete() self.dataset.delete() self.dataset_file.delete() self.acl.delete() def testExperimentView(self): """ test some template context parameters for an experiment view """ from tardis.tardis_portal.views import view_experiment from tardis.tardis_portal.shortcuts import render_response_index from django.http import HttpRequest from django.template import Context import sys # Default behavior views_module = flexmock(sys.modules['tardis.tardis_portal.views']) request = HttpRequest() request.user=self.user request.groups=[] context = {'organization': ['classic', 'test', 'test2'], 'default_organization': 'classic', 'default_format': 'zip', 'protocol': [['zip', '/download/experiment/1/zip/'], ['tar', '/download/experiment/1/tar/']]} views_module.should_call('render_response_index'). \ with_args(_AnyMatcher(), "tardis_portal/view_experiment.html", _ContextMatcher(context)) response = view_experiment(request, experiment_id=self.exp.id) self.assertEqual(response.status_code, 200) # Behavior with USER_AGENT_SENSING enabled and a request.user_agent saved_setting = getattr(settings, "USER_AGENT_SENSING", None) try: setattr(settings, "USER_AGENT_SENSING", True) request = HttpRequest() request.user=self.user request.groups=[] mock_agent = _MiniMock(os=_MiniMock(family="Macintosh")) setattr(request, 'user_agent', mock_agent); context = {'organization': ['classic', 'test', 'test2'], 'default_organization': 'classic', 'default_format': 'tar', 'protocol': [['tar', '/download/experiment/1/tar/']]} views_module.should_call('render_response_index'). \ with_args(_AnyMatcher(), "tardis_portal/view_experiment.html", _ContextMatcher(context)) response = view_experiment(request, experiment_id=self.exp.id) self.assertEqual(response.status_code, 200) finally: if saved_setting != None: setattr(settings, "USER_AGENT_SENSING", saved_setting) else: delattr(settings, "USER_AGENT_SENSING") def testDatasetView(self): """ test some context parameters for a dataset view """ from tardis.tardis_portal.views import view_dataset from tardis.tardis_portal.shortcuts import render_response_index from django.http import HttpRequest from django.template import Context import sys views_module = flexmock(sys.modules['tardis.tardis_portal.views']) request = HttpRequest() request.user=self.user request.groups=[] context = {'default_organization': 'classic', 'default_format': 'zip'} views_module.should_call('render_response_index'). \ with_args(_AnyMatcher(), "tardis_portal/view_dataset.html", _ContextMatcher(context)) response = view_dataset(request, dataset_id=self.dataset.id) self.assertEqual(response.status_code, 200) # Behavior with USER_AGENT_SENSING enabled and a request.user_agent saved_setting = getattr(settings, "USER_AGENT_SENSING", None) try: setattr(settings, "USER_AGENT_SENSING", True) request = HttpRequest() request.user=self.user request.groups=[] mock_agent = _MiniMock(os=_MiniMock(family="Macintosh")) setattr(request, 'user_agent', mock_agent); context = {'default_organization': 'classic', 'default_format': 'tar'} views_module.should_call('render_response_index'). \ with_args(_AnyMatcher(), "tardis_portal/view_dataset.html", _ContextMatcher(context)) response = view_dataset(request, dataset_id=self.dataset.id) self.assertEqual(response.status_code, 200) finally: if saved_setting != None: setattr(settings, "USER_AGENT_SENSING", saved_setting) else: delattr(settings, "USER_AGENT_SENSING")