def test_all_users(self): """ check for all_users """ # create a sorted list of users with adding them to BD users_list = [] for i in '654321': user_for_test = 'test-user-' + i password_for_test = 'test-password-' + i user_obj = UserManager(self._session_for_user, user_for_test) user_obj.add_user(password_for_test) users_list.append(user_for_test) users_list.sort() # check the equivalence of the list of users with the data from the all_users method users_list_from_BD = user_obj.all_users() self.assertEqual(users_list, users_list_from_BD)
def setUp(self) -> None: """Настройка окружения""" # Инициализация User # БД в памяти, без файла engine = create_engine(f'sqlite:///{self.file_path}', echo=False) # Создание файла БД, если его нет if not os.path.isdir(self.dir_path): os.makedirs(self.dir_path) Base.metadata.create_all(engine) self._session_for_user = sessionmaker(bind=engine)() # Добавление тестового пользователя user_obj = UserManager(self._session_for_user, self._test_user) user_obj.add_user(self._test_pwd_user) # Инициализация sqlite3 self._conn_sqlite = sqlite3.connect(self.file_path) self._cursor_sqlite = self._conn_sqlite.cursor()
def test_add_user(self): """ check for add_user """ user_for_test = 'test-user' password_for_test = 'test-password' user_obj = UserManager(self._session_for_user, user_for_test) user_obj.add_user(password_for_test) sql = "SELECT user, password FROM users WHERE user = ?" self._cursor_sqlite.execute(sql, ([user_for_test])) result = self._cursor_sqlite.fetchall() # check that the result is only one line self.assertEqual(1, len(result)) # check that 'user' field is correct self.assertEqual(user_for_test, result[0][0]) # check that the hash(user + password) is written correctly pass_hash = get_hash( (user_for_test + password_for_test).encode("utf-8")) self.assertEqual(pass_hash, result[0][1]) # сheck that exeption is occur when adding a user that already exists exception_occur = False try: user_obj.add_user(password_for_test) except Exception: exception_occur = True self.assertEqual(True, exception_occur)
def test_del_user(self): """ check for del_user """ # add user to BD user_for_test = 'test-user' password_for_test = 'test-password' user_obj = UserManager(self._session_for_user, user_for_test) user_obj.add_user(password_for_test) # delete user from BD, than check that it isn't exists in BD user_obj.del_user() user_exist = user_obj.check_user() self.assertEqual(False, user_exist)
def test_check_user_password(self): """ check for check_user_password """ # add user to BD user_for_test = 'test-user' password_for_test = 'test-password' user_obj = UserManager(self._session_for_user, user_for_test) user_obj.add_user(password_for_test) # check that the check_user_password method confirms the correct password pass_is_correct = user_obj.check_user_password(password_for_test) self.assertEqual(True, pass_is_correct) # check that the check_user_password method doesn't confirm incorrect password pass_is_correct = user_obj.check_user_password( 'some-incorrect-password') self.assertEqual(False, pass_is_correct)
def test_check_user(self): """ check for check_user """ # add user to BD user_for_test = 'test-user' password_for_test = 'test-password' user_obj = UserManager(self._session_for_user, user_for_test) user_obj.add_user(password_for_test) # check that the check_user method confirms user existence in BD user_exist = user_obj.check_user() self.assertEqual(True, user_exist) # check that the check_user method confirms the absence of a user that hasn't been added to BD sql = "SELECT * FROM users WHERE user = ?" self._cursor_sqlite.execute(sql, (['non-existent-user'])) result = self._cursor_sqlite.fetchall() self.assertEqual([], result) # user 'non-existent-user' isn't exists in BD user_exist = user_obj.check_user('non-existent-user') self.assertEqual(False, user_exist)
def test_update_user(self): """ check for update_user """ user_obj = UserManager(self._session_for_user, self._test_user) # add unit to DB unit_obj = UnitManager(self._session_for_user, self._test_user) unit_obj.add_unit( self._test_user, self._test_pwd_user, self._test_login, self._test_pwd_login, self._test_name ) # update username and password new_user = '******' new_password = '******' user_obj.update_user(self.file_path, self._test_pwd_user, new_user, new_password) # check that original user doesn't exist in DB self.assertEqual(False, user_obj.check_user(self._test_user)) # check that new user exist in DB self.assertEqual(True, user_obj.check_user(new_user)) # check that new password for new user is saved correctly user_obj = UserManager(self._session_for_user, new_user) self.assertEqual(True, user_obj.check_user_password(new_password)) # check that unit from original user belongs now to new user and we can get its password unit_obj = UnitManager(self._session_for_user, new_user) self.assertEqual(self._test_pwd_login, unit_obj.get_password(new_user, new_password, self._test_login, self._test_name)) # update username without changing password (the password must be stay on new_password) newer_user = '******' user_obj.update_user(self.file_path, new_password, newer_user) # check that parent user doesn't exist in DB self.assertEqual(False, user_obj.check_user(new_user)) # check that newer user exist in DB self.assertEqual(True, user_obj.check_user(newer_user)) # check that password for newer user doesn't changed user_obj = UserManager(self._session_for_user, newer_user) self.assertEqual(True, user_obj.check_user_password(new_password)) # check that unit from parent user belongs now to newer user and we can get its password unit_obj = UnitManager(self._session_for_user, newer_user) self.assertEqual(self._test_pwd_login, unit_obj.get_password(newer_user, new_password, self._test_login, self._test_name))