Exemplo n.º 1
0
def test_extra_headers():
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body="hello",
                  extra_headers={'X-Extra-Header': 'Yes'})
    assert 'X-Extra-Header: Yes' in msg.as_string()
Exemplo n.º 2
0
def test_unicode_sender():
    msg = Message(subject="subject",
                  sender='ÄÜÖ → ✓ <*****@*****.**>>',
                  recipients=["*****@*****.**"])

    assert 'From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <*****@*****.**>' in msg.as_string(
    )
Exemplo n.º 3
0
def test_emails_are_sanitized():
    msg = Message(subject="testing",
                  sender="sender\r\[email protected]",
                  reply_to="reply_to\r\[email protected]",
                  recipients=["recipient\r\[email protected]"])
    assert '*****@*****.**' in msg.as_string()
    assert '*****@*****.**' in msg.as_string()
    assert '*****@*****.**' in msg.as_string()
Exemplo n.º 4
0
def test_plain_message():
    plain_text = "Hello Joe,\nHow are you?"
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body=plain_text)
    assert plain_text == msg.body
    assert 'Content-Type: text/plain' in msg.as_string()
Exemplo n.º 5
0
def test_json_message():
    json_text = '{"msg": "Hello World!}'
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  alts={'json': json_text})

    assert json_text == msg.alts['json']
    assert 'Content-Type: multipart/alternative' in msg.as_string()
Exemplo n.º 6
0
def test_html_message():
    html_text = "<p>Hello World</p>"
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  html=html_text)

    assert html_text == msg.html
    assert 'Content-Type: multipart/alternative' in msg.as_string()
Exemplo n.º 7
0
def test_plain_message_with_attachments():
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body="hello")

    msg.attach(data=b"this is a test", content_type="text/plain")

    assert 'Content-Type: multipart/mixed' in msg.as_string()
Exemplo n.º 8
0
def test_msgid_header():
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body="hello")

    # see RFC 5322 section 3.6.4. for the exact format specification
    r = re.compile(r"<\S+@\S+>").match(msg.msgId)
    assert r is not None
    assert 'Message-ID: ' + msg.msgId in msg.as_string()
Exemplo n.º 9
0
def test_connection_send_ascii_recipient_single():
    mail = Mail(**test_mail_options)
    msg = Message(sender="*****@*****.**",
                  recipients=["*****@*****.**"],
                  body="normal ascii text")
    with mail.connect() as conn:
        with patch.object(conn, 'host') as host:
            conn.send(msg)
            host.sendmail.assert_called_once_with(msg.sender, msg.recipients,
                                                  msg.as_bytes(),
                                                  msg.mail_options,
                                                  msg.rcpt_options)
Exemplo n.º 10
0
def test_date_header():
    before = time.time()
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body="hello",
                  date=time.time())
    after = time.time()

    assert before <= msg.date <= after
    date_formatted = email.utils.formatdate(msg.date, localtime=True)
    assert 'Date: ' + date_formatted in msg.as_string()
Exemplo n.º 11
0
def test_plain_message_with_ascii_converted_attachment():
    msg = Message(subject="subject",
                  recipients=["*****@*****.**"],
                  body="hello",
                  sender="*****@*****.**",
                  ascii_attachments=True)

    msg.attach(data=b"this is a test",
               content_type="text/plain",
               filename='ünicödeß ←.→ ✓.txt')

    assert 'Content-Disposition: attachment; filename="unicode . .txt"' in msg.as_string(
    )
Exemplo n.º 12
0
def test_connection_send_single(mock_smtp):
    mail = Mail(**test_mail_options)
    mail.mail_suppress_send = False
    msg = Message(sender="*****@*****.**",
                  recipients=["*****@*****.**"],
                  body="normal ascii text")
    mock_smtp.return_value = MagicMock(spec=SMTP)
    with mail.connect() as conn:
        conn.send(msg)
        host = conn.host
        host.sendmail.assert_called_with(msg.sender, msg.recipients,
                                         msg.as_bytes(), msg.mail_options,
                                         msg.rcpt_options)
Exemplo n.º 13
0
def test_connection_send_non_ascii_recipient_single():
    mail = Mail(**test_mail_options)
    with mail.connect() as conn:
        with patch.object(conn, 'host') as host:
            msg = Message(subject="testing",
                          sender="*****@*****.**",
                          recipients=[u'ÄÜÖ → ✓ <*****@*****.**>'],
                          body="testing")
            conn.send(msg)

            host.sendmail.assert_called_once_with(
                "*****@*****.**",
                ["=?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <*****@*****.**>"],
                msg.as_bytes(), msg.mail_options, msg.rcpt_options)
Exemplo n.º 14
0
def test_message_init():
    msg = Message(subject="subject",
                  recipients=["*****@*****.**"],
                  body="body")
    assert msg.subject == "subject"
    assert msg.recipients == ["*****@*****.**"]
    assert msg.body == "body"
Exemplo n.º 15
0
def test_bad_header_with_a_newline():
    mail = Mail(**test_mail_options)
    msg = Message(subject="\ntesting\r\ntesting",
                  body="testing",
                  recipients=["*****@*****.**"])

    with pytest.raises(BadHeaderError):
        mail.send(msg)
Exemplo n.º 16
0
def test_bad_header_subject_trailing_whitespace():
    mail = Mail(**test_mail_options)
    msg = Message(subject="testing\r\n\t",
                  body="testing",
                  recipients=["*****@*****.**"])

    with pytest.raises(BadHeaderError):
        mail.send(msg)
Exemplo n.º 17
0
def test_message_ascii_attachments_config():
    mail = Mail(**test_mail_options)
    mail.mail_ascii_attachments = True
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"])
    mail.send(msg)
    assert msg.ascii_attachments
Exemplo n.º 18
0
def test_plain_message_with_unicode_attachment():
    msg = Message(
        subject="subject",
        recipients=["*****@*****.**"],
        body="hello",
        sender="*****@*****.**",
    )

    msg.attach(data=b"this is a test",
               content_type="text/plain",
               filename='ünicöde ←→ ✓.txt')

    parsed = email.message_from_string(msg.as_string())

    assert re.sub(
        r'\s+', ' ',
        parsed.get_payload()[1].get('Content-Disposition')
    ) in [
        'attachment; filename*="UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt"',
        'attachment; filename*=UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt'
    ]
Exemplo n.º 19
0
def test_bad_header_subject_with_no_trailing_whitespace():
    """
    Exercises line `if linenum > 0 and line[0] not in '\t ':`

    This is a bit of a strange test but we aren't changing the bad_header check from flask_mail
    """
    mail = Mail(**test_mail_options)
    msg = Message(subject="testing\r\ntesting",
                  body="testing",
                  recipients=["*****@*****.**"])

    with pytest.raises(BadHeaderError):
        mail.send(msg)
Exemplo n.º 20
0
def test_unicode_headers():
    msg = Message(subject="subject",
                  sender='ÄÜÖ → ✓ <*****@*****.**>',
                  recipients=["Ä <*****@*****.**>", "Ü <*****@*****.**>"],
                  cc=["Ö <*****@*****.**>"])

    response = msg.as_string()
    a1 = sanitize_address("Ä <*****@*****.**>")
    a2 = sanitize_address("Ü <*****@*****.**>")
    h1_a = email.header.Header("To: %s, %s" % (a1, a2))
    h1_b = email.header.Header("To: %s, %s" % (a2, a1))
    h2 = email.header.Header("From: %s" %
                             sanitize_address("ÄÜÖ → ✓ <*****@*****.**>"))
    h3 = email.header.Header("Cc: %s" % sanitize_address("Ö <*****@*****.**>"))

    # Ugly, but there's no guaranteed order of the recipients in the header
    try:
        assert h1_a.encode() in response
    except AssertionError:
        assert h1_b.encode() in response

    assert h2.encode() in response
    assert h3.encode() in response
Exemplo n.º 21
0
def test_connection_send_many(mock_smtp):
    mail = Mail(**test_mail_options)
    mail.mail_suppress_send = False
    mail.mail_max_emails = 50
    mock_smtp.return_value = MagicMock(spec=SMTP)
    with mail.connect() as conn:
        host = conn.host
        conn.configure_host = MagicMock()
        conn.configure_host.return_value = None
        for i in range(100):
            msg = Message(sender="*****@*****.**",
                          recipients=["*****@*****.**"],
                          body="normal ascii text")

            conn.send(msg)
        assert host.quit.called
        assert conn.configure_host.called
Exemplo n.º 22
0
def test_html_message_with_attachments():
    html_text = "<p>Hello World</p>"
    plain_text = 'Hello World'
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  body=plain_text,
                  html=html_text)
    msg.attach(data=b"this is a test", content_type="text/plain")

    assert html_text == msg.html
    assert 'Content-Type: multipart/alternative' in msg.as_string()

    parsed = email.message_from_string(msg.as_string())
    assert len(parsed.get_payload()) == 2

    body, attachment = parsed.get_payload()
    assert len(body.get_payload()) == 2

    plain, html = body.get_payload()
    assert plain.get_payload() == plain_text
    assert html.get_payload() == html_text
    assert base64.b64decode(attachment.get_payload()), b'this is a test'
Exemplo n.º 23
0
def test_sender_as_tuple():
    msg = Message(subject='test', sender=('Me', '*****@*****.**'))
    assert msg.sender == 'Me <*****@*****.**>'
Exemplo n.º 24
0
def test_esmtp_options_properly_initialized():
    msg = Message(subject='subject')
    assert msg.mail_options == []
    assert msg.rcpt_options == []
Exemplo n.º 25
0
def test_empty_cc_list():
    msg = Message(subject="subject", recipients=['*****@*****.**'])
    assert msg.cc == []
Exemplo n.º 26
0
def test_empty_recipient_list_init():
    msg = Message(subject="subject")
    assert msg.recipients == []
Exemplo n.º 27
0
def test_empty_subject_header():
    mail = Mail(**test_mail_options)
    msg = Message(sender="*****@*****.**", recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    mail.send(msg)
    assert 'Subject:' not in msg.as_string()
Exemplo n.º 28
0
def test_message_default_sender():
    msg = Message(recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    mail = Mail(**test_mail_options)
    mail.send(msg)
    assert msg.sender == '*****@*****.**'
Exemplo n.º 29
0
def test_message_as_bytes():
    msg = Message(sender="*****@*****.**", recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    assert bytes(msg) == msg.as_bytes()
Exemplo n.º 30
0
def test_add_recipient():
    msg1 = Message(subject="subject")
    assert msg1.recipients == []
    msg1.add_recipient("*****@*****.**")
    assert len(msg1.recipients) == 1
    assert msg1.recipients[0] == "*****@*****.**"