summaryrefslogtreecommitdiffstats
path: root/tests/unit/test-011-itip.py
blob: a08d05f0c65e21cb78945d7e6b7f091914b7c996 (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
import pykolab
import datetime
import pytz
import kolabformat

from pykolab import itip
from pykolab.xml import Event
from pykolab.xml import participant_status_label
from pykolab.translate import _

from icalendar import Calendar
from email import message
from email import message_from_string
from wallace import module_resources
from twisted.trial import unittest

# define some iTip MIME messages

itip_multipart = """MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_c8894dbdb8baeedacae836230e3436fd"
From: "Doe, John" <john.doe@example.org>
Date: Fri, 13 Jul 2012 13:54:14 +0100
Message-ID: <240fe7ae7e139129e9eb95213c1016d7@example.org>
User-Agent: Roundcube Webmail/0.9-0.3.el6.kolab_3.0
To: resource-collection-car@example.org
Subject: "test" has been updated

--=_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: quoted-printable

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:626421779C777FBE9C9B85A80D04DDFA-A4BF5BBB9FEAA271
DTSTAMP:20120713T1254140
DTSTART;TZID=3DEurope/London:20120713T100000
DTEND;TZID=3DEurope/London:20120713T110000
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN=3D"Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=3DREQ-PARTICIPANT;PARTSTAT=3DNEEDS-ACTION;RSVP=3DTRUE:mailt=
o:resource-collection-car@example.org
ATTENDEE;ROLE=3DOPT-PARTICIPANT;PARTSTAT=3DNEEDS-ACTION;RSVP=3DTRUE:mailto:anoth=
er-resource@example.org
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

--=_c8894dbdb8baeedacae836230e3436fd--
"""

itip_non_multipart = """Return-Path: <john.doe@example.org>
Sender: john.doe@example.org
Content-Type: text/calendar; method=REQUEST; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
To: resource-collection-car@example.org
From: john.doe@example.org
Date: Mon, 24 Feb 2014 11:27:28 +0100
Message-ID: <1a3aa8995e83dd24cf9247e538ac913a@example.org>
Subject: test

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:626421779C777FBE9C9B85A80D04DDFA-A4BF5BBB9FEAA271
DTSTAMP:20120713T1254140
DTSTART;TZID=3DEurope/London:20120713T100000
DTEND;TZID=3DEurope/London:20120713T110000
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN=3D"Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=3DREQ-PARTICIPANT;PARTSTAT=3DACCEPTED;RSVP=3DTRUE:mailt=
o:resource-collection-car@example.org
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""

itip_google_multipart = """MIME-Version: 1.0
Message-ID: <001a11c2ad84243e0604f3246bae@google.com>
Date: Mon, 24 Feb 2014 10:27:28 +0000
Subject: =?ISO-8859-1?Q?Invitation=3A_iTip_from_Apple_=40_Mon_Feb_24=2C_2014_12pm_?=
	=?ISO-8859-1?Q?=2D_1pm_=28Tom_=26_T=E4m=29?=
From: "john.doe" <john.doe@gmail.com>
To: <john.sample@example.org>
Content-Type: multipart/mixed; boundary=001a11c2ad84243df004f3246bad

--001a11c2ad84243df004f3246bad
Content-Type: multipart/alternative; boundary=001a11c2ad84243dec04f3246bab

--001a11c2ad84243dec04f3246bab
Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes

<some text content here>

--001a11c2ad84243dec04f3246bab
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div style=3D""><!-- some HTML message content here --></div>
--001a11c2ad84243dec04f3246bab
Content-Type: text/calendar; charset=UTF-8; method=REQUEST
Content-Transfer-Encoding: 7bit

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20140224T110000Z
DTEND:20140224T120000Z
DTSTAMP:20140224T102728Z
ORGANIZER:mailto:kepjllr6mcq7d0959u4cdc7000@group.calendar.google.com
UID:0BE2F640-5814-47C9-ABAE-E7E959204E76
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE
 ;X-NUM-GUESTS=0:mailto:kepjllr6mcq7d0959u4cdc7000@group.calendar.google.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
 TRUE;CN=John Sample;X-NUM-GUESTS=0:mailto:john.sample@example.org
CREATED:20140224T102728Z
DESCRIPTION:Testing Multipart structure\\nView your event at http://www.goog
 le.com/calendar/event?action=VIEW&eid=XzYxMTRhY2k2Nm9xMzBiOWw3MG9qOGI5azZ0M
 WppYmExODkwa2FiYTU2dDJqaWQ5cDY4bzM4aDluNm8gdGhvbWFzQGJyb3RoZXJsaS5jaA&tok=N
 TIja2VwamxscjZtY3E3ZDA5NTl1NGNkYzcwMDBAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbTkz
 NTcyYTU2YmUwNWMxNjY0Zjc3OTU0MzhmMDcwY2FhN2NjZjIzYWM&ctz=Europe/Zurich&hl=en
 .
LAST-MODIFIED:20140224T102728Z
LOCATION:
SEQUENCE:5
STATUS:CONFIRMED
SUMMARY:iTip from Apple
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

--001a11c2ad84243dec04f3246bab--
--001a11c2ad84243df004f3246bad
Content-Type: application/ics; name="invite.ics"
Content-Disposition: attachment; filename="invite.ics"
Content-Transfer-Encoding: base64

QkVHSU46VkNBTEVOREFSDQpQUk9ESUQ6LS8vR29vZ2xlIEluYy8vR29vZ2xlIENhbGVuZGFyIDcw
LjkwNTQvL0VODQpWRVJTSU9OOjIuMA0KQ0FMU0NBTEU6R1JFR09SSUFODQpNRVRIT0Q6UkVRVUVT
VA0KQkVHSU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TJrMk5tOXhNekJpT1d3
M01HOXFPR0k1YXpaME0NCiBXcHBZbUV4T0Rrd2EyRmlZVFUyZERKcWFXUTVjRFk0YnpNNGFEbHVO
bThnZEdodmJXRnpRR0p5YjNSb1pYSnNhUzVqYUEmdG9rPU4NCiBUSWphMlZ3YW14c2NqWnRZM0Uz
WkRBNU5UbDFOR05rWXpjd01EQkFaM0p2ZFhBdVkyRnNaVzVrWVhJdVoyOXZaMnhsTG1OdmJUa3oN
CiBOVGN5WVRVMlltVXdOV014TmpZMFpqYzNPVFUwTXpobU1EY3dZMkZoTjJOalpqSXpZV00mY3R6
PUV1cm9wZS9adXJpY2gmaGw9ZW4NCiAuDQpMQVNULU1PRElGSUVEOjIwMTQwMjI0VDEwMjcyOFoN
CkxPQ0FUSU9OOg0KU0VRVUVOQ0U6NQ0KU1RBVFVTOkNPTkZJUk1FRA0KU1VNTUFSWTppVGlwIGZy
b20gQXBwbGUNClRSQU5TUDpPUEFRVUUNCkVORDpWRVZFTlQNCkVORDpWQ0FMRU5EQVINCg==
--001a11c2ad84243df004f3246bad--
"""

itip_application_ics = """MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_c8894dbdb8baeedacae836230e3436fd"
From: "Doe, John" <john.doe@example.org>
Date: Fri, 13 Jul 2012 13:54:14 +0100
Message-ID: <240fe7ae7e139129e9eb95213c101622@example.org>
User-Agent: Roundcube Webmail/0.9-0.3.el6.kolab_3.0
To: resource-collection-car@example.org
Subject: "test" has been updated

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

<some text here>

--=_c8894dbdb8baeedacae836230e3436fd
Content-Type: application/ics; charset=UTF-8; method=REQUEST;
 name=event.ics
Content-Transfer-Encoding: quoted-printable

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:626421779C777FBE9C9B85A80D04DDFA-A4BF5BBB9FEAA271
DTSTAMP:20120713T1254140
DTSTART;TZID=3DEurope/London:20120713T100000
DTEND;TZID=3DEurope/London:20120713T110000
SUMMARY:test
DESCRIPTION:test
ORGANIZER;CN=3D"Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=3DREQ-PARTICIPANT;PARTSTAT=3DNEEDS-ACTION;RSVP=3DTRUE:mailt=
o:resource-collection-car@example.org
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

--=_c8894dbdb8baeedacae836230e3436fd--
"""

itip_recurring = """Return-Path: <john.doe@example.org>
Sender: john.doe@example.org
Content-Type: text/calendar; method=REQUEST; charset=UTF-8
Content-Transfer-Encoding: 8bit
From: john.doe@example.org
Date: Mon, 24 Feb 2014 11:27:28 +0100
Message-ID: <1a3aa8995e83dd24cf9247e538ac913a@example.org>
Subject: Recurring

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.9.2//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:dbdb8baeedacae836230e3436fd-5e83dd24cf92
DTSTAMP:20140213T1254140
DTSTART;TZID=Europe/London:20120709T100000
DTEND;TZID=Europe/London:20120709T120000
RRULE:FREQ=DAILY;INTERVAL=1;COUNT=5
SUMMARY:Recurring
ORGANIZER;CN="Doe, John":mailto:john.doe@example.org
ATTENDEE;ROLE=REQ-PARTICIPANT;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:jane@example.com
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
"""

itip_empty = """MIME-Version: 1.0
Date: Fri, 17 Jan 2014 13:51:50 +0100
From: <john.doe@example.org>
User-Agent: Roundcube Webmail/0.9.5
To: john.sample@example.org
Subject: "test" has been sent
Message-ID: <52D92766.5040508@somedomain.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Message plain text goes here...
"""

conf = pykolab.getConf()

if not hasattr(conf, 'defaults'):
    conf.finalize_conf()

class TestITip(unittest.TestCase):

    def setUp(self):
        # intercept calls to smtplib.SMTP.sendmail()
        import smtplib
        self.patch(smtplib.SMTP, "__init__", self._mock_smtp_init)
        self.patch(smtplib.SMTP, "quit", self._mock_nop)
        self.patch(smtplib.SMTP, "sendmail", self._mock_smtp_sendmail)

        self.smtplog = [];

    def _mock_nop(self, domain=None):
        pass

    def _mock_smtp_init(self, host=None, port=None, local_hostname=None, timeout=0):
        pass

    def _mock_smtp_sendmail(self, from_addr, to_addr, message, mail_options=None, rcpt_options=None):
        self.smtplog.append((from_addr, to_addr, message))


    def test_001_itip_events_from_message(self):
        itips1 = itip.events_from_message(message_from_string(itip_multipart))
        self.assertEqual(len(itips1), 1, "Multipart iTip message with text/calendar")
        self.assertEqual(itips1[0]['method'], "REQUEST", "iTip request method property")

        itips2 = itip.events_from_message(message_from_string(itip_non_multipart))
        self.assertEqual(len(itips2), 1, "Detect non-multipart iTip messages")

        itips3 = itip.events_from_message(message_from_string(itip_application_ics))
        self.assertEqual(len(itips3), 1, "Multipart iTip message with application/ics attachment")

        itips4 = itip.events_from_message(message_from_string(itip_google_multipart))
        self.assertEqual(len(itips4), 1, "Multipart iTip message from Google")

        itips5 = itip.events_from_message(message_from_string(itip_empty))
        self.assertEqual(len(itips5), 0, "Simple plain text message")

        # invalid itip blocks
        self.assertRaises(Exception, itip.events_from_message, message_from_string(itip_multipart.replace("BEGIN:VEVENT", "")))

        itips6 = itip.events_from_message(message_from_string(itip_multipart.replace("DTSTART;", "X-DTSTART;")))
        self.assertEqual(len(itips6), 0, "Event with not DTSTART")

        itips7 = itip.events_from_message(message_from_string(itip_non_multipart.replace("METHOD:REQUEST", "METHOD:PUBLISH").replace("method=REQUEST", "method=PUBLISH")))
        self.assertEqual(len(itips7), 0, "Invalid METHOD")


    def test_002_check_date_conflict(self):
        astart = datetime.datetime(2014,7,13, 10,0,0)
        aend   = astart + datetime.timedelta(hours=2)

        bstart = datetime.datetime(2014,7,13, 10,0,0)
        bend   = astart + datetime.timedelta(hours=1)
        self.assertTrue(itip.check_date_conflict(astart, aend, bstart, bend))

        bstart = datetime.datetime(2014,7,13, 11,0,0)
        bend   = astart + datetime.timedelta(minutes=30)
        self.assertTrue(itip.check_date_conflict(astart, aend, bstart, bend))

        bend   = astart + datetime.timedelta(hours=2)
        self.assertTrue(itip.check_date_conflict(astart, aend, bstart, bend))

        bstart = datetime.datetime(2014,7,13, 12,0,0)
        bend   = astart + datetime.timedelta(hours=1)
        self.assertFalse(itip.check_date_conflict(astart, aend, bstart, bend))

        bstart = datetime.datetime(2014,6,13, 10,0,0)
        bend   = datetime.datetime(2014,6,14, 12,0,0)
        self.assertFalse(itip.check_date_conflict(astart, aend, bstart, bend))

        bstart = datetime.datetime(2014,7,10, 12,0,0)
        bend   = datetime.datetime(2014,7,14, 14,0,0)
        self.assertTrue(itip.check_date_conflict(astart, aend, bstart, bend))


    def test_002_check_event_conflict(self):
        itip_event = itip.events_from_message(message_from_string(itip_non_multipart))[0]

        event = Event()
        event.set_start(datetime.datetime(2012,7,13, 9,30,0, tzinfo=itip_event['start'].tzinfo))
        event.set_end(datetime.datetime(2012,7,13, 10,30,0, tzinfo=itip_event['start'].tzinfo))

        self.assertTrue(itip.check_event_conflict(event, itip_event), "Conflicting dates")

        event.set_uid(itip_event['uid'])
        self.assertFalse(itip.check_event_conflict(event, itip_event), "No conflict for same UID")

        allday = Event()
        allday.set_start(datetime.date(2012,7,13))
        allday.set_end(datetime.date(2012,7,13))

        self.assertTrue(itip.check_event_conflict(allday, itip_event), "Conflicting allday event")

        allday.set_transparency(True)
        self.assertFalse(itip.check_event_conflict(allday, itip_event), "No conflict if event is set to transparent")

        event2 = Event()
        event2.set_start(datetime.datetime(2012,7,13, 10,0,0, tzinfo=pytz.timezone("US/Central")))
        event2.set_end(datetime.datetime(2012,7,13, 11,0,0, tzinfo=pytz.timezone("US/Central")))

        self.assertFalse(itip.check_event_conflict(event, itip_event), "No conflict with timezone shift")

        rrule = kolabformat.RecurrenceRule()
        rrule.setFrequency(kolabformat.RecurrenceRule.Weekly)
        rrule.setCount(10)

        event3 = Event()
        event3.set_recurrence(rrule);
        event3.set_start(datetime.datetime(2012,6,29, 9,30,0, tzinfo=pytz.utc))
        event3.set_end(datetime.datetime(2012,6,29, 10,30,0, tzinfo=pytz.utc))

        self.assertTrue(itip.check_event_conflict(event3, itip_event), "Conflict in (3rd) recurring event instance")

        itip_event = itip.events_from_message(message_from_string(itip_recurring))[0]
        self.assertTrue(itip.check_event_conflict(event3, itip_event), "Conflict in two recurring events")

        event4 = Event()
        event4.set_recurrence(rrule);
        event4.set_start(datetime.datetime(2012,7,1, 9,30,0, tzinfo=pytz.utc))
        event4.set_end(datetime.datetime(2012,7,1, 10,30,0, tzinfo=pytz.utc))
        self.assertFalse(itip.check_event_conflict(event4, itip_event), "No conflict in two recurring events")


    def test_003_send_reply(self):
        itip_events = itip.events_from_message(message_from_string(itip_non_multipart))
        itip.send_reply("resource-collection-car@example.org", itip_events, "SUMMARY=%(summary)s; STATUS=%(status)s; NAME=%(name)s;")

        self.assertEqual(len(self.smtplog), 1)
        self.assertEqual(self.smtplog[0][0], 'resource-collection-car@example.org', "From attendee")
        self.assertEqual(self.smtplog[0][1], 'john.doe@example.org', "To organizer")

        _accepted = participant_status_label('ACCEPTED')
        message = message_from_string(self.smtplog[0][2])
        self.assertEqual(message.get('Subject'), _("Invitation for %(summary)s was %(status)s") % { 'summary':'test', 'status':_accepted })

        text = str(message.get_payload(0));
        self.assertIn('SUMMARY=test', text)
        self.assertIn('STATUS=' + _accepted, text)