forked from vladimarius/imapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
620 lines (516 loc) · 20.9 KB
/
tests.py
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
# -*- coding: utf-8 -*-
""" Tests for imapy """
import pytest
import imaplib
import time
import os
import imapy
import email
from imapy import (imap, mail_folder, exceptions as e)
from imapy.query_builder import Q
from imapy.packages import (six)
from imapy.email_message import EmailMessage
from settings import (host, username, password, ssl)
from email.mime.text import MIMEText
from imapy import utils
def is_ascii(self, txt):
"""Returns True if string consists of ASCII characters only,
False otherwise
"""
try:
txt.encode('utf-8').decode('ASCII')
except UnicodeDecodeError:
return False
return True
def get_text_email(efrom, eto, esubject, etext, ebcc=None, ecc=None):
"""returns email object"""
msg = MIMEText(etext, "plain", "utf-8")
msg['Subject'] = esubject
msg['From'] = efrom
msg['To'] = eto
if ebcc:
msg['Bcc'] = ebcc
if ecc:
msg['Cc'] = ecc
return msg
def setup_function(module):
""" Delete all testing folders and empties Trash"""
box = imapy.connect(
host=host,
username=username,
password=password,
ssl=ssl)
folders = box.folders()
sep = box.separator
bad_folders = ['imapy-del1', 'imapy-del2', 'imapy-search test',
'Imapy-delete', 'Imapy-delete123', 'Imapy тест 123',
'Imapy тест 123' + sep + 'Subfolder 456 тест']
trash_folders = ['Deleted', 'Удаленные', 'Trash',
'[Gmail]' + sep + 'Trash']
for f in folders:
# delete folders inside trash folder: '[Gmail]/Trash/123'
for tf in trash_folders:
if tf in f and tf != f:
box.folder().delete(f)
# delete bad folders
if f in bad_folders:
box.folder().delete(f)
# delete messages inside trash folder
trash_folders = [t.rstrip(sep) for t in trash_folders]
server_trash_folders = list(set(trash_folders) & set(box.folders()))
for f in server_trash_folders:
emails = box.folder(f).emails(-100)
if emails:
for em in emails:
em.delete()
def test_mail_folder_class():
m = mail_folder.MailFolder()
if six.PY2:
raw_folders = ['(\\HasNoChildren) "/" "INBOX"',
'(\\HasNoChildren) "/" "Junk"',
'(\\HasNoChildren) "/" "Personal"',
'(\\HasNoChildren) "/" "Receipts"',
'(\\HasNoChildren) "/" "Trash"',
'(\\HasNoChildren) "/" "Travel"',
'(\\HasNoChildren) "/" "Work"',
'(\\HasChildren \\Noselect) "/" "[Gmail]"',
'(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"',
'(\\HasNoChildren \\Drafts) "/" "[Gmail]/Drafts"',
'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"',
'(\\Sent \\HasNoChildren) "/" "[Gmail]/Sent Mail"',
'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"',
'(\\HasNoChildren \\Flagged) "/" "[Gmail]/Starred"',
'(\\HasChildren \\Trash) "/" "[Gmail]/Trash"',
'(\\HasNoChildren) "/" "[Gmail]/Trash/3"',
'(\\HasChildren) "/" "&BB4EPwQ1BEAEMARGBDgETw- &BCs-"',
'(\\HasNoChildren) "/" "&BB4EPwQ1BEAEMARGBDgETw- &BCs-'
'/&BB4EPwQ1BEAEMARGBDgETw- &BCM-"']
bad_folders = ['(\\HasNoChildren) "Receipts"']
if six.PY3:
raw_folders = [b'(\\HasNoChildren) "/" "INBOX"',
b'(\\HasNoChildren) "/" "Junk"',
b'(\\HasNoChildren) "/" "Personal"',
b'(\\HasNoChildren) "/" "Receipts"',
b'(\\HasNoChildren) "/" "Trash"',
b'(\\HasNoChildren) "/" "Travel"',
b'(\\HasNoChildren) "/" "Work"',
b'(\\HasChildren \\Noselect) "/" "[Gmail]"',
b'(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"',
b'(\\HasNoChildren \\Drafts) "/" "[Gmail]/Drafts"',
b'(\\HasNoChildren \\Important) "/" '
b'"[Gmail]/Important"',
b'(\\Sent \\HasNoChildren) "/" "[Gmail]/Sent Mail"',
b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"',
b'(\\HasNoChildren \\Flagged) "/" "[Gmail]/Starred"',
b'(\\HasChildren \\Trash) "/" "[Gmail]/Trash"',
b'(\\HasNoChildren) "/" "[Gmail]/Trash/3"',
b'(\\HasChildren) "/" "&BB4EPwQ1BEAEMARGBDgETw- &BCs-"',
b'(\\HasNoChildren) "/" "&BB4EPwQ1BEAEMARGBDgETw- '
b'&BCs-/&BB4EPwQ1BEAEMARGBDgETw- &BCM-"']
bad_folders = [b'(\\HasNoChildren) "Receipts"']
folders_tree, children = m._get_folders_tree_and_children(raw_folders)
assert type(folders_tree) is dict
assert len(folders_tree) == 9
with pytest.raises(e.EmailFolderParsingError):
m._get_folders_tree_and_children(bad_folders)
def test_wrong_login_credentials():
with pytest.raises(e.ConnectionRefused):
imapy.connect(
host='nonexistent',
username='username',
password='password',
ssl=True)
with pytest.raises(imaplib.IMAP4_SSL.error):
imapy.connect(
host=host,
username='username',
password='password',
ssl=True)
def test_search_params():
box = imapy.connect(
host=host,
username=username,
password=password,
ssl=ssl)
from1 = '[email protected]'
to1 = '[email protected]'
subject1 = 'Samplissimo subject. Всякое разное'
contents1 = 'Lorem ipsum dorem sit amet.' +\
'Съешь ещё этих мягких французских булок, да выпей же чаю. ' +\
'http://www.cnn.com https://wikipedia.org http://del.icio.us'
bcc1 = '[email protected]'
ecc1 = '[email protected]'
text_email1 = get_text_email(from1, to1, subject1, contents1, bcc1, ecc1)
# append email to folder
folder_name = 'imapy-search test'
box.folder().make_folder(folder_name)
box.folder(folder_name).append(
text_email1,
flags=['answered', 'draft', 'flagged', 'seen'])
q = Q()
with pytest.raises(e.SearchSyntaxNotSupported):
emails = box.folder(folder_name).emails(
q.subject('Съешь').sender('ещё')
)
q = Q()
with pytest.raises(e.WrongDateFormat):
emails = box.folder(folder_name).emails(
q.since('2014-Jan-1')
)
q = Q()
today = time.strftime('%d-%b-%Y')
query = q.sender(from1).bcc(bcc1).answered().before('1-Jan-2050').\
body('ipsum').cc(ecc1).draft().flagged().larger(1).seen().\
sent_before('1-Jan-2050').sent_since('1-Jan-2014').\
since('1-Jan-2014').smaller(10000000).text('ipsum').\
recipient(to1).undeleted().sent_on(today).on(today)
emails = box.folder(folder_name).emails(
query
)
''' we don't test the amount of results because
different imap servers handle search differently
and may not return any results
'''
assert isinstance(emails, list)
q = Q()
# the queries below are pointless and used only for testing
query = q.sender('abc').unseen().unflagged().undraft().unanswered().uid('123')
emails = box.folder(folder_name).emails(
query
)
assert isinstance(emails, list)
q = Q()
query = q.recent()
emails = box.folder(folder_name).emails(
query
)
assert isinstance(emails, list)
q = Q()
query = q.sender('abcsdfsd').old()
emails = box.folder(folder_name).emails(
query
)
assert isinstance(emails, list)
q = Q()
query = q.sender('abcsdfsd').new()
emails = box.folder(folder_name).emails(
query
)
assert isinstance(emails, list)
q = Q()
query = q.sender('abcsdfsd').\
header('some-header', 'some value').deleted()
emails = box.folder(folder_name).emails(
query
)
assert isinstance(emails, list)
box.logout()
def test_operations():
box = imapy.connect(
host=host,
username=username,
password=password,
ssl=ssl)
assert isinstance(box, imap.IMAP)
assert box.lib in (
imaplib.IMAP4_SSL,
imaplib.IMAP4,
imaplib.IMAP4_stream)
# folders()
folders = box.folders()
assert len(folders) > 0
for f in folders:
assert len(f) > 0
folders2 = box.folders()
assert folders2 == folders
# no children
assert box.children() == []
# folder()
assert box.selected_folder is None
box.folder(folders[0])
assert box.selected_folder is not None
# children()
assert type(box.children()) is list
with pytest.raises(e.NonexistentFolderError):
box.folder('NonexistentFolder')
'''
Create, rename, delete folder
'''
# create list of folders
folders_list = ['imapy-del1', 'imapy-del2']
box.folder().make_folder(folders_list)
for f in folders_list:
assert f in box.folders()
box.folder().delete(folders_list)
for f in folders_list:
assert f not in box.folders()
# create folder
delete_name = 'Imapy-delete'
box.folder().make_folder(delete_name)
assert delete_name in box.folders()
with pytest.raises(e.InvalidFolderName):
box.make_folder('BadFolder' + box.separator + 'Name')
# rename
new_name = delete_name + '123'
box.folder(delete_name).rename(new_name)
assert new_name in box.folders()
# delete
box.folder().delete(new_name)
assert (new_name) not in box.folders()
'''
Create subfolders, append messages, check messages,
folder info, email search, mark messages with tags,
delete messages, select parent folder
'''
# create folder (ascii + unicode)
test_folder = 'Imapy тест 123'
box.folder().make_folder(test_folder)
assert test_folder in box.mail_folders
# create subfolder (ascii + unicode)
test_subfolder = 'Subfolder 456 тест'
box.folder(test_folder).make_folder(test_subfolder)
test_folder_full_name = test_folder + box.separator + test_subfolder
assert test_folder_full_name in box.mail_folders
from1 = '[email protected]'
to1 = '[email protected]'
subject1 = 'Email subject заголовок -*-'
test_subject1 = 'subject'
contents1 = 'Email contents содержание'
text_email1 = get_text_email(from1, to1, subject1, contents1)
# check status1 before append
status1 = box.folder(test_folder).info()
assert status1['total'] == 0
# append email to folder
non_standard_flags = ['really-non-standard-flag', 'weird-flag']
box.folder(test_folder).append(
text_email1, flags=non_standard_flags)
with pytest.raises(e.UnknownEmailMessageType):
box.folder(test_folder).append({'dummy': 'dict'})
# check status1 after append
status1 = box.folder(test_folder).info()
assert status1['total'] == 1
# check appended email flags
emails = box.folder(test_folder).emails(-1)
for f in non_standard_flags:
assert f not in emails[0]['flags']
from2 = '[email protected]'
to2 = '[email protected]'
subject2 = 'Email subject 2заголовок2 -*-'
test_subject2 = 'subject'
contents2 = 'Email contents содержание2'
text_email2 = get_text_email(from2, to2, subject2, contents2)
children = box.folder(test_folder).children()
subfolder_name = children[0]
# check parent selection
parent_folder_obj = box.folder(test_folder).__dict__
selected_parent_folder_obj = box.folder(subfolder_name).parent().__dict__
assert parent_folder_obj == selected_parent_folder_obj
# check parent selection when parent is already on topmost level
parent_folder_obj = box.folder(test_folder).__dict__
selected_parent_folder_obj =\
box.folder(subfolder_name).parent().parent().__dict__
assert parent_folder_obj == selected_parent_folder_obj
# check status2 before append
status2 = box.folder(subfolder_name).info()
assert status2['total'] == 0
# append email to subfolder
box.folder(subfolder_name).append(text_email2, flags=['flagged'])
# check status2 after append
status2 = box.folder(subfolder_name).info()
assert status2['total'] == 1
# search non-existent emails
q = Q()
query = q.subject('nonEXISTENTemailSEARCHstring')
emails = box.folder(test_folder).emails(
query
)
assert emails == []
# search all emails in folder
emails = box.folder(test_folder).emails()
assert len(emails) > 0
# search email 1
q = Q()
query = q.subject(test_subject1)
emails = box.folder(test_folder).emails(
query
)
assert len(emails) > 0
# add flag to email
email1 = emails[0]
email1.mark(['seen', 'unflagged'])
with pytest.raises(e.TagNotSupported):
email1.mark(['unsupported_tag', 'really_nonstandard'])
# check email attributes
assert 'seen' in email1['flags']
assert 'flagged' not in email1['flags']
assert email1['from'] == from1
assert email1['to'] == to1
assert email1['subject'] == subject1
assert email1['attachments']['text'][0]['text'] == contents1
# get email flags from server to recheck
emails_flags = box.folder(test_folder).emails(
query
)
email_flags = emails_flags[0]
assert 'seen' in email_flags['flags']
assert 'flagged' not in email_flags['flags']
# search for email 2
q = Q()
query = q.subject(test_subject2)
emails = box.folder(subfolder_name).emails(
query
)
assert len(emails) > 0
# check email attributes
email2 = emails[0]
assert email2['from'] == from2
assert email2['to'] == to2
assert email2['subject'] == subject2
assert email2['attachments']['text'][0]['text'] == contents2
# delete email1 from folder
email1.delete()
# check status1 after delete
status1 = box.folder(test_folder).info()
assert status1['total'] == 0
# delete email2 from subfolder
email2.delete()
# check status2 after delete
status2 = box.folder(subfolder_name).info()
assert status2['total'] == 0
"""
Copy email
"""
# append email
from3 = '[email protected]'
to3 = '[email protected]'
subject3 = 'Copy email testing, заголовок -*-'
contents3 = 'Testing email copying. Проверка копирования email'
text_email3 = get_text_email(from3, to3, subject3, contents3)
box.folder(test_folder).append(text_email3)
status3 = box.folder(test_folder).info()
assert status3['total'] == 1
# copy email
emails = box.folder(test_folder).emails(-1)
emails[0].copy(subfolder_name)
status4 = box.folder(subfolder_name).info()
assert status4['total'] == 1
"""
Move email
"""
from4 = '[email protected]'
to4 = '[email protected]'
subject4 = 'Move email testing'
contents4 = 'Testing email moving'
text_email4 = get_text_email(from4, to4, subject4, contents4)
subfolder_before_move_status = box.folder(subfolder_name).info()
box.folder(test_folder).append(text_email4)
emails = box.folder(test_folder).emails(-1)
assert len(emails) > 0
emails[0].move(subfolder_name)
subfolder_after_move_status = box.folder(subfolder_name).info()
assert subfolder_before_move_status['total'] != \
subfolder_after_move_status['total']
"""
Emails selecting
"""
with pytest.raises(e.InvalidSearchQuery):
box.folder(test_folder).emails(1, 2, 3)
with pytest.raises(e.InvalidSearchQuery):
box.folder(test_folder).emails('something', 'weird')
with pytest.raises(e.InvalidSearchQuery):
box.folder(test_folder).emails('weird')
with pytest.raises(e.InvalidSearchQuery):
box.folder(test_folder).emails(1, -10)
with pytest.raises(e.InvalidSearchQuery):
box.folder(test_folder).emails(-1, 10)
# log out
box.log_out()
with pytest.raises(e.ImapyLoggedOut):
box.folders()
def test_parsing_sample_emails():
# check parsing sample emails (msg1, msg2 ...)
msg_dir = "test_emails"
fnames = os.listdir(msg_dir)
for fn in fnames:
if '.msg' in fn:
f = open(msg_dir + os.sep + fn, 'rb')
raw_email = f.read()
email_obj = email.message_from_string(utils.b_to_str(raw_email))
email_parsed = EmailMessage(
folder='no_folder', uid=999, flags=[],
email_obj=email_obj, imap_obj=None)
msg_num = int(fn.split('.')[0])
if msg_num == 1:
assert email_parsed['from_WHOM'] == 'Dropbox'
assert email_parsed['from'] ==\
'Dropbox <[email protected]>'
assert email_parsed['from_email'] == '[email protected]'
assert email_parsed['cc'] == []
assert email_parsed['subject'] ==\
'Update: Changes to better serve our users around the' +\
' world'
assert email_parsed['date'] == 'Sat, 2 May 2015 13:46:01 +0000'
assert 'outside of North America' in\
email_parsed['attachments']['text'][0]['text_normalized']
assert len(email_parsed['attachments']['html']) > 0
elif msg_num == 2:
assert email_parsed['from_WHOM'] == \
'Надежда ДранинаfS (via Twitter)'
assert 'Надежда ДранинаfS' in email_parsed['from'] and\
'<[email protected]>' in email_parsed['from']
assert email_parsed['from_email'] == '[email protected]'
assert email_parsed['cc'] == []
assert email_parsed['subject'] ==\
'Надежда ДранинаfS (@dranina73) is now following' + \
' you on Twitter!'
assert email_parsed['date'] ==\
'Wed, 22 Apr 2015 05:00:36 +0000'
assert 'You have a new follower on' in\
email_parsed['attachments']['text'][0]['text_normalized']
assert len(email_parsed['attachments']['html']) > 0
elif msg_num == 3:
assert email_parsed['from_WHOM'] == \
'"USPS"'
assert email_parsed['from'] ==\
'"USPS" <[email protected]>'
assert email_parsed['from_email'] == '[email protected]'
assert email_parsed['cc'] == []
assert email_parsed['subject'] ==\
'Shipment status change for package # 23696393'
assert email_parsed['date'] ==\
'Thu, 30 Apr 2015 09:32:16 -0700'
assert 'The package could not be delivered' in\
email_parsed['attachments']['text'][0]['text_normalized']
assert len(email_parsed['attachments']['html']) == 0
elif msg_num == 4:
assert email_parsed['from_WHOM'] == \
'François Schiettecatte'
assert 'François Schiettecatte' in email_parsed['from'] and\
'<[email protected]>' in email_parsed['from']
assert email_parsed['from_email'] == '[email protected]'
assert email_parsed['cc'] == []
assert email_parsed['subject'] ==\
'Re: Require code explaination'
assert email_parsed['date'] ==\
'Wed, 29 Apr 2015 09:46:37 -0400'
assert 'Django does not implement X-Accel-Redirect' in\
email_parsed['attachments']['text'][0]['text_normalized']
assert len(email_parsed['attachments']['html']) == 0
elif msg_num == 5:
assert email_parsed['from_WHOM'] == \
'Orange API contact'
assert email_parsed['from'] ==\
'Orange API contact <[email protected]>'
assert email_parsed['from_email'] == \
assert email_parsed['cc'] == []
assert email_parsed['subject'] ==\
utils.str_to_b(
"Orange API : Annule et Remplace, l'Opération de " +\
"maintenance Orange API aura lieu le jeudi 26 juillet" +\
" au lieu de mercredi 25 juillet"
)
assert email_parsed['date'] ==\
'Tue, 24 Jul 2012 16:49:56 +0200 (CEST)'
assert 'des travaux sur son infrastructure' in\
email_parsed['attachments']['html'][0]