mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Rewrite imap_email_content tests to use pytest (#41200)
This commit is contained in:
parent
357a0e9d7e
commit
8bcd6c1880
@ -4,14 +4,11 @@ import datetime
|
|||||||
import email
|
import email
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
import unittest
|
|
||||||
|
|
||||||
from homeassistant.components.imap_email_content import sensor as imap_email_content
|
from homeassistant.components.imap_email_content import sensor as imap_email_content
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import async_track_state_change
|
||||||
from homeassistant.helpers.template import Template
|
from homeassistant.helpers.template import Template
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
|
|
||||||
class FakeEMailReader:
|
class FakeEMailReader:
|
||||||
"""A test class for sending test emails."""
|
"""A test class for sending test emails."""
|
||||||
@ -31,15 +28,7 @@ class FakeEMailReader:
|
|||||||
return self._messages.popleft()
|
return self._messages.popleft()
|
||||||
|
|
||||||
|
|
||||||
class EmailContentSensor(unittest.TestCase):
|
async def test_allowed_sender(hass):
|
||||||
"""Test the IMAP email content sensor."""
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.hass.stop)
|
|
||||||
|
|
||||||
def test_allowed_sender(self):
|
|
||||||
"""Test emails from allowed sender."""
|
"""Test emails from allowed sender."""
|
||||||
test_message = email.message.Message()
|
test_message = email.message.Message()
|
||||||
test_message["From"] = "sender@test.com"
|
test_message["From"] = "sender@test.com"
|
||||||
@ -48,7 +37,7 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
test_message.set_payload("Test Message")
|
test_message.set_payload("Test Message")
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([test_message])),
|
FakeEMailReader(deque([test_message])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
@ -56,8 +45,8 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Test" == sensor.state
|
assert "Test" == sensor.state
|
||||||
assert "Test Message" == sensor.device_state_attributes["body"]
|
assert "Test Message" == sensor.device_state_attributes["body"]
|
||||||
assert "sender@test.com" == sensor.device_state_attributes["from"]
|
assert "sender@test.com" == sensor.device_state_attributes["from"]
|
||||||
@ -67,7 +56,8 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
== sensor.device_state_attributes["date"]
|
== sensor.device_state_attributes["date"]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_multi_part_with_text(self):
|
|
||||||
|
async def test_multi_part_with_text(hass):
|
||||||
"""Test multi part emails."""
|
"""Test multi part emails."""
|
||||||
msg = MIMEMultipart("alternative")
|
msg = MIMEMultipart("alternative")
|
||||||
msg["Subject"] = "Link"
|
msg["Subject"] = "Link"
|
||||||
@ -83,7 +73,7 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
msg.attach(htmlPart)
|
msg.attach(htmlPart)
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([msg])),
|
FakeEMailReader(deque([msg])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
@ -91,12 +81,13 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Link" == sensor.state
|
assert "Link" == sensor.state
|
||||||
assert "Test Message" == sensor.device_state_attributes["body"]
|
assert "Test Message" == sensor.device_state_attributes["body"]
|
||||||
|
|
||||||
def test_multi_part_only_html(self):
|
|
||||||
|
async def test_multi_part_only_html(hass):
|
||||||
"""Test multi part emails with only HTML."""
|
"""Test multi part emails with only HTML."""
|
||||||
msg = MIMEMultipart("alternative")
|
msg = MIMEMultipart("alternative")
|
||||||
msg["Subject"] = "Link"
|
msg["Subject"] = "Link"
|
||||||
@ -109,7 +100,7 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
msg.attach(htmlPart)
|
msg.attach(htmlPart)
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([msg])),
|
FakeEMailReader(deque([msg])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
@ -117,15 +108,16 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Link" == sensor.state
|
assert "Link" == sensor.state
|
||||||
assert (
|
assert (
|
||||||
"<html><head></head><body>Test Message</body></html>"
|
"<html><head></head><body>Test Message</body></html>"
|
||||||
== sensor.device_state_attributes["body"]
|
== sensor.device_state_attributes["body"]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_multi_part_only_other_text(self):
|
|
||||||
|
async def test_multi_part_only_other_text(hass):
|
||||||
"""Test multi part emails with only other text."""
|
"""Test multi part emails with only other text."""
|
||||||
msg = MIMEMultipart("alternative")
|
msg = MIMEMultipart("alternative")
|
||||||
msg["Subject"] = "Link"
|
msg["Subject"] = "Link"
|
||||||
@ -138,7 +130,7 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
msg.attach(htmlPart)
|
msg.attach(htmlPart)
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([msg])),
|
FakeEMailReader(deque([msg])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
@ -146,12 +138,13 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Link" == sensor.state
|
assert "Link" == sensor.state
|
||||||
assert "Test Message" == sensor.device_state_attributes["body"]
|
assert "Test Message" == sensor.device_state_attributes["body"]
|
||||||
|
|
||||||
def test_multiple_emails(self):
|
|
||||||
|
async def test_multiple_emails(hass):
|
||||||
"""Test multiple emails."""
|
"""Test multiple emails."""
|
||||||
states = []
|
states = []
|
||||||
|
|
||||||
@ -170,10 +163,10 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
def state_changed_listener(entity_id, from_s, to_s):
|
def state_changed_listener(entity_id, from_s, to_s):
|
||||||
states.append(to_s)
|
states.append(to_s)
|
||||||
|
|
||||||
track_state_change(self.hass, ["sensor.emailtest"], state_changed_listener)
|
async_track_state_change(hass, ["sensor.emailtest"], state_changed_listener)
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([test_message1, test_message2])),
|
FakeEMailReader(deque([test_message1, test_message2])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
@ -182,17 +175,18 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
|
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert "Test" == states[0].state
|
assert "Test" == states[0].state
|
||||||
assert "Test 2" == states[1].state
|
assert "Test 2" == states[1].state
|
||||||
|
|
||||||
assert "Test Message 2" == sensor.device_state_attributes["body"]
|
assert "Test Message 2" == sensor.device_state_attributes["body"]
|
||||||
|
|
||||||
def test_sender_not_allowed(self):
|
|
||||||
|
async def test_sender_not_allowed(hass):
|
||||||
"""Test not whitelisted emails."""
|
"""Test not whitelisted emails."""
|
||||||
test_message = email.message.Message()
|
test_message = email.message.Message()
|
||||||
test_message["From"] = "sender@test.com"
|
test_message["From"] = "sender@test.com"
|
||||||
@ -201,7 +195,7 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
test_message.set_payload("Test Message")
|
test_message.set_payload("Test Message")
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([test_message])),
|
FakeEMailReader(deque([test_message])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["other@test.com"],
|
["other@test.com"],
|
||||||
@ -209,11 +203,12 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert sensor.state is None
|
assert sensor.state is None
|
||||||
|
|
||||||
def test_template(self):
|
|
||||||
|
async def test_template(hass):
|
||||||
"""Test value template."""
|
"""Test value template."""
|
||||||
test_message = email.message.Message()
|
test_message = email.message.Message()
|
||||||
test_message["From"] = "sender@test.com"
|
test_message["From"] = "sender@test.com"
|
||||||
@ -222,16 +217,14 @@ class EmailContentSensor(unittest.TestCase):
|
|||||||
test_message.set_payload("Test Message")
|
test_message.set_payload("Test Message")
|
||||||
|
|
||||||
sensor = imap_email_content.EmailContentSensor(
|
sensor = imap_email_content.EmailContentSensor(
|
||||||
self.hass,
|
hass,
|
||||||
FakeEMailReader(deque([test_message])),
|
FakeEMailReader(deque([test_message])),
|
||||||
"test_emails_sensor",
|
"test_emails_sensor",
|
||||||
["sender@test.com"],
|
["sender@test.com"],
|
||||||
Template(
|
Template("{{ subject }} from {{ from }} with message {{ body }}", hass),
|
||||||
"{{ subject }} from {{ from }} with message {{ body }}", self.hass
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
sensor.entity_id = "sensor.emailtest"
|
sensor.entity_id = "sensor.emailtest"
|
||||||
sensor.schedule_update_ha_state(True)
|
sensor.async_schedule_update_ha_state(True)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "Test from sender@test.com with message Test Message" == sensor.state
|
assert "Test from sender@test.com with message Test Message" == sensor.state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user