def test_set_profile_image(admin_stub, contact_profile_id, random_image,
                           random_image_base64_string):
    # print("random_image: {}".format(random_image))
    # print("random_image_base64_string: {}".format(random_image_base64_string))
    # Set the profile image to something new
    admin_stub.SetSqueakProfileImage(
        squeak_admin_pb2.SetSqueakProfileImageRequest(
            profile_id=contact_profile_id,
            profile_image=random_image_base64_string,
        ))

    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile.profile_image == random_image_base64_string
    assert squeak_profile.has_custom_profile_image

    # Clear the profile image
    admin_stub.ClearSqueakProfileImage(
        squeak_admin_pb2.ClearSqueakProfileImageRequest(
            profile_id=contact_profile_id, ))

    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile.profile_image != random_image_base64_string
    assert not squeak_profile.has_custom_profile_image
def test_make_signing_profile(admin_stub):
    # Create a new signing profile
    profile_name = "test_signing_profile_name"
    profile_id = create_signing_profile(admin_stub, profile_name)

    # Get the new squeak profile
    squeak_profile = get_squeak_profile(admin_stub, profile_id)
    assert squeak_profile.profile_name == profile_name
    squeak_profile_pubkey = squeak_profile.pubkey

    # Get all signing profiles
    get_signing_profiles_response = admin_stub.GetSigningProfiles(
        squeak_admin_pb2.GetSigningProfilesRequest()
    )
    signing_profile_names = [
        profile.profile_name
        for profile in get_signing_profiles_response.squeak_profiles
    ]
    assert profile_name in signing_profile_names

    # Get squeak profile by address
    # get_profile_by_address_response = admin_stub.GetSqueakProfileByAddress(
    #     squeak_admin_pb2.GetSqueakProfileByAddressRequest(
    #         pubkey=squeak_profile_pubkey
    #     )
    # )
    profile = get_profile_by_pubkey(admin_stub, squeak_profile_pubkey)
    assert profile.profile_name == profile_name

    # Export the private key, delete the profile, and re-import it.
    get_private_key_response = admin_stub.GetSqueakProfilePrivateKey(
        squeak_admin_pb2.GetSqueakProfilePrivateKeyRequest(
            profile_id=profile_id,
        )
    )
    private_key = get_private_key_response.private_key

    delete_profile(admin_stub, profile_id)
    new_profile_id = import_signing_profile(
        admin_stub,
        "imported_profile_name",
        private_key,
    )

    # Get the new imported profile
    squeak_profile = get_squeak_profile(admin_stub, new_profile_id)
    assert squeak_profile.profile_name == "imported_profile_name"
    assert squeak_profile.pubkey == squeak_profile_pubkey
def test_get_squeak_by_lookup(
    admin_stub,
    other_admin_stub,
    connected_tcp_peer_id,
    signing_profile_id,
    saved_squeak_hash,
):
    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    squeak_profile_address = squeak_profile.address
    squeak_profile.profile_name

    # Get the squeak display item
    squeak_display_entry = get_squeak_display(other_admin_stub,
                                              saved_squeak_hash)
    assert squeak_display_entry is None

    # Sync squeaks
    download_result = download_squeaks(
        other_admin_stub,
        [squeak_profile_address],
        -1,
        -1,
        None,
    )
    print(download_result)
    assert download_result.number_downloaded == 1
    assert download_result.number_requested == 10

    # Get the squeak display item
    squeak_display_entry = get_squeak_display(other_admin_stub,
                                              saved_squeak_hash)
    assert squeak_display_entry.squeak_hash == saved_squeak_hash
def test_get_profile(admin_stub, signing_profile_id):
    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    # address = squeak_profile.address
    pubkey = squeak_profile.pubkey
    name = squeak_profile.profile_name

    # Get the same squeak profile by address
    # get_squeak_profile_by_pubkey_response = admin_stub.GetSqueakProfileByAddress(
    #     squeak_admin_pb2.GetSqueakProfileByAddressRequest(
    #         pubkey=pubkey,
    #     )
    # )
    profile = get_profile_by_pubkey(admin_stub, pubkey)
    # assert address == get_squeak_profile_by_address_response.squeak_profile.address
    assert pubkey == profile.pubkey
    assert name == profile.profile_name

    # Get the same squeak profile by name
    get_squeak_profile_by_name_response = admin_stub.GetSqueakProfileByName(
        squeak_admin_pb2.GetSqueakProfileByNameRequest(
            name=name,
        )
    )
    assert pubkey == get_squeak_profile_by_name_response.squeak_profile.pubkey
    assert name == get_squeak_profile_by_name_response.squeak_profile.profile_name
def test_rename_profile(admin_stub, contact_profile_id, random_name):
    # Rename the profile to something new
    admin_stub.RenameSqueakProfile(
        squeak_admin_pb2.RenameSqueakProfileRequest(
            profile_id=contact_profile_id,
            profile_name=random_name,
        ))

    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile.profile_name == random_name
def test_make_squeak(admin_stub, signing_profile_id):
    # Create a new squeak using the new profile
    make_squeak_content = "Hello from the profile on the server!"
    make_squeak_hash = make_squeak(admin_stub, signing_profile_id,
                                   make_squeak_content)
    assert len(make_squeak_hash) == 32 * 2

    # Get the squeak display item
    get_squeak_display_entry = get_squeak_display(admin_stub, make_squeak_hash)
    assert get_squeak_display_entry.squeak_hash == make_squeak_hash
    assert (get_squeak_display_entry.content_str ==
            "Hello from the profile on the server!")
    # assert get_squeak_display_response.squeak_display_entry.author_address == signing_profile_address
    assert get_squeak_display_entry.is_author_known
    assert get_squeak_display_entry.HasField("author")
    assert len(get_squeak_display_entry.author.profile_image) > 0
    assert not get_squeak_display_entry.is_reply
    assert not bool(get_squeak_display_entry.reply_to)
    assert len(get_squeak_display_entry.secret_key_hex) == 32 * 2

    # Block time should be within the past hour
    block_time = datetime.datetime.fromtimestamp(
        get_squeak_display_entry.block_time, )
    one_hour = datetime.timedelta(hours=1)
    assert block_time > datetime.datetime.now() - one_hour
    assert block_time < datetime.datetime.now() + one_hour

    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    squeak_profile_address = squeak_profile.address
    squeak_profile_name = squeak_profile.profile_name

    # Get all squeak displays for the known address
    get_address_squeak_display_response = admin_stub.GetAddressSqueakDisplays(
        squeak_admin_pb2.GetAddressSqueakDisplaysRequest(
            address=squeak_profile_address,
            limit=100,
        ), )
    assert len(get_address_squeak_display_response.squeak_display_entries) == 1
    for (squeak_display_entry
         ) in get_address_squeak_display_response.squeak_display_entries:
        assert squeak_display_entry.author.profile_name == squeak_profile_name
        assert squeak_display_entry.author.address == squeak_profile_address

    # check serialized squeak hex string
    serialized_squeak_hex = get_squeak_display_entry.serialized_squeak_hex
    # print("serialized_squeak_hex: {}".format(serialized_squeak_hex))
    assert len(serialized_squeak_hex) > 200
    serialized_squeak = bytes.fromhex(serialized_squeak_hex)
    deserialized_squeak = CSqueak.deserialize(serialized_squeak)
    assert get_hash(deserialized_squeak) == make_squeak_hash
    CheckSqueak(deserialized_squeak)
def test_set_profile_following(admin_stub, contact_profile_id):
    # Set the profile to be following
    admin_stub.SetSqueakProfileFollowing(
        squeak_admin_pb2.SetSqueakProfileFollowingRequest(
            profile_id=contact_profile_id,
            following=True,
        ))

    # Get the squeak profile again
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile.following

    # Set the profile to be not following
    admin_stub.SetSqueakProfileFollowing(
        squeak_admin_pb2.SetSqueakProfileFollowingRequest(
            profile_id=contact_profile_id,
            following=False,
        ))

    # Get the squeak profile again
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert not squeak_profile.following
def test_subscribe_squeaks(
    admin_stub,
    other_admin_stub,
    signing_profile_id,
):

    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    squeak_profile_address = squeak_profile.address
    squeak_profile_name = squeak_profile.profile_name

    # Add the contact profile to the other server and set the profile to be following
    contact_profile_id = create_contact_profile(other_admin_stub,
                                                squeak_profile_name,
                                                squeak_profile_address)
    other_admin_stub.SetSqueakProfileFollowing(
        squeak_admin_pb2.SetSqueakProfileFollowingRequest(
            profile_id=contact_profile_id,
            following=True,
        ))

    with free_price(admin_stub), \
        open_peer_connection(
        other_admin_stub,
        "test_peer",
        "squeaknode",
        18777,
    ):
        # Create a new squeak using the new profile
        make_squeak_content = "Hello this message should be subscribed!"
        make_squeak_hash = make_squeak(
            admin_stub,
            signing_profile_id,
            make_squeak_content,
        )

        time.sleep(2)

        # Get the squeak display item
        squeak_display_entry = get_squeak_display(
            other_admin_stub,
            make_squeak_hash,
        )
        assert squeak_display_entry is not None

        # Get the squeak display item
        get_squeak_display_entry = get_squeak_display(
            other_admin_stub,
            make_squeak_hash,
        )
        assert (get_squeak_display_entry.content_str == make_squeak_content)
def test_get_profile(admin_stub, signing_profile_id):
    # Get the squeak profile
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    address = squeak_profile.address
    name = squeak_profile.profile_name

    # Get the same squeak profile by address
    get_squeak_profile_by_address_response = admin_stub.GetSqueakProfileByAddress(
        squeak_admin_pb2.GetSqueakProfileByAddressRequest(address=address, ))
    assert address == get_squeak_profile_by_address_response.squeak_profile.address
    assert name == get_squeak_profile_by_address_response.squeak_profile.profile_name

    # Get the same squeak profile by name
    get_squeak_profile_by_name_response = admin_stub.GetSqueakProfileByName(
        squeak_admin_pb2.GetSqueakProfileByNameRequest(name=name, ))
    assert address == get_squeak_profile_by_name_response.squeak_profile.address
    assert name == get_squeak_profile_by_name_response.squeak_profile.profile_name
def test_delete_profile(admin_stub, random_name, squeak_address,
                        contact_profile_id):
    # Delete the profile
    delete_profile(admin_stub, contact_profile_id)

    # Try to get the profile and fail
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile is None

    get_squeak_profile_by_name_response = admin_stub.GetSqueakProfileByName(
        squeak_admin_pb2.GetSqueakProfileByNameRequest(name=random_name, ))
    assert not get_squeak_profile_by_name_response.HasField("squeak_profile")

    get_squeak_profile_by_address_response = admin_stub.GetSqueakProfileByAddress(
        squeak_admin_pb2.GetSqueakProfileByAddressRequest(
            address=squeak_address, ))
    assert not get_squeak_profile_by_address_response.HasField(
        "squeak_profile")
def test_download_squeaks_for_address(
    admin_stub,
    other_admin_stub,
    connected_tcp_peer_id,
    signing_profile_id,
    saved_squeak_hash,
):
    squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
    squeak_profile_address = squeak_profile.address

    with subscribe_squeaks_for_address(
            other_admin_stub, squeak_profile_address) as subscription_queue:

        # Get the squeak display item (should be empty)
        squeak_display_entry = get_squeak_display(other_admin_stub,
                                                  saved_squeak_hash)
        assert squeak_display_entry is None

        # Get buy offers for the squeak hash (should be empty)
        get_buy_offers_response = other_admin_stub.GetBuyOffers(
            squeak_admin_pb2.GetBuyOffersRequest(
                squeak_hash=saved_squeak_hash, ))
        # print(get_buy_offers_response)
        assert len(get_buy_offers_response.offers) == 0

        # Download squeaks for address
        download_result = download_squeaks_for_address(other_admin_stub,
                                                       squeak_profile_address)
        assert download_result.number_downloaded == 1
        assert download_result.number_requested == 10

        # Get the squeak display item
        squeak_display_entry = get_squeak_display(other_admin_stub,
                                                  saved_squeak_hash)
        assert squeak_display_entry is not None

        item = subscription_queue.get()
        print("item:")
        print(item)
        assert item.squeak_hash == saved_squeak_hash
def test_delete_profile(admin_stub, random_name, public_key, contact_profile_id):
    # Delete the profile
    delete_profile(admin_stub, contact_profile_id)

    # Try to get the profile and fail
    squeak_profile = get_squeak_profile(admin_stub, contact_profile_id)
    assert squeak_profile is None

    get_squeak_profile_by_name_response = admin_stub.GetSqueakProfileByName(
        squeak_admin_pb2.GetSqueakProfileByNameRequest(
            name=random_name,
        )
    )
    assert not get_squeak_profile_by_name_response.HasField("squeak_profile")

    # get_squeak_profile_by_pubkey_response = admin_stub.GetSqueakProfileByAddress(
    #     squeak_admin_pb2.GetSqueakProfileByAddressRequest(
    #         pubkey=public_key.to_bytes().hex(),
    #     )
    # )
    profile = get_profile_by_pubkey(admin_stub, public_key.to_bytes().hex())
    # assert not get_squeak_profile_by_pubkey_response.HasField(
    #     "squeak_profile")
    assert profile is None