Beispiel #1
0
def test_make_user_block(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    make_user_block(user1, user2)

    with session_scope() as session:
        blocked_user_list = (
            session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
        )
        assert len(blocked_user_list) == 1
Beispiel #2
0
def test_relationships_userblock_dot_user(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()

    make_user_block(user1, user2)

    with session_scope() as session:
        block = session.execute(
            select(UserBlock).where((UserBlock.blocking_user_id == user1.id) & (UserBlock.blocked_user_id == user2.id))
        ).scalar_one_or_none()
        assert block.blocking_user.username == user1.username
        assert block.blocked_user.username == user2.username
Beispiel #3
0
def test_GetBlockedUsers(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()

    with blocking_session(token1) as user_blocks:
        # Check no blocked users to start
        blocked_user_list = user_blocks.GetBlockedUsers(empty_pb2.Empty())
        assert len(blocked_user_list.blocked_usernames) == 0

        make_user_block(user1, user2)
        make_user_block(user1, user3)
        blocked_user_list = user_blocks.GetBlockedUsers(empty_pb2.Empty())
        assert len(blocked_user_list.blocked_usernames) == 2
Beispiel #4
0
def test_select_dot_where_users_visible(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user(delete_user=True)
    user3, token3 = generate_user()
    user4, token4 = generate_user()

    make_user_block(user1, user3)
    make_user_block(user4, user1)

    context = _FakeContext(user1.id)
    with session_scope() as session:
        assert session.execute(
            select(func.count()).select_from(User).where_users_visible(
                context)).scalar_one() == 1
Beispiel #5
0
def test_select_dot_where_users_column_visible(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    user4, token4 = generate_user()
    user5, token5 = generate_user()

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

    make_user_invisible(user3.id)
    make_user_block(user1, user4)
    make_user_block(user5, user1)

    context = _FakeContext(user1.id)
    with session_scope() as session:
        assert (session.execute(
            select(func.count()).select_from(
                FriendRelationship).where_users_column_visible(
                    context, FriendRelationship.to_user_id)).scalar_one() == 1)
Beispiel #6
0
def test_UnblockUser(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    make_user_block(user1, user2)

    with blocking_session(token1) as user_blocks:
        user_blocks.UnblockUser(blocking_pb2.UnblockUserReq(username=user2.username))

    with session_scope() as session:
        blocked_users = session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
        assert len(blocked_users) == 0

    with blocking_session(token1) as user_blocks:
        with pytest.raises(grpc.RpcError) as e:
            user_blocks.UnblockUser(blocking_pb2.UnblockUserReq(username=user2.username))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.USER_NOT_BLOCKED

        # Test re-blocking
        user_blocks.BlockUser(blocking_pb2.BlockUserReq(username=user2.username))

    with session_scope() as session:
        blocked_users = session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
        assert len(blocked_users) == 1
def test_AvailableWriteReferences_and_ListPendingReferencesToWrite(db):
    user1, token1 = generate_user()
    user2, token2 = generate_user()
    user3, token3 = generate_user()
    user4, token4 = generate_user()
    user5, token5 = generate_user(delete_user=True)
    user6, token6 = generate_user()
    user7, token7 = generate_user()
    make_user_block(user1, user6)
    make_user_block(user7, user1)

    with session_scope() as session:
        # too old
        hr1 = create_host_request(session, user3.id, user1.id,
                                  timedelta(days=20))

        # already wrote friend ref to user3
        create_friend_reference(session, user1.id, user3.id,
                                timedelta(days=15, seconds=70))

        # already given
        _, hr2 = create_host_reference(session,
                                       user2.id,
                                       user1.id,
                                       timedelta(days=10, seconds=110),
                                       surfing=True)
        create_host_reference(session,
                              user1.id,
                              user2.id,
                              timedelta(days=10, seconds=100),
                              host_request_id=hr2)

        # valid hosted
        hr3 = create_host_request(session, user3.id, user1.id,
                                  timedelta(days=8))

        # valid surfed
        hr4 = create_host_request(session, user1.id, user4.id,
                                  timedelta(days=5))

        # not yet complete
        hr5 = create_host_request(session,
                                  user2.id,
                                  user1.id,
                                  timedelta(days=2),
                                  status=HostRequestStatus.pending)

        # already wrote friend ref to user2
        create_friend_reference(session, user1.id, user2.id, timedelta(days=1))

        # user5 deleted, reference won't show up as pending
        create_host_request(session, user1.id, user5.id, timedelta(days=5))

        # user6 blocked, reference won't show up as pending
        create_host_request(session, user1.id, user6.id, timedelta(days=5))

        # user7 blocking, reference won't show up as pending
        create_host_request(session, user1.id, user7.id, timedelta(days=5))

    with references_session(token1) as api:
        # can't write reference for invisible user
        with pytest.raises(grpc.RpcError) as e:
            api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user5.id))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.USER_NOT_FOUND

        # can't write reference for blocking user
        with pytest.raises(grpc.RpcError) as e:
            api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user7.id))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.USER_NOT_FOUND

        # can't write reference for blocked user
        with pytest.raises(grpc.RpcError) as e:
            api.AvailableWriteReferences(
                references_pb2.AvailableWriteReferencesReq(
                    to_user_id=user6.id))
        assert e.value.code() == grpc.StatusCode.NOT_FOUND
        assert e.value.details() == errors.USER_NOT_FOUND

        # can't write anything to myself
        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user1.id))
        assert not res.can_write_friend_reference
        assert len(res.available_write_references) == 0

        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user2.id))
        # can't write friend ref to user2
        assert not res.can_write_friend_reference
        # none we can write for user2
        assert len(res.available_write_references) == 0

        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user3.id))
        # can't write friend ref to user3
        assert not res.can_write_friend_reference
        # can write one reference because we hosted user3
        assert len(res.available_write_references) == 1
        w = res.available_write_references[0]
        assert w.host_request_id == hr3
        assert w.reference_type == references_pb2.REFERENCE_TYPE_HOSTED
        assert now() + timedelta(days=6) <= to_aware_datetime(
            w.time_expires) <= now() + timedelta(days=7)

        res = api.AvailableWriteReferences(
            references_pb2.AvailableWriteReferencesReq(to_user_id=user4.id))
        # can write friend ref to user4
        assert res.can_write_friend_reference
        # can write one reference because we surfed with user4
        assert len(res.available_write_references) == 1
        w = res.available_write_references[0]
        assert w.host_request_id == hr4
        assert w.reference_type == references_pb2.REFERENCE_TYPE_SURFED
        assert now() + timedelta(days=9) <= to_aware_datetime(
            w.time_expires) <= now() + timedelta(days=10)

        # finally check the general list
        res = api.ListPendingReferencesToWrite(empty_pb2.Empty())
        assert len(res.pending_references) == 2
        w = res.pending_references[0]
        assert w.host_request_id == hr3
        assert w.reference_type == references_pb2.REFERENCE_TYPE_HOSTED
        assert now() + timedelta(days=6) <= to_aware_datetime(
            w.time_expires) <= now() + timedelta(days=7)
        w = res.pending_references[1]
        assert w.host_request_id == hr4
        assert w.reference_type == references_pb2.REFERENCE_TYPE_SURFED
        assert now() + timedelta(days=9) <= to_aware_datetime(
            w.time_expires) <= now() + timedelta(days=10)
Beispiel #8
0
def test_process_send_reference_reminders(db):
    # need to test:
    # case 1: bidirectional (no emails)
    # case 2: host left ref (surfer needs an email)
    # case 3: surfer left ref (host needs an email)
    # case 4: neither left ref (host & surfer need an email)

    process_send_reference_reminders(empty_pb2.Empty())

    # case 1: bidirectional (no emails)
    user1, token1 = generate_user(email="*****@*****.**",
                                  name="User 1")
    user2, token2 = generate_user(email="*****@*****.**",
                                  name="User 2")

    # case 2: host left ref (surfer needs an email)
    # host
    user3, token3 = generate_user(email="*****@*****.**",
                                  name="User 3")
    # surfer
    user4, token4 = generate_user(email="*****@*****.**",
                                  name="User 4")

    # case 3: surfer left ref (host needs an email)
    # host
    user5, token5 = generate_user(email="*****@*****.**",
                                  name="User 5")
    # surfer
    user6, token6 = generate_user(email="*****@*****.**",
                                  name="User 6")

    # case 4: neither left ref (host & surfer need an email)
    # host
    user7, token7 = generate_user(email="*****@*****.**",
                                  name="User 7")
    # surfer
    user8, token8 = generate_user(email="*****@*****.**",
                                  name="User 8")

    # case 5: neither left ref, but host blocked surfer, so neither should get an email
    # host
    user9, token9 = generate_user(email="*****@*****.**",
                                  name="User 9")
    # surfer
    user10, token10 = generate_user(email="*****@*****.**",
                                    name="User 10")

    make_user_block(user9, user10)

    with session_scope() as session:
        # note that create_host_reference creates a host request whose age is one day older than the timedelta here

        # case 1: bidirectional (no emails)
        ref1, hr1 = create_host_reference(session,
                                          user2.id,
                                          user1.id,
                                          timedelta(days=7),
                                          surfing=True)
        create_host_reference(session,
                              user1.id,
                              user2.id,
                              timedelta(days=7),
                              host_request_id=hr1)

        # case 2: host left ref (surfer needs an email)
        ref2, hr2 = create_host_reference(session,
                                          user3.id,
                                          user4.id,
                                          timedelta(days=11),
                                          surfing=False)

        # case 3: surfer left ref (host needs an email)
        ref3, hr3 = create_host_reference(session,
                                          user6.id,
                                          user5.id,
                                          timedelta(days=9),
                                          surfing=True)

        # case 4: neither left ref (host & surfer need an email)
        hr4 = create_host_request(session, user7.id, user8.id,
                                  timedelta(days=4))

        # case 5: neither left ref, but host blocked surfer, so neither should get an email
        hr5 = create_host_request(session, user9.id, user10.id,
                                  timedelta(days=7))

    expected_emails = [
        ("*****@*****.**",
         "[TEST] You have 3 days to write a reference for User 3!"),
        ("*****@*****.**",
         "[TEST] You have 7 days to write a reference for User 6!"),
        ("*****@*****.**",
         "[TEST] You have 14 days to write a reference for User 8!"),
        ("*****@*****.**",
         "[TEST] You have 14 days to write a reference for User 7!"),
    ]

    process_send_reference_reminders(empty_pb2.Empty())

    while process_job():
        pass

    with session_scope() as session:
        emails = [
            (email.recipient, email.subject) for email in session.execute(
                select(Email).order_by(Email.recipient.asc())).scalars().all()
        ]

        print(emails)
        print(expected_emails)

        assert emails == expected_emails