#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_writeown_caldav_scheduleaddress
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $caldav = $self->{caldav};

    xlog "Create sharee user";
    $self->{instance}->create_user("sharee");
    my $service = $self->{instance}->get_service("http");
    my $shareeCaldav = Net::CalDAVTalk->new(
        user => "sharee",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    xlog "Share calendar with mayWriteOwn only";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    shareWith => {
                        sharee => {
                            mayReadItems => JSON::true,
                            mayWriteOwn => JSON::true,
                        },
                    },
                },
            },
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    xlog "Create event organized by the owner";
    $caldav->Request('PUT',
        '/dav/calendars/user/cassandane/Default/cassowner.ics',
        <<'EOF', 'Content-Type' => 'text/calendar');
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Cassandane//EN
BEGIN:VEVENT
DTSTART:20260928T160000Z
DURATION:PT1H
UID:40d6fe3c-6a51-489e-823e-3ea22f427a3e
DTSTAMP:20260928T132434Z
SUMMARY:cassowner
ORGANIZER:mailto:cassandane@example.com
ATTENDEE;PARTSTAT=ACCEPTED:mailto:cassandane@example.com
ATTENDEE:mailto:attendee@example.com
END:VEVENT
END:VCALENDAR
EOF
    my $href = '/dav/calendars/user/sharee/cassandane.Default/cassowner.ics';

    xlog "Sharee fetches the owner's event";
    $res = $shareeCaldav->SafeRequest('GET', $href, '');
    $self->assert_num_equals(200, $res->{http_response}{status});

    my $updatedIcal = ($res->{payload}{content} =~
                       s{^SUMMARY:cassowner(\r?)$}{SUMMARY:cassownerChanged$1}mr);
    $self->assert_matches(qr/^SUMMARY:cassownerChanged\r?$/m, $updatedIcal);

    xlog "Baseline: update as sharee without Schedule-Address is forbidden";
    $res = $shareeCaldav->SafeRequest('PUT', $href, $updatedIcal,
        'Content-Type' => 'text/calendar');
    $self->assert_num_equals(403, $res->{http_response}{status});

    xlog "Baseline: delete as sharee without Schedule-Address is forbidden";
    $res = $shareeCaldav->SafeRequest('DELETE', $href, '');
    $self->assert_num_equals(403, $res->{http_response}{status});

    xlog "Update as sharee claiming the owner's Schedule-Address is forbidden";
    $res = $shareeCaldav->SafeRequest('PUT', $href, $updatedIcal,
        'Content-Type' => 'text/calendar',
        'Schedule-Address' => 'cassandane@example.com');
    $self->assert_num_equals(403, $res->{http_response}{status});

    xlog "Delete as sharee claiming the owner's Schedule-Address is forbidden";
    $res = $shareeCaldav->SafeRequest('DELETE', $href, '',
        'Schedule-Address' => 'cassandane@example.com');
    $self->assert_num_equals(403, $res->{http_response}{status});

    xlog "The owner's event must be unchanged";
    $res = $jmap->CallMethods([
        ['CalendarEvent/get', { properties => ['title'] }, 'R1'],
    ]);
    $self->assert_str_equals('cassowner', $res->[0][1]{list}[0]{title});
}
