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
|
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from twisted.trial import unittest
import pixelated.support.date
from pixelated.adapter.model.mail import InputMail
import base64
def simple_mail_dict():
return {
'body': 'Este \xe9 o corpo',
'header': {
'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'],
'to': ['to@pixelated.org', 'anotherto@pixelated.org'],
'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'],
'subject': 'Oi'
},
'ident': '',
'tags': ['sent']
}
def multipart_mail_dict():
return {
'body': [{'content-type': 'plain', 'raw': 'Hello world!'},
{'content-type': 'html', 'raw': '<p>Hello html world!</p>'}],
'header': {
'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'],
'to': ['to@pixelated.org', 'anotherto@pixelated.org'],
'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'],
'subject': 'Oi',
},
'ident': '',
'tags': ['sent']
}
class InputMailTest(unittest.TestCase):
def test_to_mime_multipart_should_add_blank_fields(self):
pixelated.support.date.mail_date_now = lambda: 'date now'
mail_dict = simple_mail_dict()
mail_dict['header']['to'] = ''
mail_dict['header']['bcc'] = ''
mail_dict['header']['cc'] = ''
mail_dict['header']['subject'] = ''
mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart()
self.assertNotRegexpMatches(mime_multipart.as_string(), "\nTo: \n")
self.assertNotRegexpMatches(mime_multipart.as_string(), "\nBcc: \n")
self.assertNotRegexpMatches(mime_multipart.as_string(), "\nCc: \n")
self.assertNotRegexpMatches(mime_multipart.as_string(), "\nSubject: \n")
def test_single_recipient(self):
mail_single_recipient = {
'body': '',
'header': {
'to': ['to@pixelated.org'],
'cc': [''],
'bcc': [''],
'subject': 'Oi'
}
}
result = InputMail.from_dict(mail_single_recipient).raw
self.assertRegexpMatches(result, 'To: to@pixelated.org')
def test_to_mime_multipart(self):
pixelated.support.date.mail_date_now = lambda: 'date now'
mime_multipart = InputMail.from_dict(simple_mail_dict()).to_mime_multipart()
self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: to@pixelated.org, anotherto@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n")
self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(simple_mail_dict()['body']))
def test_to_mime_multipart_with_special_chars(self):
mail_dict = simple_mail_dict()
mail_dict['header']['to'] = u'"Älbert Übrö \xF0\x9F\x92\xA9" <äüö@example.mail>'
pixelated.support.date.mail_date_now = lambda: 'date now'
mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart()
expected_part_of_encoded_to = 'Iiwgw4QsIGwsIGIsIGUsIHIsIHQsICAsIMOcLCBiLCByLCDDtiwgICwgw7As'
self.assertRegexpMatches(mime_multipart.as_string(), expected_part_of_encoded_to)
def test_smtp_format(self):
InputMail.FROM_EMAIL_ADDRESS = 'pixelated@org'
smtp_format = InputMail.from_dict(simple_mail_dict()).to_smtp_format()
self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org")
def test_to_mime_multipart_handles_alternative_bodies(self):
mime_multipart = InputMail.from_dict(multipart_mail_dict()).to_mime_multipart()
part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!'
part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n<p>Hello html world!</p>'
self.assertRegexpMatches(mime_multipart.as_string(), part_one)
self.assertRegexpMatches(mime_multipart.as_string(), part_two)
|