#!perl
use Cassandane::Tiny;

sub test_imip_invite_funky_uid
    :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}" "added" {
        setflag "\\\\Flagged";
    }
}
EOF
    );

    my $itip_b64 = <<EOF;
QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KUFJPRElEOi0vL0N5cnVzSU1BUC5vcmcvQ3ly
dXMgDQogMy43LjAtYWxwaGEwLTExODUtZzg0MTE1NzMwMGEtZm0tMjAyMjEyMDguMDAyLWc4NDEx
NTczMC8vRU4NCk1FVEhPRDpSRVFVRVNUDQpDQUxTQ0FMRTpHUkVHT1JJQU4NCkJFR0lOOlZFVkVO
VA0KVUlEOiA2ZGUyODBjOS1lZGZmLTQwMTktDQ0NCiA4ZWJkLWNmZWJjNzNmODIwMQ0KU0VRVUVO
Q0U6MA0KRFRTVEFNUDoyMDIyMTIxM1QxOTI4MzdaDQpDUkVBVEVEOjIwMjIxMjEzVDE5MjgzMloN
CkRUU1RBUlQ6MjAyMjEyMTNUMjAyODAwDQpEVVJBVElPTjpQVDFIDQpQUklPUklUWTowDQpTVU1N
QVJZOjU0ODA0ODJBLTdCMUMtMTFFRC05QUNCLTJFMzE5NTYyMjlCMQ0KT1JHQU5JWkVSO1gtSk1B
UC1JRD01NDgwNjUxMi03QjFDLTExRUQtOUFDQi0yRTMxOTU2MjI5QjE7Q049VGhlIE9yZ2FuaXpl
cjsNCiBFTUFJTD10Z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23 Sep 2021 09:06:18 -0400
From: Foo <foo\@example.net>
To: Cassandane <cassandane\@example.com>
Message-ID: <$uuid\@example.net>
Content-Type: text/calendar; method=REQUEST; component=VEVENT
Content-Transfer-Encoding: base64
X-Cassandane-Unique: $uuid

$itip_b64
EOF

    xlog $self, "Deliver iMIP invite";
    my $msg = Cassandane::Message->new(raw => $imip);
    $msg->set_attribute(uid => 1,
                        flags => [ '\\Recent', '\\Flagged' ]);
    $self->{instance}->deliver($msg);

    xlog $self, "Check that the message made it to INBOX";
    $self->check_messages({ 1 => $msg }, check_guid => 0);

    xlog $self, "Check that the event made it to calendar";
    my $events = $CalDAV->GetEvents($CalendarId);
    $self->assert_equals(1, scalar @$events);
    $self->assert_str_equals($uuid, $events->[0]{uid});
}
