1 | n/a | import io |
---|
2 | n/a | import textwrap |
---|
3 | n/a | import unittest |
---|
4 | n/a | from email import message_from_string, message_from_bytes |
---|
5 | n/a | from email.message import EmailMessage |
---|
6 | n/a | from email.generator import Generator, BytesGenerator |
---|
7 | n/a | from email import policy |
---|
8 | n/a | from test.test_email import TestEmailBase, parameterize |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | @parameterize |
---|
12 | n/a | class TestGeneratorBase: |
---|
13 | n/a | |
---|
14 | n/a | policy = policy.default |
---|
15 | n/a | |
---|
16 | n/a | def msgmaker(self, msg, policy=None): |
---|
17 | n/a | policy = self.policy if policy is None else policy |
---|
18 | n/a | return self.msgfunc(msg, policy=policy) |
---|
19 | n/a | |
---|
20 | n/a | refold_long_expected = { |
---|
21 | n/a | 0: textwrap.dedent("""\ |
---|
22 | n/a | To: whom_it_may_concern@example.com |
---|
23 | n/a | From: nobody_you_want_to_know@example.com |
---|
24 | n/a | Subject: We the willing led by the unknowing are doing the |
---|
25 | n/a | impossible for the ungrateful. We have done so much for so long with so little |
---|
26 | n/a | we are now qualified to do anything with nothing. |
---|
27 | n/a | |
---|
28 | n/a | None |
---|
29 | n/a | """), |
---|
30 | n/a | # From is wrapped because wrapped it fits in 40. |
---|
31 | n/a | 40: textwrap.dedent("""\ |
---|
32 | n/a | To: whom_it_may_concern@example.com |
---|
33 | n/a | From: |
---|
34 | n/a | nobody_you_want_to_know@example.com |
---|
35 | n/a | Subject: We the willing led by the |
---|
36 | n/a | unknowing are doing the impossible for |
---|
37 | n/a | the ungrateful. We have done so much |
---|
38 | n/a | for so long with so little we are now |
---|
39 | n/a | qualified to do anything with nothing. |
---|
40 | n/a | |
---|
41 | n/a | None |
---|
42 | n/a | """), |
---|
43 | n/a | # Neither to nor from fit even if put on a new line, |
---|
44 | n/a | # so we leave them sticking out on the first line. |
---|
45 | n/a | 20: textwrap.dedent("""\ |
---|
46 | n/a | To: whom_it_may_concern@example.com |
---|
47 | n/a | From: nobody_you_want_to_know@example.com |
---|
48 | n/a | Subject: We the |
---|
49 | n/a | willing led by the |
---|
50 | n/a | unknowing are doing |
---|
51 | n/a | the impossible for |
---|
52 | n/a | the ungrateful. We |
---|
53 | n/a | have done so much |
---|
54 | n/a | for so long with so |
---|
55 | n/a | little we are now |
---|
56 | n/a | qualified to do |
---|
57 | n/a | anything with |
---|
58 | n/a | nothing. |
---|
59 | n/a | |
---|
60 | n/a | None |
---|
61 | n/a | """), |
---|
62 | n/a | } |
---|
63 | n/a | refold_long_expected[100] = refold_long_expected[0] |
---|
64 | n/a | |
---|
65 | n/a | refold_all_expected = refold_long_expected.copy() |
---|
66 | n/a | refold_all_expected[0] = ( |
---|
67 | n/a | "To: whom_it_may_concern@example.com\n" |
---|
68 | n/a | "From: nobody_you_want_to_know@example.com\n" |
---|
69 | n/a | "Subject: We the willing led by the unknowing are doing the " |
---|
70 | n/a | "impossible for the ungrateful. We have done so much for " |
---|
71 | n/a | "so long with so little we are now qualified to do anything " |
---|
72 | n/a | "with nothing.\n" |
---|
73 | n/a | "\n" |
---|
74 | n/a | "None\n") |
---|
75 | n/a | refold_all_expected[100] = ( |
---|
76 | n/a | "To: whom_it_may_concern@example.com\n" |
---|
77 | n/a | "From: nobody_you_want_to_know@example.com\n" |
---|
78 | n/a | "Subject: We the willing led by the unknowing are doing the " |
---|
79 | n/a | "impossible for the ungrateful. We have\n" |
---|
80 | n/a | " done so much for so long with so little we are now qualified " |
---|
81 | n/a | "to do anything with nothing.\n" |
---|
82 | n/a | "\n" |
---|
83 | n/a | "None\n") |
---|
84 | n/a | |
---|
85 | n/a | length_params = [n for n in refold_long_expected] |
---|
86 | n/a | |
---|
87 | n/a | def length_as_maxheaderlen_parameter(self, n): |
---|
88 | n/a | msg = self.msgmaker(self.typ(self.refold_long_expected[0])) |
---|
89 | n/a | s = self.ioclass() |
---|
90 | n/a | g = self.genclass(s, maxheaderlen=n, policy=self.policy) |
---|
91 | n/a | g.flatten(msg) |
---|
92 | n/a | self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) |
---|
93 | n/a | |
---|
94 | n/a | def length_as_max_line_length_policy(self, n): |
---|
95 | n/a | msg = self.msgmaker(self.typ(self.refold_long_expected[0])) |
---|
96 | n/a | s = self.ioclass() |
---|
97 | n/a | g = self.genclass(s, policy=self.policy.clone(max_line_length=n)) |
---|
98 | n/a | g.flatten(msg) |
---|
99 | n/a | self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) |
---|
100 | n/a | |
---|
101 | n/a | def length_as_maxheaderlen_parm_overrides_policy(self, n): |
---|
102 | n/a | msg = self.msgmaker(self.typ(self.refold_long_expected[0])) |
---|
103 | n/a | s = self.ioclass() |
---|
104 | n/a | g = self.genclass(s, maxheaderlen=n, |
---|
105 | n/a | policy=self.policy.clone(max_line_length=10)) |
---|
106 | n/a | g.flatten(msg) |
---|
107 | n/a | self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) |
---|
108 | n/a | |
---|
109 | n/a | def length_as_max_line_length_with_refold_none_does_not_fold(self, n): |
---|
110 | n/a | msg = self.msgmaker(self.typ(self.refold_long_expected[0])) |
---|
111 | n/a | s = self.ioclass() |
---|
112 | n/a | g = self.genclass(s, policy=self.policy.clone(refold_source='none', |
---|
113 | n/a | max_line_length=n)) |
---|
114 | n/a | g.flatten(msg) |
---|
115 | n/a | self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[0])) |
---|
116 | n/a | |
---|
117 | n/a | def length_as_max_line_length_with_refold_all_folds(self, n): |
---|
118 | n/a | msg = self.msgmaker(self.typ(self.refold_long_expected[0])) |
---|
119 | n/a | s = self.ioclass() |
---|
120 | n/a | g = self.genclass(s, policy=self.policy.clone(refold_source='all', |
---|
121 | n/a | max_line_length=n)) |
---|
122 | n/a | g.flatten(msg) |
---|
123 | n/a | self.assertEqual(s.getvalue(), self.typ(self.refold_all_expected[n])) |
---|
124 | n/a | |
---|
125 | n/a | def test_crlf_control_via_policy(self): |
---|
126 | n/a | source = "Subject: test\r\n\r\ntest body\r\n" |
---|
127 | n/a | expected = source |
---|
128 | n/a | msg = self.msgmaker(self.typ(source)) |
---|
129 | n/a | s = self.ioclass() |
---|
130 | n/a | g = self.genclass(s, policy=policy.SMTP) |
---|
131 | n/a | g.flatten(msg) |
---|
132 | n/a | self.assertEqual(s.getvalue(), self.typ(expected)) |
---|
133 | n/a | |
---|
134 | n/a | def test_flatten_linesep_overrides_policy(self): |
---|
135 | n/a | source = "Subject: test\n\ntest body\n" |
---|
136 | n/a | expected = source |
---|
137 | n/a | msg = self.msgmaker(self.typ(source)) |
---|
138 | n/a | s = self.ioclass() |
---|
139 | n/a | g = self.genclass(s, policy=policy.SMTP) |
---|
140 | n/a | g.flatten(msg, linesep='\n') |
---|
141 | n/a | self.assertEqual(s.getvalue(), self.typ(expected)) |
---|
142 | n/a | |
---|
143 | n/a | def test_set_mangle_from_via_policy(self): |
---|
144 | n/a | source = textwrap.dedent("""\ |
---|
145 | n/a | Subject: test that |
---|
146 | n/a | from is mangled in the body! |
---|
147 | n/a | |
---|
148 | n/a | From time to time I write a rhyme. |
---|
149 | n/a | """) |
---|
150 | n/a | variants = ( |
---|
151 | n/a | (None, True), |
---|
152 | n/a | (policy.compat32, True), |
---|
153 | n/a | (policy.default, False), |
---|
154 | n/a | (policy.default.clone(mangle_from_=True), True), |
---|
155 | n/a | ) |
---|
156 | n/a | for p, mangle in variants: |
---|
157 | n/a | expected = source.replace('From ', '>From ') if mangle else source |
---|
158 | n/a | with self.subTest(policy=p, mangle_from_=mangle): |
---|
159 | n/a | msg = self.msgmaker(self.typ(source)) |
---|
160 | n/a | s = self.ioclass() |
---|
161 | n/a | g = self.genclass(s, policy=p) |
---|
162 | n/a | g.flatten(msg) |
---|
163 | n/a | self.assertEqual(s.getvalue(), self.typ(expected)) |
---|
164 | n/a | |
---|
165 | n/a | |
---|
166 | n/a | class TestGenerator(TestGeneratorBase, TestEmailBase): |
---|
167 | n/a | |
---|
168 | n/a | msgfunc = staticmethod(message_from_string) |
---|
169 | n/a | genclass = Generator |
---|
170 | n/a | ioclass = io.StringIO |
---|
171 | n/a | typ = str |
---|
172 | n/a | |
---|
173 | n/a | |
---|
174 | n/a | class TestBytesGenerator(TestGeneratorBase, TestEmailBase): |
---|
175 | n/a | |
---|
176 | n/a | msgfunc = staticmethod(message_from_bytes) |
---|
177 | n/a | genclass = BytesGenerator |
---|
178 | n/a | ioclass = io.BytesIO |
---|
179 | n/a | typ = lambda self, x: x.encode('ascii') |
---|
180 | n/a | |
---|
181 | n/a | def test_cte_type_7bit_handles_unknown_8bit(self): |
---|
182 | n/a | source = ("Subject: Maintenant je vous présente mon " |
---|
183 | n/a | "collègue\n\n").encode('utf-8') |
---|
184 | n/a | expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' |
---|
185 | n/a | 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') |
---|
186 | n/a | msg = message_from_bytes(source) |
---|
187 | n/a | s = io.BytesIO() |
---|
188 | n/a | g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) |
---|
189 | n/a | g.flatten(msg) |
---|
190 | n/a | self.assertEqual(s.getvalue(), expected) |
---|
191 | n/a | |
---|
192 | n/a | def test_cte_type_7bit_transforms_8bit_cte(self): |
---|
193 | n/a | source = textwrap.dedent("""\ |
---|
194 | n/a | From: foo@bar.com |
---|
195 | n/a | To: Dinsdale |
---|
196 | n/a | Subject: Nudge nudge, wink, wink |
---|
197 | n/a | Mime-Version: 1.0 |
---|
198 | n/a | Content-Type: text/plain; charset="latin-1" |
---|
199 | n/a | Content-Transfer-Encoding: 8bit |
---|
200 | n/a | |
---|
201 | n/a | oh là là , know what I mean, know what I mean? |
---|
202 | n/a | """).encode('latin1') |
---|
203 | n/a | msg = message_from_bytes(source) |
---|
204 | n/a | expected = textwrap.dedent("""\ |
---|
205 | n/a | From: foo@bar.com |
---|
206 | n/a | To: Dinsdale |
---|
207 | n/a | Subject: Nudge nudge, wink, wink |
---|
208 | n/a | Mime-Version: 1.0 |
---|
209 | n/a | Content-Type: text/plain; charset="iso-8859-1" |
---|
210 | n/a | Content-Transfer-Encoding: quoted-printable |
---|
211 | n/a | |
---|
212 | n/a | oh l=E0 l=E0, know what I mean, know what I mean? |
---|
213 | n/a | """).encode('ascii') |
---|
214 | n/a | s = io.BytesIO() |
---|
215 | n/a | g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', |
---|
216 | n/a | linesep='\n')) |
---|
217 | n/a | g.flatten(msg) |
---|
218 | n/a | self.assertEqual(s.getvalue(), expected) |
---|
219 | n/a | |
---|
220 | n/a | def test_smtputf8_policy(self): |
---|
221 | n/a | msg = EmailMessage() |
---|
222 | n/a | msg['From'] = "Páolo <fÅo@bar.com>" |
---|
223 | n/a | msg['To'] = 'Dinsdale' |
---|
224 | n/a | msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' |
---|
225 | n/a | msg.set_content("oh là là , know what I mean, know what I mean?") |
---|
226 | n/a | expected = textwrap.dedent("""\ |
---|
227 | n/a | From: Páolo <fÅo@bar.com> |
---|
228 | n/a | To: Dinsdale |
---|
229 | n/a | Subject: Nudge nudge, wink, wink \u1F609 |
---|
230 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
231 | n/a | Content-Transfer-Encoding: 8bit |
---|
232 | n/a | MIME-Version: 1.0 |
---|
233 | n/a | |
---|
234 | n/a | oh là là , know what I mean, know what I mean? |
---|
235 | n/a | """).encode('utf-8').replace(b'\n', b'\r\n') |
---|
236 | n/a | s = io.BytesIO() |
---|
237 | n/a | g = BytesGenerator(s, policy=policy.SMTPUTF8) |
---|
238 | n/a | g.flatten(msg) |
---|
239 | n/a | self.assertEqual(s.getvalue(), expected) |
---|
240 | n/a | |
---|
241 | n/a | |
---|
242 | n/a | if __name__ == '__main__': |
---|
243 | n/a | unittest.main() |
---|