#!perl
use Cassandane::Tiny;

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

    xlog $self, "Create event with TEXT-escaped geo: URIs";
    my $ical = <<'EOF';
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.9.5//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART;TZID=Europe/Vienna:20160928T160000
DURATION:PT1H
UID:40d6fe3c-6a51-489e-823e-3ea22f427a3e
DTSTAMP:20150928T132434Z
CREATED:20150928T125212Z
SUMMARY:test
X-APPLE-STRUCTURED-LOCATION
 ;X-JMAP-ID=location1
 ;VALUE=URI;X-APPLE-RADIUS=141.175139;X-TITLE=test1
 :geo:13.4125\,103.8667
LOCATION
 ;X-JMAP-ID=location1
 :test1
X-JMAP-LOCATION
 ;X-JMAP-ID=location2
 ;VALUE=TEXT;X-JMAP-GEO="geo:14.4125\,104.8667"
 :test2
LAST-MODIFIED:20150928T132434Z
END:VEVENT
END:VCALENDAR
EOF
    my $res = $caldav->Request('PUT',
        '/dav/calendars/user/cassandane/Default/test.ics',
        $ical, 'Content-Type' => 'text/calendar');

    xlog $self, "Assert text-escaped geo: URI values are sanitized when read";

    $res = $jmap->CallMethods([
        ['CalendarEvent/get', {
            properties => ['locations'],
        }, 'R1']
    ]);
    $self->assert_str_equals('geo:13.4125,103.8667',
        $res->[0][1]{list}[0]{locations}{location1}{coordinates});
    $self->assert_str_equals('geo:14.4125,104.8667',
        $res->[0][1]{list}[0]{locations}{location2}{coordinates});

    xlog $self, "Assert text-escaped geo: URI values are rejected when set";

    my $eventId = $res->[0][1]{list}[0]{id};
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    'locations/location1/coordinates' => 'geo:13.4125\,103.8667',
                }
            },
        }, 'R1']
    ]);
    $self->assert_deep_equals(['locations/location1/coordinates'],
        $res->[0][1]{notUpdated}{$eventId}{properties});
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    'locations/location2/coordinates' => 'geo:14.4125\,104.8667',
                }
            },
        }, 'R1']
    ]);
    $self->assert_deep_equals(['locations/location2/coordinates'],
        $res->[0][1]{notUpdated}{$eventId}{properties});
}
