Mail related functional tests
=============================

Some initial setup:

  >>> from plone.testing.zope import Browser
  >>> import six
  >>> app = layer['app']
  >>> browser = Browser(app)
  >>> browser.handleErrors = False


Contact form
------------

Let's go to the contact form:

  >>> browser.open('http://nohost/plone/contact-info')

Now fill in the form:

  >>> form = browser.getForm(id='form')

  >>> form.getControl(name='form.widgets.sender_fullname').value = 'Täst user'
  >>> form.getControl(name='form.widgets.sender_from_address').value = 'test@plone.test'
  >>> form.getControl(name='form.widgets.subject').value = 'Some täst subject.'
  >>> form.getControl(name='form.widgets.message').value = 'Another täst message.'

And submit it:

  >>> form.getControl(name='form.buttons.send').click()

We expect to return to the same page.

  >>> browser.url
  'http://nohost/plone/contact-info'

  >>> 'A mail has now been sent' in browser.contents
  True

As part of our test setup, we replaced the original MailHost with our
own version.  Our version doesn't mail messages, it just collects them
in a list called ``messages``:

  >>> mailhost = layer['portal'].MailHost
  >>> len(mailhost.messages)
  1
  >>> msg = mailhost.messages[0]

Now that we have the message, we want to look at its contents:

  >>> b'To: mail@plone.test' in msg
  True

  >>> b'From: mail@plone.test' in msg
  True

We expect the headers to be properly header encoded (7-bit):

  >>> b'Subject: =?utf-8?q?Some_t=C3=A4st_subject=2E?=' in msg
  True

The output should be encoded in a reasonable manner, in this case quoted-printable.
On Python 3 there may be problems with quoted printable messages on some mail servers.
See https://github.com/zopefoundation/Products.MailHost/issues/35
When '\r\n' is used as line ending, all is well.
On Python 2 the line endings are plain '\n', but there it is no problem.

  >>> True if six.PY2 else msg.count(b'\r\n') > 0
  True

There may be some small differences in where exactly the lines are cut off,
so we turn the message into one line first:

  >>> msg.replace(b'\r\n', b'\n').replace(b'=\n', b'').replace(b'\n', b' ')
  b'...Another t=C3=A4st message...You are receiving this mail because T=C3=A4st user test@plone.test...is sending feedback about the site you administer at...'

We can also decode the string, though we should still be careful about
lines ending in different spots:

  >>> import quopri
  >>> quopri.decodestring(msg).replace(b'\n', b' ')
  b'...Another t\xc3\xa4st message...You are receiving this mail because T\xc3\xa4st user test@plone.test...is sending feedback about the site you administer at...'
