1 import unittest
2 import pyzmail
3 from pyzmail.generate import *
4 from pyzmail.parse import *
5
7
10
12 """test generate and parse"""
13
14 sender=(u'Me', 'me@foo.com')
15 recipients=[(u'Him', 'him@bar.com'), 'just@me.com']
16 subject=u'Le sujet en Fran\xe7ais'
17 text_content=u'Bonjour aux Fran\xe7ais'
18 prefered_encoding='iso-8859-1'
19 text_encoding='iso-8859-1'
20 attachments=[('attached content', 'text', 'plain', 'textfile1.txt', 'us-ascii'),
21 (u'Fran\xe7ais', 'text', 'plain', 'textfile2.txt', 'iso-8859-1'),
22 ('Fran\xe7ais', 'text', 'plain', 'textfile3.txt', 'iso-8859-1'),
23 (b'image', 'image', 'jpg', 'imagefile.jpg', None),
24 ]
25 embeddeds=[(u'embedded content', 'text', 'plain', 'embedded', 'us-ascii'),
26 (b'picture', 'image', 'png', 'picture', None),
27 ]
28 headers=[ ('X-extra', u'extra value'), ('X-extra2', u"Seconde ent\xe8te"), ('X-extra3', u'last extra'),]
29
30 message_id_string='pyzmail'
31 date=1313558269
32
33 payload, mail_from, rcpt_to, msg_id=pyzmail.compose_mail(\
34 sender, \
35 recipients, \
36 subject, \
37 prefered_encoding, \
38 (text_content, text_encoding), \
39 html=None, \
40 attachments=attachments, \
41 embeddeds=embeddeds, \
42 headers=headers, \
43 message_id_string=message_id_string, \
44 date=date\
45 )
46
47 msg=PyzMessage.factory(payload)
48
49 self.assertEqual(sender, msg.get_address('from'))
50 self.assertEqual(recipients[0], msg.get_addresses('to')[0])
51 self.assertEqual(recipients[1], msg.get_addresses('to')[1][1])
52 self.assertEqual(subject, msg.get_subject())
53 self.assertEqual(subject, msg.get_decoded_header('subject'))
54
55
56 mail_date=list(email.utils.parsedate(msg.get_decoded_header('date')))
57 self.assertEqual(mail_date[:6], list(time.localtime(date))[:6])
58
59 self.assertNotEqual(msg.get('message-id').find(message_id_string), -1)
60 for name, value in headers:
61 self.assertEqual(value, msg.get_decoded_header(name))
62
63 for mailpart in msg.mailparts:
64 if mailpart.is_body:
65 self.assertEqual(mailpart.content_id, None)
66 self.assertEqual(mailpart.filename, None)
67 self.assertEqual(type(mailpart.sanitized_filename), str)
68 if mailpart.type=='text/plain':
69 self.assertEqual(mailpart.get_payload(), text_content.encode(text_encoding))
70 else:
71 self.fail('found unknown body part')
72 else:
73 if mailpart.filename:
74 lst=attachments
75 self.assertEqual(mailpart.filename, mailpart.sanitized_filename)
76 self.assertEqual(mailpart.content_id, None)
77 elif mailpart.content_id:
78 lst=embeddeds
79 self.assertEqual(mailpart.filename, None)
80 else:
81 self.fail('found unknown part')
82
83 found=False
84 for attach in lst:
85 found=(mailpart.filename and attach[3]==mailpart.filename) \
86 or (mailpart.content_id and attach[3]==mailpart.content_id)
87 if found:
88 break
89
90 if found:
91 self.assertEqual(mailpart.type, attach[1]+'/'+attach[2])
92 payload=mailpart.get_payload()
93 if attach[1]=='text' and attach[4] and isinstance(attach[0], unicode):
94 payload=payload.decode(attach[4])
95 self.assertEqual(payload, attach[0])
96 else:
97 self.fail('found unknown attachment')
98