def test_delete_fail(user_id, admin_token): """ Tries to delete user in empty DB. """ data = api.users_list(admin_token) log.debug('empty db users ' + str(data)) existed_users = set() for user in data: existed_users.add(user['id']) log.debug('existed id ' + str(existed_users)) while str(user_id) in existed_users: user_id += 1 api.delete_user(admin_token, user_id, expected_statuses=[400])
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 deregister(user_id): return delete_user(user_id)
def delete(self): if len(self.data) > 0: user = Table.get_selected_row(self) if delete_user(user[1]): del self.data[user[0]] return True
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 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