#!perl
use Cassandane::Tiny;

sub test_imip_strip_personal_data
    :needs_component_httpd :want_service_http
{
    my ($self) = @_;

    my $IMAP = $self->{store}->get_client();
    $self->{store}->_select();
    $self->assert_num_equals(1, $IMAP->uid());
    $self->{store}->set_fetch_attributes(qw(uid flags));

    xlog $self, "Create calendar user";
    my $CalDAV = $self->{caldav};
    my $CalendarId = 'Default';
    my $uuid = "6de280c9-edff-4019-8ebd-cfebc73f8201";

    xlog $self, "Install a sieve script to process iMIP";
    $self->{instance}->install_sieve_script(<<EOF
require ["body", "variables", "imap4flags", "processcalendar"];
if body :content "text/calendar" :contains "\nMETHOD:" {
    processcalendar :outcome "outcome";
    if string "\${outcome}" "updated" {
        setflag "\\\\Flagged";
    }
}
EOF
    );

    my $imip = <<EOF;
Date: Thu, 23 Sep 2021 09:06:18 -0400
From: Foo <foo\@example.net>
To: Cassandane <cassandane\@example.com>
Message-ID: <$uuid-0\@example.net>
Content-Type: text/calendar; method=REQUEST; component=VEVENT
X-Cassandane-Unique: $uuid-0

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.10.4//EN
METHOD:REQUEST
BEGIN:VEVENT
CREATED:20210923T034327Z
UID:$uuid
DTEND;TZID=America/New_York:20210923T183000
TRANSP:OPAQUE
COLOR:red
SUMMARY:An Event
DTSTART;TZID=America/New_York:20210923T153000
DTSTAMP:20210923T034327Z
SEQUENCE:0
RRULE:FREQ=WEEKLY
ORGANIZER;CN=Test User:MAILTO:foo\@example.net
ATTENDEE;CN=Test User;PARTSTAT=ACCEPTED;RSVP=TRUE:MAILTO:foo\@example.net
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:cassandane\@example.com
CATEGORIES:#0000FF
CATEGORIES:blue
CATEGORIES:foo
BEGIN:VALARM
UID:myalarm
TRIGGER;RELATED=START:PT0S
ACTION:DISPLAY
DESCRIPTION:CYR-140
END:VALARM
END:VEVENT
END:VCALENDAR
EOF

    xlog $self, "Deliver iMIP invite";
    my $msg = Cassandane::Message->new(raw => $imip);
    $self->{instance}->deliver($msg);

    xlog $self, "Get the event and verify that personal data has been stripped";
    my $events = $CalDAV->GetEvents($CalendarId);
    my $response = $CalDAV->Request('GET', $events->[0]{href});
    my $ical = $response->{content};
    $self->assert_does_not_match(qr/\r\nBEGIN:VALARM/, $ical);
    $self->assert_does_not_match(qr/\r\nCOLOR:/, $ical);
    $self->assert_does_not_match(qr/\r\nTRANSP:/, $ical);
    $self->assert_does_not_match(qr/\r\nCATEGORIES:#0000FF/, $ical);
    $self->assert_does_not_match(qr/\r\nCATEGORIES:blue/, $ical);
    $self->assert_matches(qr/\r\nCATEGORIES:foo/, $ical);
}
