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,271 +1,228 @@
"""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."""
def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, 'zone')
mock_component(self.hass, 'group')
self.host = "127.0.0.1"
def teardown_method(self):
"""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( aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host), "http://{}/common_page/login.html".format(HOST),
cookies={'sessionToken': '654321'} exc=asyncio.TimeoutError()
) )
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',
) )
with assert_setup_component(1, DOMAIN): assert await async_setup_component(
assert setup_component( hass, DOMAIN, {
self.hass, DOMAIN, {DOMAIN: { DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}})
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
@patch('homeassistant.components.device_tracker._LOGGER.error') assert 'Error setting up platform' in caplog.text
def test_setup_platform_timeout_webservice(self, mock_error,
aioclient_mock):
async def test_setup_platform_timeout_webservice(hass, caplog, aioclient_mock):
"""Set up a platform with api timeout.""" """Set up a platform with api timeout."""
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'},
content=b'successful', content=b'successful',
exc=asyncio.TimeoutError() exc=asyncio.TimeoutError()
) )
with assert_setup_component(1, DOMAIN): assert await async_setup_component(
assert setup_component( hass, DOMAIN, {
self.hass, DOMAIN, {DOMAIN: { DOMAIN: {CONF_PLATFORM: 'upc_connect', CONF_HOST: HOST}})
CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host
}})
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert 'Error setting up platform' in \ assert 'Error setting up platform' in caplog.text
str(mock_error.call_args_list[-1])
@patch('homeassistant.components.device_tracker._LOGGER.error')
def test_setup_platform_timeout_loginpage(self, mock_error, @patch('homeassistant.components.device_tracker.upc_connect.'
aioclient_mock): 'UPCDeviceScanner.async_scan_devices',
"""Set up a platform with timeout on loginpage.""" return_value=async_scan_devices_mock)
async def test_setup_platform(scan_mock, hass, aioclient_mock):
"""Set up a platform."""
aioclient_mock.get( aioclient_mock.get(
"http://{}/common_page/login.html".format(self.host), "http://{}/common_page/login.html".format(HOST),
exc=asyncio.TimeoutError() 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'
) )
with assert_setup_component(1, DOMAIN): with assert_setup_component(1, DOMAIN):
assert setup_component( assert await async_setup_component(
self.hass, DOMAIN, {DOMAIN: { hass, DOMAIN, {DOMAIN: {
CONF_PLATFORM: 'upc_connect', CONF_PLATFORM: 'upc_connect',
CONF_HOST: self.host CONF_HOST: HOST
}}) }})
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert 'Error setting up platform' in \
str(mock_error.call_args_list[-1])
def test_scan_devices(self, aioclient_mock): async def test_scan_devices(hass, aioclient_mock):
"""Set up a upc platform and scan device.""" """Set up a upc platform and scan device."""
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.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'}
) )
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) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123' 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', 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(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):
async def test_scan_devices_without_session_wrong_re(hass, aioclient_mock):
"""Set up a upc platform and scan device with no token and wrong.""" """Set up a upc platform and scan device with no token and wrong."""
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),
status=400, status=400,
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 == [] assert mac_list == []
def test_scan_devices_parse_error(self, aioclient_mock):
async def test_scan_devices_parse_error(hass, aioclient_mock):
"""Set up a upc platform and scan device with parse error.""" """Set up a upc platform and scan device with parse error."""
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.post( aioclient_mock.post(
"http://{}/xml/getter.xml".format(self.host), "http://{}/xml/getter.xml".format(HOST),
text="Blablebla blabalble", text="Blablebla blabalble",
cookies={'sessionToken': '1235678'} cookies={'sessionToken': '1235678'}
) )
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) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123' assert aioclient_mock.mock_calls[0][2] == 'token=654321&fun=123'