def test_cant_friend_request_twice(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 pytest.raises(grpc.RpcError) as e: api.SendFriendRequest( api_pb2.SendFriendRequestReq(user_id=user2.id)) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING
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
def test_cant_friend_request_already_friends(db): user1, token1 = generate_user() user2, token2 = generate_user() make_friends(user1, user2) with api_session(token1) as api: with pytest.raises(grpc.RpcError) as e: api.SendFriendRequest( api_pb2.SendFriendRequestReq(user_id=user2.id)) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.FRIENDS_ALREADY_OR_PENDING 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
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
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
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
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