def test_create_user_happy_flow(self): """ Standard flow for creating a user. """ initial_user_count = dao.get_all_users() data = dict(username="******", password=md5("test_password").hexdigest(), email="*****@*****.**", role="user", comment="") self.user_service.create_user(**data) final_user_count = dao.get_all_users() self.assertEqual(len(initial_user_count), len(final_user_count) - 1, "User count was not increased after create.") inserted_user = dao.get_user_by_name("test_user") self.assertEqual(inserted_user.password, md5("test_password").hexdigest(), "Incorrect password") self.assertEqual(inserted_user.email, "*****@*****.**", "The email inserted is not correct.") self.assertEqual(inserted_user.role, "user", "The role inserted is not correct.") self.assertFalse(inserted_user.validated, "User validation is not correct.")
def retrieve_all_users(username, current_page=1): """ Return all users from the database except the given user """ start_idx = USERS_PAGE_SIZE * (current_page - 1) total = dao.get_all_users(username, is_count=True) user_list = dao.get_all_users(username, start_idx, USERS_PAGE_SIZE) pages_no = total // USERS_PAGE_SIZE + (1 if total % USERS_PAGE_SIZE else 0) return user_list, pages_no
def retrieve_users_except(username, current_page, page_size): # type: (str, int, int) -> (list, int) """ Return all users from the database except the given user """ start_idx = page_size * (current_page - 1) total = dao.get_all_users(username, is_count=True) user_list = dao.get_all_users(username, start_idx, page_size) pages_no = total // page_size + (1 if total % page_size else 0) return user_list, pages_no
def test_add_entity_forget_commit(self): """ Commit should be done automatically if you forget for some reason to do so in case of new/update/deletes. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 self._dao_add_user_forget_commit() final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count + 1, final_user_count, "Commit should have been done automatically and one more user expected." "Expected %s but got %s"%(initial_user_count, final_user_count))
def test_add_entity_forget_commit(self): """ Commit should be done automatically if you forget for some reason to do so in case of new/update/deletes. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 self._dao_add_user_forget_commit() final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count + 1, final_user_count, "Commit should have been done automatically and one " "more user expected. Expected %s but got %s" % ( initial_user_count, final_user_count))
def test_delete_entity_forget_commit(self): """ Commit should be done automatically if you forget for some reason to do so in case of new/update/deletes. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 stored_user = TestFactory.create_user('username', 'password', 'mail', True, 'role') user_id = stored_user.id self._dao_delete_user_forget_commit(user_id) final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count, final_user_count, "Added user should have been deleted even without explicti commit call.." "Expected %s but got %s"%(initial_user_count, final_user_count))
def test_transaction_happy_flow(self): """ In case no exception is raised the transactional decorator should not influence the data in any way. A successfull commit will be made and the data should be visible in the database. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_users = 21 self._store_users_happy_flow(n_of_users) final_user_count = dao.get_all_users(is_count=True) error_msg = ("Transaction should have committed and %s more users should have been available in the database. " "Expected %s but got %s" % (n_of_users, initial_user_count + n_of_users, final_user_count)) self.assertEqual(initial_user_count + n_of_users, final_user_count, error_msg)
def test_delete_entity_forget_commit(self): """ Commit should be done automatically if you forget for some reason to do so in case of new/update/deletes. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 stored_user = TestFactory.create_user('username', 'password', 'mail', True, 'role') user_id = stored_user.id self._dao_delete_user_forget_commit(user_id) final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count, final_user_count, "Added user should have been deleted even without explicit commit call.." "Expected %s but got %s" % (initial_user_count, final_user_count))
def test_transaction_happy_flow(self): """ In case no exception is raised the transactional decorator should not influence the data in any way. A successfull commit will be made and the data should be visible in the database. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_users = 21 self._store_users_happy_flow(n_of_users) final_user_count = dao.get_all_users(is_count=True) error_msg = ("Transaction should have committed and %s more users should have been available in the database. " "Expected %s but got %s" % (n_of_users, initial_user_count + n_of_users, final_user_count)) assert initial_user_count + n_of_users, final_user_count == error_msg
def test_initialize_startup(self): """ Test "reset_database" and "initialize_startup" calls. """ reset_database() # Table USERS should not exist: with pytest.raises(Exception): dao.get_all_users() initialize_startup() # Table exists, but no rows assert 0 == len(dao.get_all_users()) assert None == dao.get_system_user() # DB revisions folder should exist: assert os.path.exists(TvbProfile.current.db.DB_VERSIONING_REPO)
def test_create_user_happy_flow(self): """ Standard flow for creating a user. """ initial_user_count = dao.get_all_users() data = dict(username="******", display_name="test_name", password=hash_password("test_password"), email="*****@*****.**", role="user", comment="") self.user_service.create_user(**data) final_user_count = dao.get_all_users() assert len(initial_user_count) == len(final_user_count) - 1, "User count was not increased after create." inserted_user = dao.get_user_by_name("test_user") assert inserted_user.password == hash_password("test_password"), "Incorrect password" assert inserted_user.email == "*****@*****.**", "The email inserted is not correct." assert inserted_user.role == "user", "The role inserted is not correct." assert not inserted_user.validated, "User validation is not correct."
def test_transaction_rollback(self): """ If an unhandled exception is raised by a method marked as transactional, all data should be rolled back properly. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_users = 6 try: self._store_users_raises_exception(n_of_users) except Exception: pass final_user_count = dao.get_all_users(is_count=True) assert initial_user_count == final_user_count, "Transaction should have rolled back due to exception." \ "Expected %s but got %s" % (initial_user_count, final_user_count)
def test_transaction_rollback(self): """ If an unhandled exception is raised by a method marked as transactional, all data should be rolled back properly. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_users = 6 try: self._store_users_raises_exception(n_of_users) except Exception: pass final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count, final_user_count, "Transaction should have rolled back due to exception." "Expected %s but got %s" % (initial_user_count, final_user_count))
def test_create_user_happy_flow(self): """ Standard flow for creating a user. """ initial_user_count = dao.get_all_users() data = dict(username="******", password=md5("test_password").hexdigest(), email="*****@*****.**", role="user", comment="") self.user_service.create_user(**data) final_user_count = dao.get_all_users() assert len(initial_user_count) == len(final_user_count) - 1, "User count was not increased after create." inserted_user = dao.get_user_by_name("test_user") assert inserted_user.password == md5("test_password").hexdigest(), "Incorrect password" assert inserted_user.email == "*****@*****.**", "The email inserted is not correct." assert inserted_user.role == "user", "The role inserted is not correct." assert not inserted_user.validated, "User validation is not correct."
def test_multi_threaded_access_overflow_db_connection(self): """ Test that there is no problem with multiple threads accessing dao. Since cfg.MAX_THREADS_NO is set to 20 we just spawn 4 threads each storing 4 users. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_threads = 18 n_of_users_per_thread = 6 self._run_transaction_multiple_threads(n_of_threads, n_of_users_per_thread) final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count + n_of_threads * n_of_users_per_thread, final_user_count, "Each of %s threads should have created %s more users to a total of %s. " "Expected %s but got %s"%(n_of_threads, n_of_users_per_thread, n_of_threads * n_of_users_per_thread, initial_user_count + n_of_threads * n_of_users_per_thread, final_user_count))
def test_multi_threaded_access_overflow_db_connection(self): """ Test that there is no problem with multiple threads accessing dao. Since cfg.MAX_THREADS_NO is set to 20 we just spawn 4 threads each storing 4 users. """ all_users = dao.get_all_users() initial_user_count = len(all_users) if all_users is not None else 0 n_of_threads = 18 n_of_users_per_thread = 6 self._run_transaction_multiple_threads(n_of_threads, n_of_users_per_thread) final_user_count = dao.get_all_users(is_count=True) self.assertEqual(initial_user_count + n_of_threads * n_of_users_per_thread, final_user_count, "Each of %s threads should have created %s more users to a total of %s. " "Expected %s but got %s" % ( n_of_threads, n_of_users_per_thread, n_of_threads * n_of_users_per_thread, initial_user_count + n_of_threads * n_of_users_per_thread, final_user_count))
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() assert 2 == len(available_users) assert self.user_service.check_login("test_user", "test_pass") is not None, "Login failed when it shouldn't."
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() assert 2 == len(available_users) assert self.user_service.check_login("bad_user", "test_pass") is None, "Login succeeded with bad userName."
def test_initialize_startup(self): """ After initialization only sys user should be in USERS table. """ reset_database() self.assertRaises(Exception, dao.get_all_users) initialize_startup() self.assertEqual(len(dao.get_all_users()), 0, "Fault in initialization!")
def test_check_login_happy_flow(self): """ Standard login flow with a valid username and password. """ user = model_project.User("test_user", 'test_name', hash_password("test_pass"), "*****@*****.**", 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", "test_pass") is not None, "Login failed when it shouldn't."
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() self.assertEqual(2, len(available_users)) self.assertTrue(self.user_service.check_login("test_user", "bad_pass") is None, "Login succeeded with bad password.")
def test_check_login_bad_user(self): """ Flow for entering a bad/invalid username. """ user = model_project.User("test_user", 'test_name', hash_password("test_pass"), "*****@*****.**", True, "user") dao.store_entity(user) available_users = dao.get_all_users() assert 2 == len(available_users) assert 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 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_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() if len(available_users) != 2: self.fail("Something went wrong with database reset!") 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() if len(available_users) != 2: self.fail("Something went wrong with database reset!") self.assertTrue(self.user_service.check_login("test_user", "test_pass") is not None, "Login failed when it shouldn't.")
def test_getmemberspage(self): """ Get the first page of the members page. """ users_count = dao.get_all_users(is_count=True) user = TestFactory.create_user('usr', 'display', 'pass') test_project = TestFactory.create_project(user, 'new_name') result = self.project_c.getmemberspage(1, test_project.id) assert result['usersMembers'] == [user.id] # Same users as before should be available since we created new one # as owned for the project. assert len(result['usersList']) == users_count
def test_getmemberspage(self): """ Get the first page of the members page. """ users_count = dao.get_all_users(is_count=True) user = TestFactory.create_user('usr', 'pass') test_project = TestFactory.create_project(user, 'new_name') result = self.project_c.getmemberspage(0, test_project.id) self.assertEqual(result['usersMembers'], []) # Same users as before should be available since we created new one # as owned for the project. self.assertEqual(len(result['usersList']), users_count)
def transactional_setup_method(self): """ Reset the database before each test . """ self.clean_database() self.user_service = UserService() self.user_service.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, skip_import=True) available_users = dao.get_all_users() if len(available_users) != 1: raise AssertionError("Something went wrong with database initialization!")
def setUp(self): """ Reset the database before each test . """ self.clean_database() self.user_service = UserService() self.user_service.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) available_users = dao.get_all_users() if len(available_users) != 1: self.fail("Something went wrong with database initialization!")
def setUp(self): """ Reset the database before each test . """ self.clean_database() self.user_service = UserService() self.user_service.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, skip_import=True) available_users = dao.get_all_users() if len(available_users) != 1: self.fail("Something went wrong with database initialization!")
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_initialize_startup(self): """ Test "reset_database" and "initialize_startup" calls. """ reset_database() # Table USERS should not exist: self.assertRaises(Exception, dao.get_all_users) initialize_startup() # Table exists, but no rows self.assertEqual(0, len(dao.get_all_users())) self.assertEqual(None, dao.get_system_user()) # DB revisions folder should exist: self.assertTrue(os.path.exists(TvbProfile.current.db.DB_VERSIONING_REPO))
def transactional_setup_method(self): """ Reset the database before each test . """ self.clean_database() self.user_service = UserService() self.user_service.create_user(username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME, display_name=TvbProfile.current.web.admin.ADMINISTRATOR_DISPLAY_NAME, password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD, email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL, role=model_project.ROLE_ADMINISTRATOR, skip_import=True) available_users = dao.get_all_users() if len(available_users) != 1: raise AssertionError("Something went wrong with database initialization!")
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 fetch_all_users(page_start=0, page_size=USERS_PAGE_SIZE): """ Return all users from the database without pagination """ return dao.get_all_users(page_size=page_size, page_start=page_start)