summaryrefslogtreecommitdiffstats
path: root/tests/unit/test-003-event.py
blob: f9ef92e05652b0ed739ba076022f9413b3e1bbac (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
import datetime
import pytz
import sys
import unittest
import kolabformat
import icalendar

from pykolab.xml import Attendee
from pykolab.xml import Event
from pykolab.xml import EventIntegrityError
from pykolab.xml import InvalidAttendeeParticipantStatusError
from pykolab.xml import InvalidEventDateError
from pykolab.xml import event_from_ical
from pykolab.xml import event_from_string

class TestEventXML(unittest.TestCase):
    event = Event()

    def assertIsInstance(self, _value, _type):
        if hasattr(unittest.TestCase, 'assertIsInstance'):
            return unittest.TestCase.assertIsInstance(self, _value, _type)
        else:
            if (type(_value)) == _type:
                return True
            else:
                raise AssertionError, "%s != %s" % (type(_value), _type)

    def test_000_no_start_date(self):
        self.assertRaises(EventIntegrityError, self.event.__str__)

    def test_001_minimal(self):
        self.event.set_start(datetime.datetime.now(pytz.timezone("Europe/London")))
        self.assertIsInstance(self.event.get_start(), datetime.datetime)
        self.assertIsInstance(self.event.__str__(), str)

    def test_002_attendees_list(self):
        self.assertIsInstance(self.event.get_attendees(), list)

    def test_003_attendees_no_default(self):
        self.assertEqual(len(self.event.get_attendees()), 0)

    def test_004_attendee_add(self):
        self.event.add_attendee("john@doe.org")
        self.assertIsInstance(self.event.get_attendees(), list)
        self.assertEqual(len(self.event.get_attendees()), 1)

    def test_005_attendee_add_name_and_props(self):
        self.event.add_attendee("jane@doe.org", "Doe, Jane", role="OPT-PARTICIPANT", cutype="RESOURCE")
        self.assertIsInstance(self.event.get_attendees(), list)
        self.assertEqual(len(self.event.get_attendees()), 2)

    def test_006_get_attendees(self):
        self.assertEqual([x.get_email() for x in self.event.get_attendees()], ["john@doe.org", "jane@doe.org"])

    def test_007_get_attendee_by_email(self):
        self.assertIsInstance(self.event.get_attendee_by_email("jane@doe.org"), Attendee)
        self.assertIsInstance(self.event.get_attendee("jane@doe.org"), Attendee)

    def test_007_get_attendee_props(self):
        self.assertEqual(self.event.get_attendee("jane@doe.org").get_cutype(), kolabformat.CutypeResource)
        self.assertEqual(self.event.get_attendee("jane@doe.org").get_role(), kolabformat.Optional)

    def test_007_get_nonexistent_attendee_by_email(self):
        self.assertRaises(ValueError, self.event.get_attendee_by_email, "nosuchattendee@invalid.domain")
        self.assertRaises(ValueError, self.event.get_attendee, "nosuchattendee@invalid.domain")

    def test_008_get_attendee_by_name(self):
        self.assertIsInstance(self.event.get_attendee_by_name("Doe, Jane"), Attendee)
        self.assertIsInstance(self.event.get_attendee("Doe, Jane"), Attendee)

    def test_008_get_nonexistent_attendee_by_name(self):
        self.assertRaises(ValueError, self.event.get_attendee_by_name, "Houdini, Harry")
        self.assertRaises(ValueError, self.event.get_attendee, "Houdini, Harry")

    def test_009_invalid_participant_status(self):
        self.assertRaises(InvalidAttendeeParticipantStatusError, self.event.set_attendee_participant_status, "jane@doe.org", "INVALID")

    def test_010_datetime_from_string(self):
        self.assertRaises(InvalidEventDateError, self.event.set_start, "2012-05-23 11:58:00")

    def test_011_attendee_equality(self):
        self.assertEqual(self.event.get_attendee("jane@doe.org").get_email(), "jane@doe.org")

    def test_012_delegate_new_attendee(self):
        self.event.delegate("jane@doe.org", "max@imum.com")

    def test_013_delegatee_is_now_attendee(self):
        delegatee = self.event.get_attendee("max@imum.com")
        self.assertIsInstance(delegatee, Attendee)
        self.assertEqual(delegatee.get_role(), kolabformat.Optional)
        self.assertEqual(delegatee.get_cutype(), kolabformat.CutypeResource)

    def test_014_delegate_attendee_adds(self):
        self.assertEqual(len(self.event.get_attendee("jane@doe.org").get_delegated_to()), 1)
        self.event.delegate("jane@doe.org", "john@doe.org")
        self.assertEqual(len(self.event.get_attendee("jane@doe.org").get_delegated_to()), 2)

    def test_015_timezone(self):
        _tz = self.event.get_start()
        self.assertIsInstance(_tz.tzinfo, datetime.tzinfo)

    def test_016_start_with_timezone(self):
        _start = datetime.datetime(2012, 05, 23, 11, 58, 00, tzinfo=pytz.timezone("Europe/Zurich"))
        _start_utc = _start.astimezone(pytz.utc)
        self.assertEqual(_start.__str__(), "2012-05-23 11:58:00+01:00")
        self.assertEqual(_start_utc.__str__(), "2012-05-23 10:58:00+00:00")
        self.event.set_start(_start)
        self.assertIsInstance(_start.tzinfo, datetime.tzinfo)
        self.assertEqual(_start.tzinfo, pytz.timezone("Europe/Zurich"))

    def test_017_allday_without_timezone(self):
        _start = datetime.date(2012, 05, 23)
        self.assertEqual(_start.__str__(), "2012-05-23")
        self.event.set_start(_start)
        self.assertEqual(hasattr(_start,'tzinfo'), False)
        self.assertEqual(self.event.get_start().__str__(), "2012-05-23")

    def test_018_load_from_ical(self):
        ical_str = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube//Roundcube libcalendaring 1.1-git//Sabre//Sabre VObject
  2.1.3//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:7a35527d-f783-4b58-b404-b1389bd2fc57
DTSTAMP;VALUE=DATE-TIME:20140407T122311Z
CREATED;VALUE=DATE-TIME:20140407T122245Z
LAST-MODIFIED;VALUE=DATE-TIME:20140407T122311Z
DTSTART;TZID=Europe/Zurich;VALUE=DATE-TIME:20140523T110000
DURATION:PT1H30M0S
RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=10
EXDATE;TZID=Europe/Zurich;VALUE=DATE-TIME:20140530T110000
EXDATE;TZID=Europe/Zurich;VALUE=DATE-TIME:20140620T110000
SUMMARY:Summary
LOCATION:Location
DESCRIPTION:Description\\n2 lines
CATEGORIES:Personal
TRANSP:OPAQUE
PRIORITY:2
SEQUENCE:2
CLASS:PUBLIC
ATTENDEE;CN="Manager, Jane";PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;CUTYP
 E=INDIVIDUAL;RSVP=TRUE:mailto:jane.manager@example.org
ATTENDEE;CUTYPE=RESOURCE;PARTSTAT=NEEDS-ACTION;ROLE=OPT-PARTICIPANT;RSVP=FA
 LSE:MAILTO:max@imum.com
ORGANIZER;CN=Doe\, John:mailto:john.doe@example.org
URL:http://somelink.com/foo
ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=image/png;X-LABEL=silhouette.pn
 g:iVBORw0KGgoAAAANSUhEUgAAAC4AAAAuCAIAAADY27xgAAAAGXRFWHRTb2Z0d2FyZQBBZG9i
 ZSBJbWFnZVJlYWR5ccllPAAAAsRJREFUeNrsmeluKjEMhTswrAWB4P3fECGx79CjsTDmOKRkpF
 xxpfoHSmchX7ybFrfb7eszpPH1MfKH8ofyH6KUtd/c7/en0wmfWBdF0Wq1Op1Ou91uNGoer6iX
 V1ar1Xa7xUJeB4qsr9frdyVlWWZH2VZyPp+xPXHIAoK70+m02+1m9JXj8bhcLi+Xi3J4xUCazS
 bUltdtd7ud7ldUIhC3u+iTwF0sFhlR4Kds4LtRZK1w4te5UM6V6JaqhqC3CQ28OAsKggJfbZ3U
 eozCqZ4koHIZCGmD9ivuos9YONFirmxrI0UNZG1kbZeUXdJQNJNa91RlqMn0ekYUMZDup6dXVV
 m+1OSZhqLx6bVCELJGSsyFQtFrF15JGYMZgoxubWGDSDVhvTipDKWhoBOIpFobxtlbJ0Gh0/tg
 lgXal4woUHi/36fQoBQncDAlupa8DeVwOPRe4lUyGAwQ+dl7W+xBXkJBhEUqR32UoJfYIKrR4d
 ZBgcdIRqfEqn+mekl9FNRbSTA249la3ev1/kXHD47ZbEYR5L9kMplkd9vNZqMFyIYxxfN8Pk8q
 QGlagT5QDtfrNYUMlWW9LiGNPPSmC/+OgpK2r4RO6dOatZd+4gAAemdIi6Fg9EKLD4vASWkzv3
 ew06NSCiA40CumAIoaIrhrcAwjF7aDo58gUchgNV+0n1BAcDgcoAZrXV9mI4qkhtK6FJFhi9Fo
 ZKPsgQI1ACJieH/Kd570t+xFoIzHYzl5Q40CFGrSqGuks3qmYIKJfIl0nPKLxAMFw7Dv1+2QYf
 vFSOBQubbOFDSc7ZcfWvHv6DzhOzT6IeOVPuz8Roex0f6EgsE/2IL4qdg7hIXz7/pBie7q1uWr
 tp66xrif0l1KwUE4P7Y9Gci/ZgtNRFX+Rw06Q2RigsjuDc3urwKHxuNITaaxyD9mT2WvSDAXn/
 Pvhh8BBgBjyfPSGbSYcwAAAABJRU5ErkJggg==
ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/plain;X-LABEL=text.txt:VGh
 pcyBpcyBhIHRleHQgZmlsZQo=
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT30M
END:VALARM
END:VEVENT
END:VCALENDAR
"""
        ical = icalendar.Calendar.from_ical(ical_str)
        event = event_from_ical(ical.walk('VEVENT')[0].to_ical())

        self.assertEqual(event.get_location(), "Location")
        self.assertEqual(str(event.get_lastmodified()), "2014-04-07 12:23:11")
        self.assertEqual(event.get_description(), "Description\n2 lines")
        self.assertEqual(event.get_url(), "http://somelink.com/foo")
        self.assertEqual(event.get_transparency(), False)
        self.assertEqual(event.get_categories(), ["Personal"])
        self.assertEqual(event.get_priority(), '2')
        self.assertEqual(event.get_classification(), kolabformat.ClassPublic)
        self.assertEqual(event.get_attendee_by_email("max@imum.com").get_cutype(), kolabformat.CutypeResource)
        self.assertEqual(event.get_sequence(), 2)
        self.assertTrue(event.is_recurring())
        self.assertIsInstance(event.get_duration(), datetime.timedelta)
        self.assertIsInstance(event.get_end(), datetime.datetime)
        self.assertEqual(str(event.get_end()), "2014-05-23 12:30:00+01:00")
        self.assertEqual(len(event.get_exception_dates()), 2)
        self.assertIsInstance(event.get_exception_dates()[0], datetime.datetime)
        self.assertEqual(len(event.get_alarms()), 1)
        self.assertEqual(len(event.get_attachments()), 2)

    def test_019_as_string_itip(self):
        self.event.set_summary("test")
        self.event.set_start(datetime.datetime(2014, 05, 23, 11, 00, 00, tzinfo=pytz.timezone("Europe/London")))
        self.event.set_end(datetime.datetime(2014, 05, 23, 12, 30, 00, tzinfo=pytz.timezone("Europe/London")))
        self.event.set_sequence(3)

        ical = icalendar.Calendar.from_ical(self.event.as_string_itip())
        event = ical.walk('VEVENT')[0]

        self.assertEqual(event['uid'], self.event.get_uid())
        self.assertEqual(event['summary'], "test")
        self.assertEqual(event['sequence'], 3)
        self.assertIsInstance(event['dtstamp'].dt, datetime.datetime)

    def test_020_calendaring_recurrence(self):
        rrule = kolabformat.RecurrenceRule()
        rrule.setFrequency(kolabformat.RecurrenceRule.Monthly)
        rrule.setCount(10)

        self.event = Event()
        self.event.set_recurrence(rrule);

        _start = datetime.datetime(2014, 5, 1, 11, 30, 00, tzinfo=pytz.timezone("Europe/London"))
        self.event.set_start(_start)
        self.event.set_end(_start + datetime.timedelta(hours=2))

        self.assertTrue(self.event.is_recurring())

        next_date = self.event.get_next_occurence(_start)
        self.assertIsInstance(next_date, datetime.datetime)
        self.assertEqual(next_date.month, 6)
        self.assertEqual(next_date.day, 1)

        end_date = self.event.get_occurence_end_date(next_date)
        self.assertIsInstance(end_date, datetime.datetime)
        self.assertEqual(end_date.month, 6)
        self.assertEqual(end_date.hour, 13)

        self.assertEqual(self.event.get_next_occurence(next_date).month, 7)

        last_date = self.event.get_last_occurrence()
        self.assertIsInstance(last_date, datetime.datetime)
        self.assertEqual(last_date.year, 2015)
        self.assertEqual(last_date.month, 2)

        self.assertEqual(self.event.get_next_occurence(last_date), None)

        # check infinite recurrence
        rrule = kolabformat.RecurrenceRule()
        rrule.setFrequency(kolabformat.RecurrenceRule.Monthly)
        self.event.set_recurrence(rrule);

        self.assertEqual(self.event.get_last_occurrence(), None)
        self.assertIsInstance(self.event.get_last_occurrence(force=True), datetime.datetime)

        # check get_next_instance() which returns a clone of the base event
        next_instance = self.event.get_next_instance(next_date)
        self.assertIsInstance(next_instance, Event)
        self.assertEqual(self.event.get_summary(), next_instance.get_summary())
        self.assertEqual(next_instance.get_start().month, 7)
        self.assertFalse(next_instance.is_recurring())

    def test_021_calendaring_no_recurrence(self):
        _start = datetime.datetime(2014, 2, 1, 14, 30, 00, tzinfo=pytz.timezone("Europe/London"))
        self.event = Event()
        self.event.set_start(_start)
        self.event.set_end(_start + datetime.timedelta(hours=2))

        self.assertEqual(self.event.get_next_occurence(_start), None)
        self.assertEqual(self.event.get_last_occurrence(), None)

    def test_022_load_from_xml(self):
        xml = """
<icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0">
  <vcalendar>
    <properties>
      <prodid>
        <text>Libkolabxml-1.1</text>
      </prodid>
      <version>
        <text>2.0</text>
      </version>
      <x-kolab-version>
        <text>3.1.0</text>
      </x-kolab-version>
    </properties>
    <components>
      <vevent>
        <properties>
          <uid>
            <text>75c740bb-b3c6-442c-8021-ecbaeb0a025e</text>
          </uid>
          <created>
            <date-time>2014-07-07T01:28:23Z</date-time>
          </created>
          <dtstamp>
            <date-time>2014-07-07T01:28:23Z</date-time>
          </dtstamp>
          <sequence>
            <integer>1</integer>
          </sequence>
          <class>
            <text>PUBLIC</text>
          </class>
          <dtstart>
            <parameters>
              <tzid>
                <text>/kolab.org/Europe/London</text>
              </tzid>
            </parameters>
            <date-time>2014-08-13T10:00:00</date-time>
          </dtstart>
          <dtend>
            <parameters>
              <tzid><text>/kolab.org/Europe/London</text></tzid>
            </parameters>
            <date-time>2014-08-13T14:00:00</date-time>
          </dtend>
          <summary>
            <text>test</text>
          </summary>
          <organizer>
            <parameters>
              <cn><text>Doe, John</text></cn>
            </parameters>
            <cal-address>mailto:%3Cjohn%40example.org%3E</cal-address>
          </organizer>
          <attendee>
            <parameters>
              <partstat><text>ACCEPTED</text></partstat>
              <role><text>REQ-PARTICIPANT</text></role>
              <rsvp><boolean>true</boolean></rsvp>
            </parameters>
            <cal-address>mailto:%3Cjane%40example.org%3E</cal-address>
          </attendee>
          <attendee>
            <parameters>
              <partstat><text>TENTATIVE</text></partstat>
              <role><text>OPT-PARTICIPANT</text></role>
            </parameters>
            <cal-address>mailto:%3Csomebody%40else.com%3E</cal-address>
          </attendee>
        </properties>
      </vevent>
    </components>
  </vcalendar>
</icalendar>
"""
        event = event_from_string(xml)
        self.assertEqual(event.uid, '75c740bb-b3c6-442c-8021-ecbaeb0a025e')
        self.assertEqual(event.get_attendee_by_email("jane@example.org").get_participant_status(), kolabformat.PartAccepted)
        self.assertEqual(event.get_sequence(), 1)
        self.assertIsInstance(event.get_start(), datetime.datetime)
        self.assertEqual(str(event.get_start()), "2014-08-13 10:00:00+00:00")


if __name__ == '__main__':
    unittest.main()