Exemple #1
0
    def SendFriendRequest(self, request, context):
        with session_scope() as session:
            from_user = session.query(User).filter(
                User.id == context.user_id).one_or_none()

            if not from_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            to_user = session.query(User).filter(
                User.id == request.user_id).one_or_none()

            if not to_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            if get_friends_status(
                    session, from_user.id,
                    to_user.id) != api_pb2.User.FriendshipStatus.NOT_FRIENDS:
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.FRIENDS_ALREADY_OR_PENDING)

            # Race condition!

            friend_relationship = FriendRelationship(
                from_user=from_user,
                to_user=to_user,
                status=FriendStatus.pending)
            session.add(friend_relationship)

            send_friend_request_email(friend_relationship)

            return empty_pb2.Empty()
Exemple #2
0
def test_friend_request_email(db):
    with session_scope(db) as session:
        from_user, api_token_from = generate_user(db)
        to_user, api_token_to = generate_user(db)
        friend_relationship = FriendRelationship(from_user=from_user,
                                                 to_user=to_user,
                                                 status=FriendStatus.pending)

        message_id = random_hex(64)

        @create_autospec
        def mock_send_email(sender_name, sender_email, recipient, subject,
                            plain, html):
            assert recipient == to_user.email
            assert "friend" in subject.lower()
            assert to_user.name in plain
            assert to_user.name in html
            assert from_user.name in subject
            assert from_user.name in plain
            assert from_user.name in html
            assert from_user.avatar_url not in plain
            assert from_user.avatar_url in html
            assert f"{config['BASE_URL']}/friends/" in plain
            assert f"{config['BASE_URL']}/friends/" in html
            return message_id

        with patch("couchers.email.send_email", mock_send_email) as mock:
            send_friend_request_email(friend_relationship)

        assert mock.call_count == 1
Exemple #3
0
def test_friend_request_email(db):
    with session_scope() as session:
        to_user, api_token_to = generate_user()
        # little trick here to get the upload correctly without invalidating users
        key = random_hex(32)
        filename = random_hex(32) + ".jpg"
        session.add(
            Upload(
                key=key,
                filename=filename,
                creator_user_id=to_user.id,
            )
        )
        session.commit()
        from_user, api_token_from = generate_user(avatar_key=key)
        friend_relationship = FriendRelationship(from_user=from_user, to_user=to_user, status=FriendStatus.pending)
        session.add(friend_relationship)

        with patch("couchers.email.queue_email") as mock:
            send_friend_request_email(friend_relationship)

        assert mock.call_count == 1
        (sender_name, sender_email, recipient, subject, plain, html), _ = mock.call_args
        assert recipient == to_user.email
        assert "friend" in subject.lower()
        assert to_user.name in plain
        assert to_user.name in html
        assert from_user.name in subject
        assert from_user.name in plain
        assert from_user.name in html
        assert from_user.avatar.thumbnail_url not in plain
        assert from_user.avatar.thumbnail_url in html
        assert f"{config['BASE_URL']}/connections/friends/" in plain
        assert f"{config['BASE_URL']}/connections/friends/" in html
Exemple #4
0
    def SendFriendRequest(self, request, context):
        if context.user_id == request.user_id:
            context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                          errors.CANT_FRIEND_SELF)

        with session_scope() as session:
            user = session.execute(
                select(User).where(User.id == context.user_id)).scalar_one()
            to_user = session.execute(
                select(User).where_users_visible(context).where(
                    User.id == request.user_id)).scalar_one_or_none()

            if not to_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            if (session.execute(
                    select(FriendRelationship).where(
                        or_(
                            and_(
                                FriendRelationship.from_user_id ==
                                context.user_id,
                                FriendRelationship.to_user_id ==
                                request.user_id,
                            ),
                            and_(
                                FriendRelationship.from_user_id ==
                                request.user_id,
                                FriendRelationship.to_user_id ==
                                context.user_id,
                            ),
                        )).
                    where(
                        or_(
                            FriendRelationship.status == FriendStatus.accepted,
                            FriendRelationship.status == FriendStatus.pending,
                        ))).scalar_one_or_none() is not None):
                context.abort(grpc.StatusCode.FAILED_PRECONDITION,
                              errors.FRIENDS_ALREADY_OR_PENDING)

            # TODO: Race condition where we can create two friend reqs, needs db constraint! See comment in table

            friend_relationship = FriendRelationship(
                from_user=user, to_user=to_user, status=FriendStatus.pending)
            session.add(friend_relationship)
            session.commit()

            send_friend_request_email(friend_relationship)

            notify(
                user_id=friend_relationship.to_user_id,
                topic="friend_request",
                key=str(friend_relationship.from_user_id),
                action="send",
                avatar_key=user.avatar.thumbnail_url if user.avatar else None,
                icon="person",
                title=f"**{user.name}** sent you a friend request",
                link=urls.friend_requests_link(),
            )

            return empty_pb2.Empty()
Exemple #5
0
    def SendFriendRequest(self, request, context):
        if context.user_id == request.user_id:
            context.abort(grpc.StatusCode.FAILED_PRECONDITION, errors.CANT_FRIEND_SELF)

        with session_scope() as session:
            user = session.query(User).filter(User.id == context.user_id).one()
            to_user = session.query(User).filter(User.id == request.user_id).one_or_none()

            if not to_user:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND)

            if (
                session.query(FriendRelationship)
                .filter(
                    or_(
                        and_(
                            FriendRelationship.from_user_id == context.user_id,
                            FriendRelationship.to_user_id == request.user_id,
                        ),
                        and_(
                            FriendRelationship.from_user_id == request.user_id,
                            FriendRelationship.to_user_id == context.user_id,
                        ),
                    )
                )
                .filter(
                    or_(
                        FriendRelationship.status == FriendStatus.accepted,
                        FriendRelationship.status == FriendStatus.pending,
                    )
                )
                .one_or_none()
                is not None
            ):
                context.abort(grpc.StatusCode.FAILED_PRECONDITION, errors.FRIENDS_ALREADY_OR_PENDING)

            # TODO: Race condition where we can create two friend reqs, needs db constraint! See comment in table

            friend_relationship = FriendRelationship(from_user=user, to_user=to_user, status=FriendStatus.pending)
            session.add(friend_relationship)

            send_friend_request_email(friend_relationship)

            return empty_pb2.Empty()
Exemple #6
0
def test_email_patching_fails(db):
    """
    There was a problem where the mocking wasn't happening and the email dev
    printing function was called instead, this makes sure the patching is
    actually done
    """
    with session_scope() as session:
        from_user, api_token_from = generate_user()
        to_user, api_token_to = generate_user()
        friend_relationship = FriendRelationship(from_user=from_user, to_user=to_user, status=FriendStatus.pending)
        session.add(friend_relationship)

        patched_msg = random_hex(64)

        def mock_queue_email(sender_name, sender_email, recipient, subject, plain, html):
            raise Exception(patched_msg)

        with pytest.raises(Exception) as e:
            with patch("couchers.email.queue_email", mock_queue_email):
                send_friend_request_email(friend_relationship)
        assert str(e.value) == patched_msg