def test_user_create_wrong_user(wrong_user, admin_token): """ Tries to create user with wrong fields. """ api.create_user(admin_token, wrong_user, expected_statuses=[501, 400]) # 400 for auto exc of transmute
def testuserinput(user): try: create_user(name=user['name'], password=user['password'], email=user['email']) except UniqueViolation as err: print(err)
def test_user_create_nopassword(nopassword_user, admin_token): """ Tries to create user with no password. """ api.create_user(admin_token, nopassword_user, expected_statuses=[501, 400]) # 400 for auto exc of transmute
def create_account(self): if self.password_field.text() == self.confirm_password_field.text(): create_user(self.username_field.text(), self.password_field.text()) self.close() else: error_dialog = QtWidgets.QErrorMessage() error_dialog.showMessage('Your password fields don\'t match!')
def newuser(): try: api.create_user(name=request.form['name'], password=request.form['password'], email=request.form['email']) return redirect('/') except UniqueViolation as err: print(err) return redirect('/signup/userexists')
def test_users_list_empty_params(user, admin_token): """ Test empty list params list """ api.create_user(admin_token, user) assert len(api.users_list(admin_token)) == 1 + DEFAULT_USERS api.users_list(admin_token, page='-1', expected_statuses=[501, 400]) # 400 for auto exc of transmute
def add_user(): new_user = json.loads(request.data) first_name = new_user.get("first_name") last_name = new_user.get("last_name") email = new_user.get("email") password = new_user.get("password") api.create_user(email, password, first_name, last_name) logging.info(" called api/signup with user " + first_name) return "Success"
def run(self, args_list): uname = input('Username: '******'Enter it again: ') if unsafe_pw == unsafe_pw1: break else: print('Passwords do not match') name = input('Name: ') description = input('Description: ') try: res = api.create_user( uname, name, description, unsafe_pw, self._hosts, ) except api.UserAlreadyExists: return 'User already exists.' except api.InvalidUsername: return 'Invalid username.' res = api.log_in(uname, unsafe_pw, self._hosts) return res
def test_users_list_columns(users, admin_token): """ Creates users and check API request user list """ for user in users: api.create_user(admin_token, user) data = api.users_list(admin_token) assert len(data) == len(users) + DEFAULT_USERS user_dict = {user['email']: user for user in data} for user in users: assert user['email'] in user_dict list_user = user_dict[user['email']] assert list_user['name'] == user['name'] assert list_user['group'] == user['group'] assert 'password' not in list_user assert 'password_hash' not in list_user
def signup_route(): if request.form: username = request.form["username"] password = request.form["password"] if request.json: username = request.json["username"] password = request.json["password"] if username is not None and password is not None: return jsonify({"message": create_user(username, password)}) return jsonify({"message": "something wrong with your credentials"})
def test_user_crud(random_user, admin_token): """ Create user, get user list, delete user. """ random_user['group'] = 'full' new_user_id = api.create_user(admin_token, random_user)['id'] data = api.users_list(admin_token) assert len(data) == 1 + DEFAULT_USERS for resp_user in data: if resp_user['email'] == random_user['email']: break else: assert False, f'Created user [{random_user}] not found in the list [{data}]' api.delete_user(admin_token, new_user_id) assert len(api.users_list(admin_token)) == DEFAULT_USERS
def mail_responder(event, _): """ Main entry point to handle the feedback form :param event: information about the email :return: True when successful, False otherwise """ logger.info('%s: Request received:%s', __name__, str(event['Records'][0]['eventSource'])) try: (source_email, recipient) = parse_ses_notification(event['Records'][0]['ses']) except Exception: logger.error('Error parsing received Email') return False LANG = CONFIG['LANG'] logger.debug('Source Email {} recipient {}'.format(source_email, recipient)) if recipient == CONFIG['TEST_EMAIL']: feedback.send_email(CONFIG['REPLY_EMAIL'], source_email, TEMPLATES['EMAIL_SUBJECT'], 'a', 'a', '', None, CONFIG['FEEDBACK_EMAIL']) return True elif recipient == CONFIG['TEST_EMAIL_NEW']: email_key(source_email, 'https://example.com') return True elif recipient == CONFIG['REPLY_EMAIL']: logger.info('Response to no-reply ignored') return True elif recipient == CONFIG['DELETE_USER_EMAIL']: try: deleted = api.delete_user(user_id=source_email) except Exception: email(source_email, 'try_again.j2') return False if deleted: email(source_email, 'unsubscribed.j2') return False elif recipient == CONFIG['GET_EMAIL']: try: user_exist = api.get_user(source_email) except Exception: logger.error('API error when checking {}'.format(source_email)) email(source_email, 'try_again.j2') return False if not user_exist: try: api.create_user(source_email, 'EM') except Exception: logger.error('API error when Creating {}'.format(source_email)) email(source_email, 'try_again.j2') return False try: new_key = api.get_new_key(user_id=source_email) except Exception: logger.error( 'API error when getting key fo {}'.format(source_email)) email(source_email, 'try_again.j2') return False if not new_key: email(source_email, 'no_key.j2') return False awsurl = ((CONFIG['OUTLINE_AWS_URL']).format( urllib.parse.quote(new_key))) email_key(source_email, awsurl) return True
def test_user_create_wrong_token(user, wrong_token): """ Create user with wrong token """ api.create_user(wrong_token, user, expected_statuses=[401])
def test_user_create_success(user, admin_token): """ Creates user. """ api.create_user(admin_token, user)
def test_user_create_by_demo(user, demo_token): """ Tries to create user with demo (non-admin) rights. """ api.create_user(demo_token, user, expected_statuses=[403])
def test_user_create_by_full(user, full_token): """ Tries to create user with full (non-admin) rights. """ api.create_user(full_token, user, expected_statuses=[403])
def full_token(api_client, full_user, admin_token): """ JWT for full user (non-admin) """ api.create_user(admin_token, full_user) return api.get_token(email=full_user['email'], password=full_user['password'])
def test_user_create_duplicate(user, admin_token): """ Tries to create duplicate users. """ api.create_user(admin_token, user) api.create_user(admin_token, user, expected_statuses=[400])
def demo_token(api_client, demo_user, admin_token): """ JWT for demo user (non-admin) """ api.create_user(admin_token, demo_user) return api.get_token(email=demo_user['email'], password=demo_user['password'])
def bot_handler(event, _): """ Main entry point to handle the bot param event: information about the chat :param _: information about the telegram message (unused) """ logger.info("%s:%s Request received:%s", __name__, str(time.time()), str(event)) try: default_language = event["lang"] logger.info("Language is %s", event["lang"]) except KeyError: default_language = "fa" logger.info("Language is not defined!") try: token = event['token'] except KeyError: logger.error("Token is not defined!") return None try: tmsg = TelegramMessage(event, default_language) logger.info("TMSG object: {}".format(tmsg)) except Exception as exc: logger.error('Error in Telegram Message parsing {} {}'.format( event, str(exc))) return None preferred_lang = dynamodb.get_user_lang(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id) if (preferred_lang is None or preferred_lang not in CONFIG['SUPPORTED_LANGUAGES']): preferred_lang = default_language current_language = CONFIG['SUPPORTED_LANGUAGES'].index(preferred_lang) logger.info('User language is {}'.format(preferred_lang)) change_lang(preferred_lang) tmsg.lang = preferred_lang if tmsg.body == globalvars.lang.text('MENU_BACK_HOME'): telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return if tmsg.command == CONFIG['TELEGRAM_START_COMMAND'] and len( tmsg.command_arg) > 0: tmsg.command = "" tmsg.body = base64.urlsafe_b64decode(tmsg.command_arg) # Check for commands (starts with /) if tmsg.command == CONFIG["TELEGRAM_START_COMMAND"]: dynamodb.create_chat_status(CONFIG['DYNAMO_TABLE'], tmsg.chat_id, STATUSES['START']) telegram.send_message( token, tmsg.chat_id, globalvars.lang.text("MSG_INITIAL_SCREEN").format( CONFIG['VERSION'])) keyboard = make_language_keyboard() telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_SELECT_LANGUAGE'), keyboard) save_chat_status(tmsg.chat_id, STATUSES['SET_LANGUAGE']) return None elif tmsg.command == CONFIG['TELEGRAM_ADMIN_COMMAND']: chat_status = int( dynamodb.get_chat_status(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id)) if not admin_menu(token, tmsg, chat_status): telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME'), globalvars.HOME_KEYBOARD) return None # non-command texts elif tmsg.command == '': # This is a message not started with / chat_status = int( dynamodb.get_chat_status(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id)) if chat_status >= STATUSES['ADMIN_SECTION_HOME']: if not admin_menu(token, tmsg, chat_status): telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME'), globalvars.HOME_KEYBOARD) return None elif chat_status == STATUSES['SET_LANGUAGE']: if (tmsg.body is None or tmsg.body not in globalvars.lang.text('SUPPORTED_LANGUAGES')): message = globalvars.lang.text('MSG_LANGUAGE_CHANGE_ERROR') else: new_lang = CONFIG['SUPPORTED_LANGUAGES'][globalvars.lang.text( 'SUPPORTED_LANGUAGES').index(tmsg.body)] dynamodb.save_user_lang(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id, language=new_lang) change_lang(new_lang) message = globalvars.lang.text('MSG_LANGUAGE_CHANGED').format( tmsg.body) telegram.send_message(token, tmsg.chat_id, message) try: user_exist = api.get_user(tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None if not user_exist: choices, a, b = get_choice(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id) if choices: keyboard = telegram.make_keyboard(choices, 2, '') telegram.send_keyboard( token, tmsg.chat_id, "{}\n{} + {}:".format( globalvars.lang.text("MSG_ASK_CAPTCHA"), a, b), keyboard) save_chat_status(tmsg.chat_id, STATUSES['FIRST_CAPTCHA']) else: telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return None elif chat_status == STATUSES['FIRST_CAPTCHA']: check = check_captcha(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id, sum=int(tmsg.body)) if check: tos = get_tos_link() pp = get_pp_link() if tos is not None: telegram.send_message(token, tmsg.chat_id, tos) if pp is not None: telegram.send_message(token, tmsg.chat_id, pp) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text("MSG_OPT_IN"), globalvars.OPT_IN_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['OPT_IN']) else: telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_WRONG_CAPTCHA')) choices, a, b = get_choice(table=CONFIG["DYNAMO_TABLE"], chat_id=tmsg.chat_id) if choices: keyboard = telegram.make_keyboard(choices, 2, '') telegram.send_keyboard( token, tmsg.chat_id, "{}\n{} + {}:".format( globalvars.lang.text("MSG_ASK_CAPTCHA"), a, b), keyboard) save_chat_status(tmsg.chat_id, STATUSES['FIRST_CAPTCHA']) return None elif chat_status == STATUSES['OPT_IN']: if tmsg.body == globalvars.lang.text( 'MENU_PRIVACY_POLICY_CONFIRM'): try: api.create_user(user_id=tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) else: telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text('MSG_PRIVACY_POLICY_DECLINE'), globalvars.OPT_IN_DECLINED_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['OPT_IN_DECLINED']) return None elif chat_status == STATUSES['OPT_IN_DECLINED']: if tmsg.body == globalvars.lang.text('MENU_BACK_PRIVACY_POLICY'): telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text("MSG_OPT_IN"), globalvars.OPT_IN_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['OPT_IN']) elif tmsg.body == globalvars.lang.text( 'MENU_HOME_CHANGE_LANGUAGE'): keyboard = make_language_keyboard() telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text('MSG_SELECT_LANGUAGE'), keyboard) save_chat_status(tmsg.chat_id, STATUSES['SET_LANGUAGE']) return None elif chat_status == STATUSES['HOME']: if tmsg.body == globalvars.lang.text('MENU_HOME_EXISTING_KEY'): try: user_exist = api.get_user(tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None if not user_exist: telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_NO_ACCOUNT'), parse='MARKDOWN') telegram.send_message(token, tmsg.chat_id, '/start') return None elif not user_exist['outline_key']: telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_NO_EXISTING_KEY')) else: awsurl = (CONFIG['OUTLINE_AWS_URL'].format( urllib.parse.quote(user_exist['outline_key']))) telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_EXISTING_KEY_A').format( awsurl), parse='MARKDOWN') telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_EXISTING_KEY_B'), parse='MARKDOWN') telegram.send_message(token, tmsg.chat_id, user_exist['outline_key']) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return None elif tmsg.body == globalvars.lang.text('MENU_CHECK_STATUS'): blocked = False banned = False try: user_info = api.get_outline_user(tmsg.user_uid) vpnuser = api.get_user(tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None banned = vpnuser['banned'] telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_ACCOUNT_INFO_BANNED') \ if banned else globalvars.lang.text('MSG_ACCOUNT_INFO_OK') ) if not banned: if user_info is not None: try: serverinfo = api.get_outline_server_info( user_info['server']) except Exception: telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None if serverinfo is not None: blocked = serverinfo['is_blocked'] telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_SERVER_INFO_BLOCKED') \ if blocked else globalvars.lang.text('MSG_SERVER_INFO_OK') ) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_NEW_KEY'): try: user_exist = api.get_user(tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None if not user_exist: logger.info("New user: {}".format(tmsg.user_uid)) telegram.send_message( token, tmsg.chat_id, globalvars.lang.text('MSG_NO_ACCOUNT'), parse='MARKDOWN') telegram.send_message(token, tmsg.chat_id, '/start') return None elif not user_exist['outline_key']: create_new_key(tmsg, token) telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return None issues_dict = api.get_issues(tmsg.lang) issues = list(issues_dict.values()) keyboard = telegram.make_keyboard(issues, 2, '') telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text("MSG_ASK_ISSUE"), keyboard) save_chat_status(tmsg.chat_id, STATUSES['ASK_ISSUE']) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_FAQ'): telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_FAQ_URL')) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_INSTRUCTION'): photo_name = "" with open(photo_name, 'rb') as photofile: telegram.send_photo(token, tmsg.chat_id, photofile.read(), "instructions") telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) return None elif tmsg.body == globalvars.lang.text( 'MENU_HOME_CHANGE_LANGUAGE'): keyboard = make_language_keyboard() telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text('MSG_SELECT_LANGUAGE'), keyboard) save_chat_status(tmsg.chat_id, STATUSES['SET_LANGUAGE']) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_PRIVACY_POLICY'): telegram.send_message(token, tmsg.chat_id, get_pp_link()) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_SUPPORT'): telegram.send_message(token, tmsg.chat_id, globalvars.lang.text("MSG_SUPPORT_BOT")) telegram.send_message(token, tmsg.chat_id, CONFIG["SUPPORT_BOT"]) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) return None elif tmsg.body == globalvars.lang.text('MENU_HOME_DELETE_ACCOUNT'): keyboard = telegram.make_keyboard( globalvars.lang.text('MENU_DELETE_REASONS'), 2, globalvars.lang.text('MENU_BACK_HOME')) telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text("MSG_ASK_DELETE_REASONS"), keyboard) save_chat_status(tmsg.chat_id, STATUSES['DELETE_ACCOUNT_REASON']) return None elif chat_status == STATUSES['ASK_ISSUE']: issues_dict = api.get_issues(tmsg.lang) issue_ids = [ key for (key, value) in issues_dict.items() if value == tmsg.body ] if not issue_ids: telegram.send_message( token, tmsg.chat_id, globalvars.lang.text("MSG_UNSUPPORTED_COMMAND")) else: create_new_key(tmsg, token, issue_ids[0]) telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return None elif chat_status == STATUSES['DELETE_ACCOUNT_REASON']: if tmsg.body in globalvars.lang.text('MENU_DELETE_REASONS'): reason_id = globalvars.lang.text('MENU_DELETE_REASONS').index( tmsg.body) logger.debug( 'user {} wants to delete her account because {}'.format( tmsg.user_uid, tmsg.body)) try: deleted = api.delete_user(user_id=tmsg.user_uid) except Exception: telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR'), globalvars.HOME_KEYBOARD) return None if deleted: telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text("MSG_DELETED_ACCOUNT"), globalvars.BACK_TO_HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['DELETE_ACCOUNT_CONFIRM']) return None else: # unsupported message from user telegram.send_message( token, tmsg.chat_id, globalvars.lang.text("MSG_UNSUPPORTED_COMMAND")) try: user_exist = api.get_user(tmsg.user_uid) except Exception: telegram.send_message(token, tmsg.chat_id, globalvars.lang.text('MSG_ERROR')) return None if not user_exist: # start from First step keyboard = make_language_keyboard() telegram.send_keyboard( token, tmsg.chat_id, globalvars.lang.text('MSG_SELECT_LANGUAGE'), keyboard) save_chat_status(tmsg.chat_id, STATUSES['SET_LANGUAGE']) return None else: telegram.send_keyboard(token, tmsg.chat_id, globalvars.lang.text('MSG_HOME_ELSE'), globalvars.HOME_KEYBOARD) save_chat_status(tmsg.chat_id, STATUSES['HOME']) return None