Example #1
0
def test_ListHostRequests_active_filter(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:
        request_id = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 1")).host_request_id
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_CANCELLED))

    with requests_session(token2) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert len(res.host_requests) == 1
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_active=True))
        assert len(res.host_requests) == 0
Example #2
0
def test_GetHostRequest(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(
                host_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.GetHostRequest(
                requests_pb2.GetHostRequestReq(host_request_id=999))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.HOST_REQUEST_NOT_FOUND

        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id, text="Test message 1"))

        res = api.GetHostRequest(
            requests_pb2.GetHostRequestReq(host_request_id=host_request_id))
        assert res.latest_message.text.text == "Test message 1"
Example #3
0
def test_regression_disappearing_refs(db, hs):
    """
    Roughly the reproduction steps are:
    * Send a host request, then have both host and surfer accept
    * Wait for it to elapse (or hack it with SQL like what you told me to do)
    * On the surfer account, leave a reference
    * Then on the host account, the option to leave a reference is then not available
    """
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    req_start = (today() + timedelta(days=2)).isoformat()
    req_end = (today() + timedelta(days=3)).isoformat()
    with requests_session(token1) as api:
        res = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                              from_date=req_start,
                                              to_date=req_end,
                                              text="Test request"))
        host_request_id = res.host_request_id
        assert (api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_sent=True)).host_requests[0].
                latest_message.text.text == "Test request")

    with requests_session(token2) as api:
        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))

    with references_session(token1) as api:
        res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
        assert len(res.pending_references) == 0
        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user2.id))
        assert len(res.available_write_references) == 0

    with references_session(token2) as api:
        res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
        assert len(res.pending_references) == 0
        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user1.id))
        assert len(res.available_write_references) == 0

    # hack the time backwards
    hack_req_start = today() - timedelta(days=10) + timedelta(days=2)
    hack_req_end = today() - timedelta(days=10) + timedelta(days=3)
    with session_scope() as session:
        host_request = session.execute(select(HostRequest)).scalar_one()
        assert host_request.conversation_id == host_request_id
        host_request.from_date = hack_req_start
        host_request.to_date = hack_req_end

    with references_session(token1) as api:
        res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
        assert len(res.pending_references) == 1
        assert res.pending_references[0].host_request_id == host_request_id
        assert res.pending_references[
            0].reference_type == references_pb2.REFERENCE_TYPE_SURFED

        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user2.id))
        assert len(res.available_write_references) == 1
        assert res.available_write_references[
            0].host_request_id == host_request_id
        assert res.available_write_references[
            0].reference_type == references_pb2.REFERENCE_TYPE_SURFED

    with references_session(token2) as api:
        res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
        assert len(res.pending_references) == 1
        assert res.pending_references[0].host_request_id == host_request_id
        assert res.pending_references[
            0].reference_type == references_pb2.REFERENCE_TYPE_HOSTED

        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user1.id))
        assert len(res.available_write_references) == 1
        assert res.available_write_references[
            0].host_request_id == host_request_id
        assert res.available_write_references[
            0].reference_type == references_pb2.REFERENCE_TYPE_HOSTED

    if hs == "host":
        with references_session(token2) as api:
            api.WriteHostRequestReference(
                references_pb2.WriteHostRequestReferenceReq(
                    host_request_id=host_request_id,
                    text="Good stuff",
                    was_appropriate=True,
                    rating=0.86,
                ))

        with references_session(token2) as api:
            res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
            assert len(res.pending_references) == 0

            res = api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user2.id))
            assert len(res.available_write_references) == 0

        with references_session(token1) as api:
            res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
            assert len(res.pending_references) == 1
            assert res.pending_references[0].host_request_id == host_request_id
            assert res.pending_references[
                0].reference_type == references_pb2.REFERENCE_TYPE_SURFED

            res = api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user2.id))
            assert len(res.available_write_references) == 1
            assert res.available_write_references[
                0].host_request_id == host_request_id
            assert res.available_write_references[
                0].reference_type == references_pb2.REFERENCE_TYPE_SURFED
    else:
        with references_session(token1) as api:
            api.WriteHostRequestReference(
                references_pb2.WriteHostRequestReferenceReq(
                    host_request_id=host_request_id,
                    text="Good stuff",
                    was_appropriate=True,
                    rating=0.86,
                ))

        with references_session(token1) as api:
            res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
            assert len(res.pending_references) == 0

            res = api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user1.id))
            assert len(res.available_write_references) == 0

        with references_session(token2) as api:
            res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
            assert len(res.pending_references) == 1
            assert res.pending_references[0].host_request_id == host_request_id
            assert res.pending_references[
                0].reference_type == references_pb2.REFERENCE_TYPE_HOSTED

            res = api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user1.id))
            assert len(res.available_write_references) == 1
            assert res.available_write_references[
                0].host_request_id == host_request_id
            assert res.available_write_references[
                0].reference_type == references_pb2.REFERENCE_TYPE_HOSTED
Example #4
0
def test_process_send_request_notifications_host_request(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()

    process_send_request_notifications(empty_pb2.Empty())

    # should find no jobs, since there's no messages
    with session_scope() as session:
        assert session.execute(
            select(func.count()).select_from(BackgroundJob)).scalar_one() == 0

    # first test that sending host request creates email
    with requests_session(token1) as requests:
        host_request_id = requests.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id

    with session_scope() as session:
        # delete send_email BackgroundJob created by CreateHostRequest
        session.execute(
            delete(BackgroundJob).execution_options(synchronize_session=False))

        # check process_send_request_notifications successfully creates background job
        with patch("couchers.jobs.handlers.now", now_5_min_in_future):
            process_send_request_notifications(empty_pb2.Empty())
        assert (session.execute(
            select(func.count()).select_from(BackgroundJob).where(
                BackgroundJob.job_type ==
                BackgroundJobType.send_email)).scalar_one() == 1)

        # delete all BackgroundJobs
        session.execute(
            delete(BackgroundJob).execution_options(synchronize_session=False))

        with patch("couchers.jobs.handlers.now", now_5_min_in_future):
            process_send_request_notifications(empty_pb2.Empty())
        # should find no messages since host has already been notified
        assert (session.execute(
            select(func.count()).select_from(BackgroundJob).where(
                BackgroundJob.job_type ==
                BackgroundJobType.send_email)).scalar_one() == 0)

    # then test that responding to host request creates email
    with requests_session(token2) as requests:
        requests.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_id,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Test request",
            ))

    with session_scope() as session:
        # delete send_email BackgroundJob created by RespondHostRequest
        session.execute(
            delete(BackgroundJob).execution_options(synchronize_session=False))

        # check process_send_request_notifications successfully creates background job
        with patch("couchers.jobs.handlers.now", now_5_min_in_future):
            process_send_request_notifications(empty_pb2.Empty())
        assert (session.execute(
            select(func.count()).select_from(BackgroundJob).where(
                BackgroundJob.job_type ==
                BackgroundJobType.send_email)).scalar_one() == 1)

        # delete all BackgroundJobs
        session.execute(
            delete(BackgroundJob).execution_options(synchronize_session=False))

        with patch("couchers.jobs.handlers.now", now_5_min_in_future):
            process_send_request_notifications(empty_pb2.Empty())
        # should find no messages since guest has already been notified
        assert (session.execute(
            select(func.count()).select_from(BackgroundJob).where(
                BackgroundJob.job_type ==
                BackgroundJobType.send_email)).scalar_one() == 0)
Example #5
0
def test_response_rate(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:
        # no requests: insufficient
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("insufficient_data")

        # send a request and back date it by 36 hours
        host_request_1 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id
        with session_scope() as session:
            session.execute(
                select(Message).where(
                    Message.conversation_id == host_request_1).where(
                        Message.message_type == MessageType.chat_created)
            ).scalar_one().time = now() - timedelta(hours=36)

        # still insufficient
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("insufficient_data")

        # send a request and back date it by 35 hours
        host_request_2 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id
        with session_scope() as session:
            session.execute(
                select(Message).where(
                    Message.conversation_id == host_request_2).where(
                        Message.message_type == MessageType.chat_created)
            ).scalar_one().time = now() - timedelta(hours=35)

        # still insufficient
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("insufficient_data")

        # send a request and back date it by 34 hours
        host_request_3 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id
        with session_scope() as session:
            session.execute(
                select(Message).where(
                    Message.conversation_id == host_request_3).where(
                        Message.message_type == MessageType.chat_created)
            ).scalar_one().time = now() - timedelta(hours=34)

        # now low
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("low")

    with requests_session(token2) as api:
        # accept a host req
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_2,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request",
            ))

    with requests_session(token1) as api:
        # now some w p33 = 35h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("some")
        assert res.some.response_time_p33.ToTimedelta() == timedelta(hours=35)

    with requests_session(token2) as api:
        # accept another host req
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_3,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request",
            ))

    with requests_session(token1) as api:
        # now most w p33 = 34h, p66 = 35h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("most")
        assert res.most.response_time_p33.ToTimedelta() == timedelta(hours=34)
        assert res.most.response_time_p66.ToTimedelta() == timedelta(hours=35)

    with requests_session(token2) as api:
        # accept last host req
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_1,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request",
            ))

    with requests_session(token1) as api:
        # now all w p33 = 34h, p66 = 35h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("almost_all")
        assert res.almost_all.response_time_p33.ToTimedelta() == timedelta(
            hours=34)
        assert res.almost_all.response_time_p66.ToTimedelta() == timedelta(
            hours=35)

        # send a request and back date it by 2 hours
        host_request_4 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id
        with session_scope() as session:
            session.execute(
                select(Message).where(
                    Message.conversation_id == host_request_4).where(
                        Message.message_type == MessageType.chat_created)
            ).scalar_one().time = now() - timedelta(hours=2)

        # send a request and back date it by 4 hours
        host_request_5 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request")).host_request_id
        with session_scope() as session:
            session.execute(
                select(Message).where(
                    Message.conversation_id == host_request_5).where(
                        Message.message_type == MessageType.chat_created)
            ).scalar_one().time = now() - timedelta(hours=4)

        # now some w p33 = 35h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("some")
        assert res.some.response_time_p33.ToTimedelta() == timedelta(hours=35)

    with requests_session(token2) as api:
        # accept host req
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_5,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request",
            ))

    with requests_session(token1) as api:
        # now most w p33 = 34h, p66 = 36h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("most")
        assert res.most.response_time_p33.ToTimedelta() == timedelta(hours=34)
        assert res.most.response_time_p66.ToTimedelta() == timedelta(hours=36)

    with requests_session(token2) as api:
        # accept host req
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_4,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request",
            ))

    with requests_session(token1) as api:
        # now most w p33 = 4h, p66 = 35h
        res = api.GetResponseRate(
            requests_pb2.GetResponseRateReq(user_id=user2.id))
        assert res.HasField("almost_all")
        assert res.almost_all.response_time_p33.ToTimedelta() == timedelta(
            hours=4)
        assert res.almost_all.response_time_p66.ToTimedelta() == timedelta(
            hours=35)
Example #6
0
def test_mark_last_seen(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(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test message 0")).host_request_id

        host_request_id_2 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test message 0a")).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",
            ))

    # test Ping unseen host request count, should be automarked after sending
    with api_session(token1) as api:
        assert api.Ping(
            api_pb2.PingReq()).unseen_received_host_request_count == 0
        assert api.Ping(api_pb2.PingReq()).unseen_sent_host_request_count == 0

    with api_session(token2) as api:
        assert api.Ping(
            api_pb2.PingReq()).unseen_received_host_request_count == 2
        assert api.Ping(api_pb2.PingReq()).unseen_sent_host_request_count == 0

    with requests_session(token2) as api:
        assert api.ListHostRequests(requests_pb2.ListHostRequestsReq(
        )).host_requests[0].last_seen_message_id == 0

        api.MarkLastSeenHostRequest(
            requests_pb2.MarkLastSeenHostRequestReq(
                host_request_id=host_request_id, last_seen_message_id=3))

        assert api.ListHostRequests(requests_pb2.ListHostRequestsReq(
        )).host_requests[0].last_seen_message_id == 3

        with pytest.raises(grpc.RpcError) as e:
            api.MarkLastSeenHostRequest(
                requests_pb2.MarkLastSeenHostRequestReq(
                    host_request_id=host_request_id, last_seen_message_id=1))
        assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
        assert e.value.details() == errors.CANT_UNSEE_MESSAGES

        # this will be used to test sent request notifications
        host_request_id_3 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user1.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Another test request")).host_request_id

        # this should make id_2 all read
        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id_2, text="Test"))

    with api_session(token2) as api:
        assert api.Ping(
            api_pb2.PingReq()).unseen_received_host_request_count == 1
        assert api.Ping(api_pb2.PingReq()).unseen_sent_host_request_count == 0

    # make sure sent and received count for unseen notifications
    with requests_session(token1) as api:
        api.SendHostRequestMessage(
            requests_pb2.SendHostRequestMessageReq(
                host_request_id=host_request_id_3, text="Test message"))

    with api_session(token2) as api:
        assert api.Ping(
            api_pb2.PingReq()).unseen_received_host_request_count == 1
        assert api.Ping(api_pb2.PingReq()).unseen_sent_host_request_count == 1
Example #7
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(
                host_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(host_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
Example #8
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(
                host_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
Example #9
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(host_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"
Example #10
0
def test_RespondHostRequests(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(
                host_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"
Example #11
0
def test_ListHostRequests_pagination_regression(db):
    """
    ListHostRequests was skipping a request when getting multiple pages
    """
    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:
        host_request_1 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 1")).host_request_id

        host_request_2 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 2")).host_request_id

        host_request_3 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 3")).host_request_id

    with requests_session(token2) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert res.no_more
        assert len(res.host_requests) == 3
        assert res.host_requests[
            0].latest_message.text.text == "Test request 3"
        assert res.host_requests[
            1].latest_message.text.text == "Test request 2"
        assert res.host_requests[
            2].latest_message.text.text == "Test request 1"

    with requests_session(token2) as api:
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_2,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request 2",
            ))
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_1,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request 1",
            ))
        api.RespondHostRequest(
            requests_pb2.RespondHostRequestReq(
                host_request_id=host_request_3,
                status=conversations_pb2.HOST_REQUEST_STATUS_ACCEPTED,
                text="Accepting host request 3",
            ))

    with requests_session(token2) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert res.no_more
        assert len(res.host_requests) == 3
        assert res.host_requests[
            0].latest_message.text.text == "Accepting host request 3"
        assert res.host_requests[
            1].latest_message.text.text == "Accepting host request 1"
        assert res.host_requests[
            2].latest_message.text.text == "Accepting host request 2"

    with requests_session(token2) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True, number=1))
        assert not res.no_more
        assert len(res.host_requests) == 1
        assert res.host_requests[
            0].latest_message.text.text == "Accepting host request 3"
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(
                only_received=True,
                number=1,
                last_request_id=res.last_request_id))
        assert not res.no_more
        assert len(res.host_requests) == 1
        assert res.host_requests[
            0].latest_message.text.text == "Accepting host request 1"
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(
                only_received=True,
                number=1,
                last_request_id=res.last_request_id))
        assert res.no_more
        assert len(res.host_requests) == 1
        assert res.host_requests[
            0].latest_message.text.text == "Accepting host request 2"
Example #12
0
def test_create_request(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()
    today_minus_2 = (today() - timedelta(days=2)).isoformat()
    today_minus_3 = (today() - timedelta(days=3)).isoformat()
    with requests_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=user1.id,
                                                  from_date=today_plus_2,
                                                  to_date=today_plus_3,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.CANT_REQUEST_SELF

        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=999,
                                                  from_date=today_plus_2,
                                                  to_date=today_plus_3,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.USER_NOT_FOUND

        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                                  from_date=today_plus_3,
                                                  to_date=today_plus_2,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.DATE_FROM_AFTER_TO

        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                                  from_date=today_minus_3,
                                                  to_date=today_plus_2,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.DATE_FROM_BEFORE_TODAY

        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                                  from_date=today_plus_2,
                                                  to_date=today_minus_2,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.DATE_FROM_AFTER_TO

        with pytest.raises(grpc.RpcError) as e:
            api.CreateHostRequest(
                requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                                  from_date="2020-00-06",
                                                  to_date=today_minus_2,
                                                  text="Test request"))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_DATE

        res = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(host_user_id=user2.id,
                                              from_date=today_plus_2,
                                              to_date=today_plus_3,
                                              text="Test request"))
        assert (api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_sent=True)).host_requests[0].
                latest_message.text.text == "Test request")

    today_ = today()
    today_plus_one_year = today_.replace(year=today_.year + 1)
    today_plus_one_year_plus_2 = (today_plus_one_year +
                                  timedelta(days=2)).isoformat()
    today_plus_one_year_plus_3 = (today_plus_one_year +
                                  timedelta(days=3)).isoformat()
    with pytest.raises(grpc.RpcError) as e:
        api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_one_year_plus_2,
                to_date=today_plus_one_year_plus_3,
                text="Test from date after one year",
            ))
    assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
    assert e.value.details() == errors.DATE_FROM_AFTER_ONE_YEAR

    with pytest.raises(grpc.RpcError) as e:
        api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_one_year_plus_3,
                text="Test to date one year after from date",
            ))
    assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
    assert e.value.details() == errors.DATE_TO_AFTER_ONE_YEAR
Example #13
0
def test_ListHostRequests(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_1 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user2.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 1")).host_request_id

        host_request_2 = api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(
                host_user_id=user3.id,
                from_date=today_plus_2,
                to_date=today_plus_3,
                text="Test request 2")).host_request_id

        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_sent=True))
        assert res.no_more
        assert len(res.host_requests) == 2

    with requests_session(token2) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert res.no_more
        assert len(res.host_requests) == 1
        assert res.host_requests[
            0].latest_message.text.text == "Test request 1"
        assert res.host_requests[0].surfer_user_id == user1.id
        assert res.host_requests[0].host_user_id == user2.id
        assert res.host_requests[
            0].status == conversations_pb2.HOST_REQUEST_STATUS_PENDING

        add_message(db, "Test request 1 message 1", user2.id, host_request_1)
        add_message(db, "Test request 1 message 2", user2.id, host_request_1)
        add_message(db, "Test request 1 message 3", user2.id, host_request_1)

        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert res.host_requests[
            0].latest_message.text.text == "Test request 1 message 3"

        api.CreateHostRequest(
            requests_pb2.CreateHostRequestReq(host_user_id=user1.id,
                                              from_date=today_plus_2,
                                              to_date=today_plus_3,
                                              text="Test request 3"))

    add_message(db, "Test request 2 message 1", user1.id, host_request_2)
    add_message(db, "Test request 2 message 2", user3.id, host_request_2)

    with requests_session(token3) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert res.no_more
        assert len(res.host_requests) == 1
        assert res.host_requests[
            0].latest_message.text.text == "Test request 2 message 2"

    with requests_session(token1) as api:
        res = api.ListHostRequests(
            requests_pb2.ListHostRequestsReq(only_received=True))
        assert len(res.host_requests) == 1

        res = api.ListHostRequests(requests_pb2.ListHostRequestsReq())
        assert len(res.host_requests) == 3