summaryrefslogtreecommitdiffstats
path: root/tests/unit/test-019-contact.py
blob: 6446ad94231eae389a8a0c9c85d135e90c2dedf8 (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
import datetime
import pytz
import unittest
import kolabformat

from pykolab.xml import Contact
from pykolab.xml import ContactIntegrityError
from pykolab.xml import contact_from_string
from pykolab.xml import contact_from_message
from email import message_from_string

xml_contact = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<vcards xmlns="urn:ietf:params:xml:ns:vcard-4.0">
  <vcard>
    <uid>
      <uri>urn:uuid:437656b2-d55e-11e4-a43b-080027b7afc5</uri>
    </uid>
    <x-kolab-version>
      <text>3.1.0</text>
    </x-kolab-version>
    <prodid>
      <text>Roundcube-libkolab-1.1 Libkolabxml-1.2</text>
    </prodid>
    <rev>
      <timestamp>20150328T152236Z</timestamp>
    </rev>
    <kind>
      <text>individual</text>
    </kind>
    <fn>
      <text>Sample Dude</text>
    </fn>
    <n>
      <surname>Dude</surname>
      <given>Sample</given>
      <additional>M.</additional>
      <prefix>Dr.</prefix>
      <suffix>Jr.</suffix>
    </n>
    <note>
      <text>This is a sample contact for testing</text>
    </note>
    <title>
      <text>Head of everything</text>
    </title>
    <group name="Affiliation">
      <org>
        <text>Kolab Inc.</text>
        <text>R&amp;D Department</text>
      </org>
      <related>
        <parameters>
          <type>
            <text>x-manager</text>
          </type>
        </parameters>
        <text>Jane Manager</text>
      </related>
      <related>
        <parameters>
          <type>
            <text>x-assistant</text>
          </type>
        </parameters>
        <text>Mrs. Moneypenny</text>
      </related>
      <adr>
        <parameters/>
        <pobox/>
        <ext/>
        <street>O-steet</street>
        <locality>San Francisco</locality>
        <region>CA</region>
        <code>55550</code>
        <country>USA</country>
      </adr>
    </group>
    <url>
      <uri>www.kolab.org</uri>
    </url>
    <adr>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <pobox/>
      <ext/>
      <street>Homestreet 11</street>
      <locality>Hometown</locality>
      <region/>
      <code>12345</code>
      <country>Germany</country>
    </adr>
    <adr>
      <parameters>
        <type>
          <text>work</text>
        </type>
      </parameters>
      <pobox/>
      <ext/>
      <street>Workstreet 22</street>
      <locality>Worktown</locality>
      <region/>
      <code>4567</code>
      <country>Switzerland</country>
    </adr>
    <nickname>
      <text>the dude</text>
    </nickname>
    <related>
      <parameters>
        <type>
          <text>spouse</text>
        </type>
      </parameters>
      <text>Leia</text>
    </related>
    <related>
      <parameters>
        <type>
          <text>child</text>
        </type>
      </parameters>
      <text>Jay</text>
    </related>
    <related>
      <parameters>
        <type>
          <text>child</text>
        </type>
      </parameters>
      <text>Bob</text>
    </related>
    <bday>
      <date>20010401</date>
    </bday>
    <anniversary>
      <date>20100705</date>
    </anniversary>
    <photo>
      <uri>data:image/gif;base64,R0lGODlhAQABAPAAAOjq6gAAACH/C1hNUCBEYXRhWE1QAT8AIfkEBQAAAAAsAAAAAAEAAQAAAgJEAQA7</uri>
    </photo>
    <gender>
      <sex>M</sex>
    </gender>
    <tel>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <text>+49-555-11223344</text>
    </tel>
    <tel>
      <parameters>
        <type>
          <text>work</text>
        </type>
      </parameters>
      <text>+49-555-44556677</text>
    </tel>
    <tel>
      <parameters>
        <type>
          <text>cell</text>
        </type>
      </parameters>
      <text>+41-777-55588899</text>
    </tel>
    <impp>
      <uri>jabber:dude@kolab.org</uri>
    </impp>
    <email>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <text>home@kolab.org</text>
    </email>
    <email>
      <parameters>
        <type>
          <text>work</text>
        </type>
      </parameters>
      <text>work@kolab.org</text>
    </email>
    <key>
      <uri>data:application/pgp-keys;base64,LS0tLS1CRUdJTiBQR1AgUFVCTElDIEtFWSBCTE9DSy0tLS0tDQpWZXJzaW9uOiBHbnVQRy9NYWNHUEcyIHYyLjAuMjINCg0KbVFHaUJFSVNOcUVSQkFDUnovb3J5L0JEY3pBWUFUR3JnTSt5WDgzV2pkaUVrNmZKNFFUekk2ZFZ1TkxTNy4uLg0KLS0tLS1FTkQgUEdQIFBVQkxJQyBLRVkgQkxPQ0stLS0tLQ==</uri>
    </key>
  </vcard>
</vcards>
"""

contact_mime_message = """MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_4ff5155d75dc1328b7f5fe10ddce8d24"
From: john.doe@example.org
To: john.doe@example.org
Date: Mon, 13 Apr 2015 15:26:44 +0200
X-Kolab-Type: application/x-vnd.kolab.contact
X-Kolab-Mime-Version: 3.0
Subject: 05cfc56d-2bb3-46d1-ada4-5f5310337fb2
User-Agent: Roundcube Webmail/1.2-git

--=_4ff5155d75dc1328b7f5fe10ddce8d24
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

This is a Kolab Groupware object. To view this object you will need an emai=
l client that understands the Kolab Groupware format. For a list of such em=
ail clients please visit http://www.kolab.org/

--=_4ff5155d75dc1328b7f5fe10ddce8d24
Content-Transfer-Encoding: 8bit
Content-Type: application/vcard+xml; charset=UTF-8;
 name=kolab.xml
Content-Disposition: attachment;
 filename=kolab.xml;
 size=1636

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<vcards xmlns="urn:ietf:params:xml:ns:vcard-4.0">
  <vcard>
    <uid>
      <uri>urn:uuid:05cfc56d-2bb3-46d1-ada4-5f5310337fb2</uri>
    </uid>
    <x-kolab-version>
      <text>3.1.0</text>
    </x-kolab-version>
    <prodid>
      <text>Roundcube-libkolab-1.1 Libkolabxml-1.1</text>
    </prodid>
    <rev>
      <timestamp>20150413T132644Z</timestamp>
    </rev>
    <kind>
      <text>individual</text>
    </kind>
    <fn>
      <text>User One</text>
    </fn>
    <n>
      <surname>User One</surname>
      <given>DAV</given>
    </n>
    <note>
      <text>This is a Kolab contact</text>
    </note>
    <tel>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <text>+1555224488</text>
    </tel>
    <email>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <text>dav.user01@example.org</text>
    </email>
    <email>
      <parameters>
        <type>
          <text>home</text>
        </type>
      </parameters>
      <text>user.one@example.org</text>
    </email>
  </vcard>
</vcards>

--=_4ff5155d75dc1328b7f5fe10ddce8d24--
"""


class TestContactXML(unittest.TestCase):
    contact = Contact()

    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.contact.set_name("test")
        self.assertEqual("test", self.contact.name())
        self.assertIsInstance(self.contact.__str__(), str)

    def test_002_full(self):
        self.contact.set_name("test")
        # TODO: add more setters and getter tests here

    def test_010_load_from_xml(self):
        contact = contact_from_string(xml_contact)
        self.assertEqual(contact.get_uid(), '437656b2-d55e-11e4-a43b-080027b7afc5')
        self.assertEqual(contact.name(), 'Sample Dude')

    def test_011_load_from_message(self):
        contact = contact_from_message(message_from_string(contact_mime_message))
        self.assertEqual(contact.get_uid(), '05cfc56d-2bb3-46d1-ada4-5f5310337fb2')
        self.assertEqual(contact.name(), 'User One')

    def test_020_to_dict(self):
        data = contact_from_string(xml_contact).to_dict()

        self.assertIsInstance(data, dict)
        self.assertIsInstance(data['lastmodified-date'], datetime.datetime)
        self.assertEqual(data['uid'], '437656b2-d55e-11e4-a43b-080027b7afc5')
        self.assertEqual(data['fn'], 'Sample Dude')
        self.assertEqual(data['given'], 'Sample')
        self.assertEqual(data['surname'], 'Dude')
        self.assertEqual(data['prefix'], 'Dr.')
        self.assertEqual(data['suffix'], 'Jr.')
        self.assertIsInstance(data['birthday'], datetime.date)
        self.assertIsInstance(data['anniversary'], datetime.date)
        self.assertEqual(data['organization'], 'Kolab Inc.')
        self.assertEqual(data['department'], 'R&D Department')
        self.assertEqual(data['manager'], ['Jane Manager'])
        self.assertEqual(data['note'], 'This is a sample contact for testing')
        self.assertEqual(len(data['address']), 3)
        self.assertEqual(data['address'][0]['type'], 'home')
        self.assertEqual(data['address'][1]['type'], 'work')
        self.assertEqual(data['address'][2]['type'], 'office')
        self.assertEqual(len(data['tel']), 3)
        self.assertEqual(data['tel'][0]['type'], 'home')
        self.assertEqual(data['tel'][0]['number'], '+49-555-11223344')
        self.assertEqual(data['tel'][1]['type'], 'work')
        self.assertEqual(data['tel'][2]['type'], 'mobile')
        self.assertEqual(len(data['email']), 2)
        self.assertEqual(data['email'][0]['type'], 'home')
        self.assertEqual(data['email'][0]['address'], 'home@kolab.org')
        self.assertEqual(len(data['url']), 1)
        self.assertEqual(len(data['key']), 1)
        self.assertEqual(data['key'][0]['type'], 'pgp')
        self.assertIsInstance(data['photo'], dict)
        self.assertEqual(data['photo']['mimetype'], 'image/gif')

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