#!perl
use Cassandane::Tiny;

sub test_contact_get_avatar_v4
    :min_version_3_5
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $service = $self->{instance}->get_service("http");
    $ENV{DEBUGDAV} = 1;
    my $carddav = Net::CardDAVTalk->new(
        user => 'cassandane',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    xlog $self, "create a v4 contact with a photo";
    my $id = '816ad14a-f9ef-43a8-9039-b57bf321de1f';
    my $href = "Default/$id.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:4.0
PRODID:+//IDN bitfire.at//DAVx5/4.2.0.3-gplay ez-vcard/0.11.3
UID:$id
FN:Foo
N:;Foo;;;
PHOTO:data:;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAA
 XNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAnBpVFh0WE1MOmNvbS5hZG9iZS54bXA
 AAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb
 3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk
 5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvd
 XQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzE
 uMC8iCiAgICAgICAgICAgIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL
 yI+CiAgICAgICAgIDx0aWZmOllSZXNvbHV0aW9uPjcyPC90aWZmOllSZXNvbHV0aW9uPgogICA
 gICAgICA8dGlmZjpDb21wcmVzc2lvbj41PC90aWZmOkNvbXByZXNzaW9uPgogICAgICAgICA8d
 GlmZjpYUmVzb2x1dGlvbj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FnXnW4COB3hzlFyvihueQAmNCZC
 0RHmDEoZRi91fyk75RgpPfk4TRBLNnFSh1zPSy0JWid1cw4fcPxrqbI0cu0pB649Gmh3ZLSSGc
 UOMMc/svu7ZvzdtMQnVJMfahynGx4oQ8O1kudZujfiPdECftwWQvKy9Lfks/JewppPSBJTLgr2
 hJE32b3h9tBW2LX8UK2IwMtIcY54yGUBh6ox7TTBtjaJY+sHEPBlthF6XyZmJkgHZ/srLlS4qm
 mLYr+yeyxDMYuEFM2LmX8mADE6fdsaJsOXDfkUzQSiMq8kZp2YCDGSTsKWTLjwo5lm/zJbAgA5
 OjzbP8PxolkCtFHqx6kOlMOID6rEi/QRGHdYjlm2LTkBWDM2tuoiF4qc5fp6Dzuc5wCIF/DwHE
 s0vNNkH7/kPm8LoeIwG1jYAAAAASUVORK5CYII=
REV:20220504T040120Z
END:VCARD
EOF

    $card =~ s/\r?\n/\r\n/gs;

    $carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

    my $res = $jmap->CallMethods([
        ['Contact/get', {
            properties => ['avatar', 'x-hasPhoto'],
        }, 'R1']
    ]);

    $self->assert_not_null($res->[0][1]{list}[0]{id});
    $self->assert_not_null($res->[0][1]{list}[0]{avatar});
    $self->assert_equals("image/png", $res->[0][1]{list}[0]{avatar}{type});
    $self->assert_equals(JSON::true, $res->[0][1]{list}[0]{'x-hasPhoto'});
}
