1 | n/a | import unittest |
---|
2 | n/a | import textwrap |
---|
3 | n/a | from email import policy, message_from_string |
---|
4 | n/a | from email.message import EmailMessage, MIMEPart |
---|
5 | n/a | from test.test_email import TestEmailBase, parameterize |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | # Helper. |
---|
9 | n/a | def first(iterable): |
---|
10 | n/a | return next(filter(lambda x: x is not None, iterable), None) |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class Test(TestEmailBase): |
---|
14 | n/a | |
---|
15 | n/a | policy = policy.default |
---|
16 | n/a | |
---|
17 | n/a | def test_error_on_setitem_if_max_count_exceeded(self): |
---|
18 | n/a | m = self._str_msg("") |
---|
19 | n/a | m['To'] = 'abc@xyz' |
---|
20 | n/a | with self.assertRaises(ValueError): |
---|
21 | n/a | m['To'] = 'xyz@abc' |
---|
22 | n/a | |
---|
23 | n/a | def test_rfc2043_auto_decoded_and_emailmessage_used(self): |
---|
24 | n/a | m = message_from_string(textwrap.dedent("""\ |
---|
25 | n/a | Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?= |
---|
26 | n/a | From: =?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com> |
---|
27 | n/a | To: "Penelope Pussycat" <"penelope@example.com"> |
---|
28 | n/a | MIME-Version: 1.0 |
---|
29 | n/a | Content-Type: text/plain; charset="utf-8" |
---|
30 | n/a | |
---|
31 | n/a | sample text |
---|
32 | n/a | """), policy=policy.default) |
---|
33 | n/a | self.assertEqual(m['subject'], "Ayons asperges pour le déjeuner") |
---|
34 | n/a | self.assertEqual(m['from'], "Pepé Le Pew <pepe@example.com>") |
---|
35 | n/a | self.assertIsInstance(m, EmailMessage) |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | @parameterize |
---|
39 | n/a | class TestEmailMessageBase: |
---|
40 | n/a | |
---|
41 | n/a | policy = policy.default |
---|
42 | n/a | |
---|
43 | n/a | # The first argument is a triple (related, html, plain) of indices into the |
---|
44 | n/a | # list returned by 'walk' called on a Message constructed from the third. |
---|
45 | n/a | # The indices indicate which part should match the corresponding part-type |
---|
46 | n/a | # when passed to get_body (ie: the "first" part of that type in the |
---|
47 | n/a | # message). The second argument is a list of indices into the 'walk' list |
---|
48 | n/a | # of the attachments that should be returned by a call to |
---|
49 | n/a | # 'iter_attachments'. The third argument is a list of indices into 'walk' |
---|
50 | n/a | # that should be returned by a call to 'iter_parts'. Note that the first |
---|
51 | n/a | # item returned by 'walk' is the Message itself. |
---|
52 | n/a | |
---|
53 | n/a | message_params = { |
---|
54 | n/a | |
---|
55 | n/a | 'empty_message': ( |
---|
56 | n/a | (None, None, 0), |
---|
57 | n/a | (), |
---|
58 | n/a | (), |
---|
59 | n/a | ""), |
---|
60 | n/a | |
---|
61 | n/a | 'non_mime_plain': ( |
---|
62 | n/a | (None, None, 0), |
---|
63 | n/a | (), |
---|
64 | n/a | (), |
---|
65 | n/a | textwrap.dedent("""\ |
---|
66 | n/a | To: foo@example.com |
---|
67 | n/a | |
---|
68 | n/a | simple text body |
---|
69 | n/a | """)), |
---|
70 | n/a | |
---|
71 | n/a | 'mime_non_text': ( |
---|
72 | n/a | (None, None, None), |
---|
73 | n/a | (), |
---|
74 | n/a | (), |
---|
75 | n/a | textwrap.dedent("""\ |
---|
76 | n/a | To: foo@example.com |
---|
77 | n/a | MIME-Version: 1.0 |
---|
78 | n/a | Content-Type: image/jpg |
---|
79 | n/a | |
---|
80 | n/a | bogus body. |
---|
81 | n/a | """)), |
---|
82 | n/a | |
---|
83 | n/a | 'plain_html_alternative': ( |
---|
84 | n/a | (None, 2, 1), |
---|
85 | n/a | (), |
---|
86 | n/a | (1, 2), |
---|
87 | n/a | textwrap.dedent("""\ |
---|
88 | n/a | To: foo@example.com |
---|
89 | n/a | MIME-Version: 1.0 |
---|
90 | n/a | Content-Type: multipart/alternative; boundary="===" |
---|
91 | n/a | |
---|
92 | n/a | preamble |
---|
93 | n/a | |
---|
94 | n/a | --=== |
---|
95 | n/a | Content-Type: text/plain |
---|
96 | n/a | |
---|
97 | n/a | simple body |
---|
98 | n/a | |
---|
99 | n/a | --=== |
---|
100 | n/a | Content-Type: text/html |
---|
101 | n/a | |
---|
102 | n/a | <p>simple body</p> |
---|
103 | n/a | --===-- |
---|
104 | n/a | """)), |
---|
105 | n/a | |
---|
106 | n/a | 'plain_html_mixed': ( |
---|
107 | n/a | (None, 2, 1), |
---|
108 | n/a | (), |
---|
109 | n/a | (1, 2), |
---|
110 | n/a | textwrap.dedent("""\ |
---|
111 | n/a | To: foo@example.com |
---|
112 | n/a | MIME-Version: 1.0 |
---|
113 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
114 | n/a | |
---|
115 | n/a | preamble |
---|
116 | n/a | |
---|
117 | n/a | --=== |
---|
118 | n/a | Content-Type: text/plain |
---|
119 | n/a | |
---|
120 | n/a | simple body |
---|
121 | n/a | |
---|
122 | n/a | --=== |
---|
123 | n/a | Content-Type: text/html |
---|
124 | n/a | |
---|
125 | n/a | <p>simple body</p> |
---|
126 | n/a | |
---|
127 | n/a | --===-- |
---|
128 | n/a | """)), |
---|
129 | n/a | |
---|
130 | n/a | 'plain_html_attachment_mixed': ( |
---|
131 | n/a | (None, None, 1), |
---|
132 | n/a | (2,), |
---|
133 | n/a | (1, 2), |
---|
134 | n/a | textwrap.dedent("""\ |
---|
135 | n/a | To: foo@example.com |
---|
136 | n/a | MIME-Version: 1.0 |
---|
137 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
138 | n/a | |
---|
139 | n/a | --=== |
---|
140 | n/a | Content-Type: text/plain |
---|
141 | n/a | |
---|
142 | n/a | simple body |
---|
143 | n/a | |
---|
144 | n/a | --=== |
---|
145 | n/a | Content-Type: text/html |
---|
146 | n/a | Content-Disposition: attachment |
---|
147 | n/a | |
---|
148 | n/a | <p>simple body</p> |
---|
149 | n/a | |
---|
150 | n/a | --===-- |
---|
151 | n/a | """)), |
---|
152 | n/a | |
---|
153 | n/a | 'html_text_attachment_mixed': ( |
---|
154 | n/a | (None, 2, None), |
---|
155 | n/a | (1,), |
---|
156 | n/a | (1, 2), |
---|
157 | n/a | textwrap.dedent("""\ |
---|
158 | n/a | To: foo@example.com |
---|
159 | n/a | MIME-Version: 1.0 |
---|
160 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
161 | n/a | |
---|
162 | n/a | --=== |
---|
163 | n/a | Content-Type: text/plain |
---|
164 | n/a | Content-Disposition: AtTaChment |
---|
165 | n/a | |
---|
166 | n/a | simple body |
---|
167 | n/a | |
---|
168 | n/a | --=== |
---|
169 | n/a | Content-Type: text/html |
---|
170 | n/a | |
---|
171 | n/a | <p>simple body</p> |
---|
172 | n/a | |
---|
173 | n/a | --===-- |
---|
174 | n/a | """)), |
---|
175 | n/a | |
---|
176 | n/a | 'html_text_attachment_inline_mixed': ( |
---|
177 | n/a | (None, 2, 1), |
---|
178 | n/a | (), |
---|
179 | n/a | (1, 2), |
---|
180 | n/a | textwrap.dedent("""\ |
---|
181 | n/a | To: foo@example.com |
---|
182 | n/a | MIME-Version: 1.0 |
---|
183 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
184 | n/a | |
---|
185 | n/a | --=== |
---|
186 | n/a | Content-Type: text/plain |
---|
187 | n/a | Content-Disposition: InLine |
---|
188 | n/a | |
---|
189 | n/a | simple body |
---|
190 | n/a | |
---|
191 | n/a | --=== |
---|
192 | n/a | Content-Type: text/html |
---|
193 | n/a | Content-Disposition: inline |
---|
194 | n/a | |
---|
195 | n/a | <p>simple body</p> |
---|
196 | n/a | |
---|
197 | n/a | --===-- |
---|
198 | n/a | """)), |
---|
199 | n/a | |
---|
200 | n/a | # RFC 2387 |
---|
201 | n/a | 'related': ( |
---|
202 | n/a | (0, 1, None), |
---|
203 | n/a | (2,), |
---|
204 | n/a | (1, 2), |
---|
205 | n/a | textwrap.dedent("""\ |
---|
206 | n/a | To: foo@example.com |
---|
207 | n/a | MIME-Version: 1.0 |
---|
208 | n/a | Content-Type: multipart/related; boundary="==="; type=text/html |
---|
209 | n/a | |
---|
210 | n/a | --=== |
---|
211 | n/a | Content-Type: text/html |
---|
212 | n/a | |
---|
213 | n/a | <p>simple body</p> |
---|
214 | n/a | |
---|
215 | n/a | --=== |
---|
216 | n/a | Content-Type: image/jpg |
---|
217 | n/a | Content-ID: <image1> |
---|
218 | n/a | |
---|
219 | n/a | bogus data |
---|
220 | n/a | |
---|
221 | n/a | --===-- |
---|
222 | n/a | """)), |
---|
223 | n/a | |
---|
224 | n/a | # This message structure will probably never be seen in the wild, but |
---|
225 | n/a | # it proves we distinguish between text parts based on 'start'. The |
---|
226 | n/a | # content would not, of course, actually work :) |
---|
227 | n/a | 'related_with_start': ( |
---|
228 | n/a | (0, 2, None), |
---|
229 | n/a | (1,), |
---|
230 | n/a | (1, 2), |
---|
231 | n/a | textwrap.dedent("""\ |
---|
232 | n/a | To: foo@example.com |
---|
233 | n/a | MIME-Version: 1.0 |
---|
234 | n/a | Content-Type: multipart/related; boundary="==="; type=text/html; |
---|
235 | n/a | start="<body>" |
---|
236 | n/a | |
---|
237 | n/a | --=== |
---|
238 | n/a | Content-Type: text/html |
---|
239 | n/a | Content-ID: <include> |
---|
240 | n/a | |
---|
241 | n/a | useless text |
---|
242 | n/a | |
---|
243 | n/a | --=== |
---|
244 | n/a | Content-Type: text/html |
---|
245 | n/a | Content-ID: <body> |
---|
246 | n/a | |
---|
247 | n/a | <p>simple body</p> |
---|
248 | n/a | <!--#include file="<include>"--> |
---|
249 | n/a | |
---|
250 | n/a | --===-- |
---|
251 | n/a | """)), |
---|
252 | n/a | |
---|
253 | n/a | |
---|
254 | n/a | 'mixed_alternative_plain_related': ( |
---|
255 | n/a | (3, 4, 2), |
---|
256 | n/a | (6, 7), |
---|
257 | n/a | (1, 6, 7), |
---|
258 | n/a | textwrap.dedent("""\ |
---|
259 | n/a | To: foo@example.com |
---|
260 | n/a | MIME-Version: 1.0 |
---|
261 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
262 | n/a | |
---|
263 | n/a | --=== |
---|
264 | n/a | Content-Type: multipart/alternative; boundary="+++" |
---|
265 | n/a | |
---|
266 | n/a | --+++ |
---|
267 | n/a | Content-Type: text/plain |
---|
268 | n/a | |
---|
269 | n/a | simple body |
---|
270 | n/a | |
---|
271 | n/a | --+++ |
---|
272 | n/a | Content-Type: multipart/related; boundary="___" |
---|
273 | n/a | |
---|
274 | n/a | --___ |
---|
275 | n/a | Content-Type: text/html |
---|
276 | n/a | |
---|
277 | n/a | <p>simple body</p> |
---|
278 | n/a | |
---|
279 | n/a | --___ |
---|
280 | n/a | Content-Type: image/jpg |
---|
281 | n/a | Content-ID: <image1@cid> |
---|
282 | n/a | |
---|
283 | n/a | bogus jpg body |
---|
284 | n/a | |
---|
285 | n/a | --___-- |
---|
286 | n/a | |
---|
287 | n/a | --+++-- |
---|
288 | n/a | |
---|
289 | n/a | --=== |
---|
290 | n/a | Content-Type: image/jpg |
---|
291 | n/a | Content-Disposition: attachment |
---|
292 | n/a | |
---|
293 | n/a | bogus jpg body |
---|
294 | n/a | |
---|
295 | n/a | --=== |
---|
296 | n/a | Content-Type: image/jpg |
---|
297 | n/a | Content-Disposition: AttacHmenT |
---|
298 | n/a | |
---|
299 | n/a | another bogus jpg body |
---|
300 | n/a | |
---|
301 | n/a | --===-- |
---|
302 | n/a | """)), |
---|
303 | n/a | |
---|
304 | n/a | # This structure suggested by Stephen J. Turnbull...may not exist/be |
---|
305 | n/a | # supported in the wild, but we want to support it. |
---|
306 | n/a | 'mixed_related_alternative_plain_html': ( |
---|
307 | n/a | (1, 4, 3), |
---|
308 | n/a | (6, 7), |
---|
309 | n/a | (1, 6, 7), |
---|
310 | n/a | textwrap.dedent("""\ |
---|
311 | n/a | To: foo@example.com |
---|
312 | n/a | MIME-Version: 1.0 |
---|
313 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
314 | n/a | |
---|
315 | n/a | --=== |
---|
316 | n/a | Content-Type: multipart/related; boundary="+++" |
---|
317 | n/a | |
---|
318 | n/a | --+++ |
---|
319 | n/a | Content-Type: multipart/alternative; boundary="___" |
---|
320 | n/a | |
---|
321 | n/a | --___ |
---|
322 | n/a | Content-Type: text/plain |
---|
323 | n/a | |
---|
324 | n/a | simple body |
---|
325 | n/a | |
---|
326 | n/a | --___ |
---|
327 | n/a | Content-Type: text/html |
---|
328 | n/a | |
---|
329 | n/a | <p>simple body</p> |
---|
330 | n/a | |
---|
331 | n/a | --___-- |
---|
332 | n/a | |
---|
333 | n/a | --+++ |
---|
334 | n/a | Content-Type: image/jpg |
---|
335 | n/a | Content-ID: <image1@cid> |
---|
336 | n/a | |
---|
337 | n/a | bogus jpg body |
---|
338 | n/a | |
---|
339 | n/a | --+++-- |
---|
340 | n/a | |
---|
341 | n/a | --=== |
---|
342 | n/a | Content-Type: image/jpg |
---|
343 | n/a | Content-Disposition: attachment |
---|
344 | n/a | |
---|
345 | n/a | bogus jpg body |
---|
346 | n/a | |
---|
347 | n/a | --=== |
---|
348 | n/a | Content-Type: image/jpg |
---|
349 | n/a | Content-Disposition: attachment |
---|
350 | n/a | |
---|
351 | n/a | another bogus jpg body |
---|
352 | n/a | |
---|
353 | n/a | --===-- |
---|
354 | n/a | """)), |
---|
355 | n/a | |
---|
356 | n/a | # Same thing, but proving we only look at the root part, which is the |
---|
357 | n/a | # first one if there isn't any start parameter. That is, this is a |
---|
358 | n/a | # broken related. |
---|
359 | n/a | 'mixed_related_alternative_plain_html_wrong_order': ( |
---|
360 | n/a | (1, None, None), |
---|
361 | n/a | (6, 7), |
---|
362 | n/a | (1, 6, 7), |
---|
363 | n/a | textwrap.dedent("""\ |
---|
364 | n/a | To: foo@example.com |
---|
365 | n/a | MIME-Version: 1.0 |
---|
366 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
367 | n/a | |
---|
368 | n/a | --=== |
---|
369 | n/a | Content-Type: multipart/related; boundary="+++" |
---|
370 | n/a | |
---|
371 | n/a | --+++ |
---|
372 | n/a | Content-Type: image/jpg |
---|
373 | n/a | Content-ID: <image1@cid> |
---|
374 | n/a | |
---|
375 | n/a | bogus jpg body |
---|
376 | n/a | |
---|
377 | n/a | --+++ |
---|
378 | n/a | Content-Type: multipart/alternative; boundary="___" |
---|
379 | n/a | |
---|
380 | n/a | --___ |
---|
381 | n/a | Content-Type: text/plain |
---|
382 | n/a | |
---|
383 | n/a | simple body |
---|
384 | n/a | |
---|
385 | n/a | --___ |
---|
386 | n/a | Content-Type: text/html |
---|
387 | n/a | |
---|
388 | n/a | <p>simple body</p> |
---|
389 | n/a | |
---|
390 | n/a | --___-- |
---|
391 | n/a | |
---|
392 | n/a | --+++-- |
---|
393 | n/a | |
---|
394 | n/a | --=== |
---|
395 | n/a | Content-Type: image/jpg |
---|
396 | n/a | Content-Disposition: attachment |
---|
397 | n/a | |
---|
398 | n/a | bogus jpg body |
---|
399 | n/a | |
---|
400 | n/a | --=== |
---|
401 | n/a | Content-Type: image/jpg |
---|
402 | n/a | Content-Disposition: attachment |
---|
403 | n/a | |
---|
404 | n/a | another bogus jpg body |
---|
405 | n/a | |
---|
406 | n/a | --===-- |
---|
407 | n/a | """)), |
---|
408 | n/a | |
---|
409 | n/a | 'message_rfc822': ( |
---|
410 | n/a | (None, None, None), |
---|
411 | n/a | (), |
---|
412 | n/a | (), |
---|
413 | n/a | textwrap.dedent("""\ |
---|
414 | n/a | To: foo@example.com |
---|
415 | n/a | MIME-Version: 1.0 |
---|
416 | n/a | Content-Type: message/rfc822 |
---|
417 | n/a | |
---|
418 | n/a | To: bar@example.com |
---|
419 | n/a | From: robot@examp.com |
---|
420 | n/a | |
---|
421 | n/a | this is a message body. |
---|
422 | n/a | """)), |
---|
423 | n/a | |
---|
424 | n/a | 'mixed_text_message_rfc822': ( |
---|
425 | n/a | (None, None, 1), |
---|
426 | n/a | (2,), |
---|
427 | n/a | (1, 2), |
---|
428 | n/a | textwrap.dedent("""\ |
---|
429 | n/a | To: foo@example.com |
---|
430 | n/a | MIME-Version: 1.0 |
---|
431 | n/a | Content-Type: multipart/mixed; boundary="===" |
---|
432 | n/a | |
---|
433 | n/a | --=== |
---|
434 | n/a | Content-Type: text/plain |
---|
435 | n/a | |
---|
436 | n/a | Your message has bounced, ser. |
---|
437 | n/a | |
---|
438 | n/a | --=== |
---|
439 | n/a | Content-Type: message/rfc822 |
---|
440 | n/a | |
---|
441 | n/a | To: bar@example.com |
---|
442 | n/a | From: robot@examp.com |
---|
443 | n/a | |
---|
444 | n/a | this is a message body. |
---|
445 | n/a | |
---|
446 | n/a | --===-- |
---|
447 | n/a | """)), |
---|
448 | n/a | |
---|
449 | n/a | } |
---|
450 | n/a | |
---|
451 | n/a | def message_as_get_body(self, body_parts, attachments, parts, msg): |
---|
452 | n/a | m = self._str_msg(msg) |
---|
453 | n/a | allparts = list(m.walk()) |
---|
454 | n/a | expected = [None if n is None else allparts[n] for n in body_parts] |
---|
455 | n/a | related = 0; html = 1; plain = 2 |
---|
456 | n/a | self.assertEqual(m.get_body(), first(expected)) |
---|
457 | n/a | self.assertEqual(m.get_body(preferencelist=( |
---|
458 | n/a | 'related', 'html', 'plain')), |
---|
459 | n/a | first(expected)) |
---|
460 | n/a | self.assertEqual(m.get_body(preferencelist=('related', 'html')), |
---|
461 | n/a | first(expected[related:html+1])) |
---|
462 | n/a | self.assertEqual(m.get_body(preferencelist=('related', 'plain')), |
---|
463 | n/a | first([expected[related], expected[plain]])) |
---|
464 | n/a | self.assertEqual(m.get_body(preferencelist=('html', 'plain')), |
---|
465 | n/a | first(expected[html:plain+1])) |
---|
466 | n/a | self.assertEqual(m.get_body(preferencelist=['related']), |
---|
467 | n/a | expected[related]) |
---|
468 | n/a | self.assertEqual(m.get_body(preferencelist=['html']), expected[html]) |
---|
469 | n/a | self.assertEqual(m.get_body(preferencelist=['plain']), expected[plain]) |
---|
470 | n/a | self.assertEqual(m.get_body(preferencelist=('plain', 'html')), |
---|
471 | n/a | first(expected[plain:html-1:-1])) |
---|
472 | n/a | self.assertEqual(m.get_body(preferencelist=('plain', 'related')), |
---|
473 | n/a | first([expected[plain], expected[related]])) |
---|
474 | n/a | self.assertEqual(m.get_body(preferencelist=('html', 'related')), |
---|
475 | n/a | first(expected[html::-1])) |
---|
476 | n/a | self.assertEqual(m.get_body(preferencelist=('plain', 'html', 'related')), |
---|
477 | n/a | first(expected[::-1])) |
---|
478 | n/a | self.assertEqual(m.get_body(preferencelist=('html', 'plain', 'related')), |
---|
479 | n/a | first([expected[html], |
---|
480 | n/a | expected[plain], |
---|
481 | n/a | expected[related]])) |
---|
482 | n/a | |
---|
483 | n/a | def message_as_iter_attachment(self, body_parts, attachments, parts, msg): |
---|
484 | n/a | m = self._str_msg(msg) |
---|
485 | n/a | allparts = list(m.walk()) |
---|
486 | n/a | attachments = [allparts[n] for n in attachments] |
---|
487 | n/a | self.assertEqual(list(m.iter_attachments()), attachments) |
---|
488 | n/a | |
---|
489 | n/a | def message_as_iter_parts(self, body_parts, attachments, parts, msg): |
---|
490 | n/a | m = self._str_msg(msg) |
---|
491 | n/a | allparts = list(m.walk()) |
---|
492 | n/a | parts = [allparts[n] for n in parts] |
---|
493 | n/a | self.assertEqual(list(m.iter_parts()), parts) |
---|
494 | n/a | |
---|
495 | n/a | class _TestContentManager: |
---|
496 | n/a | def get_content(self, msg, *args, **kw): |
---|
497 | n/a | return msg, args, kw |
---|
498 | n/a | def set_content(self, msg, *args, **kw): |
---|
499 | n/a | self.msg = msg |
---|
500 | n/a | self.args = args |
---|
501 | n/a | self.kw = kw |
---|
502 | n/a | |
---|
503 | n/a | def test_get_content_with_cm(self): |
---|
504 | n/a | m = self._str_msg('') |
---|
505 | n/a | cm = self._TestContentManager() |
---|
506 | n/a | self.assertEqual(m.get_content(content_manager=cm), (m, (), {})) |
---|
507 | n/a | msg, args, kw = m.get_content('foo', content_manager=cm, bar=1, k=2) |
---|
508 | n/a | self.assertEqual(msg, m) |
---|
509 | n/a | self.assertEqual(args, ('foo',)) |
---|
510 | n/a | self.assertEqual(kw, dict(bar=1, k=2)) |
---|
511 | n/a | |
---|
512 | n/a | def test_get_content_default_cm_comes_from_policy(self): |
---|
513 | n/a | p = policy.default.clone(content_manager=self._TestContentManager()) |
---|
514 | n/a | m = self._str_msg('', policy=p) |
---|
515 | n/a | self.assertEqual(m.get_content(), (m, (), {})) |
---|
516 | n/a | msg, args, kw = m.get_content('foo', bar=1, k=2) |
---|
517 | n/a | self.assertEqual(msg, m) |
---|
518 | n/a | self.assertEqual(args, ('foo',)) |
---|
519 | n/a | self.assertEqual(kw, dict(bar=1, k=2)) |
---|
520 | n/a | |
---|
521 | n/a | def test_set_content_with_cm(self): |
---|
522 | n/a | m = self._str_msg('') |
---|
523 | n/a | cm = self._TestContentManager() |
---|
524 | n/a | m.set_content(content_manager=cm) |
---|
525 | n/a | self.assertEqual(cm.msg, m) |
---|
526 | n/a | self.assertEqual(cm.args, ()) |
---|
527 | n/a | self.assertEqual(cm.kw, {}) |
---|
528 | n/a | m.set_content('foo', content_manager=cm, bar=1, k=2) |
---|
529 | n/a | self.assertEqual(cm.msg, m) |
---|
530 | n/a | self.assertEqual(cm.args, ('foo',)) |
---|
531 | n/a | self.assertEqual(cm.kw, dict(bar=1, k=2)) |
---|
532 | n/a | |
---|
533 | n/a | def test_set_content_default_cm_comes_from_policy(self): |
---|
534 | n/a | cm = self._TestContentManager() |
---|
535 | n/a | p = policy.default.clone(content_manager=cm) |
---|
536 | n/a | m = self._str_msg('', policy=p) |
---|
537 | n/a | m.set_content() |
---|
538 | n/a | self.assertEqual(cm.msg, m) |
---|
539 | n/a | self.assertEqual(cm.args, ()) |
---|
540 | n/a | self.assertEqual(cm.kw, {}) |
---|
541 | n/a | m.set_content('foo', bar=1, k=2) |
---|
542 | n/a | self.assertEqual(cm.msg, m) |
---|
543 | n/a | self.assertEqual(cm.args, ('foo',)) |
---|
544 | n/a | self.assertEqual(cm.kw, dict(bar=1, k=2)) |
---|
545 | n/a | |
---|
546 | n/a | # outcome is whether xxx_method should raise ValueError error when called |
---|
547 | n/a | # on multipart/subtype. Blank outcome means it depends on xxx (add |
---|
548 | n/a | # succeeds, make raises). Note: 'none' means there are content-type |
---|
549 | n/a | # headers but payload is None...this happening in practice would be very |
---|
550 | n/a | # unusual, so treating it as if there were content seems reasonable. |
---|
551 | n/a | # method subtype outcome |
---|
552 | n/a | subtype_params = ( |
---|
553 | n/a | ('related', 'no_content', 'succeeds'), |
---|
554 | n/a | ('related', 'none', 'succeeds'), |
---|
555 | n/a | ('related', 'plain', 'succeeds'), |
---|
556 | n/a | ('related', 'related', ''), |
---|
557 | n/a | ('related', 'alternative', 'raises'), |
---|
558 | n/a | ('related', 'mixed', 'raises'), |
---|
559 | n/a | ('alternative', 'no_content', 'succeeds'), |
---|
560 | n/a | ('alternative', 'none', 'succeeds'), |
---|
561 | n/a | ('alternative', 'plain', 'succeeds'), |
---|
562 | n/a | ('alternative', 'related', 'succeeds'), |
---|
563 | n/a | ('alternative', 'alternative', ''), |
---|
564 | n/a | ('alternative', 'mixed', 'raises'), |
---|
565 | n/a | ('mixed', 'no_content', 'succeeds'), |
---|
566 | n/a | ('mixed', 'none', 'succeeds'), |
---|
567 | n/a | ('mixed', 'plain', 'succeeds'), |
---|
568 | n/a | ('mixed', 'related', 'succeeds'), |
---|
569 | n/a | ('mixed', 'alternative', 'succeeds'), |
---|
570 | n/a | ('mixed', 'mixed', ''), |
---|
571 | n/a | ) |
---|
572 | n/a | |
---|
573 | n/a | def _make_subtype_test_message(self, subtype): |
---|
574 | n/a | m = self.message() |
---|
575 | n/a | payload = None |
---|
576 | n/a | msg_headers = [ |
---|
577 | n/a | ('To', 'foo@bar.com'), |
---|
578 | n/a | ('From', 'bar@foo.com'), |
---|
579 | n/a | ] |
---|
580 | n/a | if subtype != 'no_content': |
---|
581 | n/a | ('content-shadow', 'Logrus'), |
---|
582 | n/a | msg_headers.append(('X-Random-Header', 'Corwin')) |
---|
583 | n/a | if subtype == 'text': |
---|
584 | n/a | payload = '' |
---|
585 | n/a | msg_headers.append(('Content-Type', 'text/plain')) |
---|
586 | n/a | m.set_payload('') |
---|
587 | n/a | elif subtype != 'no_content': |
---|
588 | n/a | payload = [] |
---|
589 | n/a | msg_headers.append(('Content-Type', 'multipart/' + subtype)) |
---|
590 | n/a | msg_headers.append(('X-Trump', 'Random')) |
---|
591 | n/a | m.set_payload(payload) |
---|
592 | n/a | for name, value in msg_headers: |
---|
593 | n/a | m[name] = value |
---|
594 | n/a | return m, msg_headers, payload |
---|
595 | n/a | |
---|
596 | n/a | def _check_disallowed_subtype_raises(self, m, method_name, subtype, method): |
---|
597 | n/a | with self.assertRaises(ValueError) as ar: |
---|
598 | n/a | getattr(m, method)() |
---|
599 | n/a | exc_text = str(ar.exception) |
---|
600 | n/a | self.assertIn(subtype, exc_text) |
---|
601 | n/a | self.assertIn(method_name, exc_text) |
---|
602 | n/a | |
---|
603 | n/a | def _check_make_multipart(self, m, msg_headers, payload): |
---|
604 | n/a | count = 0 |
---|
605 | n/a | for name, value in msg_headers: |
---|
606 | n/a | if not name.lower().startswith('content-'): |
---|
607 | n/a | self.assertEqual(m[name], value) |
---|
608 | n/a | count += 1 |
---|
609 | n/a | self.assertEqual(len(m), count+1) # +1 for new Content-Type |
---|
610 | n/a | part = next(m.iter_parts()) |
---|
611 | n/a | count = 0 |
---|
612 | n/a | for name, value in msg_headers: |
---|
613 | n/a | if name.lower().startswith('content-'): |
---|
614 | n/a | self.assertEqual(part[name], value) |
---|
615 | n/a | count += 1 |
---|
616 | n/a | self.assertEqual(len(part), count) |
---|
617 | n/a | self.assertEqual(part.get_payload(), payload) |
---|
618 | n/a | |
---|
619 | n/a | def subtype_as_make(self, method, subtype, outcome): |
---|
620 | n/a | m, msg_headers, payload = self._make_subtype_test_message(subtype) |
---|
621 | n/a | make_method = 'make_' + method |
---|
622 | n/a | if outcome in ('', 'raises'): |
---|
623 | n/a | self._check_disallowed_subtype_raises(m, method, subtype, make_method) |
---|
624 | n/a | return |
---|
625 | n/a | getattr(m, make_method)() |
---|
626 | n/a | self.assertEqual(m.get_content_maintype(), 'multipart') |
---|
627 | n/a | self.assertEqual(m.get_content_subtype(), method) |
---|
628 | n/a | if subtype == 'no_content': |
---|
629 | n/a | self.assertEqual(len(m.get_payload()), 0) |
---|
630 | n/a | self.assertEqual(m.items(), |
---|
631 | n/a | msg_headers + [('Content-Type', |
---|
632 | n/a | 'multipart/'+method)]) |
---|
633 | n/a | else: |
---|
634 | n/a | self.assertEqual(len(m.get_payload()), 1) |
---|
635 | n/a | self._check_make_multipart(m, msg_headers, payload) |
---|
636 | n/a | |
---|
637 | n/a | def subtype_as_make_with_boundary(self, method, subtype, outcome): |
---|
638 | n/a | # Doing all variation is a bit of overkill... |
---|
639 | n/a | m = self.message() |
---|
640 | n/a | if outcome in ('', 'raises'): |
---|
641 | n/a | m['Content-Type'] = 'multipart/' + subtype |
---|
642 | n/a | with self.assertRaises(ValueError) as cm: |
---|
643 | n/a | getattr(m, 'make_' + method)() |
---|
644 | n/a | return |
---|
645 | n/a | if subtype == 'plain': |
---|
646 | n/a | m['Content-Type'] = 'text/plain' |
---|
647 | n/a | elif subtype != 'no_content': |
---|
648 | n/a | m['Content-Type'] = 'multipart/' + subtype |
---|
649 | n/a | getattr(m, 'make_' + method)(boundary="abc") |
---|
650 | n/a | self.assertTrue(m.is_multipart()) |
---|
651 | n/a | self.assertEqual(m.get_boundary(), 'abc') |
---|
652 | n/a | |
---|
653 | n/a | def test_policy_on_part_made_by_make_comes_from_message(self): |
---|
654 | n/a | for method in ('make_related', 'make_alternative', 'make_mixed'): |
---|
655 | n/a | m = self.message(policy=self.policy.clone(content_manager='foo')) |
---|
656 | n/a | m['Content-Type'] = 'text/plain' |
---|
657 | n/a | getattr(m, method)() |
---|
658 | n/a | self.assertEqual(m.get_payload(0).policy.content_manager, 'foo') |
---|
659 | n/a | |
---|
660 | n/a | class _TestSetContentManager: |
---|
661 | n/a | def set_content(self, msg, content, *args, **kw): |
---|
662 | n/a | msg['Content-Type'] = 'text/plain' |
---|
663 | n/a | msg.set_payload(content) |
---|
664 | n/a | |
---|
665 | n/a | def subtype_as_add(self, method, subtype, outcome): |
---|
666 | n/a | m, msg_headers, payload = self._make_subtype_test_message(subtype) |
---|
667 | n/a | cm = self._TestSetContentManager() |
---|
668 | n/a | add_method = 'add_attachment' if method=='mixed' else 'add_' + method |
---|
669 | n/a | if outcome == 'raises': |
---|
670 | n/a | self._check_disallowed_subtype_raises(m, method, subtype, add_method) |
---|
671 | n/a | return |
---|
672 | n/a | getattr(m, add_method)('test', content_manager=cm) |
---|
673 | n/a | self.assertEqual(m.get_content_maintype(), 'multipart') |
---|
674 | n/a | self.assertEqual(m.get_content_subtype(), method) |
---|
675 | n/a | if method == subtype or subtype == 'no_content': |
---|
676 | n/a | self.assertEqual(len(m.get_payload()), 1) |
---|
677 | n/a | for name, value in msg_headers: |
---|
678 | n/a | self.assertEqual(m[name], value) |
---|
679 | n/a | part = m.get_payload()[0] |
---|
680 | n/a | else: |
---|
681 | n/a | self.assertEqual(len(m.get_payload()), 2) |
---|
682 | n/a | self._check_make_multipart(m, msg_headers, payload) |
---|
683 | n/a | part = m.get_payload()[1] |
---|
684 | n/a | self.assertEqual(part.get_content_type(), 'text/plain') |
---|
685 | n/a | self.assertEqual(part.get_payload(), 'test') |
---|
686 | n/a | if method=='mixed': |
---|
687 | n/a | self.assertEqual(part['Content-Disposition'], 'attachment') |
---|
688 | n/a | elif method=='related': |
---|
689 | n/a | self.assertEqual(part['Content-Disposition'], 'inline') |
---|
690 | n/a | else: |
---|
691 | n/a | # Otherwise we don't guess. |
---|
692 | n/a | self.assertIsNone(part['Content-Disposition']) |
---|
693 | n/a | |
---|
694 | n/a | class _TestSetRaisingContentManager: |
---|
695 | n/a | def set_content(self, msg, content, *args, **kw): |
---|
696 | n/a | raise Exception('test') |
---|
697 | n/a | |
---|
698 | n/a | def test_default_content_manager_for_add_comes_from_policy(self): |
---|
699 | n/a | cm = self._TestSetRaisingContentManager() |
---|
700 | n/a | m = self.message(policy=self.policy.clone(content_manager=cm)) |
---|
701 | n/a | for method in ('add_related', 'add_alternative', 'add_attachment'): |
---|
702 | n/a | with self.assertRaises(Exception) as ar: |
---|
703 | n/a | getattr(m, method)('') |
---|
704 | n/a | self.assertEqual(str(ar.exception), 'test') |
---|
705 | n/a | |
---|
706 | n/a | def message_as_clear(self, body_parts, attachments, parts, msg): |
---|
707 | n/a | m = self._str_msg(msg) |
---|
708 | n/a | m.clear() |
---|
709 | n/a | self.assertEqual(len(m), 0) |
---|
710 | n/a | self.assertEqual(list(m.items()), []) |
---|
711 | n/a | self.assertIsNone(m.get_payload()) |
---|
712 | n/a | self.assertEqual(list(m.iter_parts()), []) |
---|
713 | n/a | |
---|
714 | n/a | def message_as_clear_content(self, body_parts, attachments, parts, msg): |
---|
715 | n/a | m = self._str_msg(msg) |
---|
716 | n/a | expected_headers = [h for h in m.keys() |
---|
717 | n/a | if not h.lower().startswith('content-')] |
---|
718 | n/a | m.clear_content() |
---|
719 | n/a | self.assertEqual(list(m.keys()), expected_headers) |
---|
720 | n/a | self.assertIsNone(m.get_payload()) |
---|
721 | n/a | self.assertEqual(list(m.iter_parts()), []) |
---|
722 | n/a | |
---|
723 | n/a | def test_is_attachment(self): |
---|
724 | n/a | m = self._make_message() |
---|
725 | n/a | self.assertFalse(m.is_attachment()) |
---|
726 | n/a | m['Content-Disposition'] = 'inline' |
---|
727 | n/a | self.assertFalse(m.is_attachment()) |
---|
728 | n/a | m.replace_header('Content-Disposition', 'attachment') |
---|
729 | n/a | self.assertTrue(m.is_attachment()) |
---|
730 | n/a | m.replace_header('Content-Disposition', 'AtTachMent') |
---|
731 | n/a | self.assertTrue(m.is_attachment()) |
---|
732 | n/a | m.set_param('filename', 'abc.png', 'Content-Disposition') |
---|
733 | n/a | self.assertTrue(m.is_attachment()) |
---|
734 | n/a | |
---|
735 | n/a | def test_iter_attachments_mutation(self): |
---|
736 | n/a | # We had a bug where iter_attachments was mutating the list. |
---|
737 | n/a | m = self._make_message() |
---|
738 | n/a | m.set_content('arbitrary text as main part') |
---|
739 | n/a | m.add_related('more text as a related part') |
---|
740 | n/a | m.add_related('yet more text as a second "attachment"') |
---|
741 | n/a | orig = m.get_payload().copy() |
---|
742 | n/a | self.assertEqual(len(list(m.iter_attachments())), 2) |
---|
743 | n/a | self.assertEqual(m.get_payload(), orig) |
---|
744 | n/a | |
---|
745 | n/a | |
---|
746 | n/a | class TestEmailMessage(TestEmailMessageBase, TestEmailBase): |
---|
747 | n/a | message = EmailMessage |
---|
748 | n/a | |
---|
749 | n/a | def test_set_content_adds_MIME_Version(self): |
---|
750 | n/a | m = self._str_msg('') |
---|
751 | n/a | cm = self._TestContentManager() |
---|
752 | n/a | self.assertNotIn('MIME-Version', m) |
---|
753 | n/a | m.set_content(content_manager=cm) |
---|
754 | n/a | self.assertEqual(m['MIME-Version'], '1.0') |
---|
755 | n/a | |
---|
756 | n/a | class _MIME_Version_adding_CM: |
---|
757 | n/a | def set_content(self, msg, *args, **kw): |
---|
758 | n/a | msg['MIME-Version'] = '1.0' |
---|
759 | n/a | |
---|
760 | n/a | def test_set_content_does_not_duplicate_MIME_Version(self): |
---|
761 | n/a | m = self._str_msg('') |
---|
762 | n/a | cm = self._MIME_Version_adding_CM() |
---|
763 | n/a | self.assertNotIn('MIME-Version', m) |
---|
764 | n/a | m.set_content(content_manager=cm) |
---|
765 | n/a | self.assertEqual(m['MIME-Version'], '1.0') |
---|
766 | n/a | |
---|
767 | n/a | def test_as_string_uses_max_header_length_by_default(self): |
---|
768 | n/a | m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n') |
---|
769 | n/a | self.assertEqual(len(m.as_string().strip().splitlines()), 3) |
---|
770 | n/a | |
---|
771 | n/a | def test_as_string_allows_maxheaderlen(self): |
---|
772 | n/a | m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n') |
---|
773 | n/a | self.assertEqual(len(m.as_string(maxheaderlen=0).strip().splitlines()), |
---|
774 | n/a | 1) |
---|
775 | n/a | self.assertEqual(len(m.as_string(maxheaderlen=34).strip().splitlines()), |
---|
776 | n/a | 6) |
---|
777 | n/a | |
---|
778 | n/a | def test_str_defaults_to_policy_max_line_length(self): |
---|
779 | n/a | m = self._str_msg('Subject: long line' + ' ab'*50 + '\n\n') |
---|
780 | n/a | self.assertEqual(len(str(m).strip().splitlines()), 3) |
---|
781 | n/a | |
---|
782 | n/a | def test_str_defaults_to_utf8(self): |
---|
783 | n/a | m = EmailMessage() |
---|
784 | n/a | m['Subject'] = 'unicöde' |
---|
785 | n/a | self.assertEqual(str(m), 'Subject: unicöde\n\n') |
---|
786 | n/a | |
---|
787 | n/a | |
---|
788 | n/a | class TestMIMEPart(TestEmailMessageBase, TestEmailBase): |
---|
789 | n/a | # Doing the full test run here may seem a bit redundant, since the two |
---|
790 | n/a | # classes are almost identical. But what if they drift apart? So we do |
---|
791 | n/a | # the full tests so that any future drift doesn't introduce bugs. |
---|
792 | n/a | message = MIMEPart |
---|
793 | n/a | |
---|
794 | n/a | def test_set_content_does_not_add_MIME_Version(self): |
---|
795 | n/a | m = self._str_msg('') |
---|
796 | n/a | cm = self._TestContentManager() |
---|
797 | n/a | self.assertNotIn('MIME-Version', m) |
---|
798 | n/a | m.set_content(content_manager=cm) |
---|
799 | n/a | self.assertNotIn('MIME-Version', m) |
---|
800 | n/a | |
---|
801 | n/a | |
---|
802 | n/a | if __name__ == '__main__': |
---|
803 | n/a | unittest.main() |
---|