コード例 #1
0
def test_mutual_friends_self(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    user4, token4 = generate_user()

    make_friends(user1, user2)
    make_friends(user2, user3)
    make_friends(user1, user4)

    with api_session(token1) as api:
        res = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user1.id))
        assert len(res.mutual_friends) == 0

    with api_session(token2) as api:
        res = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user2.id))
        assert len(res.mutual_friends) == 0

    with api_session(token3) as api:
        res = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user3.id))
        assert len(res.mutual_friends) == 0

    with api_session(token4) as api:
        res = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user4.id))
        assert len(res.mutual_friends) == 0
コード例 #2
0
def test_hosting_preferences(db):
    user1, token1 = generate_user(db)
    user2, token2 = generate_user(db)

    with api_session(db, token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user2.username))
        assert not res.HasField("max_guests")
        assert not res.HasField("multiple_groups")
        assert not res.HasField("last_minute")
        assert not res.HasField("accepts_pets")
        assert not res.HasField("accepts_kids")
        assert not res.HasField("wheelchair_accessible")
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
        assert not res.HasField("sleeping_arrangement")
        assert not res.HasField("area")
        assert not res.HasField("house_rules")

        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                max_guests=api_pb2.NullableUInt32Value(value=3),
                wheelchair_accessible=api_pb2.NullableBoolValue(value=False),
                smoking_allowed=api_pb2.SMOKING_LOCATION_WINDOW,
                house_rules=api_pb2.NullableStringValue(value="RULES!"),
            ))

    with api_session(db, token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert res.max_guests.value == 3
        assert not res.HasField("multiple_groups")
        assert not res.HasField("last_minute")
        assert not res.HasField("accepts_pets")
        assert not res.HasField("accepts_kids")
        assert not res.wheelchair_accessible.value
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_WINDOW
        assert not res.HasField("sleeping_arrangement")
        assert not res.HasField("area")
        assert res.house_rules.value == "RULES!"

    with api_session(db, token1) as api:
        # test unsetting
        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                max_guests=api_pb2.NullableUInt32Value(is_null=True),
                wheelchair_accessible=api_pb2.NullableBoolValue(value=True),
                smoking_allowed=api_pb2.SMOKING_LOCATION_UNKNOWN,
                area=api_pb2.NullableStringValue(value="area!"),
                house_rules=api_pb2.NullableStringValue(is_null=True),
            ))

        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert not res.HasField("max_guests")
        assert not res.HasField("multiple_groups")
        assert not res.HasField("last_minute")
        assert not res.HasField("accepts_pets")
        assert not res.HasField("accepts_kids")
        assert res.wheelchair_accessible.value
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
        assert not res.HasField("sleeping_arrangement")
        assert res.area.value == "area!"
        assert not res.HasField("house_rules")
コード例 #3
0
def test_friend_request_flow(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")
    user3, token3 = generate_user("user3")

    # send friend request from user1 to user2
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        # check it went through
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 1
        assert len(res.received) == 0

        assert res.sent[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.sent[0].user_id == user2.id

    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))

        # check it's gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check we're friends now
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user1.id

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

        # check we're friends now
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user2.id
コード例 #4
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_cant_friend_request_pending(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")
    user3, token3 = generate_user("user3")

    # send friend request from user1 to user2
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

    with api_session(token2) as api, pytest.raises(grpc.RpcError):
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user1.id))
コード例 #5
0
def test_reject_friend_request(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.sent[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.sent[0].user_id == user2.id

    with api_session(token2) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        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

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

        # check it's gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check not friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 0

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

        # check we're not friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 0

        # check we can send another friend req
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.sent[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.sent[0].user_id == user2.id
コード例 #6
0
def test_cant_friend_request_pending(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    # send friend request from user1 to user2
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

    with api_session(token2) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.SendFriendRequest(
                api_pb2.SendFriendRequestReq(user_id=user1.id))
        assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
        assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
コード例 #7
0
ファイル: test_api.py プロジェクト: Couchers-org/couchers
def test_get_user(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user2.username))
        assert res.user_id == user2.id
        assert res.username == user2.username
        assert res.name == user2.name

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=str(user2.id)))
        assert res.user_id == user2.id
        assert res.username == user2.username
        assert res.name == user2.name
コード例 #8
0
def test_media_upload(db):
    user, token = generate_user("tester")

    media_bearer_token = random_hex(32)

    with api_session(token) as api:
        res = api.InitiateMediaUpload(empty_pb2.Empty())

    params = parse_qs(urlparse(res.upload_url).query)
    data = urlsafe_b64decode(params["data"][0])

    response = media_pb2.UploadRequest.FromString(data)
    key = response.key

    filename = random_hex(32)

    req = media_pb2.UploadConfirmationReq(key=key, filename=filename)

    with session_scope() as session:
        # make sure it exists
        session.query(InitiatedUpload).filter(InitiatedUpload.key == key).one()

    with media_session(media_bearer_token) as media:
        res = media.UploadConfirmation(req)

    with session_scope() as session:
        # make sure it exists
        upload = (session.query(Upload).filter(Upload.key == key).filter(
            Upload.filename == filename).filter(
                Upload.creator_user_id == user.id).one())

    with session_scope() as session:
        # make sure it was deleted
        assert not session.query(InitiatedUpload).one_or_none()
コード例 #9
0
def test_VerifyPhone():
    user, token = generate_user()
    user_id = user.id
    with account_session(token) as account, api_session(token) as api:

        with pytest.raises(grpc.RpcError) as e:
            account.VerifyPhone(account_pb2.VerifyPhoneReq(token="123455"))
        assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION

        res = api.GetUser(api_pb2.GetUserReq(user=str(user_id)))
        assert res.verification == 0.0

        with session_scope() as session:
            user = session.execute(
                select(User).where(User.id == user_id)).scalar_one()
            user.phone = "+46701740605"
            user.phone_verification_token = "111112"
            user.phone_verification_sent = now()

        account.VerifyPhone(account_pb2.VerifyPhoneReq(token="111112"))

        res = api.GetUser(api_pb2.GetUserReq(user=str(user_id)))
        assert res.verification == 1.0

        # Phone number should finally show up on in your profile settings
        res = account.GetAccountInfo(empty_pb2.Empty())
        assert res.phone == "+46701740605"
コード例 #10
0
ファイル: test_api.py プロジェクト: ineswq/couchers
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

    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
コード例 #11
0
def test_ListMutualFriends(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    user4, token4 = generate_user()
    user5, token5 = generate_user()

    # arrange friends like this: 1<->2, 1<->3, 1<->4, 1<->5, 3<->2, 3<->4,
    # so 1 and 2 should have mutual friend 3 only
    make_friends(user1, user2)
    make_friends(user1, user3)
    make_friends(user1, user4)
    make_friends(user1, user5)
    make_friends(user3, user2)
    make_friends(user3, user4)

    with api_session(token1) as api:
        mutual_friends = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends
        assert len(mutual_friends) == 1
        assert mutual_friends[0].user_id == user3.id

    # and other way around same
    with api_session(token2) as api:
        mutual_friends = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends
        assert len(mutual_friends) == 1
        assert mutual_friends[0].user_id == user3.id

        # Check pending request doesn't have effect
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user5.id))

        mutual_friends = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user1.id)).mutual_friends
        assert len(mutual_friends) == 1
        assert mutual_friends[0].user_id == user3.id

    # both ways
    with api_session(token1) as api:
        mutual_friends = api.ListMutualFriends(
            api_pb2.ListMutualFriendsReq(user_id=user2.id)).mutual_friends
        assert len(mutual_friends) == 1
        assert mutual_friends[0].user_id == user3.id
コード例 #12
0
def test_accept_friend_request(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    with session_scope() as session:
        friend_request = FriendRelationship(from_user_id=user1.id,
                                            to_user_id=user2.id,
                                            status=FriendStatus.pending)
        session.add(friend_request)
        session.commit()
        friend_request_id = friend_request.id

    with api_session(token2) as api:
        # check request pending
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.received) == 1
        assert res.received[0].user_id == user1.id

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

        # check request is gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check now friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user1.id

    with api_session(token1) as api:
        # check request gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check now friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user2.id
コード例 #13
0
def test_CancelFriendRequest(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")

    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.sent[0].user_id == user2.id
        fr_id = res.sent[0].friend_request_id

        api.CancelFriendRequest(
            api_pb2.CancelFriendRequestReq(friend_request_id=fr_id))

        # check it's gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check not friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 0

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

        # check we're not friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 0

    with api_session(token1) as api:
        # check we can send another friend req
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.sent[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.sent[0].user_id == user2.id
コード例 #14
0
def test_search(db):
    user1, token1 = generate_user(db, "user1")
    user2, token2 = generate_user(db, "user2")
    user3, token3 = generate_user(db, "user3")
    user4, token4 = generate_user(db, "user4")

    with api_session(db, token1) as api:
        res = api.Search(api_pb2.SearchReq(query="user"))
        assert len(res.users) == 4

        res = api.Search(api_pb2.SearchReq(query="user5"))
        assert len(res.users) == 0
コード例 #15
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_mutual_friends_self(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")
    user3, token3 = generate_user("user3")
    user4, token4 = generate_user("user4")

    make_friends(user1, user2)
    make_friends(user2, user3)
    make_friends(user1, user4)

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user3.username))
        assert len(res.mutual_friends) == 1
        assert res.mutual_friends[0].user_id == user2.id

    with api_session(token3) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert len(res.mutual_friends) == 1
        assert res.mutual_friends[0].user_id == user2.id

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert len(res.mutual_friends) == 0

    with api_session(token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user2.username))
        assert len(res.mutual_friends) == 0

    with api_session(token3) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user3.username))
        assert len(res.mutual_friends) == 0

    with api_session(token4) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user4.username))
        assert len(res.mutual_friends) == 0
コード例 #16
0
def test_pending_friend_request_count(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")
    user3, token3 = generate_user("user3")

    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
コード例 #17
0
def test_reporting(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    with api_session(token1) as api:
        res = api.Report(
            api_pb2.ReportReq(reported_user_id=user2.id,
                              reason="reason text",
                              description="description text"))
    assert isinstance(res, empty_pb2.Empty)

    with session_scope() as session:
        entries = session.query(Complaint).all()

        assert len(entries) == 1
        assert entries[0].author_user_id == user1.id
        assert entries[0].reported_user_id == user2.id
        assert entries[0].reason == "reason text"
        assert entries[0].description == "description text"

    # Test that reporting oneself and reporting nonexisting user fails
    report_req = api_pb2.ReportReq(reported_user_id=user1.id,
                                   reason="foo",
                                   description="bar")
    with api_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.Report(report_req)
    assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
    assert e.value.details() == errors.CANT_REPORT_SELF

    report_req = api_pb2.ReportReq(reported_user_id=0x7FFFFFFFFFFFFFFF,
                                   reason="foo",
                                   description="bar")
    with api_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.Report(report_req)
    assert e.value.code() == grpc.StatusCode.NOT_FOUND
    assert e.value.details() == errors.USER_NOT_FOUND
コード例 #18
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_CancelFriendRequest(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")

    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.sent[0].user_id == user2.id
        fr_id = res.sent[0].friend_request_id

        api.CancelFriendRequest(
            api_pb2.CancelFriendRequestReq(friend_request_id=fr_id))

        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
コード例 #19
0
def test_VerifyPhone_antibrute():
    user, token = generate_user()
    user_id = user.id
    with account_session(token) as account, api_session(token) as api:

        with session_scope() as session:
            user = session.execute(
                select(User).where(User.id == user_id)).scalar_one()
            user.phone_verification_token = "111112"
            user.phone_verification_sent = now()
            user.phone = "+46701740605"

        for i in range(10):
            with pytest.raises(grpc.RpcError) as e:
                account.VerifyPhone(account_pb2.VerifyPhoneReq(token="123455"))
            if e.value.code() != grpc.StatusCode.NOT_FOUND:
                break
        assert e.value.code() == grpc.StatusCode.RESOURCE_EXHAUSTED
コード例 #20
0
def test_update_profile(db):
    user, token = generate_user(db)

    with api_session(db, token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(color=wrappers_pb2.StringValue(
                    value="color")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT

        res = api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                gender=wrappers_pb2.StringValue(value="Bot"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                color=wrappers_pb2.StringValue(value="#111111"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                languages=api_pb2.RepeatedStringValue(
                    exists=True, value=["Binary", "English"]),
                countries_visited=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                countries_lived=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
            ))
        # all fields changed
        for field, value in res.ListFields():
            assert value == True

        user = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user.name == "New name"
        assert user.city == "Timbuktu"
        assert user.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert "Binary" in user.languages
        assert "English" in user.languages
コード例 #21
0
def test_signup_incremental(db):
    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(basic=auth_pb2.SignupBasic(
                name="testing", email="*****@*****.**"), ))

    flow_token = res.flow_token
    assert res.flow_token
    assert not res.HasField("auth_res")
    assert not res.need_basic
    assert res.need_account
    assert res.need_feedback
    assert res.need_verify_email
    assert res.need_accept_community_guidelines

    # read out the signup token directly from the database for now
    with session_scope() as session:
        flow = session.execute(
            select(SignupFlow).where(
                SignupFlow.flow_token == flow_token)).scalar_one()
        assert flow.email_sent
        assert not flow.email_verified
        email_token = flow.email_token

    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(flow_token=flow_token))

    assert res.flow_token == flow_token
    assert not res.HasField("auth_res")
    assert not res.need_basic
    assert res.need_account
    assert res.need_feedback
    assert res.need_verify_email
    assert res.need_accept_community_guidelines

    # Add feedback
    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(
                flow_token=flow_token,
                feedback=auth_pb2.ContributorForm(
                    ideas="I'm a robot, incapable of original ideation",
                    features="I love all your features",
                    experience="I haven't done couch surfing before",
                    contribute=auth_pb2.CONTRIBUTE_OPTION_YES,
                    contribute_ways=["serving", "backend"],
                    expertise=
                    "I'd love to be your server: I can compute very fast, but only simple opcodes",
                ),
            ))

    assert res.flow_token == flow_token
    assert not res.HasField("auth_res")
    assert not res.need_basic
    assert res.need_account
    assert not res.need_feedback
    assert res.need_verify_email
    assert res.need_accept_community_guidelines

    # Agree to community guidelines
    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(
                flow_token=flow_token,
                accept_community_guidelines=wrappers_pb2.BoolValue(value=True),
            ))

    assert res.flow_token == flow_token
    assert not res.HasField("auth_res")
    assert not res.need_basic
    assert res.need_account
    assert not res.need_feedback
    assert res.need_verify_email
    assert not res.need_accept_community_guidelines

    # Verify email
    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(
                flow_token=flow_token,
                email_token=email_token,
            ))

    assert res.flow_token == flow_token
    assert not res.HasField("auth_res")
    assert not res.need_basic
    assert res.need_account
    assert not res.need_feedback
    assert not res.need_verify_email
    assert not res.need_accept_community_guidelines

    # Finally finish off account info
    with auth_api_session() as (auth_api, metadata_interceptor):
        res = auth_api.SignupFlow(
            auth_pb2.SignupFlowReq(
                flow_token=flow_token,
                account=auth_pb2.SignupAccount(
                    username="******",
                    password="******",
                    birthdate="1970-01-01",
                    gender="Bot",
                    hosting_status=api_pb2.HOSTING_STATUS_MAYBE,
                    city="New York City",
                    lat=40.7331,
                    lng=-73.9778,
                    radius=500,
                    accept_tos=True,
                ),
            ))

    assert not res.flow_token
    assert res.HasField("auth_res")
    assert res.auth_res.user_id
    assert not res.auth_res.jailed
    assert not res.need_basic
    assert not res.need_account
    assert not res.need_feedback
    assert not res.need_verify_email
    assert not res.need_accept_community_guidelines

    user_id = res.auth_res.user_id

    sess_token = get_session_cookie_token(metadata_interceptor)

    with api_session(sess_token) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=str(user_id)))

    assert res.username == "frodo"
    assert res.gender == "Bot"
    assert res.hosting_status == api_pb2.HOSTING_STATUS_MAYBE
    assert res.city == "New York City"
    assert res.lat == 40.7331
    assert res.lng == -73.9778
    assert res.radius == 500

    with session_scope() as session:
        form = session.execute(select(ContributorForm)).scalar_one()

        assert form.ideas == "I'm a robot, incapable of original ideation"
        assert form.features == "I love all your features"
        assert form.experience == "I haven't done couch surfing before"
        assert form.contribute == ContributeOption.yes
        assert form.contribute_ways == ["serving", "backend"]
        assert form.expertise == "I'd love to be your server: I can compute very fast, but only simple opcodes"
コード例 #22
0
def test_hosting_preferences(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert not res.HasField("max_guests")
        assert not res.HasField("last_minute")
        assert not res.HasField("has_pets")
        assert not res.HasField("accepts_pets")
        assert not res.HasField("pet_details")
        assert not res.HasField("has_kids")
        assert not res.HasField("accepts_kids")
        assert not res.HasField("kid_details")
        assert not res.HasField("has_housemates")
        assert not res.HasField("housemate_details")
        assert not res.HasField("wheelchair_accessible")
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
        assert not res.HasField("smokes_at_home")
        assert not res.HasField("drinking_allowed")
        assert not res.HasField("drinks_at_home")
        assert not res.HasField("other_host_info")
        assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN
        assert not res.HasField("sleeping_details")
        assert not res.HasField("area")
        assert not res.HasField("house_rules")
        assert not res.HasField("parking")
        assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN
        assert not res.HasField("camping_ok")

        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                max_guests=api_pb2.NullableUInt32Value(value=3),
                last_minute=api_pb2.NullableBoolValue(value=True),
                has_pets=api_pb2.NullableBoolValue(value=False),
                accepts_pets=api_pb2.NullableBoolValue(value=True),
                pet_details=api_pb2.NullableStringValue(value="I love dogs"),
                has_kids=api_pb2.NullableBoolValue(value=False),
                accepts_kids=api_pb2.NullableBoolValue(value=True),
                kid_details=api_pb2.NullableStringValue(value="I hate kids"),
                has_housemates=api_pb2.NullableBoolValue(value=False),
                housemate_details=api_pb2.NullableStringValue(
                    value="I have 7 housemates"),
                wheelchair_accessible=api_pb2.NullableBoolValue(value=True),
                smoking_allowed=api_pb2.SMOKING_LOCATION_WINDOW,
                area=api_pb2.NullableStringValue(value="area!"),
                smokes_at_home=api_pb2.NullableBoolValue(value=False),
                drinking_allowed=api_pb2.NullableBoolValue(value=True),
                drinks_at_home=api_pb2.NullableBoolValue(value=False),
                other_host_info=api_pb2.NullableStringValue(
                    value="I'm pretty swell"),
                sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_COMMON,
                sleeping_details=api_pb2.NullableStringValue(
                    value="Couch in living room"),
                house_rules=api_pb2.NullableStringValue(value="RULES!"),
                parking=api_pb2.NullableBoolValue(value=True),
                parking_details=api_pb2.PARKING_DETAILS_PAID_ONSITE,
                camping_ok=api_pb2.NullableBoolValue(value=False),
            ))

    # Use a second user to view the hosting preferences just to check
    # that it is public information.
    with api_session(token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert res.max_guests.value == 3
        assert res.last_minute.value is True
        assert res.has_pets.value is False
        assert res.accepts_pets.value is True
        assert res.pet_details.value == "I love dogs"
        assert res.has_kids.value is False
        assert res.accepts_kids.value is True
        assert res.kid_details.value == "I hate kids"
        assert res.has_housemates.value is False
        assert res.housemate_details.value == "I have 7 housemates"
        assert res.wheelchair_accessible.value is True
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_WINDOW
        assert res.smokes_at_home.value is False
        assert res.drinking_allowed.value is True
        assert res.drinks_at_home.value is False
        assert res.other_host_info.value == "I'm pretty swell"
        assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_COMMON
        assert res.sleeping_details.value == "Couch in living room"
        assert res.area.value == "area!"
        assert res.house_rules.value == "RULES!"
        assert res.parking.value is True
        assert res.parking_details == api_pb2.PARKING_DETAILS_PAID_ONSITE
        assert res.camping_ok.value is False

    # test unsetting
    with api_session(token1) as api:
        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                max_guests=api_pb2.NullableUInt32Value(is_null=True),
                last_minute=api_pb2.NullableBoolValue(is_null=True),
                has_pets=api_pb2.NullableBoolValue(is_null=True),
                accepts_pets=api_pb2.NullableBoolValue(is_null=True),
                pet_details=api_pb2.NullableStringValue(is_null=True),
                has_kids=api_pb2.NullableBoolValue(is_null=True),
                accepts_kids=api_pb2.NullableBoolValue(is_null=True),
                kid_details=api_pb2.NullableStringValue(is_null=True),
                has_housemates=api_pb2.NullableBoolValue(is_null=True),
                housemate_details=api_pb2.NullableStringValue(is_null=True),
                wheelchair_accessible=api_pb2.NullableBoolValue(is_null=True),
                smoking_allowed=api_pb2.SMOKING_LOCATION_UNKNOWN,
                area=api_pb2.NullableStringValue(is_null=True),
                smokes_at_home=api_pb2.NullableBoolValue(is_null=True),
                drinking_allowed=api_pb2.NullableBoolValue(is_null=True),
                drinks_at_home=api_pb2.NullableBoolValue(is_null=True),
                other_host_info=api_pb2.NullableStringValue(is_null=True),
                sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN,
                sleeping_details=api_pb2.NullableStringValue(is_null=True),
                house_rules=api_pb2.NullableStringValue(is_null=True),
                parking=api_pb2.NullableBoolValue(is_null=True),
                parking_details=api_pb2.PARKING_DETAILS_UNKNOWN,
                camping_ok=api_pb2.NullableBoolValue(is_null=True),
            ))

        res = api.GetUser(api_pb2.GetUserReq(user=user1.username))
        assert not res.HasField("max_guests")
        assert not res.HasField("last_minute")
        assert not res.HasField("has_pets")
        assert not res.HasField("accepts_pets")
        assert not res.HasField("pet_details")
        assert not res.HasField("has_kids")
        assert not res.HasField("accepts_kids")
        assert not res.HasField("kid_details")
        assert not res.HasField("has_housemates")
        assert not res.HasField("housemate_details")
        assert not res.HasField("wheelchair_accessible")
        assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN
        assert not res.HasField("smokes_at_home")
        assert not res.HasField("drinking_allowed")
        assert not res.HasField("drinks_at_home")
        assert not res.HasField("other_host_info")
        assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN
        assert not res.HasField("sleeping_details")
        assert not res.HasField("area")
        assert not res.HasField("house_rules")
        assert not res.HasField("parking")
        assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN
        assert not res.HasField("camping_ok")
コード例 #23
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
コード例 #24
0
ファイル: test_api.py プロジェクト: Couchers-org/couchers
def test_friend_request_flow(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()

    # send friend request from user1 to user2
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

    with session_scope() as session:
        friend_request_id = (session.execute(
            select(FriendRelationship).where(
                FriendRelationship.from_user_id == user1.id
                and FriendRelationship.to_user_id
                == user2.id)).scalar_one_or_none()).id

    with api_session(token1) as api:
        # check it went through
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 1
        assert len(res.received) == 0

        assert res.sent[
            0].state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert res.sent[0].user_id == user2.id
        assert res.sent[0].friend_request_id == friend_request_id

    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))

        # check it's gone
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.sent) == 0
        assert len(res.received) == 0

        # check we're friends now
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user1.id

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

        # check we're friends now
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert res.user_ids[0] == user2.id
コード例 #25
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_update_profile(db):
    user, token = generate_user()

    with api_session(token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_NAME

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    lat=wrappers_pb2.DoubleValue(value=0),
                    lng=wrappers_pb2.DoubleValue(value=0)))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_COORDINATE

        # changing gender shouldn't be allowed
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(gender=wrappers_pb2.StringValue(
                    value="newgender")))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED

        res = api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                hometown=api_pb2.NullableStringValue(value="Walla Walla"),
                lat=wrappers_pb2.DoubleValue(value=0.01),
                lng=wrappers_pb2.DoubleValue(value=-2),
                radius=wrappers_pb2.DoubleValue(value=321),
                pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                education=api_pb2.NullableStringValue(value="Couchers U"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                my_travels=api_pb2.NullableStringValue(
                    value="Oh the places you'll go!"),
                things_i_like=api_pb2.NullableStringValue(value="Couchers"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                languages=api_pb2.RepeatedStringValue(
                    exists=True, value=["Binary", "English"]),
                countries_visited=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                countries_lived=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                additional_information=api_pb2.NullableStringValue(
                    value="I <3 Couchers"),
            ))
        # all fields changed
        for field, value in res.ListFields():
            assert value == True

        user = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user.name == "New name"
        assert user.city == "Timbuktu"
        assert user.hometown == "Walla Walla"
        assert user.pronouns == "Ro, Robo, Robots"
        assert user.education == "Couchers U"
        assert user.my_travels == "Oh the places you'll go!"
        assert user.things_i_like == "Couchers"
        assert user.lat == 0.01
        assert user.lng == -2
        assert user.radius == 321
        assert user.occupation == "Testing"
        assert user.about_me == "I rule"
        assert user.about_place == "My place"
        assert user.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert "Binary" in user.languages
        assert "English" in user.languages
        assert user.additional_information == "I <3 Couchers"
        assert "UK" in user.countries_visited
        assert "Aus" in user.countries_visited
        assert "UK" in user.countries_lived
        assert "Aus" in user.countries_lived
コード例 #26
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_mutual_friends(db):
    user1, token1 = generate_user("user1")
    user2, token2 = generate_user("user2")
    user3, token3 = generate_user("user3")
    user4, token4 = generate_user("user4")
    user5, token5 = generate_user("user5")

    # arrange friends like this: 1<->2, 1<->3, 1<->4, 1<->5, 3<->2, 3<->4,
    # 2<->5 pending
    # so 1 and 2 should have mutual friend 3 only
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user3.id))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user4.id))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user5.id))

    with api_session(token3) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.received[0].user_id == user1.id
        fr_id = res.received[0].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user4.id))

    with api_session(token5) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.received[0].user_id == user1.id
        fr_id = res.received[0].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))

    with api_session(token2) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.received[0].user_id == user1.id
        fr_id = res.received[0].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))
        assert res.received[1].user_id == user3.id
        fr_id = res.received[1].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))

    with api_session(token4) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert res.received[0].user_id == user1.id
        fr_id = res.received[0].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))
        assert res.received[1].user_id == user3.id
        fr_id = res.received[1].friend_request_id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user5.id))

    with api_session(token1) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=str(user2.username)))
        assert len(res.mutual_friends) == 1
        assert res.mutual_friends[0].user_id == user3.id

    # and other way around same
    with api_session(token2) as api:
        res = api.GetUser(api_pb2.GetUserReq(user=str(user1.username)))
        assert len(res.mutual_friends) == 1
        assert res.mutual_friends[0].user_id == user3.id
コード例 #27
0
def test_ListFriends(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()

    # send friend request from user1 to user2 and user3
    with api_session(token1) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user3.id))

    with api_session(token3) as api:
        api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=user2.id))

    with api_session(token2) as api:
        res = api.ListFriendRequests(empty_pb2.Empty())
        assert len(res.received) == 2

        # order is an implementation detail
        user1_req = [req for req in res.received if req.user_id == user1.id][0]
        user3_req = [req for req in res.received if req.user_id == user3.id][0]

        assert user1_req.state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert user1_req.user_id == user1.id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(
                friend_request_id=user1_req.friend_request_id, accept=True))

        assert user3_req.state == api_pb2.FriendRequest.FriendRequestStatus.PENDING
        assert user3_req.user_id == user3.id
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(
                friend_request_id=user3_req.friend_request_id, accept=True))

        # check we now have two friends
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 2
        assert user1.id in res.user_ids
        assert user3.id in res.user_ids

    with api_session(token3) as api:
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 1
        assert user2.id in res.user_ids

        res = api.ListFriendRequests(empty_pb2.Empty())
        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
        api.RespondFriendRequest(
            api_pb2.RespondFriendRequestReq(friend_request_id=fr_id,
                                            accept=True))

        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 2
        assert user1.id in res.user_ids
        assert user2.id in res.user_ids

    with api_session(token1) as api:
        res = api.ListFriends(empty_pb2.Empty())
        assert len(res.user_ids) == 2
        assert user2.id in res.user_ids
        assert user3.id in res.user_ids
コード例 #28
0
def test_update_profile(db):
    user, token = generate_user()

    with api_session(token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_NAME

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    lat=wrappers_pb2.DoubleValue(value=0),
                    lng=wrappers_pb2.DoubleValue(value=0)))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_COORDINATE

        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                hometown=api_pb2.NullableStringValue(value="Walla Walla"),
                lat=wrappers_pb2.DoubleValue(value=0.01),
                lng=wrappers_pb2.DoubleValue(value=-2),
                radius=wrappers_pb2.DoubleValue(value=321),
                pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                education=api_pb2.NullableStringValue(value="Couchers U"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                my_travels=api_pb2.NullableStringValue(
                    value="Oh the places you'll go!"),
                things_i_like=api_pb2.NullableStringValue(value="Couchers"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                languages=api_pb2.RepeatedStringValue(
                    exists=True, value=["Binary", "English"]),
                countries_visited=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                countries_lived=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                additional_information=api_pb2.NullableStringValue(
                    value="I <3 Couchers"),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user_details.name == "New name"
        assert user_details.city == "Timbuktu"
        assert user_details.hometown == "Walla Walla"
        assert user_details.pronouns == "Ro, Robo, Robots"
        assert user_details.education == "Couchers U"
        assert user_details.my_travels == "Oh the places you'll go!"
        assert user_details.things_i_like == "Couchers"
        assert user_details.lat == 0.01
        assert user_details.lng == -2
        assert user_details.radius == 321
        assert user_details.occupation == "Testing"
        assert user_details.about_me == "I rule"
        assert user_details.about_place == "My place"
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert "Binary" in user_details.languages
        assert "English" in user_details.languages
        assert user_details.additional_information == "I <3 Couchers"
        assert "UK" in user_details.countries_visited
        assert "Aus" in user_details.countries_visited
        assert "UK" in user_details.countries_lived
        assert "Aus" in user_details.countries_lived

        # Test unset values
        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                hometown=api_pb2.NullableStringValue(is_null=True),
                radius=wrappers_pb2.DoubleValue(value=0),
                pronouns=api_pb2.NullableStringValue(is_null=True),
                occupation=api_pb2.NullableStringValue(is_null=True),
                education=api_pb2.NullableStringValue(is_null=True),
                about_me=api_pb2.NullableStringValue(is_null=True),
                my_travels=api_pb2.NullableStringValue(is_null=True),
                things_i_like=api_pb2.NullableStringValue(is_null=True),
                about_place=api_pb2.NullableStringValue(is_null=True),
                hosting_status=api_pb2.HOSTING_STATUS_UNKNOWN,
                meetup_status=api_pb2.MEETUP_STATUS_UNKNOWN,
                languages=api_pb2.RepeatedStringValue(exists=True, value=[]),
                countries_visited=api_pb2.RepeatedStringValue(exists=True,
                                                              value=[]),
                countries_lived=api_pb2.RepeatedStringValue(exists=True,
                                                            value=[]),
                additional_information=api_pb2.NullableStringValue(
                    is_null=True),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert not user_details.hometown
        assert not user_details.radius
        assert not user_details.pronouns
        assert not user_details.occupation
        assert not user_details.education
        assert not user_details.about_me
        assert not user_details.my_travels
        assert not user_details.things_i_like
        assert not user_details.about_place
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_UNKNOWN
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_UNKNOWN
        assert not user_details.languages
        assert not user_details.countries_visited
        assert not user_details.countries_lived
        assert not user_details.additional_information
コード例 #29
0
ファイル: test_api.py プロジェクト: ineswq/couchers
def test_references(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    alltypes = set([
        api_pb2.ReferenceType.FRIEND, api_pb2.ReferenceType.HOSTED,
        api_pb2.ReferenceType.SURFED
    ])
    # write all three reference types
    for typ in alltypes:
        req = api_pb2.WriteReferenceReq(to_user_id=user2.id,
                                        reference_type=typ,
                                        text="kinda weird sometimes")
        with api_session(token1) as api:
            res = api.WriteReference(req)
        assert isinstance(res, empty_pb2.Empty)

    # See what I have written. Paginate it.
    seen_types = set()
    for i in range(3):
        req = api_pb2.GetGivenReferencesReq(from_user_id=user1.id,
                                            number=1,
                                            start_at=i)
        with api_session(token1) as api:
            res = api.GetGivenReferences(req)
        assert res.total_matches == 3
        assert len(res.references) == 1
        assert res.references[0].from_user_id == user1.id
        assert res.references[0].to_user_id == user2.id
        assert res.references[0].text == "kinda weird sometimes"
        assert abs(to_aware_datetime(res.references[0].written_time) -
                   now()) <= timedelta(days=32)
        assert res.references[0].reference_type not in seen_types
        seen_types.add(res.references[0].reference_type)
    assert seen_types == alltypes

    # See what user2 have received. Paginate it.
    seen_types = set()
    for i in range(3):
        req = api_pb2.GetReceivedReferencesReq(to_user_id=user2.id,
                                               number=1,
                                               start_at=i)
        with api_session(token1) as api:
            res = api.GetReceivedReferences(req)
        assert res.total_matches == 3
        assert len(res.references) == 1
        assert res.references[0].from_user_id == user1.id
        assert res.references[0].to_user_id == user2.id
        assert res.references[0].text == "kinda weird sometimes"
        assert res.references[0].reference_type not in seen_types
        seen_types.add(res.references[0].reference_type)
    assert seen_types == alltypes

    # Check available types
    with api_session(token1) as api:
        res = api.AvailableWriteReferenceTypes(
            api_pb2.AvailableWriteReferenceTypesReq(to_user_id=user2.id))
    assert res.reference_types == []

    with api_session(token2) as api:
        res = api.AvailableWriteReferenceTypes(
            api_pb2.AvailableWriteReferenceTypesReq(to_user_id=user1.id))
    assert set(res.reference_types) == alltypes

    # Forbidden to write a second reference of the same type
    req = api_pb2.WriteReferenceReq(
        to_user_id=user2.id,
        reference_type=api_pb2.ReferenceType.HOSTED,
        text="ok")
    with api_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.WriteReference(req)
        assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
        assert e.value.details() == errors.REFERENCE_ALREADY_GIVEN

    # Nonexisting user
    req = api_pb2.WriteReferenceReq(
        to_user_id=0x7FFFFFFFFFFFFFFF,
        reference_type=api_pb2.ReferenceType.HOSTED,
        text="ok")
    with api_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.WriteReference(req)
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.USER_NOT_FOUND

    # yourself
    req = api_pb2.WriteReferenceReq(
        to_user_id=user1.id,
        reference_type=api_pb2.ReferenceType.HOSTED,
        text="ok")
    with api_session(token1) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.WriteReference(req)
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.CANT_REFER_SELF

    with api_session(token2) as api:
        # test the number of references in GetUser and Ping
        res = api.GetUser(api_pb2.GetUserReq(user=user2.username))
        assert res.num_references == 3

        res = api.Ping(api_pb2.PingReq())
        assert res.user.num_references == 3
コード例 #30
0
ファイル: test_requests.py プロジェクト: anuragb26/couchers
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(
                to_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(
                to_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(
                to_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