async def check_service(request: CheckRequest) -> Verdict: async with Api(f'http://{request.hostname}:3000') as api: first_user = User() second_user = User() try: resp = await api.register(first_user.get_register_data()) if 'userId' not in resp: return Verdict.MUMBLE('Invalid contract in user login', '') first_user.user_id = resp['userId'] resp = await api.register(second_user.get_register_data()) if 'userId' not in resp: return Verdict.MUMBLE('Invalid contract in user login', '') second_user.user_id = resp['userId'] except InvalidResponseException: return Verdict.MUMBLE('Could not register login.', traceback.format_exc()) except: return Verdict.DOWN('Could not connect to service.', traceback.format_exc()) verdict = await check_user_info(api, first_user) if verdict: return verdict verdict = await check_chats(api, first_user, second_user) if verdict: return verdict verdict = await check_users_searching(api, first_user) if verdict: return verdict return Verdict.OK()
async def put_flag_in_deleted_messages(request: PutRequest) -> Verdict: async with Api(f'http://{request.hostname}:3000') as api: user = User() try: await api.register(user.get_register_data()) await api.login(user.username, user.password) except InvalidResponseException as e: return Verdict.MUMBLE('Could not login or register', traceback.format_exc()) except: return Verdict.DOWN('Could not connect to service', traceback.format_exc()) chat_name = utils.generate_random_text() try: chat_resp = await api.create(chat_name) chat_id = chat_resp['chatId'] message_id_resp = await api.send_message(chat_id, request.flag) message_id = message_id_resp['messageId'] await api.delete_message(message_id) except: return Verdict.MUMBLE( 'Could not create chat or invite link or send message', traceback.format_exc()) return Verdict.OK( f'{user.username}:{user.password}:{chat_id}:{message_id}')
async def get_flag_from_messages(request: GetRequest) -> Verdict: chat_id, inv_link, message_id = request.flag_id.split(':') async with Api(f'http://{request.hostname}:3000') as api: user = User() try: await api.register(user.get_register_data()) await api.login(user.username, user.password) await api.join(chat_id, inv_link) except InvalidResponseException as e: return Verdict.MUMBLE('Could not login or register', traceback.format_exc()) except: return Verdict.DOWN('Could not connect to service', traceback.format_exc()) try: resp = await api.read_messages(chat_id) if 'messages' not in resp: return Verdict.MUMBLE('Invalid contract in message getting', '') messages = resp['messages'] message = list( filter(lambda m: int(m['id']) == int(message_id), messages)) if len(message) != 1: return Verdict.CORRUPT('Invalid messages count', f'with id: {message_id}') if 'text' not in message[0]: return Verdict.MUMBLE('Invalid contract in message getting', '') message_content = message[0]['text'] except: return Verdict.DOWN('Invalid response from service', traceback.format_exc()) try: resp = await api.get_chats() if 'chats' not in resp: return Verdict.MUMBLE('Invalid contract in chats listing', 'invalid data') chats = resp['chats'] if not chat_in_chats(chats, chat_id): return Verdict.MUMBLE('Can not find chat in chats', 'invalid /chats') except: return Verdict.DOWN('Invalid response from service', traceback.format_exc()) if message_content != request.flag: return Verdict.CORRUPT( 'Invalid flag', f'{request.flag}, chat id: {chat_id}, invite link: {inv_link}') return Verdict.OK()
async def put_flag_in_bio(request: PutRequest) -> Verdict: async with Api(f'http://{request.hostname}:3000') as api: user = User() try: await api.register(user.get_register_data()) except InvalidResponseException: return Verdict.MUMBLE('Could not register.', traceback.format_exc()) except: return Verdict.DOWN('Could not connect to service.', traceback.format_exc()) try: login_resp = await api.login(user.username, user.password) user_id = login_resp['userId'] await api.edit_user({'biography': request.flag}) except: return Verdict.MUMBLE('Could not login or edit user.', traceback.format_exc()) return Verdict.OK(user_id)