Rewrite APNS tests to use pytest (#41684)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Edward Knight 2020-10-16 12:43:02 +01:00 committed by GitHub
parent 11d2e0c671
commit b39d82c154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,17 +1,17 @@
"""The tests for the APNS component."""
import io
import unittest
from apns2.errors import Unregistered
import pytest
import yaml
import homeassistant.components.apns.notify as apns
import homeassistant.components.notify as notify
from homeassistant.core import State
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.async_mock import Mock, mock_open, patch
from tests.common import assert_setup_component, get_test_home_assistant
from tests.common import assert_setup_component
CONFIG = {
notify.DOMAIN: {
@ -23,31 +23,26 @@ CONFIG = {
}
@patch("homeassistant.components.apns.notify.open", mock_open(), create=True)
class TestApns(unittest.TestCase):
"""Test the APNS component."""
@pytest.fixture(scope="module", autouse=True)
def mock_apns_notify_open():
"""Mock builtins.open for apns.notfiy."""
with patch("homeassistant.components.apns.notify.open", mock_open(), create=True):
yield
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
def _setup_notify(self):
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
async def _setup_notify(hass_):
assert isinstance(apns.load_yaml_config_file, Mock), "Found unmocked load_yaml"
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, CONFIG)
assert await async_setup_component(hass_, notify.DOMAIN, CONFIG)
assert handle_config[notify.DOMAIN]
@patch("os.path.isfile", return_value=True)
@patch("os.access", return_value=True)
def test_apns_setup_full(self, mock_access, mock_isfile):
@patch("os.path.isfile", return_value=True)
@patch("os.access", return_value=True)
async def test_apns_setup_full(mock_access, mock_isfile, hass):
"""Test setup with all data."""
config = {
"notify": {
@ -60,10 +55,11 @@ class TestApns(unittest.TestCase):
}
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert await async_setup_component(hass, notify.DOMAIN, config)
assert handle_config[notify.DOMAIN]
def test_apns_setup_missing_name(self):
async def test_apns_setup_missing_name(hass):
"""Test setup with missing name."""
config = {
"notify": {
@ -73,10 +69,11 @@ class TestApns(unittest.TestCase):
}
}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert await async_setup_component(hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_apns_setup_missing_certificate(self):
async def test_apns_setup_missing_certificate(hass):
"""Test setup with missing certificate."""
config = {
"notify": {
@ -86,10 +83,11 @@ class TestApns(unittest.TestCase):
}
}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert await async_setup_component(hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_apns_setup_missing_topic(self):
async def test_apns_setup_missing_topic(hass):
"""Test setup with missing topic."""
config = {
"notify": {
@ -99,11 +97,12 @@ class TestApns(unittest.TestCase):
}
}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert await async_setup_component(hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
@patch("homeassistant.components.apns.notify._write_device")
def test_register_new_device(self, mock_write):
@patch("homeassistant.components.apns.notify._write_device")
async def test_register_new_device(mock_write, hass):
"""Test registering a new device with a name."""
yaml_file = {5678: {"name": "test device 2"}}
@ -119,9 +118,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
apns.DOMAIN,
"apns_test_app",
{"push_id": "1234", "name": "test device"},
@ -131,8 +130,9 @@ class TestApns(unittest.TestCase):
assert len(written_devices) == 1
assert written_devices[0].name == "test device"
@patch("homeassistant.components.apns.notify._write_device")
def test_register_device_without_name(self, mock_write):
@patch("homeassistant.components.apns.notify._write_device")
async def test_register_device_without_name(mock_write, hass):
"""Test registering a without a name."""
yaml_file = {
1234: {"name": "test device 1", "tracking_device_id": "tracking123"},
@ -151,9 +151,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
apns.DOMAIN, "apns_test_app", {"push_id": "1234"}, blocking=True
)
@ -164,8 +164,9 @@ class TestApns(unittest.TestCase):
assert test_device is not None
assert test_device.name is None
@patch("homeassistant.components.apns.notify._write_device")
def test_update_existing_device(self, mock_write):
@patch("homeassistant.components.apns.notify._write_device")
async def test_update_existing_device(mock_write, hass):
"""Test updating an existing device."""
yaml_file = {1234: {"name": "test device 1"}, 5678: {"name": "test device 2"}}
@ -181,9 +182,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
apns.DOMAIN,
"apns_test_app",
{"push_id": "1234", "name": "updated device 1"},
@ -200,8 +201,9 @@ class TestApns(unittest.TestCase):
assert "updated device 1" == test_device_1.name
@patch("homeassistant.components.apns.notify._write_device")
def test_update_existing_device_with_tracking_id(self, mock_write):
@patch("homeassistant.components.apns.notify._write_device")
async def test_update_existing_device_with_tracking_id(mock_write, hass):
"""Test updating an existing device that has a tracking id."""
yaml_file = {
1234: {"name": "test device 1", "tracking_device_id": "tracking123"},
@ -220,9 +222,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
apns.DOMAIN,
"apns_test_app",
{"push_id": "1234", "name": "updated device 1"},
@ -240,8 +242,9 @@ class TestApns(unittest.TestCase):
assert "tracking123" == test_device_1.tracking_device_id
assert "tracking456" == test_device_2.tracking_device_id
@patch("homeassistant.components.apns.notify.APNsClient")
def test_send(self, mock_client):
@patch("homeassistant.components.apns.notify.APNsClient")
async def test_send(mock_client, hass):
"""Test updating an existing device."""
send = mock_client.return_value.send_notification
@ -251,9 +254,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
"notify",
"test_app",
{
@ -275,8 +278,9 @@ class TestApns(unittest.TestCase):
assert "test.mp3" == payload.sound
assert "testing" == payload.category
@patch("homeassistant.components.apns.notify.APNsClient")
def test_send_when_disabled(self, mock_client):
@patch("homeassistant.components.apns.notify.APNsClient")
async def test_send_when_disabled(mock_client, hass):
"""Test updating an existing device."""
send = mock_client.return_value.send_notification
@ -286,9 +290,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
"notify",
"test_app",
{
@ -300,8 +304,9 @@ class TestApns(unittest.TestCase):
assert not send.called
@patch("homeassistant.components.apns.notify.APNsClient")
def test_send_with_state(self, mock_client):
@patch("homeassistant.components.apns.notify.APNsClient")
async def test_send_with_state(mock_client, hass):
"""Test updating an existing device."""
send = mock_client.return_value.send_notification
@ -314,8 +319,13 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
), patch("os.path.isfile", Mock(return_value=True)):
notify_service = apns.ApnsNotificationService(
self.hass, "test_app", "testapp.appname", False, "test_app.pem"
notify_service = await hass.async_add_executor_job(
apns.ApnsNotificationService,
hass,
"test_app",
"testapp.appname",
False,
"test_app.pem",
)
notify_service.device_state_changed_listener(
@ -335,9 +345,10 @@ class TestApns(unittest.TestCase):
assert "5678" == target
assert "Hello" == payload.alert
@patch("homeassistant.components.apns.notify.APNsClient")
@patch("homeassistant.components.apns.notify._write_device")
def test_disable_when_unregistered(self, mock_write, mock_client):
@patch("homeassistant.components.apns.notify.APNsClient")
@patch("homeassistant.components.apns.notify._write_device")
async def test_disable_when_unregistered(mock_write, mock_client, hass):
"""Test disabling a device when it is unregistered."""
send = mock_client.return_value.send_notification
send.side_effect = Unregistered()
@ -359,9 +370,9 @@ class TestApns(unittest.TestCase):
"homeassistant.components.apns.notify.load_yaml_config_file",
Mock(return_value=yaml_file),
):
self._setup_notify()
await _setup_notify(hass)
assert self.hass.services.call(
assert await hass.services.async_call(
"notify", "test_app", {"message": "Hello"}, blocking=True
)
@ -372,7 +383,7 @@ class TestApns(unittest.TestCase):
assert test_device_1.disabled is True
def test_write_device():
async def test_write_device():
"""Test writing device."""
out = io.StringIO()
device = apns.ApnsDevice("123", "name", "track_id", True)