2016-08-09 00:36:49 +00:00
|
|
|
"""The tests for the notify smtp platform."""
|
|
|
|
import unittest
|
2017-01-18 06:00:15 +00:00
|
|
|
from unittest.mock import patch
|
2016-08-09 00:36:49 +00:00
|
|
|
|
|
|
|
from homeassistant.components.notify import smtp
|
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
|
|
|
|
class MockSMTP(smtp.MailNotificationService):
|
|
|
|
"""Test SMTP object that doesn't need a working server."""
|
|
|
|
|
|
|
|
def _send_email(self, msg):
|
|
|
|
"""Just return string for testing."""
|
|
|
|
return msg.as_string()
|
|
|
|
|
|
|
|
|
|
|
|
class TestNotifySmtp(unittest.TestCase):
|
|
|
|
"""Test the smtp notify."""
|
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-08-09 00:36:49 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-03-14 18:00:16 +00:00
|
|
|
self.mailer = MockSMTP('localhost', 25, 5, 'test@test.com', 1,
|
2017-04-28 09:29:30 +00:00
|
|
|
'testuser', 'testpass',
|
2017-05-15 07:23:57 +00:00
|
|
|
['recip1@example.com', 'testrecip@test.com'],
|
|
|
|
'HomeAssistant', 0)
|
2016-08-09 00:36:49 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2018-05-13 09:06:15 +00:00
|
|
|
"""Stop down everything that was started."""
|
2016-08-09 00:36:49 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2017-01-18 06:00:15 +00:00
|
|
|
@patch('email.utils.make_msgid', return_value='<mock@mock>')
|
|
|
|
def test_text_email(self, mock_make_msgid):
|
2016-08-09 00:36:49 +00:00
|
|
|
"""Test build of default text email behavior."""
|
|
|
|
msg = self.mailer.send_message('Test msg')
|
2016-12-04 00:56:42 +00:00
|
|
|
expected = ('^Content-Type: text/plain; charset="us-ascii"\n'
|
2016-08-09 00:36:49 +00:00
|
|
|
'MIME-Version: 1.0\n'
|
|
|
|
'Content-Transfer-Encoding: 7bit\n'
|
2016-09-01 13:35:46 +00:00
|
|
|
'Subject: Home Assistant\n'
|
2017-04-28 09:29:30 +00:00
|
|
|
'To: recip1@example.com,testrecip@test.com\n'
|
2017-05-15 07:23:57 +00:00
|
|
|
'From: HomeAssistant <test@test.com>\n'
|
2016-08-09 00:36:49 +00:00
|
|
|
'X-Mailer: HomeAssistant\n'
|
2016-12-04 00:56:42 +00:00
|
|
|
'Date: [^\n]+\n'
|
|
|
|
'Message-Id: <[^@]+@[^>]+>\n'
|
2016-08-09 00:36:49 +00:00
|
|
|
'\n'
|
2016-12-04 00:56:42 +00:00
|
|
|
'Test msg$')
|
|
|
|
self.assertRegex(msg, expected)
|
2016-08-09 00:36:49 +00:00
|
|
|
|
2017-01-18 06:00:15 +00:00
|
|
|
@patch('email.utils.make_msgid', return_value='<mock@mock>')
|
|
|
|
def test_mixed_email(self, mock_make_msgid):
|
2016-08-09 00:36:49 +00:00
|
|
|
"""Test build of mixed text email behavior."""
|
|
|
|
msg = self.mailer.send_message('Test msg',
|
|
|
|
data={'images': ['test.jpg']})
|
|
|
|
self.assertTrue('Content-Type: multipart/related' in msg)
|
2017-05-15 07:23:57 +00:00
|
|
|
|
|
|
|
@patch('email.utils.make_msgid', return_value='<mock@mock>')
|
|
|
|
def test_html_email(self, mock_make_msgid):
|
|
|
|
"""Test build of html email behavior."""
|
|
|
|
html = '''
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<head><meta charset="UTF-8"></head>
|
|
|
|
<body>
|
|
|
|
<div>
|
|
|
|
<h1>Intruder alert at apartment!!</h1>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<img alt="test.jpg" src="cid:test.jpg"/>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>'''
|
|
|
|
msg = self.mailer.send_message('Test msg',
|
|
|
|
data={'html': html,
|
|
|
|
'images': ['test.jpg']})
|
|
|
|
self.assertTrue('Content-Type: multipart/related' in msg)
|