2017-10-27 08:15:47 +00:00
|
|
|
"""Test the NO-IP component."""
|
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.components import no_ip
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "test.example.com"
|
2017-10-27 08:15:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PASSWORD = "xyz789"
|
2017-10-27 08:15:47 +00:00
|
|
|
|
|
|
|
UPDATE_URL = no_ip.UPDATE_URL
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
USERNAME = "abc@123.com"
|
2017-10-27 08:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def setup_no_ip(hass, aioclient_mock):
|
|
|
|
"""Fixture that sets up NO-IP."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="good 0.0.0.0")
|
|
|
|
|
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(
|
|
|
|
hass,
|
|
|
|
no_ip.DOMAIN,
|
|
|
|
{
|
|
|
|
no_ip.DOMAIN: {
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"username": USERNAME,
|
|
|
|
"password": PASSWORD,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2017-10-27 08:15:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_setup(hass, aioclient_mock):
|
|
|
|
"""Test setup works if update passes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nochg 0.0.0.0")
|
|
|
|
|
|
|
|
result = yield from async_setup_component(
|
|
|
|
hass,
|
|
|
|
no_ip.DOMAIN,
|
|
|
|
{no_ip.DOMAIN: {"domain": DOMAIN, "username": USERNAME, "password": PASSWORD}},
|
|
|
|
)
|
2017-10-27 08:15:47 +00:00
|
|
|
assert result
|
|
|
|
assert aioclient_mock.call_count == 1
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(minutes=5))
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert aioclient_mock.call_count == 2
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_setup_fails_if_update_fails(hass, aioclient_mock):
|
|
|
|
"""Test setup fails if first update fails."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nohost")
|
|
|
|
|
|
|
|
result = yield from async_setup_component(
|
|
|
|
hass,
|
|
|
|
no_ip.DOMAIN,
|
|
|
|
{no_ip.DOMAIN: {"domain": DOMAIN, "username": USERNAME, "password": PASSWORD}},
|
|
|
|
)
|
2017-10-27 08:15:47 +00:00
|
|
|
assert not result
|
|
|
|
assert aioclient_mock.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_setup_fails_if_wrong_auth(hass, aioclient_mock):
|
|
|
|
"""Test setup fails if first update fails through wrong authentication."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="badauth")
|
|
|
|
|
|
|
|
result = yield from async_setup_component(
|
|
|
|
hass,
|
|
|
|
no_ip.DOMAIN,
|
|
|
|
{no_ip.DOMAIN: {"domain": DOMAIN, "username": USERNAME, "password": PASSWORD}},
|
|
|
|
)
|
2017-10-27 08:15:47 +00:00
|
|
|
assert not result
|
|
|
|
assert aioclient_mock.call_count == 1
|