Beispiel #1
0
def test_jail_basic(db):
    user1, token1 = generate_user()

    with real_api_session(token1) as api:
        res = api.Ping(api_pb2.PingReq())

    with real_jail_session(token1) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        # check every field is false
        for field in res.DESCRIPTOR.fields:
            assert not getattr(res, field.name)

        assert not res.jailed

    # make the user jailed
    user2, token2 = generate_user(accepted_tos=0)

    with real_api_session(token2) as api, pytest.raises(grpc.RpcError) as e:
        res = api.Ping(api_pb2.PingReq())
    assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED

    with real_jail_session(token2) as jail:
        res = jail.JailInfo(empty_pb2.Empty())

        assert res.jailed

        reason_count = 0

        # check at least one field is true
        for field in res.DESCRIPTOR.fields:
            reason_count += getattr(res, field.name) == True

        assert reason_count > 0
Beispiel #2
0
def test_pending_friend_request_count(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()

    with api_session(token2) as api:
        res = api.Ping(api_pb2.PingReq())
        assert res.pending_friend_request_count == 0

    with api_session(token1) as api:
        res = api.Ping(api_pb2.PingReq())
        assert res.pending_friend_request_count == 0
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
        res = api.Ping(api_pb2.PingReq())
        assert res.pending_friend_request_count == 0

    with api_session(token2) as api:
        res = api.Ping(api_pb2.PingReq())
        assert res.pending_friend_request_count == 1

    with api_session(token2) as api:
        # check it's there
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 1

        assert res.received[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.received[0].user_id == user1.id

        fr_id = res.received[0].friend_request_id

        # accept it
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))

        res = api.Ping(api_pb2.PingReq())
        assert res.pending_friend_request_count == 0
Beispiel #3
0
def test_JailInfo(db):
    user1, token1 = generate_user(accepted_tos=0)

    with real_jail_session(token1) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        assert res.jailed
        assert res.has_not_accepted_tos

    with real_api_session(token1) as api, pytest.raises(grpc.RpcError) as e:
        res = api.Ping(api_pb2.PingReq())
    assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED

    # make the user not jailed
    user2, token2 = generate_user()

    with real_jail_session(token2) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        assert not res.jailed
        assert not res.has_not_accepted_tos

    with real_api_session(token2) as api:
        res = api.Ping(api_pb2.PingReq())
Beispiel #4
0
def test_TOS_increase(db, monkeypatch):
    # test if the TOS version is updated

    # not jailed yet
    user, token = generate_user()

    with real_jail_session(token) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        assert not res.jailed
        assert not res.has_not_accepted_tos

    with real_api_session(token) as api:
        res = api.Ping(api_pb2.PingReq())

    # now we pretend to update the TOS version
    new_TOS_VERSION = TOS_VERSION + 1

    monkeypatch.setattr(models, "TOS_VERSION", new_TOS_VERSION)
    monkeypatch.setattr(servicers_jail, "TOS_VERSION", new_TOS_VERSION)

    # make sure we're jailed
    with real_api_session(token) as api, pytest.raises(grpc.RpcError) as e:
        res = api.Ping(api_pb2.PingReq())
    assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED

    with real_jail_session(token) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        assert res.jailed
        assert res.has_not_accepted_tos

        # now accept
        res = jail.AcceptTOS(jail_pb2.AcceptTOSReq(accept=True))

        res = jail.JailInfo(empty_pb2.Empty())
        assert not res.jailed
        assert not res.has_not_accepted_tos
Beispiel #5
0
def test_ping(db):
    user, token = generate_user()

    with real_api_session(token) as api:
        res = api.Ping(api_pb2.PingReq())

    assert res.user.user_id == user.id
    assert res.user.username == user.username
    assert res.user.name == user.name
    assert res.user.city == user.city
    assert res.user.hometown == user.hometown
    assert res.user.verification == 0.0
    assert res.user.community_standing == user.community_standing
    assert res.user.num_references == 0
    assert res.user.gender == user.gender
    assert res.user.pronouns == user.pronouns
    assert res.user.age == user.age

    assert (res.user.lat, res.user.lng) == (user.coordinates or (0, 0))

    # the joined time is fuzzed
    # but shouldn't be before actual joined time, or more than one hour behind
    assert user.joined - timedelta(hours=1) <= to_aware_datetime(
        res.user.joined) <= user.joined
    # same for last_active
    assert user.last_active - timedelta(hours=1) <= to_aware_datetime(
        res.user.last_active) <= user.last_active

    assert res.user.hosting_status == api_pb2.HOSTING_STATUS_CANT_HOST
    assert res.user.meetup_status == api_pb2.MEETUP_STATUS_OPEN_TO_MEETUP

    assert res.user.occupation == user.occupation
    assert res.user.education == user.education
    assert res.user.about_me == user.about_me
    assert res.user.my_travels == user.my_travels
    assert res.user.things_i_like == user.things_i_like
    assert set(language_ability.code
               for language_ability in res.user.language_abilities) == set(
                   ["fin", "fra"])
    assert res.user.about_place == user.about_place
    assert res.user.regions_visited == [
        "FIN", "REU", "CHE"
    ]  # Tests alphabetization by region name
    assert res.user.regions_lived == ["EST", "FRA", "ESP"]  # Ditto
    assert res.user.additional_information == user.additional_information

    assert res.user.friends == api_pb2.User.FriendshipStatus.NA
    assert not res.user.HasField("pending_friend_request")
Beispiel #6
0
def test_coords(db):
    # make them have not added a location
    user1, token1 = generate_user(geom=None, geom_radius=None)
    user2, token2 = generate_user()

    with api_session(token2) as api:
        res = api.Ping(api_pb2.PingReq())
        assert res.user.city == user2.city
        lat, lng = user2.coordinates or (0, 0)
        assert res.user.lat == lat
        assert res.user.lng == lng
        assert res.user.radius == user2.geom_radius

    with api_session(token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert res.city == user1.city
        assert res.lat == 0.0
        assert res.lng == 0.0
        assert res.radius == 0.0

    # Check coordinate wrapping
    user3, token3 = generate_user(geom=create_coordinate(40.0, -180.5))
    user4, token4 = generate_user(geom=create_coordinate(40.0, 20.0))
    user5, token5 = generate_user(geom=create_coordinate(90.5, 20.0))

    with api_session(token3) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user3.username))
        assert res.lat == 40.0
        assert res.lng == 179.5

    with api_session(token4) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user4.username))
        assert res.lat == 40.0
        assert res.lng == 20.0

    # PostGIS does not wrap longitude for latitude overflow
    with api_session(token5) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user5.username))
        assert res.lat == 89.5
        assert res.lng == 20.0

    with real_jail_session(token1) as jail:
        res = jail.JailInfo(empty_pb2.Empty())
        assert res.jailed
        assert res.has_not_added_location

        res = jail.SetLocation(
            jail_pb2.SetLocationReq(
                city="New York City",
                lat=40.7812,
                lng=-73.9647,
                radius=250,
            ))

        assert not res.jailed
        assert not res.has_not_added_location

        res = jail.JailInfo(empty_pb2.Empty())
        assert not res.jailed
        assert not res.has_not_added_location

    with api_session(token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert res.city == "New York City"
        assert res.lat == 40.7812
        assert res.lng == -73.9647
        assert res.radius == 250
Beispiel #7
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