2017-10-13 06:58:23 +00:00
|
|
|
"""Test the NamecheapDNS component."""
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components import namecheapdns
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 13:46:24 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2017-10-13 06:58:23 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
2023-02-08 15:48:54 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2017-10-13 06:58:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
HOST = "test"
|
|
|
|
DOMAIN = "bla"
|
|
|
|
PASSWORD = "abcdefgh"
|
2017-10-13 06:58:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def setup_namecheapdns(hass, aioclient_mock):
|
|
|
|
"""Fixture that sets up NamecheapDNS."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
namecheapdns.UPDATE_URL,
|
|
|
|
params={"host": HOST, "domain": DOMAIN, "password": PASSWORD},
|
|
|
|
text="<interface-response><ErrCount>0</ErrCount></interface-response>",
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(
|
|
|
|
hass,
|
|
|
|
namecheapdns.DOMAIN,
|
|
|
|
{"namecheapdns": {"host": HOST, "domain": DOMAIN, "password": PASSWORD}},
|
|
|
|
)
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_setup(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
|
2017-10-13 06:58:23 +00:00
|
|
|
"""Test setup works if update passes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
namecheapdns.UPDATE_URL,
|
|
|
|
params={"host": HOST, "domain": DOMAIN, "password": PASSWORD},
|
|
|
|
text="<interface-response><ErrCount>0</ErrCount></interface-response>",
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
|
2020-01-06 00:22:22 +00:00
|
|
|
result = await async_setup_component(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
namecheapdns.DOMAIN,
|
|
|
|
{"namecheapdns": {"host": HOST, "domain": DOMAIN, "password": PASSWORD}},
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
assert result
|
|
|
|
assert aioclient_mock.call_count == 1
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(minutes=5))
|
2020-01-06 00:22:22 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-10-13 06:58:23 +00:00
|
|
|
assert aioclient_mock.call_count == 2
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_setup_fails_if_update_fails(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2017-10-13 06:58:23 +00:00
|
|
|
"""Test setup fails if first update fails."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
namecheapdns.UPDATE_URL,
|
|
|
|
params={"host": HOST, "domain": DOMAIN, "password": PASSWORD},
|
|
|
|
text="<interface-response><ErrCount>1</ErrCount></interface-response>",
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
|
2020-01-06 00:22:22 +00:00
|
|
|
result = await async_setup_component(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
namecheapdns.DOMAIN,
|
|
|
|
{"namecheapdns": {"host": HOST, "domain": DOMAIN, "password": PASSWORD}},
|
|
|
|
)
|
2017-10-13 06:58:23 +00:00
|
|
|
assert not result
|
|
|
|
assert aioclient_mock.call_count == 1
|