#!perl
use Cassandane::Tiny;

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

    xlog "Create event via CalDAV";
    my $rawIcal = <<'EOF';
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.9.5//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
TRANSP:TRANSPARENT
DTSTART:20160928T160000Z
DTEND:20160928T170000Z
UID:2a358cee-6489-4f14-a57f-c104db4dc357
DTSTAMP:20150928T132434Z
CREATED:20150928T125212Z
SUMMARY:test
ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/plain:aGVsbG8=
SEQUENCE:0
LAST-MODIFIED:20150928T132434Z
END:VEVENT
END:VCALENDAR
EOF
    $caldav->Request('PUT', 'Default/test.ics', $rawIcal,
        'Content-Type' => 'text/calendar');

    xlog "Fetch with Cyrus extension";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/get', {
            properties => ['links'],
        }, 'R1'],
    ]);
    my $event = $res->[0][1]{list}[0];
    $self->assert_not_null($event);

    my @links = values %{$event->{links}};
    $self->assert_num_equals(1, scalar @links);
    $self->assert_null($links[0]{href});
    $self->assert_str_equals('text/plain', $links[0]{contentType});
    my $blobId = $links[0]{blobId};
    $self->assert_not_null($blobId);

    xlog "Fetch blob";
    $res = $jmap->Download('cassandane', $blobId);
    $self->assert_str_equals("hello", $res->{content});

    xlog "Fetch without Cyrus extension";
    $res = $jmap->CallMethods([
        ['CalendarEvent/get', {
            properties => ['links'],
        }, 'R2'],
    ], [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:calendars',
        'urn:ietf:params:jmap:principals',
    ]);
    $event = $res->[0][1]{list}[0];
    $self->assert_not_null($event);

    @links = values %{$event->{links}};
    $self->assert_num_equals(1, scalar @links);
    $self->assert_str_equals('data:text/plain;base64,aGVsbG8=', $links[0]{href});
    $self->assert_str_equals('text/plain', $links[0]{contentType});
}
