Consolidate all platforms that have tests (#22109)
* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
This commit is contained in:
1 parent
46ece3603f
commit
f195ecca4b
486 files changed
+662
-455
No files matched your search
@@ -1,175 +0,0 @@
|
||||
"""The tests for generic camera component."""
|
||||
import asyncio
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_fetching_url(aioclient_mock, hass, hass_client):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get('http://example.com', text='hello world')
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'generic',
|
||||
'still_image_url': 'http://example.com',
|
||||
'username': 'user',
|
||||
'password': 'pass'
|
||||
}})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
|
||||
assert resp.status == 200
|
||||
assert aioclient_mock.call_count == 1
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello world'
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 2
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_fetching_without_verify_ssl(aioclient_mock, hass, hass_client):
|
||||
"""Test that it fetches the given url when ssl verify is off."""
|
||||
aioclient_mock.get('https://example.com', text='hello world')
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'generic',
|
||||
'still_image_url': 'https://example.com',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'verify_ssl': 'false',
|
||||
}})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_fetching_url_with_verify_ssl(aioclient_mock, hass, hass_client):
|
||||
"""Test that it fetches the given url when ssl verify is explicitly on."""
|
||||
aioclient_mock.get('https://example.com', text='hello world')
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'generic',
|
||||
'still_image_url': 'https://example.com',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'verify_ssl': 'true',
|
||||
}})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_limit_refetch(aioclient_mock, hass, hass_client):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get('http://example.com/5a', text='hello world')
|
||||
aioclient_mock.get('http://example.com/10a', text='hello world')
|
||||
aioclient_mock.get('http://example.com/15a', text='hello planet')
|
||||
aioclient_mock.get('http://example.com/20a', status=404)
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'generic',
|
||||
'still_image_url':
|
||||
'http://example.com/{{ states.sensor.temp.state + "a" }}',
|
||||
'limit_refetch_to_url_change': True,
|
||||
}})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
|
||||
hass.states.async_set('sensor.temp', '5')
|
||||
|
||||
with mock.patch('async_timeout.timeout',
|
||||
side_effect=asyncio.TimeoutError()):
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 0
|
||||
assert resp.status == 500
|
||||
|
||||
hass.states.async_set('sensor.temp', '10')
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello world'
|
||||
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello world'
|
||||
|
||||
hass.states.async_set('sensor.temp', '15')
|
||||
|
||||
# Url change = fetch new image
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello planet'
|
||||
|
||||
# Cause a template render error
|
||||
hass.states.async_remove('sensor.temp')
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello planet'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_camera_content_type(aioclient_mock, hass, hass_client):
|
||||
"""Test generic camera with custom content_type."""
|
||||
svg_image = '<some image>'
|
||||
urlsvg = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg'
|
||||
aioclient_mock.get(urlsvg, text=svg_image)
|
||||
|
||||
cam_config_svg = {
|
||||
'name': 'config_test_svg',
|
||||
'platform': 'generic',
|
||||
'still_image_url': urlsvg,
|
||||
'content_type': 'image/svg+xml',
|
||||
}
|
||||
cam_config_normal = cam_config_svg.copy()
|
||||
cam_config_normal.pop('content_type')
|
||||
cam_config_normal['name'] = 'config_test_jpg'
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': [cam_config_svg, cam_config_normal]})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
resp_1 = yield from client.get('/api/camera_proxy/camera.config_test_svg')
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert resp_1.status == 200
|
||||
assert resp_1.content_type == 'image/svg+xml'
|
||||
body = yield from resp_1.text()
|
||||
assert body == svg_image
|
||||
|
||||
resp_2 = yield from client.get('/api/camera_proxy/camera.config_test_jpg')
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert resp_2.status == 200
|
||||
assert resp_2.content_type == 'image/jpeg'
|
||||
body = yield from resp_2.text()
|
||||
assert body == svg_image
|
||||
@@ -1,150 +0,0 @@
|
||||
"""The tests for local file camera component."""
|
||||
import asyncio
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant.components.camera import DOMAIN
|
||||
from homeassistant.components.camera.local_file import (
|
||||
SERVICE_UPDATE_FILE_PATH)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_registry
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_loading_file(hass, hass_client):
|
||||
"""Test that it loads image from disk."""
|
||||
mock_registry(hass)
|
||||
|
||||
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
||||
mock.patch('os.access', mock.Mock(return_value=True)):
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'local_file',
|
||||
'file_path': 'mock.file',
|
||||
}})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
m_open = mock.mock_open(read_data=b'hello')
|
||||
with mock.patch(
|
||||
'homeassistant.components.camera.local_file.open',
|
||||
m_open, create=True
|
||||
):
|
||||
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
||||
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'hello'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_file_not_readable(hass, caplog):
|
||||
"""Test a warning is shown setup when file is not readable."""
|
||||
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
||||
mock.patch('os.access', mock.Mock(return_value=False)):
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'name': 'config_test',
|
||||
'platform': 'local_file',
|
||||
'file_path': 'mock.file',
|
||||
}})
|
||||
|
||||
assert 'Could not read' in caplog.text
|
||||
assert 'config_test' in caplog.text
|
||||
assert 'mock.file' in caplog.text
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_camera_content_type(hass, hass_client):
|
||||
"""Test local_file camera content_type."""
|
||||
cam_config_jpg = {
|
||||
'name': 'test_jpg',
|
||||
'platform': 'local_file',
|
||||
'file_path': '/path/to/image.jpg',
|
||||
}
|
||||
cam_config_png = {
|
||||
'name': 'test_png',
|
||||
'platform': 'local_file',
|
||||
'file_path': '/path/to/image.png',
|
||||
}
|
||||
cam_config_svg = {
|
||||
'name': 'test_svg',
|
||||
'platform': 'local_file',
|
||||
'file_path': '/path/to/image.svg',
|
||||
}
|
||||
cam_config_noext = {
|
||||
'name': 'test_no_ext',
|
||||
'platform': 'local_file',
|
||||
'file_path': '/path/to/image',
|
||||
}
|
||||
|
||||
yield from async_setup_component(hass, 'camera', {
|
||||
'camera': [cam_config_jpg, cam_config_png,
|
||||
cam_config_svg, cam_config_noext]})
|
||||
|
||||
client = yield from hass_client()
|
||||
|
||||
image = 'hello'
|
||||
m_open = mock.mock_open(read_data=image.encode())
|
||||
with mock.patch('homeassistant.components.camera.local_file.open',
|
||||
m_open, create=True):
|
||||
resp_1 = yield from client.get('/api/camera_proxy/camera.test_jpg')
|
||||
resp_2 = yield from client.get('/api/camera_proxy/camera.test_png')
|
||||
resp_3 = yield from client.get('/api/camera_proxy/camera.test_svg')
|
||||
resp_4 = yield from client.get('/api/camera_proxy/camera.test_no_ext')
|
||||
|
||||
assert resp_1.status == 200
|
||||
assert resp_1.content_type == 'image/jpeg'
|
||||
body = yield from resp_1.text()
|
||||
assert body == image
|
||||
|
||||
assert resp_2.status == 200
|
||||
assert resp_2.content_type == 'image/png'
|
||||
body = yield from resp_2.text()
|
||||
assert body == image
|
||||
|
||||
assert resp_3.status == 200
|
||||
assert resp_3.content_type == 'image/svg+xml'
|
||||
body = yield from resp_3.text()
|
||||
assert body == image
|
||||
|
||||
# default mime type
|
||||
assert resp_4.status == 200
|
||||
assert resp_4.content_type == 'image/jpeg'
|
||||
body = yield from resp_4.text()
|
||||
assert body == image
|
||||
|
||||
|
||||
async def test_update_file_path(hass):
|
||||
"""Test update_file_path service."""
|
||||
# Setup platform
|
||||
|
||||
mock_registry(hass)
|
||||
|
||||
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
||||
mock.patch('os.access', mock.Mock(return_value=True)):
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'local_file',
|
||||
'file_path': 'mock/path.jpg'
|
||||
}
|
||||
})
|
||||
|
||||
# Fetch state and check motion detection attribute
|
||||
state = hass.states.get('camera.local_file')
|
||||
assert state.attributes.get('friendly_name') == 'Local File'
|
||||
assert state.attributes.get('file_path') == 'mock/path.jpg'
|
||||
|
||||
service_data = {
|
||||
"entity_id": 'camera.local_file',
|
||||
"file_path": 'new/path.jpg'
|
||||
}
|
||||
|
||||
await hass.services.async_call(DOMAIN,
|
||||
SERVICE_UPDATE_FILE_PATH,
|
||||
service_data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('camera.local_file')
|
||||
assert state.attributes.get('file_path') == 'new/path.jpg'
|
||||
@@ -1,66 +0,0 @@
|
||||
"""The tests for generic camera component."""
|
||||
import io
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
|
||||
async def test_bad_posting(hass, aiohttp_client):
|
||||
"""Test that posting to wrong api endpoint fails."""
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
'webhook_id': 'camera.config_test'
|
||||
}})
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get('camera.config_test') is not None
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
# missing file
|
||||
async with client.post('/api/webhook/camera.config_test') as resp:
|
||||
assert resp.status == 200 # webhooks always return 200
|
||||
|
||||
camera_state = hass.states.get('camera.config_test')
|
||||
assert camera_state.state == 'idle' # no file supplied we are still idle
|
||||
|
||||
|
||||
async def test_posting_url(hass, aiohttp_client):
|
||||
"""Test that posting to api endpoint works."""
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
'webhook_id': 'camera.config_test'
|
||||
}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
files = {'image': io.BytesIO(b'fake')}
|
||||
|
||||
# initial state
|
||||
camera_state = hass.states.get('camera.config_test')
|
||||
assert camera_state.state == 'idle'
|
||||
|
||||
# post image
|
||||
resp = await client.post(
|
||||
'/api/webhook/camera.config_test',
|
||||
data=files)
|
||||
assert resp.status == 200
|
||||
|
||||
# state recording
|
||||
camera_state = hass.states.get('camera.config_test')
|
||||
assert camera_state.state == 'recording'
|
||||
|
||||
# await timeout
|
||||
shifted_time = dt_util.utcnow() + timedelta(seconds=15)
|
||||
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: shifted_time})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# back to initial state
|
||||
camera_state = hass.states.get('camera.config_test')
|
||||
assert camera_state.state == 'idle'
|
||||
@@ -1,338 +0,0 @@
|
||||
"""The tests for UVC camera module."""
|
||||
import socket
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import requests
|
||||
from uvcclient import camera
|
||||
from uvcclient import nvr
|
||||
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.components.camera import uvc
|
||||
from tests.common import get_test_home_assistant
|
||||
import pytest
|
||||
|
||||
|
||||
class TestUVCSetup(unittest.TestCase):
|
||||
"""Test the UVC camera platform."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@mock.patch('uvcclient.nvr.UVCRemote')
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
def test_setup_full_config(self, mock_uvc, mock_remote):
|
||||
"""Test the setup with full configuration."""
|
||||
config = {
|
||||
'platform': 'uvc',
|
||||
'nvr': 'foo',
|
||||
'password': 'bar',
|
||||
'port': 123,
|
||||
'key': 'secret',
|
||||
}
|
||||
mock_cameras = [
|
||||
{'uuid': 'one', 'name': 'Front', 'id': 'id1'},
|
||||
{'uuid': 'two', 'name': 'Back', 'id': 'id2'},
|
||||
{'uuid': 'three', 'name': 'Old AirCam', 'id': 'id3'},
|
||||
]
|
||||
|
||||
def mock_get_camera(uuid):
|
||||
"""Create a mock camera."""
|
||||
if uuid == 'id3':
|
||||
return {'model': 'airCam'}
|
||||
return {'model': 'UVC'}
|
||||
|
||||
mock_remote.return_value.index.return_value = mock_cameras
|
||||
mock_remote.return_value.get_camera.side_effect = mock_get_camera
|
||||
mock_remote.return_value.server_version = (3, 2, 0)
|
||||
|
||||
assert setup_component(self.hass, 'camera', {'camera': config})
|
||||
|
||||
assert mock_remote.call_count == 1
|
||||
assert mock_remote.call_args == mock.call(
|
||||
'foo',
|
||||
123,
|
||||
'secret',
|
||||
ssl=False
|
||||
)
|
||||
mock_uvc.assert_has_calls([
|
||||
mock.call(mock_remote.return_value, 'id1', 'Front', 'bar'),
|
||||
mock.call(mock_remote.return_value, 'id2', 'Back', 'bar'),
|
||||
])
|
||||
|
||||
@mock.patch('uvcclient.nvr.UVCRemote')
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
def test_setup_partial_config(self, mock_uvc, mock_remote):
|
||||
"""Test the setup with partial configuration."""
|
||||
config = {
|
||||
'platform': 'uvc',
|
||||
'nvr': 'foo',
|
||||
'key': 'secret',
|
||||
}
|
||||
mock_cameras = [
|
||||
{'uuid': 'one', 'name': 'Front', 'id': 'id1'},
|
||||
{'uuid': 'two', 'name': 'Back', 'id': 'id2'},
|
||||
]
|
||||
mock_remote.return_value.index.return_value = mock_cameras
|
||||
mock_remote.return_value.get_camera.return_value = {'model': 'UVC'}
|
||||
mock_remote.return_value.server_version = (3, 2, 0)
|
||||
|
||||
assert setup_component(self.hass, 'camera', {'camera': config})
|
||||
|
||||
assert mock_remote.call_count == 1
|
||||
assert mock_remote.call_args == mock.call(
|
||||
'foo',
|
||||
7080,
|
||||
'secret',
|
||||
ssl=False
|
||||
)
|
||||
mock_uvc.assert_has_calls([
|
||||
mock.call(mock_remote.return_value, 'id1', 'Front', 'ubnt'),
|
||||
mock.call(mock_remote.return_value, 'id2', 'Back', 'ubnt'),
|
||||
])
|
||||
|
||||
@mock.patch('uvcclient.nvr.UVCRemote')
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
def test_setup_partial_config_v31x(self, mock_uvc, mock_remote):
|
||||
"""Test the setup with a v3.1.x server."""
|
||||
config = {
|
||||
'platform': 'uvc',
|
||||
'nvr': 'foo',
|
||||
'key': 'secret',
|
||||
}
|
||||
mock_cameras = [
|
||||
{'uuid': 'one', 'name': 'Front', 'id': 'id1'},
|
||||
{'uuid': 'two', 'name': 'Back', 'id': 'id2'},
|
||||
]
|
||||
mock_remote.return_value.index.return_value = mock_cameras
|
||||
mock_remote.return_value.get_camera.return_value = {'model': 'UVC'}
|
||||
mock_remote.return_value.server_version = (3, 1, 3)
|
||||
|
||||
assert setup_component(self.hass, 'camera', {'camera': config})
|
||||
|
||||
assert mock_remote.call_count == 1
|
||||
assert mock_remote.call_args == mock.call(
|
||||
'foo',
|
||||
7080,
|
||||
'secret',
|
||||
ssl=False
|
||||
)
|
||||
mock_uvc.assert_has_calls([
|
||||
mock.call(mock_remote.return_value, 'one', 'Front', 'ubnt'),
|
||||
mock.call(mock_remote.return_value, 'two', 'Back', 'ubnt'),
|
||||
])
|
||||
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
def test_setup_incomplete_config(self, mock_uvc):
|
||||
"""Test the setup with incomplete configuration."""
|
||||
assert setup_component(
|
||||
self.hass, 'camera', {'platform': 'uvc', 'nvr': 'foo'})
|
||||
assert not mock_uvc.called
|
||||
assert setup_component(
|
||||
self.hass, 'camera', {'platform': 'uvc', 'key': 'secret'})
|
||||
assert not mock_uvc.called
|
||||
assert setup_component(
|
||||
self.hass, 'camera', {'platform': 'uvc', 'port': 'invalid'})
|
||||
assert not mock_uvc.called
|
||||
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
@mock.patch('uvcclient.nvr.UVCRemote')
|
||||
def setup_nvr_errors_during_indexing(self, error, mock_remote, mock_uvc):
|
||||
"""Set up test for NVR errors during indexing."""
|
||||
config = {
|
||||
'platform': 'uvc',
|
||||
'nvr': 'foo',
|
||||
'key': 'secret',
|
||||
}
|
||||
mock_remote.return_value.index.side_effect = error
|
||||
assert setup_component(self.hass, 'camera', {'camera': config})
|
||||
assert not mock_uvc.called
|
||||
|
||||
def test_setup_nvr_error_during_indexing_notauthorized(self):
|
||||
"""Test for error: nvr.NotAuthorized."""
|
||||
self.setup_nvr_errors_during_indexing(nvr.NotAuthorized)
|
||||
|
||||
def test_setup_nvr_error_during_indexing_nvrerror(self):
|
||||
"""Test for error: nvr.NvrError."""
|
||||
self.setup_nvr_errors_during_indexing(nvr.NvrError)
|
||||
pytest.raises(PlatformNotReady)
|
||||
|
||||
def test_setup_nvr_error_during_indexing_connectionerror(self):
|
||||
"""Test for error: requests.exceptions.ConnectionError."""
|
||||
self.setup_nvr_errors_during_indexing(
|
||||
requests.exceptions.ConnectionError)
|
||||
pytest.raises(PlatformNotReady)
|
||||
|
||||
@mock.patch.object(uvc, 'UnifiVideoCamera')
|
||||
@mock.patch('uvcclient.nvr.UVCRemote.__init__')
|
||||
def setup_nvr_errors_during_initialization(self, error, mock_remote,
|
||||
mock_uvc):
|
||||
"""Set up test for NVR errors during initialization."""
|
||||
config = {
|
||||
'platform': 'uvc',
|
||||
'nvr': 'foo',
|
||||
'key': 'secret',
|
||||
}
|
||||
mock_remote.return_value = None
|
||||
mock_remote.side_effect = error
|
||||
assert setup_component(self.hass, 'camera', {'camera': config})
|
||||
assert not mock_remote.index.called
|
||||
assert not mock_uvc.called
|
||||
|
||||
def test_setup_nvr_error_during_initialization_notauthorized(self):
|
||||
"""Test for error: nvr.NotAuthorized."""
|
||||
self.setup_nvr_errors_during_initialization(nvr.NotAuthorized)
|
||||
|
||||
def test_setup_nvr_error_during_initialization_nvrerror(self):
|
||||
"""Test for error: nvr.NvrError."""
|
||||
self.setup_nvr_errors_during_initialization(nvr.NvrError)
|
||||
pytest.raises(PlatformNotReady)
|
||||
|
||||
def test_setup_nvr_error_during_initialization_connectionerror(self):
|
||||
"""Test for error: requests.exceptions.ConnectionError."""
|
||||
self.setup_nvr_errors_during_initialization(
|
||||
requests.exceptions.ConnectionError)
|
||||
pytest.raises(PlatformNotReady)
|
||||
|
||||
|
||||
class TestUVC(unittest.TestCase):
|
||||
"""Test class for UVC."""
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up the mock camera."""
|
||||
self.nvr = mock.MagicMock()
|
||||
self.uuid = 'uuid'
|
||||
self.name = 'name'
|
||||
self.password = 'seekret'
|
||||
self.uvc = uvc.UnifiVideoCamera(self.nvr, self.uuid, self.name,
|
||||
self.password)
|
||||
self.nvr.get_camera.return_value = {
|
||||
'model': 'UVC Fake',
|
||||
'recordingSettings': {
|
||||
'fullTimeRecordEnabled': True,
|
||||
},
|
||||
'host': 'host-a',
|
||||
'internalHost': 'host-b',
|
||||
'username': 'admin',
|
||||
}
|
||||
self.nvr.server_version = (3, 2, 0)
|
||||
|
||||
def test_properties(self):
|
||||
"""Test the properties."""
|
||||
assert self.name == self.uvc.name
|
||||
assert self.uvc.is_recording
|
||||
assert 'Ubiquiti' == self.uvc.brand
|
||||
assert 'UVC Fake' == self.uvc.model
|
||||
|
||||
@mock.patch('uvcclient.store.get_info_store')
|
||||
@mock.patch('uvcclient.camera.UVCCameraClientV320')
|
||||
def test_login(self, mock_camera, mock_store):
|
||||
"""Test the login."""
|
||||
self.uvc._login()
|
||||
assert mock_camera.call_count == 1
|
||||
assert mock_camera.call_args == mock.call('host-a', 'admin', 'seekret')
|
||||
assert mock_camera.return_value.login.call_count == 1
|
||||
assert mock_camera.return_value.login.call_args == mock.call()
|
||||
|
||||
@mock.patch('uvcclient.store.get_info_store')
|
||||
@mock.patch('uvcclient.camera.UVCCameraClient')
|
||||
def test_login_v31x(self, mock_camera, mock_store):
|
||||
"""Test login with v3.1.x server."""
|
||||
self.nvr.server_version = (3, 1, 3)
|
||||
self.uvc._login()
|
||||
assert mock_camera.call_count == 1
|
||||
assert mock_camera.call_args == mock.call('host-a', 'admin', 'seekret')
|
||||
assert mock_camera.return_value.login.call_count == 1
|
||||
assert mock_camera.return_value.login.call_args == mock.call()
|
||||
|
||||
@mock.patch('uvcclient.store.get_info_store')
|
||||
@mock.patch('uvcclient.camera.UVCCameraClientV320')
|
||||
def test_login_tries_both_addrs_and_caches(self, mock_camera, mock_store):
|
||||
"""Test the login tries."""
|
||||
responses = [0]
|
||||
|
||||
def mock_login(*a):
|
||||
"""Mock login."""
|
||||
try:
|
||||
responses.pop(0)
|
||||
raise socket.error
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
mock_store.return_value.get_camera_password.return_value = None
|
||||
mock_camera.return_value.login.side_effect = mock_login
|
||||
self.uvc._login()
|
||||
assert 2 == mock_camera.call_count
|
||||
assert 'host-b' == self.uvc._connect_addr
|
||||
|
||||
mock_camera.reset_mock()
|
||||
self.uvc._login()
|
||||
assert mock_camera.call_count == 1
|
||||
assert mock_camera.call_args == mock.call('host-b', 'admin', 'seekret')
|
||||
assert mock_camera.return_value.login.call_count == 1
|
||||
assert mock_camera.return_value.login.call_args == mock.call()
|
||||
|
||||
@mock.patch('uvcclient.store.get_info_store')
|
||||
@mock.patch('uvcclient.camera.UVCCameraClientV320')
|
||||
def test_login_fails_both_properly(self, mock_camera, mock_store):
|
||||
"""Test if login fails properly."""
|
||||
mock_camera.return_value.login.side_effect = socket.error
|
||||
assert self.uvc._login() is None
|
||||
assert self.uvc._connect_addr is None
|
||||
|
||||
def test_camera_image_tries_login_bails_on_failure(self):
|
||||
"""Test retrieving failure."""
|
||||
with mock.patch.object(self.uvc, '_login') as mock_login:
|
||||
mock_login.return_value = False
|
||||
assert self.uvc.camera_image() is None
|
||||
assert mock_login.call_count == 1
|
||||
assert mock_login.call_args == mock.call()
|
||||
|
||||
def test_camera_image_logged_in(self):
|
||||
"""Test the login state."""
|
||||
self.uvc._camera = mock.MagicMock()
|
||||
assert self.uvc._camera.get_snapshot.return_value == \
|
||||
self.uvc.camera_image()
|
||||
|
||||
def test_camera_image_error(self):
|
||||
"""Test the camera image error."""
|
||||
self.uvc._camera = mock.MagicMock()
|
||||
self.uvc._camera.get_snapshot.side_effect = camera.CameraConnectError
|
||||
assert self.uvc.camera_image() is None
|
||||
|
||||
def test_camera_image_reauths(self):
|
||||
"""Test the re-authentication."""
|
||||
responses = [0]
|
||||
|
||||
def mock_snapshot():
|
||||
"""Mock snapshot."""
|
||||
try:
|
||||
responses.pop()
|
||||
raise camera.CameraAuthError()
|
||||
except IndexError:
|
||||
pass
|
||||
return 'image'
|
||||
|
||||
self.uvc._camera = mock.MagicMock()
|
||||
self.uvc._camera.get_snapshot.side_effect = mock_snapshot
|
||||
with mock.patch.object(self.uvc, '_login') as mock_login:
|
||||
assert 'image' == self.uvc.camera_image()
|
||||
assert mock_login.call_count == 1
|
||||
assert mock_login.call_args == mock.call()
|
||||
assert [] == responses
|
||||
|
||||
def test_camera_image_reauths_only_once(self):
|
||||
"""Test if the re-authentication only happens once."""
|
||||
self.uvc._camera = mock.MagicMock()
|
||||
self.uvc._camera.get_snapshot.side_effect = camera.CameraAuthError
|
||||
with mock.patch.object(self.uvc, '_login') as mock_login:
|
||||
with pytest.raises(camera.CameraAuthError):
|
||||
self.uvc.camera_image()
|
||||
assert mock_login.call_count == 1
|
||||
assert mock_login.call_args == mock.call()
|
||||
Reference in new issue
Block a user