2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Unifi WAP device tracker platform."""
|
2016-02-19 23:19:03 +00:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
import urllib
|
|
|
|
|
2016-12-06 05:35:54 +00:00
|
|
|
from pyunifi import controller
|
2016-09-04 08:06:16 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-12-04 06:11:52 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
2016-09-04 08:06:16 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN, unifi as unifi
|
|
|
|
from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
|
2017-03-10 10:15:21 +00:00
|
|
|
CONF_PLATFORM, CONF_VERIFY_SSL)
|
2016-02-19 23:19:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestUnifiScanner(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Unifiy platform."""
|
|
|
|
|
2016-12-04 06:11:52 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""Initialize values for this testcase class."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
2016-02-19 23:19:03 +00:00
|
|
|
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
|
|
|
@mock.patch.object(controller, 'Controller')
|
|
|
|
def test_config_minimal(self, mock_ctrl, mock_scanner):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup with minimal configuration."""
|
2016-02-19 23:19:03 +00:00
|
|
|
config = {
|
2016-09-04 08:06:16 +00:00
|
|
|
DOMAIN: unifi.PLATFORM_SCHEMA({
|
|
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
2016-02-19 23:19:03 +00:00
|
|
|
CONF_USERNAME: 'foo',
|
|
|
|
CONF_PASSWORD: 'password',
|
2016-09-04 08:06:16 +00:00
|
|
|
})
|
2016-02-19 23:19:03 +00:00
|
|
|
}
|
2016-12-04 06:11:52 +00:00
|
|
|
result = unifi.get_scanner(self.hass, config)
|
2016-09-04 08:06:16 +00:00
|
|
|
self.assertEqual(mock_scanner.return_value, result)
|
2016-10-16 23:13:27 +00:00
|
|
|
self.assertEqual(mock_ctrl.call_count, 1)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_ctrl.call_args,
|
2017-03-10 10:15:21 +00:00
|
|
|
mock.call('localhost', 'foo', 'password', 8443,
|
|
|
|
version='v4', site_id='default', ssl_verify=True)
|
2016-10-16 23:13:27 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(mock_scanner.call_count, 1)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_scanner.call_args,
|
|
|
|
mock.call(mock_ctrl.return_value)
|
|
|
|
)
|
2016-02-19 23:19:03 +00:00
|
|
|
|
|
|
|
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
|
|
|
@mock.patch.object(controller, 'Controller')
|
|
|
|
def test_config_full(self, mock_ctrl, mock_scanner):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup with full configuration."""
|
2016-02-19 23:19:03 +00:00
|
|
|
config = {
|
2016-09-04 08:06:16 +00:00
|
|
|
DOMAIN: unifi.PLATFORM_SCHEMA({
|
|
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
2016-02-19 23:19:03 +00:00
|
|
|
CONF_USERNAME: 'foo',
|
|
|
|
CONF_PASSWORD: 'password',
|
|
|
|
CONF_HOST: 'myhost',
|
2017-03-10 10:15:21 +00:00
|
|
|
CONF_VERIFY_SSL: False,
|
2016-02-19 23:19:03 +00:00
|
|
|
'port': 123,
|
2016-07-04 15:20:00 +00:00
|
|
|
'site_id': 'abcdef01',
|
2016-09-04 08:06:16 +00:00
|
|
|
})
|
2016-02-19 23:19:03 +00:00
|
|
|
}
|
2016-12-04 06:11:52 +00:00
|
|
|
result = unifi.get_scanner(self.hass, config)
|
2016-09-04 08:06:16 +00:00
|
|
|
self.assertEqual(mock_scanner.return_value, result)
|
2016-10-16 23:13:27 +00:00
|
|
|
self.assertEqual(mock_ctrl.call_count, 1)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_ctrl.call_args,
|
2017-03-10 10:15:21 +00:00
|
|
|
mock.call('myhost', 'foo', 'password', 123,
|
|
|
|
version='v4', site_id='abcdef01', ssl_verify=False)
|
2016-10-16 23:13:27 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(mock_scanner.call_count, 1)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_scanner.call_args,
|
|
|
|
mock.call(mock_ctrl.return_value)
|
|
|
|
)
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2016-09-04 08:06:16 +00:00
|
|
|
def test_config_error(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for configuration errors."""
|
2016-09-04 08:06:16 +00:00
|
|
|
with self.assertRaises(vol.Invalid):
|
|
|
|
unifi.PLATFORM_SCHEMA({
|
|
|
|
# no username
|
|
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
2016-02-19 23:19:03 +00:00
|
|
|
CONF_HOST: 'myhost',
|
|
|
|
'port': 123,
|
2016-09-04 08:06:16 +00:00
|
|
|
})
|
|
|
|
with self.assertRaises(vol.Invalid):
|
|
|
|
unifi.PLATFORM_SCHEMA({
|
|
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
2016-02-19 23:19:03 +00:00
|
|
|
CONF_USERNAME: 'foo',
|
|
|
|
CONF_PASSWORD: 'password',
|
|
|
|
CONF_HOST: 'myhost',
|
2016-09-04 08:06:16 +00:00
|
|
|
'port': 'foo', # bad port!
|
|
|
|
})
|
2016-02-19 23:19:03 +00:00
|
|
|
|
|
|
|
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
|
|
|
@mock.patch.object(controller, 'Controller')
|
|
|
|
def test_config_controller_failed(self, mock_ctrl, mock_scanner):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for controller failure."""
|
2016-02-19 23:19:03 +00:00
|
|
|
config = {
|
|
|
|
'device_tracker': {
|
2016-09-04 08:06:16 +00:00
|
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
2016-02-19 23:19:03 +00:00
|
|
|
CONF_USERNAME: 'foo',
|
|
|
|
CONF_PASSWORD: 'password',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mock_ctrl.side_effect = urllib.error.HTTPError(
|
|
|
|
'/', 500, 'foo', {}, None)
|
2016-12-04 06:11:52 +00:00
|
|
|
result = unifi.get_scanner(self.hass, config)
|
2016-02-19 23:19:03 +00:00
|
|
|
self.assertFalse(result)
|
|
|
|
|
2016-09-04 08:06:16 +00:00
|
|
|
def test_scanner_update(self): # pylint: disable=no-self-use
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the scanner update."""
|
2016-02-19 23:19:03 +00:00
|
|
|
ctrl = mock.MagicMock()
|
|
|
|
fake_clients = [
|
|
|
|
{'mac': '123'},
|
|
|
|
{'mac': '234'},
|
|
|
|
]
|
|
|
|
ctrl.get_clients.return_value = fake_clients
|
|
|
|
unifi.UnifiScanner(ctrl)
|
2016-10-16 23:13:27 +00:00
|
|
|
self.assertEqual(ctrl.get_clients.call_count, 1)
|
|
|
|
self.assertEqual(ctrl.get_clients.call_args, mock.call())
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2016-09-04 08:06:16 +00:00
|
|
|
def test_scanner_update_error(self): # pylint: disable=no-self-use
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the scanner update for error."""
|
2016-02-19 23:19:03 +00:00
|
|
|
ctrl = mock.MagicMock()
|
|
|
|
ctrl.get_clients.side_effect = urllib.error.HTTPError(
|
|
|
|
'/', 500, 'foo', {}, None)
|
|
|
|
unifi.UnifiScanner(ctrl)
|
|
|
|
|
|
|
|
def test_scan_devices(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the scanning for devices."""
|
2016-02-19 23:19:03 +00:00
|
|
|
ctrl = mock.MagicMock()
|
|
|
|
fake_clients = [
|
|
|
|
{'mac': '123'},
|
|
|
|
{'mac': '234'},
|
|
|
|
]
|
|
|
|
ctrl.get_clients.return_value = fake_clients
|
|
|
|
scanner = unifi.UnifiScanner(ctrl)
|
|
|
|
self.assertEqual(set(['123', '234']), set(scanner.scan_devices()))
|
|
|
|
|
|
|
|
def test_get_device_name(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the getting of device names."""
|
2016-02-19 23:19:03 +00:00
|
|
|
ctrl = mock.MagicMock()
|
|
|
|
fake_clients = [
|
|
|
|
{'mac': '123', 'hostname': 'foobar'},
|
|
|
|
{'mac': '234', 'name': 'Nice Name'},
|
|
|
|
{'mac': '456'},
|
|
|
|
]
|
|
|
|
ctrl.get_clients.return_value = fake_clients
|
|
|
|
scanner = unifi.UnifiScanner(ctrl)
|
|
|
|
self.assertEqual('foobar', scanner.get_device_name('123'))
|
|
|
|
self.assertEqual('Nice Name', scanner.get_device_name('234'))
|
|
|
|
self.assertEqual(None, scanner.get_device_name('456'))
|
|
|
|
self.assertEqual(None, scanner.get_device_name('unknown'))
|