summaryrefslogtreecommitdiff
path: root/tests/functional/test_wallace/test_005_resource_invitation.py
blob: e7ac0f35e8d6104347adb1f6e2a01bb36af27d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
import time
import pykolab
import smtplib
import email
import datetime
import uuid
import re

from pykolab.imap import IMAP
from wallace import module_resources

from pykolab.translate import _
from pykolab.xml import utils as xmlutils
from pykolab.xml import event_from_message
from pykolab.xml import participant_status_label
from pykolab.itip import events_from_message
from email import message_from_string
from twisted.trial import unittest

import tests.functional.resource_func as funcs

conf = pykolab.getConf()

itip_invitation = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube Webmail 0.9-0.3.el6.kolab_3.0//NONSGML Calendar//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:%s%s
DTSTAMP:20140213T125414Z
DTSTART;TZID=Europe/London:%s
DTEND;TZID=Europe/London:%s
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:%s
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=TENTATIVE;CN=Somebody Else:mailto:somebody@else.com
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""

itip_update = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube Webmail 0.9-0.3.el6.kolab_3.0//NONSGML Calendar//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:%s%s
DTSTAMP:20140215T125414Z
DTSTART;TZID=Europe/London:%s
DTEND;TZID=Europe/London:%s
SEQUENCE:2
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:%s
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""

itip_delegated = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube//Roundcube libcalendaring 1.0-git//Sabre//Sabre VObject
  2.1.3//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:%s%s
DTSTAMP;VALUE=DATE-TIME:20140227T141939Z
DTSTART;VALUE=DATE-TIME;TZID=Europe/London:%s
DTEND;VALUE=DATE-TIME;TZID=Europe/London:%s
SUMMARY:test
SEQUENCE:4
ATTENDEE;CN=Company Cars;PARTSTAT=DELEGATED;ROLE=NON-PARTICIPANT;CUTYPE=IND
 IVIDUAL;RSVP=TRUE;DELEGATED-TO=resource-car-audia4@example.org:mailto:reso
 urce-collection-companycars@example.org
ATTENDEE;CN=The Delegate;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT;CUTYPE=INDI
 VIDUAL;RSVP=TRUE;DELEGATED-FROM=resource-collection-companycars@example.or
 g:mailto:resource-car-audia4@example.org
ORGANIZER;CN=:mailto:john.doe@example.org
DESCRIPTION:Sent to %s
END:VEVENT
END:VCALENDAR
"""

itip_cancellation = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube Webmail 0.9-0.3.el6.kolab_3.0//NONSGML Calendar//EN
CALSCALE:GREGORIAN
METHOD:CANCEL
BEGIN:VEVENT
UID:%s%s
DTSTAMP:20140218T125414Z
DTSTART;TZID=Europe/London:20120713T100000
DTEND;TZID=Europe/London:20120713T110000
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE:mailt=
 o:%s
TRANSP:OPAQUE
SEQUENCE:3
END:VEVENT
END:VCALENDAR
"""

itip_allday = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube Webmail 0.9-0.3.el6.kolab_3.0//NONSGML Calendar//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:%s%s
DTSTAMP:20140213T125414Z
DTSTART;VALUE=DATE:%s
DTEND;VALUE=DATE:%s
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:%s
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""


itip_recurring = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.9.2//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:%s%s
DTSTAMP:20140213T125414Z
DTSTART;TZID=Europe/London:%s
DTEND;TZID=Europe/London:%s
RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=10
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:%s
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""

mime_message = """MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_c8894dbdb8baeedacae836230e3436fd"
From: "Doe, John" <john.doe@example.org>
Date: Tue, 25 Feb 2014 13:54:14 +0100
Message-ID: <240fe7ae7e139129e9eb95213c1016d7@example.org>
User-Agent: Roundcube Webmail/0.9-0.3.el6.kolab_3.0
To: %s
Subject: "test"

--=_c8894dbdb8baeedacae836230e3436fd
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

*test*

--=_c8894dbdb8baeedacae836230e3436fd
Content-Type: text/calendar; charset=UTF-8; method=REQUEST; name=event.ics
Content-Disposition: attachment; filename=event.ics
Content-Transfer-Encoding: 8bit

%s
--=_c8894dbdb8baeedacae836230e3436fd--
"""

class TestResourceInvitation(unittest.TestCase):

    john = None

    @classmethod
    def setUp(self):
        """ Compatibility for twisted.trial.unittest
        """
        if not self.john:
            self.setup_class()

    @classmethod
    def setup_class(self, *args, **kw):
        # set language to default
        pykolab.translate.setUserLanguage(conf.get('kolab','default_locale'))

        self.itip_reply_subject = _("Reservation Request for %(summary)s was %(status)s")

        from tests.functional.purge_users import purge_users
        purge_users()

        self.john = {
            'displayname': 'John Doe',
            'mail': 'john.doe@example.org',
            'sender': 'John Doe <john.doe@example.org>',
            'mailbox': 'user/john.doe@example.org',
            'dn': 'uid=doe,ou=People,dc=example,dc=org'
        }

        self.jane = {
            'displayname': 'Jane Manager',
            'mail': 'jane.manager@example.org',
            'sender': 'Jane Manager <jane.manager@example.org>',
            'mailbox': 'user/jane.manager@example.org',
            'dn': 'uid=manager,ou=People,dc=example,dc=org'
        }

        from tests.functional.user_add import user_add
        user_add("John", "Doe", kolabinvitationpolicy='ALL_MANUAL')
        user_add("Jane", "Manager", kolabinvitationpolicy='ALL_MANUAL')

        funcs.purge_resources()
        self.audi = funcs.resource_add("car", "Audi A4")
        self.passat = funcs.resource_add("car", "VW Passat")
        self.boxter = funcs.resource_add("car", "Porsche Boxter S")
        self.cars = funcs.resource_add("collection", "Company Cars", [ self.audi['dn'], self.passat['dn'], self.boxter['dn'] ])

        self.room1 = funcs.resource_add("confroom", "Room 101", owner=self.jane['dn'], kolabinvitationpolicy='ACT_ACCEPT_AND_NOTIFY')
        self.room2 = funcs.resource_add("confroom", "Conference Room B-222")
        self.rooms = funcs.resource_add("collection", "Rooms", [ self.room1['dn'], self.room2['dn'] ], self.jane['dn'], kolabinvitationpolicy='ACT_ACCEPT_AND_NOTIFY')

        self.room3 = funcs.resource_add("confroom", "CEOs Office 303")
        self.viprooms = funcs.resource_add("collection", "VIP Rooms", [ self.room3['dn'] ], self.jane['dn'], kolabinvitationpolicy='ACT_MANUAL')

        time.sleep(1)
        from tests.functional.synchronize import synchronize_once
        synchronize_once()

    def send_message(self, itip_payload, to_addr, from_addr=None):
        if from_addr is None:
            from_addr = self.john['mail']

        smtp = smtplib.SMTP('localhost', 10026)
        smtp.sendmail(from_addr, to_addr, mime_message % (to_addr, itip_payload))
        smtp.quit()

    def send_itip_invitation(self, resource_email, start=None, allday=False, template=None, uid=None, instance=None):
        if start is None:
            start = datetime.datetime.now()

        if uid is None:
            uid = str(uuid.uuid4())

        if allday:
            default_template = itip_allday
            end = start + datetime.timedelta(days=1)
            date_format = '%Y%m%d'
        else:
            end = start + datetime.timedelta(hours=4)
            default_template = itip_invitation
            date_format = '%Y%m%dT%H%M%S'

        recurrence_id = ''
        if instance is not None:
            recurrence_id = "\nRECURRENCE-ID;TZID=Europe/London:" + instance.strftime(date_format)

        self.send_message((template if template is not None else default_template) % (
                uid,
                recurrence_id,
                start.strftime(date_format),
                end.strftime(date_format),
                resource_email
            ),
            resource_email)

        return uid

    def send_itip_update(self, resource_email, uid, start=None, template=None, sequence=None, instance=None):
        if start is None:
            start = datetime.datetime.now()

        end = start + datetime.timedelta(hours=4)

        recurrence_id = ''
        if instance is not None:
            recurrence_id = "\nRECURRENCE-ID;TZID=Europe/London:" + instance.strftime('%Y%m%dT%H%M%S')

        if sequence is not None:
            if not template:
                template = itip_update
            template = re.sub(r'SEQUENCE:\d+', 'SEQUENCE:' + str(sequence), template)

        self.send_message((template if template is not None else itip_update) % (
                uid,
                recurrence_id,
                start.strftime('%Y%m%dT%H%M%S'),
                end.strftime('%Y%m%dT%H%M%S'),
                resource_email
            ),
            resource_email)

        return uid

    def send_itip_cancel(self, resource_email, uid, instance=None):
        recurrence_id = ''
        if instance is not None:
            recurrence_id = "\nRECURRENCE-ID;TZID=Europe/London:" + instance.strftime('%Y%m%dT%H%M%S')

        self.send_message(itip_cancellation % (
                uid,
                recurrence_id,
                resource_email
            ),
            resource_email)

        return uid


    def send_owner_response(self, event, partstat, from_addr=None):
        if from_addr is None:
            from_addr = self.jane['mail']

        itip_reply = event.to_message_itip(from_addr,
            method="REPLY",
            participant_status=partstat,
            message_text="Request " + partstat,
            subject="Booking has been %s" % (partstat)
        )

        smtp = smtplib.SMTP('localhost', 10026)
        smtp.sendmail(from_addr, str(event.get_organizer().email()), str(itip_reply))
        smtp.quit()

    def check_message_received(self, subject, from_addr=None, mailbox=None):
        if mailbox is None:
            mailbox = self.john['mailbox']

        imap = IMAP()
        imap.connect()
        imap.set_acl(mailbox, "cyrus-admin", "lrs")
        imap.imap.m.select(mailbox)

        found = None
        retries = 15

        while not found and retries > 0:
            retries -= 1

            typ, data = imap.imap.m.search(None, '(UNDELETED HEADER FROM "%s")' % (from_addr) if from_addr else 'UNDELETED')
            for num in data[0].split():
                typ, msg = imap.imap.m.fetch(num, '(RFC822)')
                message = message_from_string(msg[0][1])
                if message['Subject'] == subject:
                    found = message
                    break

            time.sleep(1)

        imap.disconnect()

        return found

    def check_resource_calendar_event(self, mailbox, uid=None, instance=None):
        imap = IMAP()
        imap.connect()

        imap.set_acl(mailbox, "cyrus-admin", "lrs")
        imap.imap.m.select(imap.folder_quote(mailbox))

        found = None
        retries = 10

        while not found and retries > 0:
            retries -= 1

            typ, data = imap.imap.m.search(None, '(UNDELETED HEADER SUBJECT "%s")' % (uid) if uid else '(UNDELETED HEADER X-Kolab-Type "application/x-vnd.kolab.event")')
            for num in data[0].split():
                typ, data = imap.imap.m.fetch(num, '(RFC822)')
                event_message = message_from_string(data[0][1])

                # return matching UID or first event found
                if uid and event_message['subject'] != uid:
                    continue

                found = event_from_message(event_message)
                if found and (instance is None or found.is_recurring() or xmlutils.dates_equal(instance, found.get_recurrence_id())):
                    break

            time.sleep(1)

        imap.disconnect()

        return found

    def purge_mailbox(self, mailbox):
        imap = IMAP()
        imap.connect()
        imap.set_acl(mailbox, "cyrus-admin", "lrwcdest")
        imap.imap.m.select(u'"'+mailbox+'"')

        typ, data = imap.imap.m.search(None, 'ALL')
        for num in data[0].split():
            imap.imap.m.store(num, '+FLAGS', '\\Deleted')

        imap.imap.m.expunge()
        imap.disconnect()


    def find_resource_by_email(self, email):
        resource = None
        for r in [self.audi, self.passat, self.boxter, self.room1, self.room2]:
            if (email.find(r['mail']) >= 0):
                resource = r
                break
        return resource


    def test_001_resource_from_email_address(self):
        resource = module_resources.resource_record_from_email_address(self.audi['mail'])
        self.assertEqual(len(resource), 1)
        self.assertEqual(resource[0], self.audi['dn'])

        collection = module_resources.resource_record_from_email_address(self.cars['mail'])
        self.assertEqual(len(collection), 1)
        self.assertEqual(collection[0], self.cars['dn'])


    def test_002_invite_resource(self):
        uid = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,7,13, 10,0,0))

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)

        event = self.check_resource_calendar_event(self.audi['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_summary(), "test")


    # @depends test_002_invite_resource
    def test_003_invite_resource_conflict(self):
        uid = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,7,13, 12,0,0))

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)

        self.assertEqual(self.check_resource_calendar_event(self.audi['kolabtargetfolder'], uid), None)


    def test_004_invite_resource_collection(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.cars['mail'], datetime.datetime(2014,7,13, 12,0,0))

        # one of the collection members accepted the reservation
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        delegatee = self.find_resource_by_email(accept['from'])
        self.assertIn(delegatee['mail'], accept['from'])

        # check booking in the delegatee's resource calendar
        self.assertIsInstance(self.check_resource_calendar_event(delegatee['kolabtargetfolder'], uid), pykolab.xml.Event)

        # resource collection responds with a DELEGATED message
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DELEGATED') }, self.cars['mail'])
        self.assertIsInstance(response, email.message.Message)
        self.assertIn("ROLE=NON-PARTICIPANT;RSVP=FALSE", str(response))


    def test_005_rescheduling_reservation(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,4,1, 10,0,0))

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)

        self.purge_mailbox(self.john['mailbox'])
        self.send_itip_update(self.audi['mail'], uid, datetime.datetime(2014,4,1, 12,0,0)) # conflict with myself

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)

        event = self.check_resource_calendar_event(self.audi['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_start().hour, 12)
        self.assertEqual(event.get_sequence(), 2)


    def test_005_rescheduling_collection(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.cars['mail'], datetime.datetime(2014,4,24, 12,0,0))

        # one of the collection members accepted the reservation
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        delegatee = self.find_resource_by_email(accept['from'])

        # book that resource for the next day
        self.send_itip_invitation(delegatee['mail'], datetime.datetime(2014,4,25, 14,0,0))
        accept2 = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })

        # re-schedule first booking to a conflicting date
        self.purge_mailbox(self.john['mailbox'])
        update_template = itip_delegated.replace("resource-car-audia4@example.org", delegatee['mail'])
        self.send_itip_update(delegatee['mail'], uid, datetime.datetime(2014,4,25, 12,0,0), template=update_template)

        # expect response from another member of the initially delegated collection
        new_accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(new_accept, email.message.Message)

        new_delegatee = self.find_resource_by_email(new_accept['from'])
        self.assertNotEqual(delegatee['mail'], new_delegatee['mail'])

        # event now booked into new delegate's calendar
        event = self.check_resource_calendar_event(new_delegatee['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)

        # old resource responds with a DELEGATED message
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DELEGATED') }, delegatee['mail'])
        self.assertIsInstance(response, email.message.Message)

        # old reservation was removed from old delegate's calendar
        self.assertEqual(self.check_resource_calendar_event(delegatee['kolabtargetfolder'], uid), None)


    def test_006_cancelling_revervation(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.boxter['mail'], datetime.datetime(2014,5,1, 10,0,0))
        self.assertIsInstance(self.check_resource_calendar_event(self.boxter['kolabtargetfolder'], uid), pykolab.xml.Event)

        self.send_itip_cancel(self.boxter['mail'], uid)

        time.sleep(2)  # wait for IMAP to update
        self.assertEqual(self.check_resource_calendar_event(self.boxter['kolabtargetfolder'], uid), None)

        # make new reservation to the now free'd slot
        self.send_itip_invitation(self.boxter['mail'], datetime.datetime(2014,5,1, 9,0,0))

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.boxter['mail'])
        self.assertIsInstance(response, email.message.Message)


    def test_007_update_delegated(self):
        self.purge_mailbox(self.john['mailbox'])

        dt = datetime.datetime(2014,8,1, 12,0,0)
        uid = self.send_itip_invitation(self.cars['mail'], dt)

        # wait for accept notification
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        delegatee = self.find_resource_by_email(accept['from'])

        # send update message to all attendees (collection and delegatee)
        self.purge_mailbox(self.john['mailbox'])
        update_template = itip_delegated.replace("resource-car-audia4@example.org", delegatee['mail'])
        self.send_itip_update(self.cars['mail'], uid, dt, template=update_template)
        self.send_itip_update(delegatee['mail'], uid, dt, template=update_template)

        # get response from delegatee
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn(delegatee['mail'], accept['from'])

        # no delegation response on updates
        self.assertEqual(self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DELEGATED') }, self.cars['mail']), None)


    def test_008_allday_reservation(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,6,2), True)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        event = self.check_resource_calendar_event(self.audi['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertIsInstance(event.get_start(), datetime.date)

        uid2 = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,6,2, 16,0,0))
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)


    def test_009_recurring_events(self):
        self.purge_mailbox(self.john['mailbox'])

        # register an infinitely recurring resource invitation
        uid = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,2,20, 12,0,0),
            template=itip_recurring.replace(";COUNT=10", ""))

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        # check non-recurring against recurring
        uid2 = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,3,13, 10,0,0))
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') }, self.audi['mail'])
        self.assertIsInstance(response, email.message.Message)

        self.purge_mailbox(self.john['mailbox'])

        # check recurring against recurring
        uid3 = self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,2,22, 8,0,0), template=itip_recurring)
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)


    def test_010_invalid_bookings(self):
        self.purge_mailbox(self.john['mailbox'])

        itip_other = itip_invitation.replace("mailto:%s", "mailto:some-other-resource@example.org\nCOMMENT: Sent to %s")
        self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,3,22, 8,0,0), template=itip_other)

        time.sleep(1)

        itip_invalid = itip_invitation.replace("DTSTART;", "X-DTSTART;")
        self.send_itip_invitation(self.audi['mail'], datetime.datetime(2014,3,24, 19,30,0), template=itip_invalid)

        self.assertEqual(self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.audi['mail']), None)


    def test_011_owner_info(self):
        self.purge_mailbox(self.john['mailbox'])

        self.send_itip_invitation(self.room1['mail'], datetime.datetime(2014,6,19, 16,0,0))

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.room1['mail'])
        self.assertIsInstance(accept, email.message.Message)
        respose_text = str(accept.get_payload(0))
        self.assertIn(self.jane['mail'], respose_text)
        self.assertIn(self.jane['displayname'], respose_text)


    def test_011_owner_info_from_collection(self):
        self.purge_mailbox(self.john['mailbox'])

        self.send_itip_invitation(self.room2['mail'], datetime.datetime(2014,6,19, 16,0,0))

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.room2['mail'])
        self.assertIsInstance(accept, email.message.Message)
        respose_text = str(accept.get_payload(0))
        self.assertIn(self.jane['mail'], respose_text)
        self.assertIn(self.jane['displayname'], respose_text)


    def test_012_owner_notification(self):
        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.jane['mailbox'])

        self.send_itip_invitation(self.room1['mail'], datetime.datetime(2014,8,4, 13,0,0))

        # check notification message sent to resource owner (jane)
        notify = self.check_message_received(_('Booking for %s has been %s') % (self.room1['cn'], participant_status_label('ACCEPTED')), self.room1['mail'], self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)

        notification_text = str(notify.get_payload())
        self.assertIn(self.john['mail'], notification_text)
        self.assertIn(participant_status_label('ACCEPTED'), notification_text)

        self.purge_mailbox(self.john['mailbox'])

        # check notification sent to collection owner (jane)
        self.send_itip_invitation(self.rooms['mail'], datetime.datetime(2014,8,4, 12,30,0))

        # one of the collection members accepted the reservation
        accepted = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        delegatee = self.find_resource_by_email(accepted['from'])

        notify = self.check_message_received(_('Booking for %s has been %s') % (delegatee['cn'], participant_status_label('ACCEPTED')), delegatee['mail'], self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)
        self.assertIn(self.john['mail'], notification_text)


    def test_013_owner_confirmation_accept(self):
        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.jane['mailbox'])

        uid = self.send_itip_invitation(self.room3['mail'], datetime.datetime(2014,9,12, 14,0,0))

        # requester (john) gets a TENTATIVE confirmation
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('TENTATIVE') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_summary(), "test")
        self.assertEqual(event.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'TENTATIVE')

        # check confirmation message sent to resource owner (jane)
        notify = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)

        # resource owner confirms reservation request
        itip_event = events_from_message(notify)[0]
        self.send_owner_response(itip_event['xml'], 'ACCEPTED', from_addr=self.jane['mail'])

        # requester (john) now gets the ACCEPTED response
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_status(True), 'CONFIRMED')
        self.assertEqual(event.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'ACCEPTED')


    def test_014_owner_confirmation_decline(self):
        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.jane['mailbox'])

        uid = self.send_itip_invitation(self.room3['mail'], datetime.datetime(2014,9,14, 9,0,0))

        # requester (john) gets a TENTATIVE confirmation
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('TENTATIVE') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        # check confirmation message sent to resource owner (jane)
        notify = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)

        itip_event = events_from_message(notify)[0]

        # resource owner declines reservation request
        itip_reply = itip_event['xml'].to_message_itip(self.jane['mail'],
            method="REPLY",
            participant_status='DECLINED',
            message_text="Request declined",
            subject=_('Booking for %s has been %s') % (self.room3['cn'], participant_status_label('DECLINED'))
        )

        smtp = smtplib.SMTP('localhost', 10026)
        smtp.sendmail(self.jane['mail'], str(itip_event['organizer']), str(itip_reply))
        smtp.quit()

        # requester (john) now gets the DECLINED response
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        # tentative reservation was set to cancelled
        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertEqual(event, None)
        #self.assertEqual(event.get_status(True), 'CANCELLED')
        #self.assertEqual(event.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'DECLINED')


    def test_015_owner_confirmation_update(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.room3['mail'], datetime.datetime(2014,8,19, 9,0,0), uid="http://a-totally.stupid/?uid")

        # requester (john) gets a TENTATIVE confirmation
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('TENTATIVE') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        # check first confirmation message sent to resource owner (jane)
        notify1 = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify1, email.message.Message)

        itip_event1 = events_from_message(notify1)[0]
        self.assertEqual(itip_event1['start'].hour, 9)

        self.purge_mailbox(self.jane['mailbox'])
        self.purge_mailbox(self.john['mailbox'])

        # send update with new date (and sequence)
        self.send_itip_update(self.room3['mail'], uid, datetime.datetime(2014,8,19, 16,0,0))

        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'TENTATIVE')

        # check second confirmation message sent to resource owner (jane)
        notify2 = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify2, email.message.Message)

        itip_event2 = events_from_message(notify2)[0]
        self.assertEqual(itip_event2['start'].hour, 16)

        # resource owner declines the first reservation request
        itip_reply = itip_event1['xml'].to_message_itip(self.jane['mail'],
            method="REPLY",
            participant_status='DECLINED',
            message_text="Request declined",
            subject=_('Booking for %s has been %s') % (self.room3['cn'], participant_status_label('DECLINED'))
        )
        smtp = smtplib.SMTP('localhost', 10026)
        smtp.sendmail(self.jane['mail'], str(itip_event1['organizer']), str(itip_reply))
        smtp.quit()

        time.sleep(5)

        # resource owner accpets the second reservation request
        itip_reply = itip_event2['xml'].to_message_itip(self.jane['mail'],
            method="REPLY",
            participant_status='ACCEPTED',
            message_text="Request accepred",
            subject=_('Booking for %s has been %s') % (self.room3['cn'], participant_status_label('ACCEPTED'))
        )
        smtp = smtplib.SMTP('localhost', 10026)
        smtp.sendmail(self.jane['mail'], str(itip_event2['organizer']), str(itip_reply))
        smtp.quit()

        # requester (john) now gets the ACCEPTED response
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)

        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(event.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'ACCEPTED')


    def test_016_collection_owner_confirmation(self):
        self.purge_mailbox(self.john['mailbox'])

        uid = self.send_itip_invitation(self.viprooms['mail'], datetime.datetime(2014,8,15, 17,0,0))

        # resource collection responds with a DELEGATED message
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DELEGATED') }, self.viprooms['mail'])
        self.assertIsInstance(response, email.message.Message)

        # the collection member tentatively accepted the reservation
        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('TENTATIVE') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn(self.room3['mail'], accept['from'])

        # check confirmation message sent to resource owner (jane)
        notify = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)


    def test_017_reschedule_single_occurrence(self):
        self.purge_mailbox(self.john['mailbox'])

        # register a recurring resource invitation
        start = datetime.datetime(2015,2,10, 12,0,0)
        uid = self.send_itip_invitation(self.audi['mail'], start, template=itip_recurring)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        self.purge_mailbox(self.john['mailbox'])

        # send rescheduling request to a single instance
        exdate = start + datetime.timedelta(days=14)
        exstart = exdate + datetime.timedelta(hours=5)
        self.send_itip_update(self.audi['mail'], uid, exstart, instance=exdate)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + exdate.strftime('%Y%m%dT%H%M%S'), str(accept))

        event = self.check_resource_calendar_event(self.audi['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(len(event.get_exceptions()), 1)

        # send new invitation for now free slot
        uid = self.send_itip_invitation(self.audi['mail'], exdate, template=itip_invitation.replace('SUMMARY:test', 'SUMMARY:new'))

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'new', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        # send rescheduling request to that single instance again: now conflicting
        exdate = start + datetime.timedelta(days=14)
        exstart = exdate + datetime.timedelta(hours=2)
        self.send_itip_update(self.audi['mail'], uid, exstart, instance=exdate, sequence=3)

        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') })
        self.assertIsInstance(response, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:", str(response))


    def test_018_invite_single_occurrence(self):
        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.boxter['kolabtargetfolder'])

        start = datetime.datetime(2015,3,2, 18,30,0)
        uid = self.send_itip_invitation(self.boxter['mail'], start, instance=start)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + start.strftime('%Y%m%dT%H%M%S'), str(accept))

        self.purge_mailbox(self.john['mailbox'])

        # send a second invitation for another instance with the same UID
        nextstart = datetime.datetime(2015,3,9, 18,30,0)
        self.send_itip_invitation(self.boxter['mail'], nextstart, uid=uid, instance=nextstart)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + nextstart.strftime('%Y%m%dT%H%M%S'), str(accept))

        self.purge_mailbox(self.john['mailbox'])

        # send rescheduling request to the first instance
        exstart = start + datetime.timedelta(hours=2)
        self.send_itip_update(self.boxter['mail'], uid, exstart, instance=start)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + start.strftime('%Y%m%dT%H%M%S'), str(accept))

        # the resource calendar now has two reservations stored
        one = self.check_resource_calendar_event(self.boxter['kolabtargetfolder'], uid, start)
        self.assertIsInstance(one, pykolab.xml.Event)
        self.assertIsInstance(one.get_recurrence_id(), datetime.datetime)
        self.assertEqual(one.get_start().hour, exstart.hour)

        two = self.check_resource_calendar_event(self.boxter['kolabtargetfolder'], uid, nextstart)
        self.assertIsInstance(two, pykolab.xml.Event)
        self.assertIsInstance(two.get_recurrence_id(), datetime.datetime)


    def test_019_cancel_single_occurrence(self):
        self.purge_mailbox(self.john['mailbox'])

        # register a recurring resource invitation
        start = datetime.datetime(2015,2,12, 14,0,0)
        uid = self.send_itip_invitation(self.passat['mail'], start, template=itip_recurring)

        accept = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('ACCEPTED') })
        self.assertIsInstance(accept, email.message.Message)

        exdate = start + datetime.timedelta(days=7)
        self.send_itip_cancel(self.passat['mail'], uid, instance=exdate)

        time.sleep(5)  # wait for IMAP to update
        event = self.check_resource_calendar_event(self.passat['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(len(event.get_exceptions()), 1)

        exception = event.get_instance(exdate)
        self.assertEqual(exception.get_status(True), 'CANCELLED')
        self.assertTrue(exception.get_transparency())


    def test_020_owner_confirmation_single_occurrence(self):
        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.jane['mailbox'])

        start = datetime.datetime(2015,4,18, 14,0,0)
        uid = self.send_itip_invitation(self.room3['mail'], start, template=itip_recurring)

        notify = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)

        # resource owner confirms reservation request (entire series)
        itip_event = events_from_message(notify)[0]
        self.send_owner_response(itip_event['xml'], 'ACCEPTED', from_addr=self.jane['mail'])

        self.purge_mailbox(self.john['mailbox'])
        self.purge_mailbox(self.jane['mailbox'])

        # send rescheduling request to a single instance
        exdate = start + datetime.timedelta(days=14)
        exstart = exdate + datetime.timedelta(hours=4)
        self.send_itip_update(self.room3['mail'], uid, exstart, instance=exdate)

        # check confirmation message sent to resource owner (jane)
        notify = self.check_message_received(_('Booking request for %s requires confirmation') % (self.room3['cn']), mailbox=self.jane['mailbox'])
        self.assertIsInstance(notify, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + exdate.strftime('%Y%m%dT%H%M%S'), str(notify))

        itip_event = events_from_message(notify)[0]
        self.assertIsInstance(itip_event['xml'].get_recurrence_id(), datetime.datetime)

        # resource owner declines reservation request
        self.send_owner_response(itip_event['xml'], 'DECLINED', from_addr=self.jane['mail'])

        # requester (john) now gets the DECLINED response
        response = self.check_message_received(self.itip_reply_subject % { 'summary':'test', 'status':participant_status_label('DECLINED') }, self.room3['mail'])
        self.assertIsInstance(response, email.message.Message)
        self.assertIn("RECURRENCE-ID;TZID=Europe/London:" + exdate.strftime('%Y%m%dT%H%M%S'), str(response))

        event = self.check_resource_calendar_event(self.room3['kolabtargetfolder'], uid)
        self.assertIsInstance(event, pykolab.xml.Event)
        self.assertEqual(len(event.get_exceptions()), 1)

        exception = event.get_instance(exdate)
        self.assertEqual(exception.get_attendee_by_email(self.room3['mail']).get_participant_status(True), 'DECLINED')