l.s.m.t.test_stub : module documentation

Part of lp.services.mail.tests

No module docstring
Function test_simple_sendmail Send an email (faked by TestMailer - no actual email is sent)
Function test_suite Undocumented
def test_simple_sendmail():

Send an email (faked by TestMailer - no actual email is sent)

>>> import email
>>> from email.mime.text import MIMEText
>>> import transaction
>>> from lp.services.mail import stub
>>> from lp.services.mail.sendmail import simple_sendmail
>>> body = 'The email body'
>>> subject = 'The email subject'
>>> message_id1 = simple_sendmail(
...     'nobody1@example.com', ['nobody2@example.com'], subject, body
...     )

We should have a message id, a string

>>> bool(message_id1)
>>> isinstance(message_id1,str)

We can also send arbitrary headers through. Note how Python's email package handles Message-Id headers

>>> message_id2 = simple_sendmail(
...     'nobody@example.com', ['nobody2@example.com'], subject, body,
...     {'Message-Id': '<myMessageId>', 'X-Fnord': 'True'}
...     )
>>> message_id2

The TestMailer stores sent emails in memory (which we cleared in the setUp() method). But the actual email has yet to be sent, as that happens when the transaction is committed.

>>> len(stub.test_emails)
>>> transaction.commit()
>>> len(stub.test_emails)
>>> stub.test_emails[0] == stub.test_emails[1]

We have two emails, but we have no idea what order they are in!

Let's sort them, and verify that the first one is the one we want because only the first one contains the string 'nobody@example.com' in its raw message.

>>> sorted_test_emails = sorted(list(stub.test_emails))
>>> for from_addr, to_addrs, raw_message in sorted_test_emails:
...     print from_addr, to_addrs, 'nobody@example.com' in raw_message
bounces@canonical.com ['nobody2@example.com'] True
bounces@canonical.com ['nobody2@example.com'] False
>>> from_addr, to_addrs, raw_message = sorted_test_emails[0]
>>> from_addr
>>> to_addrs

The message should be a sane RFC2822 document

>>> message = email.message_from_string(raw_message)
>>> message['From']
>>> message['To']
>>> message['Subject'] == subject
>>> message['Message-Id']
>>> message.get_payload() == body

Character set should be utf-8 as per Bug #39758. utf8 isn't good enough.

>>> message['Content-Type']
'text/plain; charset="utf-8"'

And we want quoted printable, as it generally makes things readable and for languages it doesn't help, the only downside to base64 is bloat.

>>> message['Content-Transfer-Encoding']

The message has a number of additional headers added by default. 'X-Generated-By' not only indicates that the source is Launchpad, but shows the bzr revision and instance name.

>>> message['X-Generated-By'].replace('\n\t', '\n ')
'Launchpad (canonical.com); Revision="1999";\n Instance="launchpad-lazr.conf"'
def test_suite():
API Documentation for Launchpad, generated by pydoctor at 2019-01-18 00:00:10.