def index(self, **data): """ Login page (with or without messages). """ template_specification = dict(mainContent="login", title="Login", data=data) if cherrypy.request.method == 'POST': form = LoginForm() try: data = form.to_python(data) username = data[KEY_USERNAME] password = data[KEY_PASSWORD] user = self.user_service.check_login(username, password) if user is not None: basecontroller.add2session(basecontroller.KEY_USER, user) basecontroller.set_info_message('Welcome ' + username) self.logger.debug("User " + username + " has just logged in!") if user.selected_project is not None: prj = user.selected_project prj = ProjectService().find_project(prj) self._mark_selected(prj) raise cherrypy.HTTPRedirect('/user/profile') else: basecontroller.set_error_message( 'Wrong username/password, or user not yet validated...' ) self.logger.debug("Wrong username " + username + " !!!") except formencode.Invalid, excep: template_specification[ basecontroller.KEY_ERRORS] = excep.unpack_errors()
def test_get_users_second_page(self): """ Try to get the second page of users for a given project """ for i in range(USERS_PAGE_SIZE + 3): exec 'user_' + str(i) + '= model.User("test_user' + str( i) + '", "test_pass", "*****@*****.**", False, "user")' exec "dao.store_entity(user_" + str(i) + ")" for i in range(USERS_PAGE_SIZE + 3): exec 'member' + str(i) + '=dao.get_user_by_name("test_user' + str( i) + '")' admin = dao.get_user_by_name("test_user1") data = dict(name='test_proj', description='test_desc', users=[ eval('member' + str(i) + '.id') for i in range(USERS_PAGE_SIZE + 3) ]) project = ProjectService().store_project(admin, True, None, **data) page_users, all_users, pag = self.user_service.get_users_for_project( admin.username, project.id, 2) self.assertEqual(len(page_users), (USERS_PAGE_SIZE + 3) % USERS_PAGE_SIZE) self.assertEqual(len(all_users), USERS_PAGE_SIZE + 3, 'Not all members returned') self.assertEqual(pag, 2, 'Invalid page number returned')
def setUp(self): """ Reset the database before each test. """ # self.clean_database() self.project_service = ProjectService() self.test_user = TestFactory.create_user()
def generate_users(nr_users, nr_projects): """ The generate_users method will create a clean state db with :param nr_users = number of users to be generated (with random roles between CLINICIAN and RESEARCHER and random validated state) :param nr_projects =maximum number of projects to be generated for each user """ EVENTS_FOLDER = '' users = [] for i in range(nr_users): coin_flip = random.randint(0, 1) role = 'CLINICIAN' if coin_flip == 1 else 'RESEARCHER' password = md5("test").hexdigest() new_user = model.User("gen" + str(i), password, "*****@*****.**", True, role) dao.store_entity(new_user) new_user = dao.get_user_by_name("gen" + str(i)) ExtremeTestFactory.VALIDATION_DICT[new_user.id] = 0 users.append(new_user) for i in range(nr_users): current_user = dao.get_user_by_name("gen" + str(i)) projects_for_user = random.randint(0, nr_projects) for j in range(projects_for_user): data = dict(name='GeneratedProject' + str(i) + '_' + str(j), description='test_desc', users=ExtremeTestFactory.get_users_ids( random.randint(0, nr_users - 3), nr_users, current_user.id, users)) ProjectService().store_project(current_user, True, None, **data) ExtremeTestFactory.VALIDATION_DICT[current_user.id] += 1
def setUp(self): """ Reset the database before each test. """ EVENTS_FOLDER = '' # self.clean_database() self.project_service = ProjectService() self.structure_helper = FilesHelper() self.test_user = TestFactory.create_user()
def create_project(admin, name="TestProject", description='description', users=[]): """ Create persisted Project entity, with no linked DataTypes. :return: Project entity after persistence. """ data = dict(name=name, description=description, users=users) return ProjectService().store_project(admin, True, None, **data)
def stop_operation(self, operation_id, is_group, remove_after_stop=False): """ Stop the operation given by operation_id. If is_group is true stop all the operations from that group. """ operation_service = OperationService() result = False if int(is_group) == 0: result = operation_service.stop_operation(operation_id) if remove_after_stop: ProjectService().remove_operation(operation_id) else: op_group = ProjectService.get_operation_group_by_id(operation_id) operations_in_group = ProjectService.get_operations_in_group( op_group) for operation in operations_in_group: tmp_res = operation_service.stop_operation(operation.id) if remove_after_stop: ProjectService().remove_operation(operation.id) result = result or tmp_res return result
def test_delete_dt_free_HDD_space(self): """ Launch two operations and give enough available space for user so that both should finish. """ module = "tvb_test.adapters.testadapter3" class_name = "TestAdapterHDDRequired" group = dao.find_group(module, class_name) adapter = FlowService().build_adapter_instance(group) output = adapter.get_output() output_type = output[0].__name__ data = {"test": 100} TVBSettings.MAX_DISK_SPACE = float( adapter.get_required_disk_size(**data)) tmp_folder = FilesHelper().get_project_folder(self.test_project, "TEMP") dts = dao.get_values_of_datatype(self.test_project.id, Datatype2) self.assertEqual(len(dts), 0) self.operation_service.initiate_operation( self.test_user, self.test_project.id, adapter, tmp_folder, method_name=ABCAdapter.LAUNCH_METHOD, **data) dts = dao.get_values_of_datatype(self.test_project.id, Datatype2) self.assertEqual(len(dts), 1) datatype = dao.get_datatype_by_id(dts[0][0]) self.assertEqual(datatype.subject, DataTypeMetaData.DEFAULT_SUBJECT, "Wrong data stored.") self.assertEqual(datatype.type, output_type, "Wrong data stored.") #Now update the maximum disk size to be the size of the previously resulted datatypes (transform from kB to MB) #plus what is estimated to be required from the next one (transform from B to MB) ProjectService().remove_datatype(self.test_project.id, datatype.gid) dts = dao.get_values_of_datatype(self.test_project.id, Datatype2) self.assertEqual(len(dts), 0) self.operation_service.initiate_operation( self.test_user, self.test_project.id, adapter, tmp_folder, method_name=ABCAdapter.LAUNCH_METHOD, **data) dts = dao.get_values_of_datatype(self.test_project.id, Datatype2) self.assertEqual(len(dts), 1) datatype = dao.get_datatype_by_id(dts[0][0]) self.assertEqual(datatype.subject, DataTypeMetaData.DEFAULT_SUBJECT, "Wrong data stored.") self.assertEqual(datatype.type, output_type, "Wrong data stored.")
def setUp(self): """ Prepare before each test. """ self.project_service = ProjectService() self.flow_service = FlowService() self.structure_helper = FilesHelper() self.test_user = TestFactory.create_user() self.test_project = TestFactory.create_project(self.test_user, "ProjectStructure") self.relevant_filter = StaticFiltersFactory.build_datatype_filters(single_filter=StaticFiltersFactory.RELEVANT_VIEW) self.full_filter = StaticFiltersFactory.build_datatype_filters(single_filter=StaticFiltersFactory.FULL_VIEW)
def remove_burst(self, burst_id): """ Remove the burst given by burst_id. If burst is still running just stop it. """ burst_entity = dao.get_burst_by_id(burst_id) if burst_entity.status == burst_entity.BURST_RUNNING: self.stop_burst(burst_entity) return False service = ProjectService() ## Remove each DataType in current burst. ## We can not leave all on cascade, because it won't work on SQLite for mapped dataTypes. datatypes = dao.get_all_datatypes_in_burst(burst_id) ## Get operations linked to current burst before removing the burst or else ## the burst won't be there to identify operations any more. remaining_ops = dao.get_operations_in_burst(burst_id) #Remove burst first to delete work-flow steps which still hold foreign keys to operations. correct = dao.remove_entity(burst_entity.__class__, burst_id) if not correct: raise RemoveDataTypeException("Could not remove Burst entity!") for datatype in datatypes: service.remove_datatype(burst_entity.fk_project, datatype.gid, True) ## Remove all Operations remained. correct = True remaining_op_groups = set() for oper in remaining_ops: is_remaining = dao.get_generic_entity(oper.__class__, oper.id) if len(is_remaining) == 0: ### Operation removed cascaded. continue if oper.fk_operation_group is not None and oper.fk_operation_group not in remaining_op_groups: is_remaining = dao.get_generic_entity(model.OperationGroup, oper.fk_operation_group) if len(is_remaining) > 0: remaining_op_groups.add(oper.fk_operation_group) correct = correct and dao.remove_entity( model.OperationGroup, oper.fk_operation_group) correct = correct and dao.remove_entity(oper.__class__, oper.id) if not correct: raise RemoveDataTypeException( "Could not remove Burst because a linked operation could not be dropped!!" ) return True
def __init__(self): now = datetime.now() micro_postfix = "_%d" % now.microsecond # Here create all structures needed later for data types creation self.files_helper = FilesHelper() # First create user user = model.User("datatype_factory_user" + micro_postfix, "test_pass", "*****@*****.**" + micro_postfix, True, "user") self.user = dao.store_entity(user) # Now create a project project_service = ProjectService() data = dict(name='DatatypesFactoryProject' + micro_postfix, description='test_desc', users=[]) self.project = project_service.store_project(self.user, True, None, **data) # Create algorithm alg_category = model.AlgorithmCategory('one', True) dao.store_entity(alg_category) alg_group = model.AlgorithmGroup("test_module1", "classname1", alg_category.id) dao.store_entity(alg_group) algorithm = model.Algorithm(alg_group.id, 'id', name='', req_data='', param_name='', output='') self.algorithm = dao.store_entity(algorithm) #Create an operation self.meta = { DataTypeMetaData.KEY_SUBJECT: self.USER_FULL_NAME, DataTypeMetaData.KEY_STATE: self.DATATYPE_STATE } operation = model.Operation(self.user.id, self.project.id, self.algorithm.id, 'test parameters', meta=json.dumps(self.meta), status="FINISHED", method_name=ABCAdapter.LAUNCH_METHOD) self.operation = dao.store_entity(operation)
def setUp(self): """ Prepare the database before each test. """ self.import_service = ImportService() self.flow_service = FlowService() self.project_service = ProjectService() self.test_user = TestFactory.create_user() self.test_project = TestFactory.create_project(self.test_user) self.operation = TestFactory.create_operation( test_user=self.test_user, test_project=self.test_project) self.adapter_instance = TestFactory.create_adapter( test_project=self.test_project) result = self.get_all_datatypes() self.assertEqual(len(result), 0, "There should be no data type in DB") TestFactory.import_cff(test_user=self.test_user, test_project=self.test_project)
def setUp(self): """ Reset the database before each test. """ self.import_service = ImportService() self.flow_service = FlowService() self.project_service = ProjectService() self.test_user = TestFactory.create_user() self.test_project = TestFactory.create_project(self.test_user, name="GeneratedProject", description="test_desc") self.operation = TestFactory.create_operation( test_user=self.test_user, test_project=self.test_project) self.adapter_instance = TestFactory.create_adapter( test_project=self.test_project) TestFactory.import_cff(test_user=self.test_user, test_project=self.test_project) self.zip_path = None
def test_get_users_second_page_del(self): """ Try to get the second page of users for a given project where only one user on last page. Then delete that user. """ for i in range(USERS_PAGE_SIZE + 1): exec 'user_' + str(i) + '= model.User("test_user' + str(i) + \ '", "test_pass", "*****@*****.**", False, "user")' exec "dao.store_entity(user_" + str(i) + ")" for i in range(USERS_PAGE_SIZE + 1): exec 'member' + str(i) + '=dao.get_user_by_name("test_user' + str( i) + '")' admin = dao.get_user_by_name("test_user1") data = dict(name='test_proj', description='test_desc', users=[ eval('member' + str(i) + '.id') for i in range(USERS_PAGE_SIZE + 1) ]) project = ProjectService().store_project(admin, True, None, **data) page_users, all_users, pag = self.user_service.get_users_for_project( admin.username, project.id, 2) self.assertEqual(len(page_users), 1, 'Paging not working properly') self.assertEqual(len(all_users), USERS_PAGE_SIZE + 1, 'Not all members returned') self.assertEqual(pag, 2, 'Invalid page number returned') self.user_service.delete_user(member2.id) page_users, all_users, pag = self.user_service.get_users_for_project( admin.username, project.id, 2) self.assertEqual(len(page_users), 0, 'Paging not working properly') self.assertEqual(len(all_users), USERS_PAGE_SIZE, 'Not all members returned') self.assertEqual(pag, 1, 'Invalid page number returned') page_users, all_users, pag = self.user_service.get_users_for_project( admin.username, project.id, 1) self.assertEqual(len(page_users), USERS_PAGE_SIZE, 'Paging not working properly') self.assertEqual(len(all_users), USERS_PAGE_SIZE, 'Not all members returned') self.assertEqual(pag, 1, 'Invalid page number returned')
def prepare_group_launch(self, group_gid, step_key, adapter_key, **data): """ Recieves as input a group gid and an algorithm given by category and id, along with data that gives the name of the required input parameter for the algorithm. Having these generate a range of gid's for all the datatypes in the group and launch a new operation group. """ prj_service = ProjectService() dt_group = prj_service.get_datatypegroup_by_gid(group_gid) datatypes = prj_service.get_datatypes_from_datatype_group(dt_group.id) range_param_name = data['range_param_name'] del data['range_param_name'] data[PARAM_RANGE_1] = range_param_name data[range_param_name] = ','.join([dt.gid for dt in datatypes]) OperationService().group_operation_launch( base.get_logged_user().id, base.get_current_project().id, int(adapter_key), int(step_key), **data) redirect_url = self._compute_back_link('operations', base.get_current_project()) raise cherrypy.HTTPRedirect(redirect_url)
def setUp(self): """ Reset the database before each test. """ initialize_storage() user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") self.test_user = dao.store_entity(user) data = dict(name='test_proj', description='desc', users=[]) self.test_project = ProjectService().store_project(self.test_user, True, None, **data) meta = {DataTypeMetaData.KEY_SUBJECT: "John Doe", DataTypeMetaData.KEY_STATE: "RAW"} algo_group = dao.find_group(SIMULATOR_MODULE, SIMULATOR_CLASS) self.simulator_adapter = FlowService().build_adapter_instance(algo_group) self.operation = model.Operation(self.test_user.id, self.test_project.id, algo_group.id, json.dumps(SIMULATOR_PARAMETERS), meta=json.dumps(meta), status=model.STATUS_STARTED, method_name=ABCAdapter.LAUNCH_METHOD) self.operation = dao.store_entity(self.operation) SIMULATOR_PARAMETERS['connectivity'] = self._create_connectivity(self.CONNECTIVITY_NODES)
def test_get_users_for_project(self): """ Get all members of a project except the current user. """ user_1 = model.User("test_user1", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user_1) user_2 = model.User("test_user2", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user_2) user_3 = model.User("test_user3", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user_3) user_4 = model.User("test_user4", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user_4) user_5 = model.User("test_user5", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user_5) admin = dao.get_user_by_name("test_user1") member1 = dao.get_user_by_name("test_user2") member2 = dao.get_user_by_name("test_user5") data = dict(name="test_proj", description="test_desc", users=[member1.id, member2.id]) project = ProjectService().store_project(admin, True, None, **data) all_users, members, pag = self.user_service.get_users_for_project( admin.username, project.id) self.assertEquals(len(members), 2, "More members than there should be.") self.assertEquals( len(all_users), 5, "Admin should not be viewed as member. " "Neither should users that were not part of the project's users list." ) self.assertEqual(pag, 1, "Invalid total pages number.") for user in all_users: self.assertNotEqual(user.username, admin.username, "Admin is in members!")
def __init__(self): super(ProjectController, self).__init__() self.project_service = ProjectService() self.import_service = ImportService()
def import_project_structure(self, uploaded, user_id): """ Execute import operations: 1. check if ZIP or folder 2. find all project nodes 3. for each project node: - create project - create all operations - import all images - create all dataTypes """ self.user_id = user_id self.created_projects = [] # Now we compute the name of the file where to store uploaded project now = datetime.now() date_str = "%d-%d-%d_%d-%d-%d_%d" % (now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond) uq_name = "%s-ImportProject" % date_str uq_file_name = os.path.join(cfg.TVB_TEMP_FOLDER, uq_name + ".zip") temp_folder = None try: if isinstance(uploaded, FieldStorage) or isinstance( uploaded, Part): if uploaded.file: file_obj = open(uq_file_name, 'wb') file_obj.write(uploaded.file.read()) file_obj.close() else: raise ProjectImportException( "Please select the archive which contains the project structure." ) else: shutil.copyfile(uploaded, uq_file_name) # Now compute the name of the folder where to explode uploaded ZIP file temp_folder = os.path.join(cfg.TVB_TEMP_FOLDER, uq_name) try: self.files_helper.unpack_zip(uq_file_name, temp_folder) except FileStructureException, excep: self.logger.exception(excep) raise ProjectImportException( "Bad ZIP archive provided. A TVB exported project is expected!" ) try: self._import_project_from_folder(temp_folder) except Exception, excep: self.logger.exception(excep) self.logger.debug( "Error encountered during import. Deleting projects created during this operation." ) # Roll back projects created so far project_service = ProjectService() for project in self.created_projects: project_service.remove_project(project.id) raise ProjectImportException(str(excep))