Skip to content

Commit

Permalink
Rewrite smtp Unittest Tests To Pytest Style (home-assistant#41720)
Browse files Browse the repository at this point in the history
* 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:- home-assistant#40894

* Made Fix Based On Code Review
To Pull Request -  home-assistant#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 home-assistant#41720.

* Made Fix Based On Code Review
To Pull Request home-assistant#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 home-assistant#41720
  • Loading branch information
punitaojha authored Oct 16, 2020
1 parent 8bcae65 commit 23b3db0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 73 deletions.
172 changes: 99 additions & 73 deletions tests/components/smtp/test_notify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""The tests for the notify smtp platform."""
from os import path
import re
import unittest

import pytest

from homeassistant import config as hass_config
import homeassistant.components.notify as notify
Expand All @@ -11,7 +12,6 @@
from homeassistant.setup import async_setup_component

from tests.async_mock import patch
from tests.common import get_test_home_assistant


class MockSMTP(MailNotificationService):
Expand All @@ -22,77 +22,6 @@ def _send_email(self, msg):
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):
"""Verify we can reload the notify service."""

Expand Down Expand Up @@ -139,3 +68,100 @@ async def test_reload_notify(hass):

def _get_fixtures_base_path():
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)
Binary file added tests/testing_config/notify/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testing_config/notify/test.pdf
Binary file not shown.

0 comments on commit 23b3db0

Please sign in to comment.