mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Rewrite smtp Unittest Tests To Pytest Style (#41720)
* Rewrite smtp unittest tests to pytest style test functions. .coverage.rc --> Remove homeassistant/component/smtp/notify.py from omit section. test_notify.py --> Convert test case to pytest style. test.jpg, test.pdf --> Add test files. Issue id:- #40894 * Made Fix Based On Code Review To Pull Request - #41720. .coveragerc --> Add file in omit section as coverage is 75%(i.e. <95%) test_notify.py --> Capitalize constant in file, Made parametrized data compatible to avoid if conditions. Code Review Submission on Pull Request #41720. * Made Fix Based On Code Review To Pull Request #41720. test_notify.py --> Add different test for simple text message to avoid if conditions check in code. * Move Notify Media Files To Folder tests/testing_config/notify. test_notify.py --> Add Relevant Path For .jpg and .pdf file. test.jpg, test.pdf --> Move to relevant location. Pull request #41720
This commit is contained in:
parent
8bcae65dd8
commit
23b3db08f3
@ -1,7 +1,8 @@
|
|||||||
"""The tests for the notify smtp platform."""
|
"""The tests for the notify smtp platform."""
|
||||||
from os import path
|
from os import path
|
||||||
import re
|
import re
|
||||||
import unittest
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config as hass_config
|
from homeassistant import config as hass_config
|
||||||
import homeassistant.components.notify as notify
|
import homeassistant.components.notify as notify
|
||||||
@ -11,7 +12,6 @@ from homeassistant.const import SERVICE_RELOAD
|
|||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import patch
|
from tests.async_mock import patch
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
|
|
||||||
class MockSMTP(MailNotificationService):
|
class MockSMTP(MailNotificationService):
|
||||||
@ -22,77 +22,6 @@ class MockSMTP(MailNotificationService):
|
|||||||
return msg.as_string()
|
return msg.as_string()
|
||||||
|
|
||||||
|
|
||||||
class TestNotifySmtp(unittest.TestCase):
|
|
||||||
"""Test the smtp notify."""
|
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.mailer = MockSMTP(
|
|
||||||
"localhost",
|
|
||||||
25,
|
|
||||||
5,
|
|
||||||
"test@test.com",
|
|
||||||
1,
|
|
||||||
"testuser",
|
|
||||||
"testpass",
|
|
||||||
["recip1@example.com", "testrecip@test.com"],
|
|
||||||
"Home Assistant",
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Stop down everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
@patch("email.utils.make_msgid", return_value="<mock@mock>")
|
|
||||||
def test_text_email(self, mock_make_msgid):
|
|
||||||
"""Test build of default text email behavior."""
|
|
||||||
msg = self.mailer.send_message("Test msg")
|
|
||||||
expected = (
|
|
||||||
'^Content-Type: text/plain; charset="us-ascii"\n'
|
|
||||||
"MIME-Version: 1.0\n"
|
|
||||||
"Content-Transfer-Encoding: 7bit\n"
|
|
||||||
"Subject: Home Assistant\n"
|
|
||||||
"To: recip1@example.com,testrecip@test.com\n"
|
|
||||||
"From: Home Assistant <test@test.com>\n"
|
|
||||||
"X-Mailer: Home Assistant\n"
|
|
||||||
"Date: [^\n]+\n"
|
|
||||||
"Message-Id: <[^@]+@[^>]+>\n"
|
|
||||||
"\n"
|
|
||||||
"Test msg$"
|
|
||||||
)
|
|
||||||
assert re.search(expected, msg)
|
|
||||||
|
|
||||||
@patch("email.utils.make_msgid", return_value="<mock@mock>")
|
|
||||||
def test_mixed_email(self, mock_make_msgid):
|
|
||||||
"""Test build of mixed text email behavior."""
|
|
||||||
msg = self.mailer.send_message("Test msg", data={"images": ["test.jpg"]})
|
|
||||||
assert "Content-Type: multipart/related" in msg
|
|
||||||
|
|
||||||
@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"]}
|
|
||||||
)
|
|
||||||
assert "Content-Type: multipart/related" in msg
|
|
||||||
|
|
||||||
|
|
||||||
async def test_reload_notify(hass):
|
async def test_reload_notify(hass):
|
||||||
"""Verify we can reload the notify service."""
|
"""Verify we can reload the notify service."""
|
||||||
|
|
||||||
@ -139,3 +68,100 @@ async def test_reload_notify(hass):
|
|||||||
|
|
||||||
def _get_fixtures_base_path():
|
def _get_fixtures_base_path():
|
||||||
return path.dirname(path.dirname(path.dirname(__file__)))
|
return path.dirname(path.dirname(path.dirname(__file__)))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def message():
|
||||||
|
"""Return MockSMTP object with test data."""
|
||||||
|
mailer = MockSMTP(
|
||||||
|
"localhost",
|
||||||
|
25,
|
||||||
|
5,
|
||||||
|
"test@test.com",
|
||||||
|
1,
|
||||||
|
"testuser",
|
||||||
|
"testpass",
|
||||||
|
["recip1@example.com", "testrecip@test.com"],
|
||||||
|
"Home Assistant",
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
yield mailer
|
||||||
|
|
||||||
|
|
||||||
|
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="tests/testing_config/notify/test.jpg" src="cid:tests/testing_config/notify/test.jpg"/>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>"""
|
||||||
|
|
||||||
|
|
||||||
|
EMAIL_DATA = [
|
||||||
|
(
|
||||||
|
"Test msg",
|
||||||
|
{"images": ["tests/testing_config/notify/test.jpg"]},
|
||||||
|
"Content-Type: multipart/related",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Test msg",
|
||||||
|
{"html": HTML, "images": ["tests/testing_config/notify/test.jpg"]},
|
||||||
|
"Content-Type: multipart/related",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Test msg",
|
||||||
|
{"html": HTML, "images": ["test.jpg"]},
|
||||||
|
"Content-Type: multipart/related",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Test msg",
|
||||||
|
{"html": HTML, "images": ["tests/testing_config/notify/test.pdf"]},
|
||||||
|
"Content-Type: multipart/related",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"message_data, data, content_type",
|
||||||
|
EMAIL_DATA,
|
||||||
|
ids=[
|
||||||
|
"Tests when sending text message and images.",
|
||||||
|
"Tests when sending text message, HTML Template and images.",
|
||||||
|
"Tests when image does not exist at mentioned location.",
|
||||||
|
"Tests when image type cannot be detected or is of wrong type.",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_send_message(message_data, data, content_type, hass, message):
|
||||||
|
"""Verify if we can send messages of all types correctly."""
|
||||||
|
sample_email = "<mock@mock>"
|
||||||
|
with patch("email.utils.make_msgid", return_value=sample_email):
|
||||||
|
result = message.send_message(message_data, data=data)
|
||||||
|
assert content_type in result
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_text_message(hass, message):
|
||||||
|
"""Verify if we can send simple text message."""
|
||||||
|
expected = (
|
||||||
|
'^Content-Type: text/plain; charset="us-ascii"\n'
|
||||||
|
"MIME-Version: 1.0\n"
|
||||||
|
"Content-Transfer-Encoding: 7bit\n"
|
||||||
|
"Subject: Home Assistant\n"
|
||||||
|
"To: recip1@example.com,testrecip@test.com\n"
|
||||||
|
"From: Home Assistant <test@test.com>\n"
|
||||||
|
"X-Mailer: Home Assistant\n"
|
||||||
|
"Date: [^\n]+\n"
|
||||||
|
"Message-Id: <[^@]+@[^>]+>\n"
|
||||||
|
"\n"
|
||||||
|
"Test msg$"
|
||||||
|
)
|
||||||
|
sample_email = "<mock@mock>"
|
||||||
|
message_data = "Test msg"
|
||||||
|
with patch("email.utils.make_msgid", return_value=sample_email):
|
||||||
|
result = message.send_message(message_data)
|
||||||
|
assert re.search(expected, result)
|
||||||
|
BIN
tests/testing_config/notify/test.jpg
Normal file
BIN
tests/testing_config/notify/test.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 56 KiB |
BIN
tests/testing_config/notify/test.pdf
Executable file
BIN
tests/testing_config/notify/test.pdf
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user