Пример #1
0
def test_get_updates(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    today_plus_2 = (today() + timedelta(days=2)).isoformat()
    today_plus_3 = (today() + timedelta(days=3)).isoformat()
    with requests_session(token1) as api:
        host_request_id = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                to_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test message 0")).host_request_id

        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 1"))
        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 2"))
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED,
                text="Test message 3",
            ))

        api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(to_user_id=user2.id,
                                              from_date=today_plus_2,
                                              to_date=today_plus_3,
                                              text="Test message 4"))

        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=host_request_id))
        assert len(res.messages) == 6
        assert res.messages[0].text.text == "Test message 3"
        assert res.messages[
            1].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_CANCELLED
        assert res.messages[2].text.text == "Test message 2"
        assert res.messages[3].text.text == "Test message 1"
        assert res.messages[4].text.text == "Test message 0"
        message_id_3 = res.messages[0].message_id
        message_id_cancel = res.messages[1].message_id
        message_id_2 = res.messages[2].message_id
        message_id_1 = res.messages[3].message_id
        message_id_0 = res.messages[4].message_id

        with pytest.raises(grpc.RpcError) as e:
            api.GetHostRequestUpdates(
                requests_pb2.GetHostRequestUpdatesReq(newest_message_id=0))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT

        res = api.GetHostRequestUpdates(
            requests_pb2.GetHostRequestUpdatesReq(
                newest_message_id=message_id_1))
        assert res.no_more
        assert len(res.updates) == 5
        assert res.updates[0].message.text.text == "Test message 2"
        assert (res.updates[1].message.host_request_status_changed.status ==
                conversations_pb2.HOST_REQUEST_STATUS_CANCELLED)
        assert res.updates[
            1].status == conversations_pb2.HOST_REQUEST_STATUS_CANCELLED
        assert res.updates[2].message.text.text == "Test message 3"
        assert res.updates[3].message.WhichOneof("content") == "chat_created"
        assert res.updates[
            3].status == conversations_pb2.HOST_REQUEST_STATUS_PENDING
        assert res.updates[4].message.text.text == "Test message 4"

        res = api.GetHostRequestUpdates(
            requests_pb2.GetHostRequestUpdatesReq(
                newest_message_id=message_id_1, number=1))
        assert not res.no_more
        assert len(res.updates) == 1
        assert res.updates[0].message.text.text == "Test message 2"
        assert res.updates[
            0].status == conversations_pb2.HOST_REQUEST_STATUS_CANCELLED

    with requests_session(token3) as api:
        # other user can't access
        res = api.GetHostRequestUpdates(
            requests_pb2.GetHostRequestUpdatesReq(
                newest_message_id=message_id_1))
        assert len(res.updates) == 0
Пример #2
0
def test_get_host_request_messages(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    today_plus_2 = (today() + timedelta(days=2)).isoformat()
    today_plus_3 = (today() + timedelta(days=3)).isoformat()
    with requests_session(token1) as api:
        res = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(to_user_id=user2.id,
                                              from_date=today_plus_2,
                                              to_date=today_plus_3,
                                              text="Test request 1"))
        conversation_id = res.host_request_id

    add_message(db, "Test request 1 message 1", user1.id, conversation_id)
    add_message(db, "Test request 1 message 2", user1.id, conversation_id)
    add_message(db, "Test request 1 message 3", user1.id, conversation_id)

    with requests_session(token2) as api:
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=conversation_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED))

        add_message(db, "Test request 1 message 4", user2.id, conversation_id)
        add_message(db, "Test request 1 message 5", user2.id, conversation_id)

        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=conversation_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_REJECTED))

    with requests_session(token1) as api:
        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=conversation_id))
        # 9 including initial message
        assert len(res.messages) == 9
        assert res.no_more

        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=conversation_id, number=3))
        assert not res.no_more
        assert len(res.messages) == 3
        assert res.messages[
            0].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_REJECTED
        assert res.messages[0].WhichOneof(
            "content") == "host_request_status_changed"
        assert res.messages[1].text.text == "Test request 1 message 5"
        assert res.messages[2].text.text == "Test request 1 message 4"

        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=conversation_id,
                last_message_id=res.messages[2].message_id,
                number=6))
        assert res.no_more
        assert len(res.messages) == 6
        assert res.messages[
            0].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED
        assert res.messages[0].WhichOneof(
            "content") == "host_request_status_changed"
        assert res.messages[1].text.text == "Test request 1 message 3"
        assert res.messages[2].text.text == "Test request 1 message 2"
        assert res.messages[3].text.text == "Test request 1 message 1"
        assert res.messages[4].text.text == "Test request 1"
        assert res.messages[5].WhichOneof("content") == "chat_created"
Пример #3
0
def test_SendHostRequestMessage(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    today_plus_2 = (today() + timedelta(days=2)).isoformat()
    today_plus_3 = (today() + timedelta(days=3)).isoformat()
    with requests_session(token1) as api:
        host_request_id = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                to_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 1")).host_request_id

        with pytest.raises(grpc.RpcError) as e:
            api.SendHostRequestMessage(
                requests_pb2.SendHostRequestMessageReq(host_request_id=999,
                                                       text="Test message 1"))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND

        with pytest.raises(grpc.RpcError) as e:
            api.SendHostRequestMessage(
                requests_pb2.SendHostRequestMessageReq(
                    host_request_id=host_request_id, text=""))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_MESSAGE

        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 1"))
        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=host_request_id))
        assert res.messages[0].text.text == "Test message 1"
        assert res.messages[0].author_user_id == user1.id

    with requests_session(token3) as api:
        # other user can't send
        with pytest.raises(grpc.RpcError) as e:
            api.SendHostRequestMessage(
                requests_pb2.SendHostRequestMessageReq(
                    host_request_id=host_request_id, text="Test message 2"))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.HOST_REQUEST_NOT_FOUND

    with requests_session(token2) as api:
        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 2"))
        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(
                host_request_id=host_request_id))
        # including 2 for creation control message and message
        assert len(res.messages) == 4
        assert res.messages[0].text.text == "Test message 2"
        assert res.messages[0].author_user_id == user2.id

        # can't send messages to a rejected, confirmed or cancelled request, but can for accepted
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_REJECTED))
        with pytest.raises(grpc.RpcError) as e:
            api.SendHostRequestMessage(
                requests_pb2.SendHostRequestMessageReq(
                    host_request_id=host_request_id, text="Test message 3"))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.HOST_REQUEST_CLOSED

        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED))

    with requests_session(token1) as api:
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CONFIRMED))
        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 3"))

        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))
        with pytest.raises(grpc.RpcError) as e:
            api.SendHostRequestMessage(
                requests_pb2.SendHostRequestMessageReq(
                    host_request_id=host_request_id, text="Test message 3"))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.HOST_REQUEST_CLOSED
Пример #4
0
def test_respond_host_requests(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    today_plus_2 = (today() + timedelta(days=2)).isoformat()
    today_plus_3 = (today() + timedelta(days=3)).isoformat()

    with requests_session(token1) as api:
        request_id = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                to_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 1")).host_request_id

    # another user can't access
    with requests_session(token3) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.HOST_REQUEST_NOT_FOUND

    with requests_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.NOT_THE_HOST

    with requests_session(token2) as api:
        # non existing id
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=9999,
                    status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND

        # host can't confirm or cancel (host should accept/reject)
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_CONFIRMED))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.INVALID_HOST_REQUEST_STATUS
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.INVALID_HOST_REQUEST_STATUS

        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_REJECTED,
                text="Test rejection message",
            ))
        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(host_request_id=request_id))
        assert res.messages[0].text.text == "Test rejection message"
        assert res.messages[1].WhichOneof(
            "content") == "host_request_status_changed"
        assert res.messages[
            1].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_REJECTED
        # should be able to move from rejected -> accepted
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED))

    with requests_session(token1) as api:
        # can't make pending
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_PENDING))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.INVALID_HOST_REQUEST_STATUS

        # can confirm then cancel
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CONFIRMED))

        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))

        # can't confirm after having cancelled
        with pytest.raises(grpc.RpcError) as e:
            api.RespondHostRequest(
                requests_pb2.RespondHostRequestReq(
                    host_request_id=request_id,
                    status=conversations_pb2.HOST_REQUEST_STATUS_CONFIRMED))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
        assert e.value.details() == errors.INVALID_HOST_REQUEST_STATUS

    # at this point there should be 7 messages
    # 2 for creation, 2 for the status change with message, 3 for the other status changed
    with requests_session(token1) as api:
        res = api.GetHostRequestMessages(
            requests_pb2.GetHostRequestMessagesReq(host_request_id=request_id))
        assert len(res.messages) == 7
        assert res.messages[
            0].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_CANCELLED
        assert res.messages[
            1].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_CONFIRMED
        assert res.messages[
            2].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED
        assert res.messages[
            4].host_request_status_changed.status == conversations_pb2.HOST_REQUEST_STATUS_REJECTED
        assert res.messages[6].WhichOneof("content") == "chat_created"