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

from pykolab.xml import Attendee
from pykolab.xml import Todo
from pykolab.xml import TodoIntegrityError
from pykolab.xml import todo_from_ical
from pykolab.xml import todo_from_string
from pykolab.xml import todo_from_message

ical_todo = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube//Roundcube libcalendaring 1.1-git//Sabre//Sabre VObject
 2.1.3//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VTODO
UID:18C2EBBD8B31D99F7AA578EDFDFB1AC0-FCBB6C4091F28CA0
DTSTAMP;VALUE=DATE-TIME:20140820T101333Z
CREATED;VALUE=DATE-TIME:20140731T100704Z
LAST-MODIFIED;VALUE=DATE-TIME:20140820T101333Z
DTSTART;VALUE=DATE-TIME;TZID=Europe/London:20140818T180000
DUE;VALUE=DATE-TIME;TZID=Europe/London:20140822T133000
SUMMARY:Sample Task assignment
DESCRIPTION:Summary: Sample Task assignment\\nDue Date: 08/11/14\\nDue Time:
 \\n13:30 AM
SEQUENCE:3
CATEGORIES:iTip
PRIORITY:1
STATUS:IN-PROCESS
PERCENT-COMPLETE:20
ATTENDEE;CN="Doe, John";PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;CUTYPE=
 INDIVIDUAL;RSVP=TRUE:mailto:john.doe@example.org
ORGANIZER;CN=Thomas:mailto:thomas.bruederli@example.org
END:VTODO
END:VCALENDAR
"""

xml_todo = """
<icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0">
  <vcalendar>
    <properties>
      <prodid>
        <text>Roundcube-libkolab-1.1 Libkolabxml-1.1</text>
      </prodid>
      <version>
        <text>2.0</text>
      </version>
      <x-kolab-version>
        <text>3.1.0</text>
      </x-kolab-version>
    </properties>
    <components>
      <vtodo>
        <properties>
          <uid>
            <text>18C2EBBD8B31D99F7AA578EDFDFB1AC0-FCBB6C4091F28CA0</text>
          </uid>
          <created>
            <date-time>2014-07-31T10:07:04Z</date-time>
          </created>
          <dtstamp>
            <date-time>2014-08-20T10:13:33Z</date-time>
          </dtstamp>
          <sequence>
            <integer>3</integer>
          </sequence>
          <class>
            <text>PUBLIC</text>
          </class>
          <categories>
            <text>iTip</text>
          </categories>
          <dtstart>
            <parameters>
              <tzid><text>/kolab.org/Europe/Berlin</text></tzid>
            </parameters>
            <date-time>2014-08-18T18:00:00</date-time>
          </dtstart>
          <due>
            <parameters>
              <tzid><text>/kolab.org/Europe/Berlin</text></tzid>
            </parameters>
            <date-time>2014-08-22T13:30:00</date-time>
          </due>
          <summary>
            <text>Sample Task assignment</text>
          </summary>
          <description>
            <text>Summary: Sample Task assignment
Due Date: 08/11/14
Due Time: 13:30 AM</text>
          </description>
          <priority>
            <integer>1</integer>
          </priority>
          <status>
            <text>IN-PROCESS</text>
          </status>
          <percent-complete>
            <integer>20</integer>
          </percent-complete>
          <organizer>
            <parameters>
              <cn><text>Thomas</text></cn>
            </parameters>
            <cal-address>mailto:%3Cthomas%40example.org%3E</cal-address>
          </organizer>
          <attendee>
            <parameters>
              <cn><text>Doe, John</text></cn>
              <partstat><text>NEEDS-ACTION</text></partstat>
              <role><text>REQ-PARTICIPANT</text></role>
              <rsvp><boolean>true</boolean></rsvp>
            </parameters>
            <cal-address>mailto:%3Cjohn%40example.org%3E</cal-address>
          </attendee>
        </properties>
        <components>
          <valarm>
            <properties>
              <action>
                <text>DISPLAY</text>
              </action>
              <description>
                <text>alarm 1</text>
              </description>
              <trigger>
                <parameters>
                  <related>
                    <text>START</text>
                  </related>
                </parameters>
                <duration>-PT2H</duration>
              </trigger>
            </properties>
          </valarm>
        </components>
      </vtodo>
    </components>
  </vcalendar>
</icalendar>
"""

class TestTodoXML(unittest.TestCase):
    todo = Todo()

    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_001_minimal(self):
        self.todo.set_summary("test")
        self.assertEqual("test", self.todo.get_summary())
        self.assertIsInstance(self.todo.__str__(), str)

    def test_002_full(self):
        pass

    def test_010_load_from_xml(self):
        todo = todo_from_string(xml_todo)
        self.assertEqual(todo.uid, '18C2EBBD8B31D99F7AA578EDFDFB1AC0-FCBB6C4091F28CA0')
        self.assertEqual(todo.get_sequence(), 3)
        self.assertIsInstance(todo.get_due(), datetime.datetime)
        self.assertEqual(str(todo.get_due()), "2014-08-22 13:30:00+01:00")
        self.assertEqual(str(todo.get_start()), "2014-08-18 18:00:00+01:00")
        self.assertEqual(todo.get_categories(), ['iTip'])
        self.assertEqual(todo.get_attendee_by_email("john@example.org").get_participant_status(), kolabformat.PartNeedsAction)
        self.assertIsInstance(todo.get_organizer(), kolabformat.ContactReference)
        self.assertEqual(todo.get_organizer().name(), "Thomas")
        self.assertEqual(todo.get_status(True), "IN-PROCESS")


    def test_020_load_from_ical(self):
        ical_str = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Roundcube//Roundcube libcalendaring 1.1.0//Sabre//Sabre VObject
  2.1.3//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
        """ + ical_todo + "END:VCALENDAR"

        ical = icalendar.Calendar.from_ical(ical_str)
        vtodo = ical.walk('VTODO')[0]
        #print vtodo
        todo = todo_from_ical(ical.walk('VTODO')[0].to_ical())
        self.assertEqual(todo.get_summary(), "Sample Task assignment")
        self.assertIsInstance(todo.get_start(), datetime.datetime)
        self.assertEqual(todo.get_percentcomplete(), 20)
        #print str(todo)

    def test_021_as_string_itip(self):
        self.todo.set_summary("test")
        self.todo.set_start(datetime.datetime(2014, 9, 20, 11, 00, 00, tzinfo=pytz.timezone("Europe/London")))
        self.todo.set_due(datetime.datetime(2014, 9, 23, 12, 30, 00, tzinfo=pytz.timezone("Europe/London")))
        self.todo.set_sequence(3)
        self.todo.add_custom_property('X-Custom', 'check')

        # render iCal and parse again using the icalendar lib
        ical = icalendar.Calendar.from_ical(self.todo.as_string_itip())
        vtodo = ical.walk('VTODO')[0]

        self.assertEqual(vtodo['uid'], self.todo.get_uid())
        self.assertEqual(vtodo['summary'], "test")
        self.assertEqual(vtodo['sequence'], 3)
        self.assertEqual(vtodo['X-CUSTOM'], "check")
        self.assertIsInstance(vtodo['due'].dt, datetime.datetime)
        self.assertIsInstance(vtodo['dtstamp'].dt, datetime.datetime)


    def test_030_to_dict(self):
        data = todo_from_string(xml_todo).to_dict()

        self.assertIsInstance(data, dict)
        self.assertIsInstance(data['start'], datetime.datetime)
        self.assertIsInstance(data['due'], datetime.datetime)
        self.assertEqual(data['uid'], '18C2EBBD8B31D99F7AA578EDFDFB1AC0-FCBB6C4091F28CA0')
        self.assertEqual(data['summary'], 'Sample Task assignment')
        self.assertEqual(data['description'], "Summary: Sample Task assignment\nDue Date: 08/11/14\nDue Time: 13:30 AM")
        self.assertEqual(data['priority'], 1)
        self.assertEqual(data['sequence'], 3)
        self.assertEqual(data['status'], 'IN-PROCESS')

        self.assertIsInstance(data['alarm'], list)
        self.assertEqual(len(data['alarm']), 1)
        self.assertEqual(data['alarm'][0]['action'], 'DISPLAY')


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