|
14 | 14 | # KIND, either express or implied. See the License for the
|
15 | 15 | # specific language governing permissions and limitations
|
16 | 16 | # under the License.
|
17 |
| - |
| 17 | +import email.parser |
| 18 | +import email.iterators |
18 | 19 | import operator
|
19 | 20 | import shutil
|
20 | 21 | from textwrap import dedent
|
|
28 | 29 | import tg
|
29 | 30 | import mock
|
30 | 31 | from tg import tmpl_context as c, app_globals as g
|
| 32 | +import pytest |
31 | 33 |
|
32 | 34 | from ming.odm import FieldProperty, Mapper
|
33 | 35 | from ming.odm import ThreadLocalODMSession
|
@@ -464,29 +466,34 @@ def test_fromaddr_objectid_not_str(self):
|
464 | 466 | return_path, rcpts, body = _client.sendmail.call_args[0]
|
465 | 467 | assert 'From: "Test Admin" <test-admin@users.localhost>' in body
|
466 | 468 |
|
467 |
| - def test_send_email_long_lines_use_quoted_printable(self): |
| 469 | + @pytest.mark.parametrize('bodychars', [ |
| 470 | + '0123456789', # plain ascii is handled different since it doesn't necessarily need to be encoded |
| 471 | + 'Громады стро ', |
| 472 | + ]) |
| 473 | + def test_send_email_long_lines(self, bodychars): |
468 | 474 | with mock.patch.object(mail_tasks.smtp_client, '_client') as _client:
|
469 | 475 | mail_tasks.sendsimplemail(
|
470 | 476 | fromaddr='"По" <foo@bar.com>',
|
471 | 477 | toaddr='blah@blah.com',
|
472 |
| - text=('0123456789' * 100) + '\n\n' + ('Громады стро ' * 100), |
| 478 | + text=bodychars * 100, |
473 | 479 | reply_to=g.noreply,
|
474 | 480 | subject='123451234512345' * 100,
|
475 | 481 | references=['foo@example.com'] * 100, # needs to handle really long headers as well
|
476 | 482 | message_id=h.gen_message_id())
|
477 | 483 | return_path, rcpts, body = _client.sendmail.call_args[0]
|
478 |
| - body = body.split(email_policy.linesep) |
| 484 | + body_lines = body.split(email_policy.linesep) |
479 | 485 |
|
480 |
| - for line in body: |
| 486 | + for line in body_lines: |
481 | 487 | assert len(line) <= MAX_MAIL_LINE_OCTETS
|
482 | 488 |
|
483 |
| - bodystr = ''.join(body) |
484 |
| - # plain text |
485 |
| - assert b64encode(b'012345678901234567890123').decode('utf8') in bodystr |
486 |
| - assert b64encode('Громады стро '.encode('utf8')).decode('utf8') in bodystr |
487 |
| - # html |
488 |
| - assert b64encode(b'<div class="markdown_content"><p>012345678901234567890123').decode('utf8') in bodystr |
489 |
| - assert b64encode('<p>Громады стро '.encode('utf8')).decode('utf8') in bodystr |
| 489 | + msg = email.parser.Parser().parsestr(body) |
| 490 | + plain_subpart = next(email.iterators.typed_subpart_iterator(msg, 'text', 'plain')) |
| 491 | + plain = plain_subpart.get_payload(decode=True).decode('utf-8') |
| 492 | + html_subpart = next(email.iterators.typed_subpart_iterator(msg, 'text', 'html')).get_payload(decode=True) |
| 493 | + html = html_subpart.decode('utf-8') |
| 494 | + |
| 495 | + assert (bodychars + bodychars) in plain |
| 496 | + assert f'<div class="markdown_content"><p>{bodychars}{bodychars}' in html |
490 | 497 |
|
491 | 498 | @td.with_wiki
|
492 | 499 | def test_receive_email_ok(self):
|
|
0 commit comments