Clean up upc_connect tests (#21150)

This commit is contained in:
Martin Hjelmare 2019-02-18 15:58:25 +01:00 committed by Andrew Sayre
parent 0b77a89a2f
commit 0ab9b006f0

View File

@ -1,273 +1,230 @@
"""The tests for the UPC ConnextBox device tracker platform.""" """The tests for the UPC ConnextBox device tracker platform."""
import asyncio import asyncio
from unittest.mock import patch
import logging
from asynctest import patch
import pytest import pytest
from homeassistant.setup import setup_component
from homeassistant.const import (
CONF_PLATFORM, CONF_HOST)
from homeassistant.components.device_tracker import DOMAIN from homeassistant.components.device_tracker import DOMAIN
import homeassistant.components.device_tracker.upc_connect as platform import homeassistant.components.device_tracker.upc_connect as platform
from homeassistant.util.async_ import run_coroutine_threadsafe from homeassistant.const import CONF_HOST, CONF_PLATFORM
from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import assert_setup_component, load_fixture, mock_component
get_test_home_assistant, assert_setup_component, load_fixture,
mock_component, mock_coro)
_LOGGER = logging.getLogger(__name__) HOST = "127.0.0.1"
@asyncio.coroutine async def async_scan_devices_mock(scanner):
def async_scan_devices_mock(scanner):
"""Mock async_scan_devices.""" """Mock async_scan_devices."""
return [] return []
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_load_config(): def setup_comp_deps(hass, mock_device_tracker_conf):
"""Mock device tracker loading config.""" """Set up component dependencies."""
with patch('homeassistant.components.device_tracker.async_load_config', mock_component(hass, 'zone')
return_value=mock_coro([])): mock_component(hass, 'group')
yield yield
class TestUPCConnect: async def test_setup_platform_timeout_loginpage(hass, caplog, aioclient_mock):
"""Tests for the Ddwrt device tracker platform.""" """Set up a platform with timeout on loginpage."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(HOST),
exc=asyncio.TimeoutError()
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
content=b'successful',
)
def setup_method(self): assert await async_setup_component(
"""Set up things to be run when tests are started.""" hass, DOMAIN, {
self.hass = get_test_home_assistant() DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
mock_component(self.hass, 'zone')
mock_component(self.hass, 'group')
self.host = "127.0.0.1" assert len(aioclient_mock.mock_calls) == 1
def teardown_method(self): assert 'Error setting up platform' in caplog.text
"""Stop everything that was started."""
self.hass.stop()
@patch('homeassistant.components.device_tracker.upc_connect.'
'UPCDeviceScanner.async_scan_devices',
return_value=async_scan_devices_mock)
def test_setup_platform(self, scan_mock, aioclient_mock):
"""Set up a platform."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
content=b'successful'
)
with assert_setup_component(1, DOMAIN): async def test_setup_platform_timeout_webservice(hass, caplog, aioclient_mock):
assert setup_component( """Set up a platform with api timeout."""
self.hass, DOMAIN, {DOMAIN: { aioclient_mock.get(
CONF_PLATFORM: 'upc_connect', "http://{}/common_page/login.html".format(HOST),
CONF_HOST: self.host cookies={'sessionToken': '654321'},
}}) content=b'successful',
exc=asyncio.TimeoutError()
)
assert len(aioclient_mock.mock_calls) == 1 assert await async_setup_component(
hass, DOMAIN, {
DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
@patch('homeassistant.components.device_tracker._LOGGER.error') assert len(aioclient_mock.mock_calls) == 1
def test_setup_platform_timeout_webservice(self, mock_error,
aioclient_mock):
"""Set up a platform with api timeout."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'},
content=b'successful',
exc=asyncio.TimeoutError()
)
with assert_setup_component(1, DOMAIN): assert 'Error setting up platform' in caplog.text
assert setup_component(
self.hass, DOMAIN, {DOMAIN: {
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}})
assert len(aioclient_mock.mock_calls) == 1
assert 'Error setting up platform' in \ @patch('homeassistant.components.device_tracker.upc_connect.'
str(mock_error.call_args_list[-1]) 'UPCDeviceScanner.async_scan_devices',
return_value=async_scan_devices_mock)
async def test_setup_platform(scan_mock, hass, aioclient_mock):
"""Set up a platform."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
content=b'successful'
)
@patch('homeassistant.components.device_tracker._LOGGER.error') with assert_setup_component(1, DOMAIN):
def test_setup_platform_timeout_loginpage(self, mock_error, assert await async_setup_component(
aioclient_mock): hass, DOMAIN, {DOMAIN: {
"""Set up a platform with timeout on loginpage.""" CONF_PLATFORM: 'upc_connect',
aioclient_mock.get( CONF_HOST: HOST
"http://{}/common_page/login.html".format(self.host), }})
exc=asyncio.TimeoutError()
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
content=b'successful',
)
with assert_setup_component(1, DOMAIN): assert len(aioclient_mock.mock_calls) == 1
assert setup_component(
self.hass, DOMAIN, {DOMAIN: {
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}})
assert len(aioclient_mock.mock_calls) == 1
assert 'Error setting up platform' in \ async def test_scan_devices(hass, aioclient_mock):
str(mock_error.call_args_list[-1]) """Set up a upc platform and scan device."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
content=b'successful',
cookies={'sessionToken': '654321'}
)
def test_scan_devices(self, aioclient_mock): scanner = await platform.async_get_scanner(
"""Set up a upc platform and scan device.""" hass, {
aioclient_mock.get( DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
content=b'successful',
cookies={'sessionToken': '654321'}
)
scanner = run_coroutine_threadsafe(platform.async_get_scanner( assert len(aioclient_mock.mock_calls) == 1
self.hass, {DOMAIN: {
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}}
), self.hass.loop).result()
assert len(aioclient_mock.mock_calls) == 1 aioclient_mock.clear_requests()
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
text=load_fixture('upc_connect.xml'),
cookies={'sessionToken': '1235678'}
)
aioclient_mock.clear_requests() mac_list = await scanner.async_scan_devices()
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
text=load_fixture('upc_connect.xml'),
cookies={'sessionToken': '1235678'}
)
mac_list = run_coroutine_threadsafe( assert len(aioclient_mock.mock_calls) == 1
scanner.async_scan_devices(), self.hass.loop).result() assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123'
assert mac_list == ['30:D3:2D:0:69:21', '5C:AA:FD:25:32:02',
'70:EE:50:27:A1:38']
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123'
assert mac_list == ['30:D3:2D:0:69:21', '5C:AA:FD:25:32:02',
'70:EE:50:27:A1:38']
def test_scan_devices_without_session(self, aioclient_mock): async def test_scan_devices_without_session(hass, aioclient_mock):
"""Set up a upc platform and scan device with no token.""" """Set up a upc platform and scan device with no token."""
aioclient_mock.get( aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host), "http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'} cookies={'sessionToken': '654321'}
) )
aioclient_mock.post( aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host), "http://{}/xml/getter.xml".format(HOST),
content=b'successful', content=b'successful',
cookies={'sessionToken': '654321'} cookies={'sessionToken': '654321'}
) )
scanner = run_coroutine_threadsafe(platform.async_get_scanner( scanner = await platform.async_get_scanner(
self.hass, {DOMAIN: { hass, {
CONF_PLATFORM: 'upc_connect', DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
CONF_HOST: self.host
}}
), self.hass.loop).result()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
aioclient_mock.get( aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host), "http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'} cookies={'sessionToken': '654321'}
) )
aioclient_mock.post( aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host), "http://{}/xml/getter.xml".format(HOST),
text=load_fixture('upc_connect.xml'), text=load_fixture('upc_connect.xml'),
cookies={'sessionToken': '1235678'} cookies={'sessionToken': '1235678'}
) )
scanner.token = None scanner.token = None
mac_list = run_coroutine_threadsafe( mac_list = await scanner.async_scan_devices()
scanner.async_scan_devices(), self.hass.loop).result()
assert len(aioclient_mock.mock_calls) == 2 assert len(aioclient_mock.mock_calls) == 2
assert aioclient_mock.mock_calls[1][2] == 'token=654321&fun=123' assert aioclient_mock.mock_calls[1][2] == 'token=654321&fun=123'
assert mac_list == ['30:D3:2D:0:69:21', '5C:AA:FD:25:32:02', assert mac_list == ['30:D3:2D:0:69:21', '5C:AA:FD:25:32:02',
'70:EE:50:27:A1:38'] '70:EE:50:27:A1:38']
def test_scan_devices_without_session_wrong_re(self, aioclient_mock):
"""Set up a upc platform and scan device with no token and wrong."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
content=b'successful',
cookies={'sessionToken': '654321'}
)
scanner = run_coroutine_threadsafe(platform.async_get_scanner( async def test_scan_devices_without_session_wrong_re(hass, aioclient_mock):
self.hass, {DOMAIN: { """Set up a upc platform and scan device with no token and wrong."""
CONF_PLATFORM: 'upc_connect', aioclient_mock.get(
CONF_HOST: self.host "http://{}/common_page/login.html".format(HOST),
}} cookies={'sessionToken': '654321'}
), self.hass.loop).result() )
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
content=b'successful',
cookies={'sessionToken': '654321'}
)
assert len(aioclient_mock.mock_calls) == 1 scanner = await platform.async_get_scanner(
hass, {
DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
aioclient_mock.clear_requests() assert len(aioclient_mock.mock_calls) == 1
aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
status=400,
cookies={'sessionToken': '1235678'}
)
scanner.token = None aioclient_mock.clear_requests()
mac_list = run_coroutine_threadsafe( aioclient_mock.get(
scanner.async_scan_devices(), self.hass.loop).result() "http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
status=400,
cookies={'sessionToken': '1235678'}
)
assert len(aioclient_mock.mock_calls) == 2 scanner.token = None
assert aioclient_mock.mock_calls[1][2] == 'token=654321&fun=123' mac_list = await scanner.async_scan_devices()
assert mac_list == []
def test_scan_devices_parse_error(self, aioclient_mock): assert len(aioclient_mock.mock_calls) == 2
"""Set up a upc platform and scan device with parse error.""" assert aioclient_mock.mock_calls[1][2] == 'token=654321&fun=123'
aioclient_mock.get( assert mac_list == []
"http://{}/common_page/login.html".format(self.host),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host),
content=b'successful',
cookies={'sessionToken': '654321'}
)
scanner = run_coroutine_threadsafe(platform.async_get_scanner(
self.hass, {DOMAIN: {
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}}
), self.hass.loop).result()
assert len(aioclient_mock.mock_calls) == 1 async def test_scan_devices_parse_error(hass, aioclient_mock):
"""Set up a upc platform and scan device with parse error."""
aioclient_mock.get(
"http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'}
)
aioclient_mock.post(
"http://{}/xml/getter.xml".format(HOST),
content=b'successful',
cookies={'sessionToken': '654321'}
)
aioclient_mock.clear_requests() scanner = await platform.async_get_scanner(
aioclient_mock.post( hass, {
"http://{}/xml/getter.xml".format(self.host), DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
text="Blablebla blabalble",
cookies={'sessionToken': '1235678'}
)
mac_list = run_coroutine_threadsafe( assert len(aioclient_mock.mock_calls) == 1
scanner.async_scan_devices(), self.hass.loop).result()
assert len(aioclient_mock.mock_calls) == 1 aioclient_mock.clear_requests()
assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123' aioclient_mock.post(
assert scanner.token is None "http://{}/xml/getter.xml".format(HOST),
assert mac_list == [] text="Blablebla blabalble",
cookies={'sessionToken': '1235678'}
)
mac_list = await scanner.async_scan_devices()
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123'
assert scanner.token is None
assert mac_list == []