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): micro_postfix = "_%d" % int(time.time() * 1000000) # 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) ad = model.Algorithm("test_module1", "classname1", alg_category.id) self.algorithm = dao.store_entity(ad) #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=model.STATUS_FINISHED) self.operation = dao.store_entity(operation)
def test_validate_user_happy_flow(self): """ Standard flow for a validate user action. """ user = model.User("test_user", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user) self.assertTrue(self.user_service.validate_user("test_user"), "Validation failed when it shouldn't have.")
def test_validate_user_validated(self): """ Flow for trying to validate a user that was already validated. """ user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") dao.store_entity(user) self.assertFalse(self.user_service.validate_user("test_user"), "Validation invalid.")
def test_remove_project_node(self): """ Test removing of a node from a project. """ inserted_project, gid, gid_op = self._create_value_wrapper(self.test_user) project_to_link = model.Project("Link", self.test_user.id, "descript") project_to_link = dao.store_entity(project_to_link) exact_data = dao.get_datatype_by_gid(gid) dao.store_entity(model.Links(exact_data.id, project_to_link.id)) assert dao.get_datatype_by_gid(gid) is not None, "Initialization problem!" operation_id = dao.get_generic_entity(model.Operation, gid_op, 'gid')[0].id op_folder = self.structure_helper.get_project_folder("test_proj", str(operation_id)) assert os.path.exists(op_folder) sub_files = os.listdir(op_folder) assert 2 == len(sub_files) ### Validate that no more files are created than needed. if(dao.get_system_user() is None): dao.store_entity(model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) self.project_service._remove_project_node_files(inserted_project.id, gid) sub_files = os.listdir(op_folder) assert 1 == len(sub_files) ### operation.xml file should still be there op_folder = self.structure_helper.get_project_folder("Link", str(operation_id + 1)) sub_files = os.listdir(op_folder) assert 2 == len(sub_files) assert dao.get_datatype_by_gid(gid) is not None, "Data should still be in DB, because of links" self.project_service._remove_project_node_files(project_to_link.id, gid) assert dao.get_datatype_by_gid(gid) is None sub_files = os.listdir(op_folder) assert 1 == len(sub_files)
def test_validate_user_non_existent(self): """ Flow for trying to validate a user that doesn't exist in the database. """ user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") dao.store_entity(user) self.assertFalse(self.user_service.validate_user("test_user2"), "Validation done even tho user is non-existent")
def initialize(introspected_modules, load_xml_events=True): """ Initialize when Application is starting. Check for new algorithms or new DataTypes. """ SettingsService().check_db_url(TvbProfile.current.db.DB_URL) ## Initialize DB is_db_empty = initialize_startup() ## Create Projects storage root in case it does not exist. initialize_storage() ## Populate DB algorithms, by introspection event_folders = [] start_introspection_time = datetime.datetime.now() for module in introspected_modules: introspector = Introspector(module) # Introspection is always done, even if DB was not empty. introspector.introspect(True) event_path = introspector.get_events_path() if event_path: event_folders.append(event_path) # Now remove or mark as removed any unverified Algo-Group, Algo-Category or Portlet to_invalidate, to_remove = dao.get_non_validated_entities( start_introspection_time) for entity in to_invalidate: entity.removed = True dao.store_entities(to_invalidate) for entity in to_remove: dao.remove_entity(entity.__class__, entity.id) ## Populate events if load_xml_events: read_events(event_folders) if not TvbProfile.is_first_run(): ## Create default users. if is_db_empty: dao.store_entity( model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) UserService().create_user( username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME, password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, role=model.ROLE_ADMINISTRATOR) ## In case actions related to latest code-changes are needed, make sure they are executed. CodeUpdateManager().run_all_updates() ## In case the H5 version changed, run updates on all DataTypes if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION: thread = threading.Thread( target=FilesUpdateManager().run_all_updates) thread.start() ## Clean tvb-first-time-run temporary folder, as we are no longer at the first run: shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True)
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 """ config.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 test_index_valid_post(self): user = model.User('valid_user', md5('valid_pass').hexdigest(), '*****@*****.**', True, 'CLINICIAN') dao.store_entity(user) login_data = { 'username' : 'valid_user', 'password' : 'valid_pass'} cherrypy.request.method = "POST" self._expect_redirect('/user/profile', self.user_c.index, **login_data)
def clean_database(self, delete_folders=True): """ Deletes data from all tables """ self.cancel_all_operations() LOGGER.warning("Your Database content will be deleted.") try: session = SessionMaker() for table in reversed(model.Base.metadata.sorted_tables): # We don't delete data from some tables, because those are # imported only during introspection which is done one time if table.name not in self.EXCLUDE_TABLES: try: session.open_session() con = session.connection() LOGGER.debug("Executing Delete From Table " + table.name) con.execute(table.delete()) session.commit() except Exception as e: # We cache exception here, in case some table does not exists and # to allow the others to be deleted LOGGER.warning(e) session.rollback() finally: session.close_session() LOGGER.info("Database was cleanup!") except Exception as excep: LOGGER.warning(excep) raise # Now if the database is clean we can delete also project folders on disk if delete_folders: self.delete_project_folders() dao.store_entity(model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None))
def create_user(self, username=None, password=None, password2=None, role=None, email=None, comment=None, email_msg=None, validated=False): """ Service Layer for creating a new user. """ #Basic fields validation. if (username is None) or len(username) < 1: raise UsernameException("Empty UserName!") if (password is None) or len(password) < 1: raise UsernameException("Empty password!") if password2 is None: password2 = password if password != password2: raise UsernameException("Passwords do not match!") try: user_validated = (role == 'ADMINISTRATOR') or validated user = model.User(username, password, email, user_validated, role) if email_msg is None: email_msg = 'Hello ' + username + TEXT_CREATE admin_msg = (TEXT_CREATE_TO_ADMIN + username + ' :\n ' + TvbProfile.current.web.BASE_URL + 'user/validate/' + username + '\n\n"' + str(comment) + '"') self.logger.info("Registering user " + username + " !") if role != 'ADMINISTRATOR' and email is not None: admins = UserService.get_administrators() admin = admins[randint(0, len(admins) - 1)] if admin.email is not None and ( admin.email != TvbProfile.current.web.admin.DEFAULT_ADMIN_EMAIL): # Do not send validation email in case default admin email remained unchanged email_sender.send(FROM_ADDRESS, admin.email, SUBJECT_REGISTER, admin_msg) self.logger.debug("Email sent to:" + admin.email + " for validating user:"******" !") email_sender.send(FROM_ADDRESS, email, SUBJECT_REGISTER, email_msg) self.logger.debug("Email sent to:" + email + " for notifying new user:"******" !") user = dao.store_entity(user) if role == model.ROLE_ADMINISTRATOR: handle_event( ".".join([self.__class__.__name__, "create_admin"]), user) else: handle_event( ".".join([self.__class__.__name__, stack()[0][3]]), user) return TEXT_DISPLAY except Exception, excep: self.logger.error("Could not create user!") self.logger.exception(excep) raise UsernameException(str(excep))
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: "INTERMEDIATE" } 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 _dao_add_user_raise_ex(self, n_users): """ A dao method that is not marked transactional. Even if exception is raised the added user should be present in database unless this is part of a parent transaction that is rolled back by it. """ self.session.add(model.User('username', 'password', 'mail', True, 'role')) self.session.commit() raise Exception("Just to trigger session level rollback instead of transaction level rollback.")
def create_user(username='******', password='******', mail='*****@*****.**', validated=True, role='test'): """ Create persisted User entity. :returns: User entity after persistence. """ user = model.User(username, password, mail, validated, role) return dao.store_entity(user)
def test_is_username_valid(self): """ Test the method that checks if a userName is valid or not (if it already exists in the database the userName is not valid). """ user = model.User("test_user", "test_pass", "*****@*****.**", False, "user") dao.store_entity(user) self.assertFalse(self.user_service.is_username_valid("test_user"), "Should be False but got True") self.assertTrue(self.user_service.is_username_valid("test_user2"), "Should be True but got False")
def setUp(self): # self.clean_database() user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") self.test_user = dao.store_entity(user) project = model.Project("test_proj", self.test_user.id, "description") self.test_project = dao.store_entity(project) import tvb_test self.old_config_file = cfg.CURRENT_DIR cfg.CURRENT_DIR = os.path.dirname(tvb_test.__file__)
def test_check_login_bad_user(self): """ Flow for entering a bad/invalid username. """ user = model.User("test_user", md5("test_pass").hexdigest(), "*****@*****.**", True, "user") dao.store_entity(user) available_users = dao.get_all_users() self.assertEqual(2, len(available_users)) self.assertTrue(self.user_service.check_login("bad_user", "test_pass") is None, "Login succeeded with bad userName.")
def test_check_login_happy_flow(self): """ Standard login flow with a valid username and password. """ user = model.User("test_user", md5("test_pass").hexdigest(), "*****@*****.**", True, "user") dao.store_entity(user) available_users = dao.get_all_users() self.assertEqual(2, len(available_users)) self.assertTrue(self.user_service.check_login("test_user", "test_pass") is not None, "Login failed when it shouldn't.")
def setUp(self): """ Sets up the environment for testing; creates a test user, a test project and saves config file """ # self.clean_database() user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") self.test_user = dao.store_entity(user) project = model.Project("test_proj", self.test_user.id, "description") self.test_project = dao.store_entity(project)
def test_check_login_bad_pass(self): """ Flow for entering a bad/invalid password. """ user = model.User("test_user", md5("test_pass").hexdigest(), "*****@*****.**", True, "user") dao.store_entity(user) available_users = dao.get_all_users() assert 2 == len(available_users) assert self.user_service.check_login( "test_user", "bad_pass") is None, "Login succeeded with bad password."
def test_check_login_bad_pass(self): """ Flow for entering a bad/invalid password. """ user = model.User("test_user", md5("test_pass").hexdigest(), "*****@*****.**", True, "user") dao.store_entity(user) available_users = dao.get_all_users() if len(available_users) != 2: self.fail("Something went wrong with database reset!") self.assertTrue( self.user_service.check_login("test_user", "bad_pass") is None, "Login succeeded with bad password.")
def setUp(self): """ Sets up the environment for testing; creates a test user, a test project and saves config file """ # self.clean_database() user = model.User("test_user", "test_pass", "*****@*****.**", True, "user") self.test_user = dao.store_entity(user) project = model.Project("test_proj", self.test_user.id, "description") self.test_project = dao.store_entity(project) import tvb_test self.old_config_file = cfg.CURRENT_DIR cfg.CURRENT_DIR = os.path.dirname(tvb_test.__file__)
def create_user(self, username=None, password=None, password2=None, role=None, email=None, comment=None, email_msg=None, validated=False, skip_import=False): """ Service Layer for creating a new user. """ if (username is None) or len(username) < 1: raise UsernameException("Empty UserName!") if (password is None) or len(password) < 1: raise UsernameException("Empty password!") if password2 is None: password2 = password if password != password2: raise UsernameException("Passwords do not match!") try: user_validated = (role == 'ADMINISTRATOR') or validated user = model.User(username, password, email, user_validated, role) if email_msg is None: email_msg = 'Hello ' + username + TEXT_CREATE admin_msg = (TEXT_CREATE_TO_ADMIN + username + ' :\n ' + TvbProfile.current.web.BASE_URL + 'user/validate/' + username + '\n\n"' + str(comment) + '"') self.logger.info("Registering user " + username + " !") if role != 'ADMINISTRATOR' and email is not None: admins = UserService.get_administrators() admin = admins[randint(0, len(admins) - 1)] if admin.email is not None and (admin.email != TvbProfile.current.web.admin.DEFAULT_ADMIN_EMAIL): # Do not send validation email in case default admin email remained unchanged email_sender.send(FROM_ADDRESS, admin.email, SUBJECT_REGISTER, admin_msg) self.logger.debug("Email sent to:" + admin.email + " for validating user:"******" !") email_sender.send(FROM_ADDRESS, email, SUBJECT_REGISTER, email_msg) self.logger.debug("Email sent to:" + email + " for notifying new user:"******" !") user = dao.store_entity(user) if role == model.ROLE_ADMINISTRATOR and not skip_import: uploaded = os.path.join(os.path.dirname(tvb_data.__file__), "Default_Project.zip") ImportService().import_project_structure(uploaded, user.id) else: try: default_prj_id = dao.get_project_by_gid(DEFAULT_PROJECT_GID).id dao.add_members_to_project(default_prj_id, [user.id]) except Exception: self.logger.warning( "Could not link user_id: %d with project_gid: %s " % (user.id, DEFAULT_PROJECT_GID)) return TEXT_DISPLAY except Exception as excep: self.logger.exception("Could not create user!") raise UsernameException(str(excep))
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 initialize(introspected_modules, load_xml_events=True): """ Initialize when Application is starting. Check for new algorithms or new DataTypes. """ SettingsService().check_db_url(cfg.DB_URL) ## Initialize DB is_db_empty = initialize_startup() ## Create Projects storage root in case it does not exist. initialize_storage() ## Populate DB algorithms, by introspection event_folders = [] start_introspection_time = datetime.datetime.now() for module in introspected_modules: introspector = Introspector(module) # Introspection is always done, even if DB was not empty. introspector.introspect(True) event_path = introspector.get_events_path() if event_path: event_folders.append(event_path) # Now remove any unverified Algo-Groups, categories or Portlets invalid_stored_entities = dao.get_non_validated_entities( start_introspection_time) for entity in invalid_stored_entities: dao.remove_entity(entity.__class__, entity.id) ## Populate events if load_xml_events: eventhandler.read_events(event_folders) ## Make sure DB events are linked. db_events.attach_db_events() ## Create default users. if is_db_empty: dao.store_entity( model.User(cfg.SYSTEM_USER_NAME, None, None, True, None)) UserService().create_user(username=cfg.ADMINISTRATOR_NAME, password=cfg.ADMINISTRATOR_PASSWORD, email=cfg.ADMINISTRATOR_EMAIL, role=ROLE_ADMINISTRATOR) ## In case actions related to latest code-changes are needed, make sure they are executed. CodeUpdateManager().update_all()
# We cache exception here, in case some table does not exists and # to allow the others to be deleted LOGGER.warning(e) session.rollback() finally: session.close_session() LOGGER.info("Database was cleanup!") except Exception, excep: LOGGER.warning(excep) raise # Now if the database is clean we can delete also project folders on disk if delete_folders: self.delete_project_folders() dao.store_entity( model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None)) def cancel_all_operations(self): """ To make sure that no running operations are left which could make some other test started afterwards to fail, cancel all operations after each test. """ LOGGER.info("Stopping all operations.") op_service = OperationService() operations = self.get_all_entities(model.Operation) for operation in operations: op_service.stop_operation(operation.id) def delete_project_folders(self): """ This method deletes folders for all projects from TVB folder.
# We cache exception here, in case some table does not exists and # to allow the others to be deleted LOGGER.warning(e) session.rollback() finally: session.close_session() LOGGER.info("Database was cleanup!") except Exception, excep: LOGGER.warning(excep) raise # Now if the database is clean we can delete also project folders on disk if delete_folders: self.delete_project_folders() dao.store_entity( model.User(cfg.SYSTEM_USER_NAME, None, None, True, None)) def cancel_all_operations(self): """ To make sure that no running operations are left which could make some other test started afterwards to fail, cancel all operations after each test. """ LOGGER.info("Stopping all operations.") op_service = OperationService() operations = self.get_all_entities(model.Operation) for operation in operations: op_service.stop_operation(operation.id) def delete_project_folders(self): """ This method deletes folders for all projects from TVB folder.
def _dao_add_user_forget_commit(self): """ Test use case where you add user but forget to commit. This should be handled automatically. """ self.session.add(model.User('username', 'password', 'mail', True, 'role'))