Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions tests/components/device_tracker/test_tplink.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
"""The tests for the tplink device tracker platform."""

import os
import unittest
import pytest

from homeassistant.components import device_tracker
from homeassistant.components.device_tracker.tplink import Tplink4DeviceScanner
from homeassistant.const import (CONF_PLATFORM, CONF_PASSWORD, CONF_USERNAME,
CONF_HOST)
import requests_mock

from tests.common import get_test_home_assistant

@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
yaml_devices = hass.config.path(device_tracker.YAML_DEVICES)
yield
if os.path.isfile(yaml_devices):
os.remove(yaml_devices)

class TestTplink4DeviceScanner(unittest.TestCase):
"""Tests for the Tplink4DeviceScanner class."""

def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()

def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
try:
os.remove(self.hass.config.path(device_tracker.YAML_DEVICES))
except FileNotFoundError:
pass

@requests_mock.mock()
def test_get_mac_addresses_from_both_bands(self, m):
"""Test grabbing the mac addresses from 2.4 and 5 GHz clients pages."""
async def test_get_mac_addresses_from_both_bands(hass):
"""Test grabbing the mac addresses from 2.4 and 5 GHz clients pages."""
with requests_mock.Mocker() as m:
conf_dict = {
CONF_PLATFORM: 'tplink',
CONF_HOST: 'fake-host',
Expand Down
238 changes: 116 additions & 122 deletions tests/components/device_tracker/test_unifi_direct.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""The tests for the Unifi direct device tracker platform."""
import os
from datetime import timedelta
import unittest
from unittest import mock
from unittest.mock import patch
from asynctest import mock, patch

import pytest
import voluptuous as vol

from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from homeassistant.components import device_tracker
from homeassistant.components.device_tracker import (
CONF_CONSIDER_HOME, CONF_TRACK_NEW, CONF_AWAY_HIDE,
Expand All @@ -19,133 +17,129 @@
CONF_HOST)

from tests.common import (
get_test_home_assistant, assert_setup_component,
mock_component, load_fixture)


class TestComponentsDeviceTrackerUnifiDirect(unittest.TestCase):
"""Tests for the Unifi direct device tracker platform."""

hass = None
scanner_path = 'homeassistant.components.device_tracker.' + \
'unifi_direct.UnifiDeviceScanner'

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')

def teardown_method(self, _):
"""Stop everything that was started."""
self.hass.stop()
try:
os.remove(self.hass.config.path(device_tracker.YAML_DEVICES))
except FileNotFoundError:
pass

@mock.patch(scanner_path,
return_value=mock.MagicMock())
def test_get_scanner(self, unifi_mock):
"""Test creating an Unifi direct scanner with a password."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
assert_setup_component, mock_component, load_fixture)

scanner_path = 'homeassistant.components.device_tracker.' + \
'unifi_direct.UnifiDeviceScanner'


@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
mock_component(hass, 'zone')
yaml_devices = hass.config.path(device_tracker.YAML_DEVICES)
yield
if os.path.isfile(yaml_devices):
os.remove(yaml_devices)


@patch(scanner_path, return_value=mock.MagicMock())
async def test_get_scanner(unifi_mock, hass):
"""Test creating an Unifi direct scanner with a password."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180),
CONF_NEW_DEVICE_DEFAULTS: {
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180),
CONF_NEW_DEVICE_DEFAULTS: {
CONF_TRACK_NEW: True,
CONF_AWAY_HIDE: False
}
CONF_AWAY_HIDE: False
}
}

with assert_setup_component(1, DOMAIN):
assert setup_component(self.hass, DOMAIN, conf_dict)

conf_dict[DOMAIN][CONF_PORT] = 22
assert unifi_mock.call_args == mock.call(conf_dict[DOMAIN])

@patch('pexpect.pxssh.pxssh')
def test_get_device_name(self, mock_ssh):
"""Testing MAC matching."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}
}

with assert_setup_component(1, DOMAIN):
assert await async_setup_component(hass, DOMAIN, conf_dict)

conf_dict[DOMAIN][CONF_PORT] = 22
assert unifi_mock.call_args == mock.call(conf_dict[DOMAIN])


@patch('pexpect.pxssh.pxssh')
async def test_get_device_name(mock_ssh, hass):
"""Testing MAC matching."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}
mock_ssh.return_value.before = load_fixture('unifi_direct.txt')
scanner = get_scanner(self.hass, conf_dict)
devices = scanner.scan_devices()
assert 23 == len(devices)
assert "iPhone" == \
scanner.get_device_name("98:00:c6:56:34:12")
assert "iPhone" == \
scanner.get_device_name("98:00:C6:56:34:12")

@patch('pexpect.pxssh.pxssh.logout')
@patch('pexpect.pxssh.pxssh.login')
def test_failed_to_log_in(self, mock_login, mock_logout):
"""Testing exception at login results in False."""
from pexpect import exceptions

conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}
}
mock_ssh.return_value.before = load_fixture('unifi_direct.txt')
scanner = get_scanner(hass, conf_dict)
devices = scanner.scan_devices()
assert 23 == len(devices)
assert "iPhone" == \
scanner.get_device_name("98:00:c6:56:34:12")
assert "iPhone" == \
scanner.get_device_name("98:00:C6:56:34:12")


@patch('pexpect.pxssh.pxssh.logout')
@patch('pexpect.pxssh.pxssh.login')
async def test_failed_to_log_in(mock_login, mock_logout, hass):
"""Testing exception at login results in False."""
from pexpect import exceptions

conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}

mock_login.side_effect = exceptions.EOF("Test")
scanner = get_scanner(self.hass, conf_dict)
assert not scanner

@patch('pexpect.pxssh.pxssh.logout')
@patch('pexpect.pxssh.pxssh.login', autospec=True)
@patch('pexpect.pxssh.pxssh.prompt')
@patch('pexpect.pxssh.pxssh.sendline')
def test_to_get_update(self, mock_sendline, mock_prompt, mock_login,
mock_logout):
"""Testing exception in get_update matching."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}
}

mock_login.side_effect = exceptions.EOF("Test")
scanner = get_scanner(hass, conf_dict)
assert not scanner


@patch('pexpect.pxssh.pxssh.logout')
@patch('pexpect.pxssh.pxssh.login', autospec=True)
@patch('pexpect.pxssh.pxssh.prompt')
@patch('pexpect.pxssh.pxssh.sendline')
async def test_to_get_update(mock_sendline, mock_prompt, mock_login,
mock_logout, hass):
"""Testing exception in get_update matching."""
conf_dict = {
DOMAIN: {
CONF_PLATFORM: 'unifi_direct',
CONF_HOST: 'fake_host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass',
CONF_PORT: 22,
CONF_TRACK_NEW: True,
CONF_CONSIDER_HOME: timedelta(seconds=180)
}
}

scanner = get_scanner(hass, conf_dict)
# mock_sendline.side_effect = AssertionError("Test")
mock_prompt.side_effect = AssertionError("Test")
devices = scanner._get_update() # pylint: disable=protected-access
assert devices is None


scanner = get_scanner(self.hass, conf_dict)
# mock_sendline.side_effect = AssertionError("Test")
mock_prompt.side_effect = AssertionError("Test")
devices = scanner._get_update() # pylint: disable=protected-access
assert devices is None
def test_good_response_parses(hass):
"""Test that the response form the AP parses to JSON correctly."""
response = _response_to_json(load_fixture('unifi_direct.txt'))
assert response != {}

def test_good_response_parses(self):
"""Test that the response form the AP parses to JSON correctly."""
response = _response_to_json(load_fixture('unifi_direct.txt'))
assert response != {}

def test_bad_response_returns_none(self):
"""Test that a bad response form the AP parses to JSON correctly."""
assert _response_to_json("{(}") == {}
def test_bad_response_returns_none(hass):
"""Test that a bad response form the AP parses to JSON correctly."""
assert _response_to_json("{(}") == {}


def test_config_error():
Expand Down
Loading