Fix cert_expiry time-based tests (#36924)

This commit is contained in:
jjlawren 2020-06-18 17:26:34 -05:00 committed by GitHub
parent 02e03340df
commit 6bffd9a892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 21 deletions

View File

@ -164,7 +164,7 @@ async def test_bad_import(hass):
async def test_abort_if_already_setup(hass):
"""Test we abort if the cert is already setup."""
MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
).add_to_hass(hass)

View File

@ -3,6 +3,7 @@ from datetime import timedelta
import socket
import ssl
from homeassistant.components.cert_expiry.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE, STATE_UNKNOWN
import homeassistant.util.dt as dt_util
@ -18,7 +19,7 @@ from tests.common import MockConfigEntry, async_fire_time_changed
async def test_async_setup_entry(mock_now, hass):
"""Test async_setup_entry."""
entry = MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
)
@ -51,7 +52,7 @@ async def test_async_setup_entry(mock_now, hass):
async def test_async_setup_entry_bad_cert(hass):
"""Test async_setup_entry with a bad/expired cert."""
entry = MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
)
@ -75,7 +76,7 @@ async def test_async_setup_entry_bad_cert(hass):
async def test_async_setup_entry_host_unavailable(hass):
"""Test async_setup_entry when host is unavailable."""
entry = MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
)
@ -106,7 +107,7 @@ async def test_async_setup_entry_host_unavailable(hass):
async def test_update_sensor(mock_now, hass):
"""Test async_update for sensor."""
entry = MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
)
@ -121,6 +122,8 @@ async def test_update_sensor(mock_now, hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
coordinator = hass.data[DOMAIN][entry.entry_id]
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -137,13 +140,11 @@ async def test_update_sensor(mock_now, hass):
timestamp2 = future_timestamp(99)
next_update = dt_util.utcnow() + timedelta(hours=12)
async_fire_time_changed(hass, next_update)
with patch(
"homeassistant.components.cert_expiry.get_cert_expiry_timestamp",
return_value=timestamp2,
):
await coordinator.async_refresh()
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
@ -165,7 +166,7 @@ async def test_update_sensor(mock_now, hass):
async def test_update_sensor_network_errors(mock_now, hass):
"""Test async_update for sensor."""
entry = MockConfigEntry(
domain="cert_expiry",
domain=DOMAIN,
data={CONF_HOST: HOST, CONF_PORT: PORT},
unique_id=f"{HOST}:{PORT}",
)
@ -180,6 +181,8 @@ async def test_update_sensor_network_errors(mock_now, hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
coordinator = hass.data[DOMAIN][entry.entry_id]
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -194,25 +197,21 @@ async def test_update_sensor_network_errors(mock_now, hass):
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
next_update = dt_util.utcnow() + timedelta(hours=12)
async_fire_time_changed(hass, next_update)
with patch(
"homeassistant.components.cert_expiry.helper.get_cert",
side_effect=socket.gaierror,
):
await coordinator.async_refresh()
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state.state == STATE_UNAVAILABLE
next_update = dt_util.utcnow() + timedelta(hours=12)
async_fire_time_changed(hass, next_update)
with patch(
"homeassistant.components.cert_expiry.get_cert_expiry_timestamp",
return_value=future_timestamp(99),
):
await coordinator.async_refresh()
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
@ -222,13 +221,11 @@ async def test_update_sensor_network_errors(mock_now, hass):
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
next_update = dt_util.utcnow() + timedelta(hours=12)
async_fire_time_changed(hass, next_update)
with patch(
"homeassistant.components.cert_expiry.helper.get_cert",
side_effect=ssl.SSLError("something bad"),
):
await coordinator.async_refresh()
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
@ -244,12 +241,10 @@ async def test_update_sensor_network_errors(mock_now, hass):
assert state.attributes.get("error") == "something bad"
assert not state.attributes.get("is_valid")
next_update = dt_util.utcnow() + timedelta(hours=12)
async_fire_time_changed(hass, next_update)
with patch(
"homeassistant.components.cert_expiry.helper.get_cert", side_effect=Exception()
):
await coordinator.async_refresh()
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")