Ejemplo n.º 1
0
 def __init__(self):
     self.username = utils.generate_random_text()
     self.password = utils.generate_random_text()
     self.biography = utils.generate_random_text()
     self.last_name = utils.generate_random_text()
     self.first_name = utils.generate_random_text()
     self.user_id = None
     self.is_admin = False
     self.chat_id = None
Ejemplo n.º 2
0
async def _try_create_user(api: BaseApi):
    current_retry_count = RETRY_CREATE_USER_COUNT
    username = utils.generate_random_text()
    password = utils.generate_random_text(64, min_length=6)
    status, user = await api.create_user(username, password)
    while current_retry_count > 0:
        if status == 200:
            return username, password, status, user
        username = utils.generate_random_text()
        password = utils.generate_random_text(64, min_length=6)
        status, user = await api.create_user(username, password)
        current_retry_count -= 1
    return username, password, status, user
Ejemplo n.º 3
0
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}')
Ejemplo n.º 4
0
async def check_service(request: CheckRequest) -> Verdict:
    async with Api(request.hostname) as api:
        data = utils.generate_random_text()
        with create_playlist_file(data) as playlist:
            try:
                downloaded_json = (await
                                   api.upload_playlist(playlist.playlist_name))
            except Exception as e:
                return Verdict.DOWN(str(e), traceback.format_exc())

        if "created" not in downloaded_json:
            return Verdict.MUMBLE("Bad json", "'created' not in answer")
        music_id = downloaded_json["created"]

        music_hashes = set(playlist.music_hashes)
        image_hashes = set(playlist.image_hashes)
        preprocessed_tags = [
            "_".join((x["artist"], x["title"], x["album"]))
            for x in playlist.tags
        ]

        for i, _ in enumerate(music_hashes):
            try:
                music = await api.download_music(music_id, i)
                image = await api.download_image(preprocessed_tags[i])
            except Exception as e:
                return Verdict.DOWN(str(e), traceback.format_exc())

            if hashlib.sha1(image).hexdigest() not in image_hashes:
                return Verdict.MUMBLE("Broken img", "Digests mismatch!")

            if hashlib.sha1(music).hexdigest() not in music_hashes:
                return Verdict.MUMBLE("Broken format", "Digests mismatch!")

    return Verdict.OK()
Ejemplo n.º 5
0
async def _check_frontend_api(request: CheckRequest) -> Verdict:
    async with FrontendApi(request.hostname) as api:
        username, password, status, user = await _try_create_user(api)
        if status != 200:
            return Verdict.MUMBLE(
                "Can't create user", f"Wrong status code [user.create],"
                f" expect = 200, real = {status}, username={username}, status={status}, after {RETRY_CREATE_USER_COUNT}"
                f" attempt")
        status, user = await api.login_user(username, password)
        if status != 200:
            return Verdict.MUMBLE(
                f"Can't login user", f"Wrong status code [user.login],"
                f"expect = 200, real = {status}")

        status, usernames = await api.our_users()
        if status != 200:
            return Verdict.MUMBLE(
                f"Can't get users", f"Wrong status code [user.usernames],"
                f"expect = 200, real = {status}")
        if user['username'] not in usernames:
            return Verdict.MUMBLE(f"Can't find username in usernames",
                                  f"usernames: {usernames.join(', ')}")

        api_verdict = await _check_api(api)
        if api_verdict != Verdict.OK():
            return api_verdict
        playlist_name = utils.generate_random_text()
        playlist_description = utils.generate_random_text(256)
        status, playlist_public = await api.create_playlist(
            playlist_name, playlist_description, False)
        if status != 200:
            return Verdict.MUMBLE(
                "Can't create playlist",
                f"Wrong status code [playlist.create], "
                f"expect = 200, real = {status}")
        status, _ = await api.get_shared_playlist(playlist_public, user)
        if status != 200:
            return Verdict.MUMBLE(
                "Can't get public playlist by hash",
                f"Wrong status code [playlist.get_by_hash], "
                f"expect = 200, real = {status}")
    return Verdict.OK()
Ejemplo n.º 6
0
async def put_flag_into_the_service(request: PutRequest) -> Verdict:
    async with FrontendApi(request.hostname) as api:
        username, password, status, user = await _try_create_user(api)
        if status != 200:
            return Verdict.MUMBLE(
                "Can't create user", f"Wrong status code [user.create],"
                f"expect = 200, real = {status}")
        status, user = await api.login_user(username, password)
        if status != 200:
            return Verdict.MUMBLE(
                f"Can't login user", f"Wrong status code [user.login],"
                f"expect = 200, real = {status}")
        playlist_name = utils.generate_random_text(length=256)
        playlist_description = request.flag
        status, playlist = await api.create_playlist(playlist_name,
                                                     playlist_description,
                                                     True)
        if status != 200:
            return Verdict.MUMBLE(
                "Can't create playlist",
                f"Wrong status code [playlist.create], "
                f"expect = 200, real = {status}")
    playlist_id = playlist["ID"]
    return Verdict.OK(f'{playlist_id}:{username}:{password}')
Ejemplo n.º 7
0
async def check_chats(api, first_user, second_user):
    try:
        await api.login(first_user.username, first_user.password)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not login.', traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())
    chat_name = utils.generate_random_text()
    try:
        resp = await api.create(chat_name)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not create chat.', traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())

    if 'chatId' not in resp:
        return Verdict.MUMBLE('Invalid contract in chat creating', '')
    chat_id = resp['chatId']

    try:
        resp = await api.get_invite_link(chat_id)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not create invite link.',
                              traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())
    if 'inviteLink' not in resp:
        return Verdict.MUMBLE('Invalid contract in generating invite link', '')

    inv_link = resp['inviteLink']
    first_message_content = utils.generate_random_text()

    try:
        resp = await api.send_message(chat_id, first_message_content)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not send message.',
                              traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())

    if 'messageId' not in resp:
        return Verdict.MUMBLE('Invalid contract in messages sending', '')
    first_message_id = resp['messageId']

    try:
        await api.login(second_user.username, second_user.password)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not login.', traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())
    try:
        await api.join(chat_id, inv_link)
    except InvalidResponseException:
        return Verdict.MUMBLE('Could not register login.',
                              traceback.format_exc())
    except:
        return Verdict.DOWN('Could not connect to service.',
                            traceback.format_exc())

    resp = await api.read_messages(chat_id)
    if 'messages' not in resp:
        return Verdict.MUMBLE('Invalid contract in messages reading', '')
    messages = resp['messages']
    if not message_in_messages(messages, first_message_id,
                               first_message_content):
        return Verdict.MUMBLE('Could not read messages of other user',
                              'deleted by other user')
Ejemplo n.º 8
0
async def _check_api(api: BaseApi):
    playlist_name = utils.generate_random_text()
    playlist_description = utils.generate_random_text(256)
    status, playlist_private = await api.create_playlist(
        playlist_name, playlist_description, True)
    if status != 200:
        return Verdict.MUMBLE(
            f"Can't create playlist", f"Wrong status code [create.playlist], "
            f"expect = 200, real = {status}."
            f"Params: playlist_name={playlist_name}, playlist_description={playlist_description}, private=false"
        )
    if not playlist_private["private"]:
        return Verdict.MUMBLE("Private playlist is corrupt",
                              "Wrong field in playlist")

    playlist_name = utils.generate_random_text()
    playlist_description = utils.generate_random_text(256)
    status, playlist_public = await api.create_playlist(
        playlist_name, playlist_description, False)
    if status != 200:
        return Verdict.MUMBLE(
            "Can't create playlist", f"Wrong status code [playlist.create], "
            f"expect = 200, real = {status}")
    if playlist_public["private"]:
        return Verdict.MUMBLE("Public playlist is corrupt",
                              "Wrong field in playlist")

    status, playlist_list = await api.list_playlists()
    if status != 200:
        return Verdict.MUMBLE(
            "Can't get playlist list",
            f"Wrong status code [playlist.get], expect = 200,"
            f"real = {status}")
    playlist_list_count = len(playlist_list)
    if playlist_list_count > 2:
        return Verdict.MUMBLE(
            "Something wrong with playlist",
            f"Playlists count {playlist_list_count}, expect = 2")

    status, _ = await api.delete_playlist(playlist_public["ID"])
    if status != 200:
        return Verdict.MUMBLE(
            "Can't delete playlist", f"Wrong status code [playlist.delete], "
            f"expect = 200, real = {status}")

    status, track = await api.create_track(playlist_private["ID"])
    if status != 200:
        return Verdict.MUMBLE(
            "Can't create track", f"Wrong status code [track.create], "
            f"expect = 200, real = {status}")

    status, playlist = await api.get_playlist(playlist_private["ID"])
    if status != 200:
        return Verdict.MUMBLE(
            "Can't get playlist",
            f"Wrong status code [playlist.get], expect = 200,"
            f"real = {status}")
    if len(playlist["tracks"]) != 1:
        return Verdict.MUMBLE(
            "Can't get tracks",
            f"Wrong status code [playlist.get], expect = 200,"
            f"real = {status}")

    status, track = await api.delete_track(track["ID"])
    if status != 200:
        return Verdict.MUMBLE(
            "Can't delete track", f"Wrong status code [track.delete], "
            f"expect = 200, real = {status}")
    return Verdict.OK()