Merge remote-tracking branch 'upstream/dev' into igd
This commit is contained in:
commit
3db766e2ec
974 files changed
+6103
-3081
No files matched your search
@@ -119,9 +119,27 @@ async def test_login(hass):
|
||||
result = await hass.auth.login_flow.async_configure(
|
||||
result['flow_id'], {'pin': 'invalid-code'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['errors']['base'] == 'invalid_auth'
|
||||
assert result['errors']['base'] == 'invalid_code'
|
||||
|
||||
result = await hass.auth.login_flow.async_configure(
|
||||
result['flow_id'], {'pin': '123456'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'].id == 'mock-user'
|
||||
|
||||
|
||||
async def test_setup_flow(hass):
|
||||
"""Test validating pin."""
|
||||
auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'insecure_example',
|
||||
'data': [{'user_id': 'test-user', 'pin': '123456'}]
|
||||
})
|
||||
|
||||
flow = await auth_module.async_setup_flow('new-user')
|
||||
|
||||
result = await flow.async_step_init()
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await flow.async_step_init({'pin': 'abcdefg'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert auth_module._data[1]['user_id'] == 'new-user'
|
||||
assert auth_module._data[1]['pin'] == 'abcdefg'
|
||||
@@ -0,0 +1,130 @@
|
||||
"""Test the Time-based One Time Password (MFA) auth module."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.auth import models as auth_models, auth_manager_from_config
|
||||
from homeassistant.auth.mfa_modules import auth_mfa_module_from_config
|
||||
from tests.common import MockUser
|
||||
|
||||
MOCK_CODE = '123456'
|
||||
|
||||
|
||||
async def test_validating_mfa(hass):
|
||||
"""Test validating mfa code."""
|
||||
totp_auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'totp'
|
||||
})
|
||||
await totp_auth_module.async_setup_user('test-user', {})
|
||||
|
||||
with patch('pyotp.TOTP.verify', return_value=True):
|
||||
assert await totp_auth_module.async_validation(
|
||||
'test-user', {'code': MOCK_CODE})
|
||||
|
||||
|
||||
async def test_validating_mfa_invalid_code(hass):
|
||||
"""Test validating an invalid mfa code."""
|
||||
totp_auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'totp'
|
||||
})
|
||||
await totp_auth_module.async_setup_user('test-user', {})
|
||||
|
||||
with patch('pyotp.TOTP.verify', return_value=False):
|
||||
assert await totp_auth_module.async_validation(
|
||||
'test-user', {'code': MOCK_CODE}) is False
|
||||
|
||||
|
||||
async def test_validating_mfa_invalid_user(hass):
|
||||
"""Test validating an mfa code with invalid user."""
|
||||
totp_auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'totp'
|
||||
})
|
||||
await totp_auth_module.async_setup_user('test-user', {})
|
||||
|
||||
assert await totp_auth_module.async_validation(
|
||||
'invalid-user', {'code': MOCK_CODE}) is False
|
||||
|
||||
|
||||
async def test_setup_depose_user(hass):
|
||||
"""Test despose user."""
|
||||
totp_auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'totp'
|
||||
})
|
||||
result = await totp_auth_module.async_setup_user('test-user', {})
|
||||
assert len(totp_auth_module._users) == 1
|
||||
result2 = await totp_auth_module.async_setup_user('test-user', {})
|
||||
assert len(totp_auth_module._users) == 1
|
||||
assert result != result2
|
||||
|
||||
await totp_auth_module.async_depose_user('test-user')
|
||||
assert len(totp_auth_module._users) == 0
|
||||
|
||||
result = await totp_auth_module.async_setup_user(
|
||||
'test-user2', {'secret': 'secret-code'})
|
||||
assert result == 'secret-code'
|
||||
assert len(totp_auth_module._users) == 1
|
||||
|
||||
|
||||
async def test_login_flow_validates_mfa(hass):
|
||||
"""Test login flow with mfa enabled."""
|
||||
hass.auth = await auth_manager_from_config(hass, [{
|
||||
'type': 'insecure_example',
|
||||
'users': [{'username': 'test-user', 'password': 'test-pass'}],
|
||||
}], [{
|
||||
'type': 'totp',
|
||||
}])
|
||||
user = MockUser(
|
||||
id='mock-user',
|
||||
is_owner=False,
|
||||
is_active=False,
|
||||
name='Paulus',
|
||||
).add_to_auth_manager(hass.auth)
|
||||
await hass.auth.async_link_user(user, auth_models.Credentials(
|
||||
id='mock-id',
|
||||
auth_provider_type='insecure_example',
|
||||
auth_provider_id=None,
|
||||
data={'username': 'test-user'},
|
||||
is_new=False,
|
||||
))
|
||||
|
||||
await hass.auth.async_enable_user_mfa(user, 'totp', {})
|
||||
|
||||
provider = hass.auth.auth_providers[0]
|
||||
|
||||
result = await hass.auth.login_flow.async_init(
|
||||
(provider.type, provider.id))
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await hass.auth.login_flow.async_configure(result['flow_id'], {
|
||||
'username': 'incorrect-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['errors']['base'] == 'invalid_auth'
|
||||
|
||||
result = await hass.auth.login_flow.async_configure(result['flow_id'], {
|
||||
'username': 'test-user',
|
||||
'password': 'incorrect-pass',
|
||||
})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['errors']['base'] == 'invalid_auth'
|
||||
|
||||
result = await hass.auth.login_flow.async_configure(result['flow_id'], {
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'mfa'
|
||||
assert result['data_schema'].schema.get('code') == str
|
||||
|
||||
with patch('pyotp.TOTP.verify', return_value=False):
|
||||
result = await hass.auth.login_flow.async_configure(
|
||||
result['flow_id'], {'code': 'invalid-code'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result['step_id'] == 'mfa'
|
||||
assert result['errors']['base'] == 'invalid_code'
|
||||
|
||||
with patch('pyotp.TOTP.verify', return_value=True):
|
||||
result = await hass.auth.login_flow.async_configure(
|
||||
result['flow_id'], {'code': MOCK_CODE})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'].id == 'mock-user'
|
||||
@@ -1,7 +1,9 @@
|
||||
"""Test the Home Assistant local auth provider."""
|
||||
from unittest.mock import Mock
|
||||
|
||||
import base64
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.auth import auth_manager_from_config, auth_store
|
||||
@@ -110,11 +112,11 @@ async def test_saving_loading(data, hass):
|
||||
async def test_not_allow_set_id():
|
||||
"""Test we are not allowed to set an ID in config."""
|
||||
hass = Mock()
|
||||
provider = await auth_provider_from_config(hass, None, {
|
||||
'type': 'homeassistant',
|
||||
'id': 'invalid',
|
||||
})
|
||||
assert provider is None
|
||||
with pytest.raises(vol.Invalid):
|
||||
await auth_provider_from_config(hass, None, {
|
||||
'type': 'homeassistant',
|
||||
'id': 'invalid',
|
||||
})
|
||||
|
||||
|
||||
async def test_new_users_populate_values(hass, data):
|
||||
@@ -132,3 +134,91 @@ async def test_new_users_populate_values(hass, data):
|
||||
user = await manager.async_get_or_create_user(credentials)
|
||||
assert user.name == 'hello'
|
||||
assert user.is_active
|
||||
|
||||
|
||||
async def test_new_hashes_are_bcrypt(data, hass):
|
||||
"""Test that newly created hashes are using bcrypt."""
|
||||
data.add_auth('newuser', 'newpass')
|
||||
found = None
|
||||
for user in data.users:
|
||||
if user['username'] == 'newuser':
|
||||
found = user
|
||||
assert found is not None
|
||||
user_hash = base64.b64decode(found['password'])
|
||||
assert (user_hash.startswith(b'$2a$')
|
||||
or user_hash.startswith(b'$2b$')
|
||||
or user_hash.startswith(b'$2x$')
|
||||
or user_hash.startswith(b'$2y$'))
|
||||
|
||||
|
||||
async def test_pbkdf2_to_bcrypt_hash_upgrade(hass_storage, hass):
|
||||
"""Test migrating user from pbkdf2 hash to bcrypt hash."""
|
||||
hass_storage[hass_auth.STORAGE_KEY] = {
|
||||
'version': hass_auth.STORAGE_VERSION,
|
||||
'key': hass_auth.STORAGE_KEY,
|
||||
'data': {
|
||||
'salt': '09c52f0b120eaa7dea5f73f9a9b985f3d493b30a08f3f2945ef613'
|
||||
'0b08e6a3ea',
|
||||
'users': [
|
||||
{
|
||||
'password': 'L5PAbehB8LAQI2Ixu+d+PDNJKmljqLnBcYWYw35onC/8D'
|
||||
'BM1SpvT6A8ZFael5+deCt+s+43J08IcztnguouHSw==',
|
||||
'username': 'legacyuser'
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
data = hass_auth.Data(hass)
|
||||
await data.async_load()
|
||||
|
||||
# verify the correct (pbkdf2) password successfuly authenticates the user
|
||||
await hass.async_add_executor_job(
|
||||
data.validate_login, 'legacyuser', 'beer')
|
||||
|
||||
# ...and that the hashes are now bcrypt hashes
|
||||
user_hash = base64.b64decode(
|
||||
hass_storage[hass_auth.STORAGE_KEY]['data']['users'][0]['password'])
|
||||
assert (user_hash.startswith(b'$2a$')
|
||||
or user_hash.startswith(b'$2b$')
|
||||
or user_hash.startswith(b'$2x$')
|
||||
or user_hash.startswith(b'$2y$'))
|
||||
|
||||
|
||||
async def test_pbkdf2_to_bcrypt_hash_upgrade_with_incorrect_pass(hass_storage,
|
||||
hass):
|
||||
"""Test migrating user from pbkdf2 hash to bcrypt hash."""
|
||||
hass_storage[hass_auth.STORAGE_KEY] = {
|
||||
'version': hass_auth.STORAGE_VERSION,
|
||||
'key': hass_auth.STORAGE_KEY,
|
||||
'data': {
|
||||
'salt': '09c52f0b120eaa7dea5f73f9a9b985f3d493b30a08f3f2945ef613'
|
||||
'0b08e6a3ea',
|
||||
'users': [
|
||||
{
|
||||
'password': 'L5PAbehB8LAQI2Ixu+d+PDNJKmljqLnBcYWYw35onC/8D'
|
||||
'BM1SpvT6A8ZFael5+deCt+s+43J08IcztnguouHSw==',
|
||||
'username': 'legacyuser'
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
data = hass_auth.Data(hass)
|
||||
await data.async_load()
|
||||
|
||||
orig_user_hash = base64.b64decode(
|
||||
hass_storage[hass_auth.STORAGE_KEY]['data']['users'][0]['password'])
|
||||
|
||||
# Make sure invalid legacy passwords fail
|
||||
with pytest.raises(hass_auth.InvalidAuth):
|
||||
await hass.async_add_executor_job(
|
||||
data.validate_login, 'legacyuser', 'wine')
|
||||
|
||||
# Make sure we don't change the password/hash when password is incorrect
|
||||
with pytest.raises(hass_auth.InvalidAuth):
|
||||
await hass.async_add_executor_job(
|
||||
data.validate_login, 'legacyuser', 'wine')
|
||||
|
||||
same_user_hash = base64.b64decode(
|
||||
hass_storage[hass_auth.STORAGE_KEY]['data']['users'][0]['password'])
|
||||
|
||||
assert orig_user_hash == same_user_hash
|
||||
@@ -74,16 +74,16 @@ async def test_login_flow(manager, provider):
|
||||
# trusted network didn't loaded
|
||||
flow = await provider.async_login_flow({'ip_address': '127.0.0.1'})
|
||||
step = await flow.async_step_init()
|
||||
assert step['step_id'] == 'init'
|
||||
assert step['errors']['base'] == 'invalid_auth'
|
||||
assert step['type'] == 'abort'
|
||||
assert step['reason'] == 'not_whitelisted'
|
||||
|
||||
provider.hass.http = Mock(trusted_networks=['192.168.0.1'])
|
||||
|
||||
# not from trusted network
|
||||
flow = await provider.async_login_flow({'ip_address': '127.0.0.1'})
|
||||
step = await flow.async_step_init()
|
||||
assert step['step_id'] == 'init'
|
||||
assert step['errors']['base'] == 'invalid_auth'
|
||||
assert step['type'] == 'abort'
|
||||
assert step['reason'] == 'not_whitelisted'
|
||||
|
||||
# from trusted network, list users
|
||||
flow = await provider.async_login_flow({'ip_address': '192.168.0.1'})
|
||||
@@ -95,11 +95,6 @@ async def test_login_flow(manager, provider):
|
||||
with pytest.raises(vol.Invalid):
|
||||
assert schema({'user': 'invalid-user'})
|
||||
|
||||
# login with invalid user
|
||||
step = await flow.async_step_init({'user': 'invalid-user'})
|
||||
assert step['step_id'] == 'init'
|
||||
assert step['errors']['base'] == 'invalid_auth'
|
||||
|
||||
# login with valid user
|
||||
step = await flow.async_step_init({'user': user.id})
|
||||
assert step['type'] == 'create_entry'
|
||||
|
||||
+43
-34
@@ -3,6 +3,7 @@ from datetime import timedelta
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import auth, data_entry_flow
|
||||
from homeassistant.auth import (
|
||||
@@ -21,33 +22,36 @@ def mock_hass(loop):
|
||||
return hass
|
||||
|
||||
|
||||
async def test_auth_manager_from_config_validates_config_and_id(mock_hass):
|
||||
async def test_auth_manager_from_config_validates_config(mock_hass):
|
||||
"""Test get auth providers."""
|
||||
with pytest.raises(vol.Invalid):
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
'name': 'Test Name',
|
||||
'type': 'insecure_example',
|
||||
'users': [],
|
||||
}, {
|
||||
'name': 'Invalid config because no users',
|
||||
'type': 'insecure_example',
|
||||
'id': 'invalid_config',
|
||||
}], [])
|
||||
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
'name': 'Test Name',
|
||||
'type': 'insecure_example',
|
||||
'users': [],
|
||||
}, {
|
||||
'name': 'Invalid config because no users',
|
||||
'type': 'insecure_example',
|
||||
'id': 'invalid_config',
|
||||
}, {
|
||||
'name': 'Test Name 2',
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
'users': [],
|
||||
}, {
|
||||
'name': 'Wrong because duplicate ID',
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
'users': [],
|
||||
}], [])
|
||||
|
||||
providers = [{
|
||||
'name': provider.name,
|
||||
'id': provider.id,
|
||||
'type': provider.type,
|
||||
} for provider in manager.auth_providers]
|
||||
'name': provider.name,
|
||||
'id': provider.id,
|
||||
'type': provider.type,
|
||||
} for provider in manager.auth_providers]
|
||||
|
||||
assert providers == [{
|
||||
'name': 'Test Name',
|
||||
'type': 'insecure_example',
|
||||
@@ -61,6 +65,26 @@ async def test_auth_manager_from_config_validates_config_and_id(mock_hass):
|
||||
|
||||
async def test_auth_manager_from_config_auth_modules(mock_hass):
|
||||
"""Test get auth modules."""
|
||||
with pytest.raises(vol.Invalid):
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
'name': 'Test Name',
|
||||
'type': 'insecure_example',
|
||||
'users': [],
|
||||
}, {
|
||||
'name': 'Test Name 2',
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
'users': [],
|
||||
}], [{
|
||||
'name': 'Module 1',
|
||||
'type': 'insecure_example',
|
||||
'data': [],
|
||||
}, {
|
||||
'name': 'Invalid config because no data',
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
}])
|
||||
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
'name': 'Test Name',
|
||||
'type': 'insecure_example',
|
||||
@@ -79,13 +103,7 @@ async def test_auth_manager_from_config_auth_modules(mock_hass):
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
'data': [],
|
||||
}, {
|
||||
'name': 'Duplicate ID',
|
||||
'type': 'insecure_example',
|
||||
'id': 'another',
|
||||
'data': [],
|
||||
}])
|
||||
|
||||
providers = [{
|
||||
'name': provider.name,
|
||||
'type': provider.type,
|
||||
@@ -410,10 +428,10 @@ async def test_login_with_auth_module(mock_hass):
|
||||
'pin': 'invalid-pin',
|
||||
})
|
||||
|
||||
# Invalid auth error
|
||||
# Invalid code error
|
||||
assert step['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert step['step_id'] == 'mfa'
|
||||
assert step['errors'] == {'base': 'invalid_auth'}
|
||||
assert step['errors'] == {'base': 'invalid_code'}
|
||||
|
||||
step = await manager.login_flow.async_configure(step['flow_id'], {
|
||||
'pin': 'test-pin',
|
||||
@@ -553,18 +571,9 @@ async def test_auth_module_expired_session(mock_hass):
|
||||
step = await manager.login_flow.async_configure(step['flow_id'], {
|
||||
'pin': 'test-pin',
|
||||
})
|
||||
# Invalid auth due session timeout
|
||||
assert step['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert step['step_id'] == 'mfa'
|
||||
assert step['errors']['base'] == 'login_expired'
|
||||
|
||||
# The second try will fail as well
|
||||
step = await manager.login_flow.async_configure(step['flow_id'], {
|
||||
'pin': 'test-pin',
|
||||
})
|
||||
assert step['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert step['step_id'] == 'mfa'
|
||||
assert step['errors']['base'] == 'login_expired'
|
||||
# login flow abort due session timeout
|
||||
assert step['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert step['reason'] == 'login_expired'
|
||||
|
||||
|
||||
async def test_enable_mfa_for_user(hass, hass_storage):
|
||||
|
||||
@@ -32,9 +32,9 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||
def test_setup_demo_platform(self):
|
||||
"""Test setup."""
|
||||
mock = MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
demo.setup_platform(self.hass, {}, add_devices)
|
||||
self.assertEqual(add_devices.call_count, 1)
|
||||
add_entities = mock.MagicMock()
|
||||
demo.setup_platform(self.hass, {}, add_entities)
|
||||
self.assertEqual(add_entities.call_count, 1)
|
||||
|
||||
def test_arm_home_no_pending(self):
|
||||
"""Test arm home method."""
|
||||
|
||||
@@ -54,7 +54,7 @@ def test_setup_platform(hass):
|
||||
|
||||
yield from spc.async_setup_platform(hass=hass,
|
||||
config={},
|
||||
async_add_devices=add_entities,
|
||||
async_add_entities=add_entities,
|
||||
discovery_info=areas)
|
||||
|
||||
assert len(added_entities) == 2
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Tests for the mfa setup flow."""
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.auth import auth_manager_from_config
|
||||
from homeassistant.components.auth import mfa_setup_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockUser, CLIENT_ID, ensure_auth_manager_loaded
|
||||
|
||||
|
||||
async def test_ws_setup_depose_mfa(hass, hass_ws_client):
|
||||
"""Test set up mfa module for current user."""
|
||||
hass.auth = await auth_manager_from_config(
|
||||
hass, provider_configs=[{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
'name': 'Test Name',
|
||||
}]
|
||||
}], module_configs=[{
|
||||
'type': 'insecure_example',
|
||||
'id': 'example_module',
|
||||
'data': [{'user_id': 'mock-user', 'pin': '123456'}]
|
||||
}])
|
||||
ensure_auth_manager_loaded(hass.auth)
|
||||
await async_setup_component(hass, 'auth', {'http': {}})
|
||||
|
||||
user = MockUser(id='mock-user').add_to_hass(hass)
|
||||
cred = await hass.auth.auth_providers[0].async_get_or_create_credentials(
|
||||
{'username': 'test-user'})
|
||||
await hass.auth.async_link_user(user, cred)
|
||||
refresh_token = await hass.auth.async_create_refresh_token(user, CLIENT_ID)
|
||||
access_token = hass.auth.async_create_access_token(refresh_token)
|
||||
|
||||
client = await hass_ws_client(hass, access_token)
|
||||
|
||||
await client.send_json({
|
||||
'id': 10,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 10
|
||||
assert result['success'] is False
|
||||
assert result['error']['code'] == 'no_module'
|
||||
|
||||
await client.send_json({
|
||||
'id': 11,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
'mfa_module_id': 'example_module',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 11
|
||||
assert result['success']
|
||||
|
||||
flow = result['result']
|
||||
assert flow['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert flow['handler'] == 'example_module'
|
||||
assert flow['step_id'] == 'init'
|
||||
assert flow['data_schema'][0] == {'type': 'string', 'name': 'pin'}
|
||||
|
||||
await client.send_json({
|
||||
'id': 12,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
'flow_id': flow['flow_id'],
|
||||
'user_input': {'pin': '654321'},
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 12
|
||||
assert result['success']
|
||||
|
||||
flow = result['result']
|
||||
assert flow['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert flow['handler'] == 'example_module'
|
||||
assert flow['data']['result'] is None
|
||||
|
||||
await client.send_json({
|
||||
'id': 13,
|
||||
'type': mfa_setup_flow.WS_TYPE_DEPOSE_MFA,
|
||||
'mfa_module_id': 'invalid_id',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 13
|
||||
assert result['success'] is False
|
||||
assert result['error']['code'] == 'disable_failed'
|
||||
|
||||
await client.send_json({
|
||||
'id': 14,
|
||||
'type': mfa_setup_flow.WS_TYPE_DEPOSE_MFA,
|
||||
'mfa_module_id': 'example_module',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 14
|
||||
assert result['success']
|
||||
assert result['result'] == 'done'
|
||||
@@ -45,17 +45,17 @@ class TestNX584SensorSetup(unittest.TestCase):
|
||||
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor')
|
||||
def test_setup_defaults(self, mock_nx, mock_watcher):
|
||||
"""Test the setup with no configuration."""
|
||||
add_devices = mock.MagicMock()
|
||||
add_entities = mock.MagicMock()
|
||||
config = {
|
||||
'host': nx584.DEFAULT_HOST,
|
||||
'port': nx584.DEFAULT_PORT,
|
||||
'exclude_zones': [],
|
||||
'zone_types': {},
|
||||
}
|
||||
self.assertTrue(nx584.setup_platform(self.hass, config, add_devices))
|
||||
self.assertTrue(nx584.setup_platform(self.hass, config, add_entities))
|
||||
mock_nx.assert_has_calls(
|
||||
[mock.call(zone, 'opening') for zone in self.fake_zones])
|
||||
self.assertTrue(add_devices.called)
|
||||
self.assertTrue(add_entities.called)
|
||||
self.assertEqual(nx584_client.Client.call_count, 1)
|
||||
self.assertEqual(
|
||||
nx584_client.Client.call_args, mock.call('http://localhost:5007')
|
||||
@@ -71,13 +71,13 @@ class TestNX584SensorSetup(unittest.TestCase):
|
||||
'exclude_zones': [2],
|
||||
'zone_types': {3: 'motion'},
|
||||
}
|
||||
add_devices = mock.MagicMock()
|
||||
self.assertTrue(nx584.setup_platform(self.hass, config, add_devices))
|
||||
add_entities = mock.MagicMock()
|
||||
self.assertTrue(nx584.setup_platform(self.hass, config, add_entities))
|
||||
mock_nx.assert_has_calls([
|
||||
mock.call(self.fake_zones[0], 'opening'),
|
||||
mock.call(self.fake_zones[2], 'motion'),
|
||||
])
|
||||
self.assertTrue(add_devices.called)
|
||||
self.assertTrue(add_entities.called)
|
||||
self.assertEqual(nx584_client.Client.call_count, 1)
|
||||
self.assertEqual(
|
||||
nx584_client.Client.call_args, mock.call('http://foo:123')
|
||||
@@ -120,9 +120,9 @@ class TestNX584SensorSetup(unittest.TestCase):
|
||||
def test_setup_no_zones(self):
|
||||
"""Test the setup with no zones."""
|
||||
nx584_client.Client.return_value.list_zones.return_value = []
|
||||
add_devices = mock.MagicMock()
|
||||
self.assertTrue(nx584.setup_platform(self.hass, {}, add_devices))
|
||||
self.assertFalse(add_devices.called)
|
||||
add_entities = mock.MagicMock()
|
||||
self.assertTrue(nx584.setup_platform(self.hass, {}, add_entities))
|
||||
self.assertFalse(add_entities.called)
|
||||
|
||||
|
||||
class TestNX584ZoneSensor(unittest.TestCase):
|
||||
|
||||
@@ -16,7 +16,7 @@ class TestRingBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -58,7 +58,7 @@ class TestRingBinarySensorSetup(unittest.TestCase):
|
||||
base_ring.setup(self.hass, VALID_CONFIG)
|
||||
ring.setup_platform(self.hass,
|
||||
self.config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
for device in self.DEVICES:
|
||||
|
||||
@@ -16,7 +16,7 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -45,7 +45,7 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
sleepiq.setup_platform(self.hass,
|
||||
self.config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
MagicMock())
|
||||
self.assertEqual(2, len(self.DEVICES))
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ def test_setup_platform(hass):
|
||||
|
||||
yield from spc.async_setup_platform(hass=hass,
|
||||
config={},
|
||||
async_add_devices=add_entities,
|
||||
async_add_entities=add_entities,
|
||||
discovery_info=zones)
|
||||
|
||||
assert len(added_entities) == 3
|
||||
|
||||
@@ -38,13 +38,13 @@ class TestTCPBinarySensor(unittest.TestCase):
|
||||
|
||||
@patch('homeassistant.components.sensor.tcp.TcpSensor.update')
|
||||
def test_setup_platform_devices(self, mock_update):
|
||||
"""Check the supplied config and call add_devices with sensor."""
|
||||
add_devices = Mock()
|
||||
ret = bin_tcp.setup_platform(None, test_tcp.TEST_CONFIG, add_devices)
|
||||
"""Check the supplied config and call add_entities with sensor."""
|
||||
add_entities = Mock()
|
||||
ret = bin_tcp.setup_platform(None, test_tcp.TEST_CONFIG, add_entities)
|
||||
assert ret is None
|
||||
assert add_devices.called
|
||||
assert add_entities.called
|
||||
assert isinstance(
|
||||
add_devices.call_args[0][0][0], bin_tcp.TcpBinarySensor)
|
||||
add_entities.call_args[0][0][0], bin_tcp.TcpBinarySensor)
|
||||
|
||||
@patch('homeassistant.components.sensor.tcp.TcpSensor.update')
|
||||
def test_is_on_true(self, mock_update):
|
||||
|
||||
@@ -26,7 +26,7 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -71,7 +71,7 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
||||
for config in self.configs:
|
||||
vultr.setup_platform(self.hass,
|
||||
config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertEqual(len(self.DEVICES), 3)
|
||||
@@ -147,7 +147,7 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
no_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertFalse(no_subs_setup)
|
||||
@@ -159,7 +159,7 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
||||
|
||||
wrong_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertFalse(wrong_subs_setup)
|
||||
@@ -56,7 +56,7 @@ class TestHoneywell(unittest.TestCase):
|
||||
honeywell.PLATFORM_SCHEMA(bad_region_config)
|
||||
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
add_entities = mock.MagicMock()
|
||||
|
||||
locations = [
|
||||
mock.MagicMock(),
|
||||
@@ -69,7 +69,7 @@ class TestHoneywell(unittest.TestCase):
|
||||
locations[0].devices_by_id.values.return_value = devices_1
|
||||
locations[1].devices_by_id.values.return_value = devices_2
|
||||
|
||||
result = honeywell.setup_platform(hass, config, add_devices)
|
||||
result = honeywell.setup_platform(hass, config, add_entities)
|
||||
self.assertTrue(result)
|
||||
self.assertEqual(mock_sc.call_count, 1)
|
||||
self.assertEqual(mock_sc.call_args, mock.call('user', 'pass'))
|
||||
@@ -86,7 +86,7 @@ class TestHoneywell(unittest.TestCase):
|
||||
def test_setup_us_failures(self, mock_sc):
|
||||
"""Test the US setup."""
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
add_entities = mock.MagicMock()
|
||||
config = {
|
||||
CONF_USERNAME: 'user',
|
||||
CONF_PASSWORD: 'pass',
|
||||
@@ -94,14 +94,14 @@ class TestHoneywell(unittest.TestCase):
|
||||
}
|
||||
|
||||
mock_sc.side_effect = somecomfort.AuthError
|
||||
result = honeywell.setup_platform(hass, config, add_devices)
|
||||
result = honeywell.setup_platform(hass, config, add_entities)
|
||||
self.assertFalse(result)
|
||||
self.assertFalse(add_devices.called)
|
||||
self.assertFalse(add_entities.called)
|
||||
|
||||
mock_sc.side_effect = somecomfort.SomeComfortError
|
||||
result = honeywell.setup_platform(hass, config, add_devices)
|
||||
result = honeywell.setup_platform(hass, config, add_entities)
|
||||
self.assertFalse(result)
|
||||
self.assertFalse(add_devices.called)
|
||||
self.assertFalse(add_entities.called)
|
||||
|
||||
@mock.patch('somecomfort.SomeComfort')
|
||||
@mock.patch('homeassistant.components.climate.'
|
||||
@@ -136,9 +136,9 @@ class TestHoneywell(unittest.TestCase):
|
||||
}
|
||||
mock_sc.return_value = mock.MagicMock(locations_by_id=locations)
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
add_entities = mock.MagicMock()
|
||||
self.assertEqual(True,
|
||||
honeywell.setup_platform(hass, config, add_devices))
|
||||
honeywell.setup_platform(hass, config, add_entities))
|
||||
|
||||
return mock_ht.call_args_list, mock_sc
|
||||
|
||||
@@ -185,8 +185,8 @@ class TestHoneywell(unittest.TestCase):
|
||||
mock_evo.return_value.temperatures.return_value = [
|
||||
{'id': 'foo'}, {'id': 'bar'}]
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
self.assertTrue(honeywell.setup_platform(hass, config, add_devices))
|
||||
add_entities = mock.MagicMock()
|
||||
self.assertTrue(honeywell.setup_platform(hass, config, add_entities))
|
||||
self.assertEqual(mock_evo.call_count, 1)
|
||||
self.assertEqual(mock_evo.call_args, mock.call('user', 'pass'))
|
||||
self.assertEqual(mock_evo.return_value.temperatures.call_count, 1)
|
||||
@@ -198,7 +198,7 @@ class TestHoneywell(unittest.TestCase):
|
||||
mock.call(mock_evo.return_value, 'foo', True, 20.0),
|
||||
mock.call(mock_evo.return_value, 'bar', False, 20.0),
|
||||
])
|
||||
self.assertEqual(2, add_devices.call_count)
|
||||
self.assertEqual(2, add_entities.call_count)
|
||||
|
||||
@mock.patch('evohomeclient.EvohomeClient')
|
||||
@mock.patch('homeassistant.components.climate.honeywell.'
|
||||
@@ -217,8 +217,8 @@ class TestHoneywell(unittest.TestCase):
|
||||
honeywell.DEFAULT_AWAY_TEMPERATURE
|
||||
|
||||
hass = mock.MagicMock()
|
||||
add_devices = mock.MagicMock()
|
||||
self.assertTrue(honeywell.setup_platform(hass, config, add_devices))
|
||||
add_entities = mock.MagicMock()
|
||||
self.assertTrue(honeywell.setup_platform(hass, config, add_entities))
|
||||
mock_round.assert_has_calls([
|
||||
mock.call(mock_evo.return_value, 'foo', True, 16),
|
||||
mock.call(mock_evo.return_value, 'bar', False, 16),
|
||||
@@ -251,9 +251,9 @@ class TestHoneywell(unittest.TestCase):
|
||||
honeywell.CONF_REGION: 'eu',
|
||||
}
|
||||
mock_evo.return_value.temperatures.side_effect = socket.error
|
||||
add_devices = mock.MagicMock()
|
||||
add_entities = mock.MagicMock()
|
||||
hass = mock.MagicMock()
|
||||
self.assertFalse(honeywell.setup_platform(hass, config, add_devices))
|
||||
self.assertFalse(honeywell.setup_platform(hass, config, add_entities))
|
||||
|
||||
|
||||
class TestHoneywellRound(unittest.TestCase):
|
||||
|
||||
@@ -76,11 +76,11 @@ class TestMelissa(unittest.TestCase):
|
||||
self.hass.data[DATA_MELISSA] = self.api
|
||||
|
||||
config = {}
|
||||
add_devices = Mock()
|
||||
add_entities = Mock()
|
||||
discovery_info = {}
|
||||
|
||||
melissa.setup_platform(self.hass, config, add_devices, discovery_info)
|
||||
add_devices.assert_called_once_with(thermostats)
|
||||
melissa.setup_platform(self.hass, config, add_entities, discovery_info)
|
||||
add_entities.assert_called_once_with(thermostats)
|
||||
|
||||
def test_get_name(self):
|
||||
"""Test name property."""
|
||||
|
||||
@@ -67,11 +67,11 @@ class TestNuHeat(unittest.TestCase):
|
||||
self.hass.data[nuheat.NUHEAT_DOMAIN] = (self.api, ["12345"])
|
||||
|
||||
config = {}
|
||||
add_devices = Mock()
|
||||
add_entities = Mock()
|
||||
discovery_info = {}
|
||||
|
||||
nuheat.setup_platform(self.hass, config, add_devices, discovery_info)
|
||||
add_devices.assert_called_once_with(thermostats, True)
|
||||
nuheat.setup_platform(self.hass, config, add_entities, discovery_info)
|
||||
add_entities.assert_called_once_with(thermostats, True)
|
||||
|
||||
@patch("homeassistant.components.climate.nuheat.NuHeatThermostat")
|
||||
def test_resume_program_service(self, mocked_thermostat):
|
||||
|
||||
@@ -7,6 +7,16 @@ from homeassistant.components import deconz
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
CONFIG = {
|
||||
"config": {
|
||||
"bridgeid": "0123456789ABCDEF",
|
||||
"mac": "12:34:56:78:90:ab",
|
||||
"modelid": "deCONZ",
|
||||
"name": "Phoscon",
|
||||
"swversion": "2.05.35"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def test_config_with_host_passed_to_config_entry(hass):
|
||||
"""Test that configured options for a host are loaded via config entry."""
|
||||
@@ -93,8 +103,11 @@ async def test_setup_entry_successful(hass):
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
with patch.object(hass, 'async_create_task') as mock_add_job, \
|
||||
patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True), \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(Mock())):
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
assert hass.data[deconz.DOMAIN]
|
||||
assert hass.data[deconz.DATA_DECONZ_ID] == {}
|
||||
@@ -117,10 +130,15 @@ async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
with patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
entry.async_unload.return_value = mock_coro(True)
|
||||
deconzmock = Mock()
|
||||
deconzmock.async_load_parameters.return_value = mock_coro(True)
|
||||
deconzmock.sensors = {}
|
||||
with patch('pydeconz.DeconzSession', return_value=deconzmock):
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
assert deconz.DATA_DECONZ_EVENT in hass.data
|
||||
|
||||
hass.data[deconz.DATA_DECONZ_EVENT].append(Mock())
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {'id': 'deconzid'}
|
||||
assert await deconz.async_unload_entry(hass, entry)
|
||||
@@ -132,6 +150,9 @@ async def test_unload_entry(hass):
|
||||
|
||||
async def test_add_new_device(hass):
|
||||
"""Test adding a new device generates a signal for platforms."""
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80,
|
||||
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
||||
new_event = {
|
||||
"t": "event",
|
||||
"e": "added",
|
||||
@@ -147,11 +168,10 @@ async def test_add_new_device(hass):
|
||||
"type": "ZHASwitch"
|
||||
}
|
||||
}
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
with patch.object(deconz, 'async_dispatcher_send') as mock_dispatch_send, \
|
||||
patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
hass.data[deconz.DOMAIN].async_event_handler(new_event)
|
||||
await hass.async_block_till_done()
|
||||
@@ -162,15 +182,16 @@ async def test_add_new_device(hass):
|
||||
async def test_add_new_remote(hass):
|
||||
"""Test new added device creates a new remote."""
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80,
|
||||
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
||||
remote = Mock()
|
||||
remote.name = 'name'
|
||||
remote.type = 'ZHASwitch'
|
||||
remote.register_async_callback = Mock()
|
||||
with patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [remote])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 1
|
||||
@@ -185,8 +206,9 @@ async def test_do_not_allow_clip_sensor(hass):
|
||||
remote.name = 'name'
|
||||
remote.type = 'CLIPSwitch'
|
||||
remote.register_async_callback = Mock()
|
||||
with patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [remote])
|
||||
|
||||
@@ -78,9 +78,9 @@ class DysonTest(unittest.TestCase):
|
||||
def test_setup_component_with_no_devices(self):
|
||||
"""Test setup component with no devices."""
|
||||
self.hass.data[dyson.DYSON_DEVICES] = []
|
||||
add_devices = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, None, add_devices)
|
||||
add_devices.assert_called_with([])
|
||||
add_entities = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, None, add_entities)
|
||||
add_entities.assert_called_with([])
|
||||
|
||||
def test_setup_component(self):
|
||||
"""Test setup component with devices."""
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""Test Hue setup process."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import hue
|
||||
@@ -145,9 +145,21 @@ async def test_config_passed_to_config_entry(hass):
|
||||
'host': '0.0.0.0',
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(hue, 'HueBridge') as mock_bridge:
|
||||
mock_registry = Mock()
|
||||
with patch.object(hue, 'HueBridge') as mock_bridge, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(mock_registry)):
|
||||
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_bridge.return_value.api.config = Mock(
|
||||
mac='mock-mac',
|
||||
bridgeid='mock-bridgeid',
|
||||
raw={
|
||||
'modelid': 'mock-modelid',
|
||||
'swversion': 'mock-swversion',
|
||||
}
|
||||
)
|
||||
# Can't set name via kwargs
|
||||
mock_bridge.return_value.api.config.name = 'mock-name'
|
||||
assert await async_setup_component(hass, hue.DOMAIN, {
|
||||
hue.DOMAIN: {
|
||||
hue.CONF_BRIDGES: {
|
||||
@@ -168,6 +180,21 @@ async def test_config_passed_to_config_entry(hass):
|
||||
assert p_allow_unreachable is True
|
||||
assert p_allow_groups is False
|
||||
|
||||
assert len(mock_registry.mock_calls) == 1
|
||||
assert mock_registry.mock_calls[0][2] == {
|
||||
'config_entry': entry.entry_id,
|
||||
'connections': {
|
||||
('mac', 'mock-mac')
|
||||
},
|
||||
'identifiers': {
|
||||
('hue', 'mock-bridgeid')
|
||||
},
|
||||
'manufacturer': 'Signify',
|
||||
'name': 'mock-name',
|
||||
'model': 'mock-modelid',
|
||||
'sw_version': 'mock-swversion'
|
||||
}
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
@@ -176,8 +203,11 @@ async def test_unload_entry(hass):
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(hue, 'HueBridge') as mock_bridge:
|
||||
with patch.object(hue, 'HueBridge') as mock_bridge, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(Mock())):
|
||||
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_bridge.return_value.api.config = Mock()
|
||||
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
|
||||
|
||||
assert len(mock_bridge.return_value.mock_calls) == 1
|
||||
|
||||
@@ -346,13 +346,13 @@ async def test_service_calls(hass):
|
||||
|
||||
async def test_invalid_service_calls(hass):
|
||||
"""Test invalid service call arguments get discarded."""
|
||||
add_devices = MagicMock()
|
||||
add_entities = MagicMock()
|
||||
await group.async_setup_platform(hass, {
|
||||
'entities': ['light.test1', 'light.test2']
|
||||
}, add_devices)
|
||||
}, add_entities)
|
||||
|
||||
assert add_devices.call_count == 1
|
||||
grouped_light = add_devices.call_args[0][0][0]
|
||||
assert add_entities.call_count == 1
|
||||
grouped_light = add_entities.call_args[0][0][0]
|
||||
grouped_light.hass = hass
|
||||
|
||||
with asynctest.patch.object(hass.services, 'async_call') as mock_call:
|
||||
|
||||
@@ -51,13 +51,13 @@ async def async_setup_cast(hass, config=None, discovery_info=None):
|
||||
"""Set up the cast platform."""
|
||||
if config is None:
|
||||
config = {}
|
||||
add_devices = Mock()
|
||||
add_entities = Mock()
|
||||
|
||||
await cast.async_setup_platform(hass, config, add_devices,
|
||||
await cast.async_setup_platform(hass, config, add_entities,
|
||||
discovery_info=discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return add_devices
|
||||
return add_entities
|
||||
|
||||
|
||||
async def async_setup_cast_internal_discovery(hass, config=None,
|
||||
@@ -67,7 +67,7 @@ async def async_setup_cast_internal_discovery(hass, config=None,
|
||||
|
||||
with patch('pychromecast.start_discovery',
|
||||
return_value=(listener, None)) as start_discovery:
|
||||
add_devices = await async_setup_cast(hass, config, discovery_info)
|
||||
add_entities = await async_setup_cast(hass, config, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -77,10 +77,13 @@ async def async_setup_cast_internal_discovery(hass, config=None,
|
||||
|
||||
def discover_chromecast(service_name: str, info: ChromecastInfo) -> None:
|
||||
"""Discover a chromecast device."""
|
||||
listener.services[service_name] = attr.astuple(info)
|
||||
listener.services[service_name] = (
|
||||
info.host, info.port, info.uuid, info.model_name,
|
||||
info.friendly_name
|
||||
)
|
||||
discovery_callback(service_name)
|
||||
|
||||
return discover_chromecast, add_devices
|
||||
return discover_chromecast, add_entities
|
||||
|
||||
|
||||
async def async_setup_media_player_cast(hass: HomeAssistantType,
|
||||
@@ -152,8 +155,7 @@ async def test_internal_discovery_callback_only_generates_once(hass):
|
||||
discover_cast('the-service', info)
|
||||
await hass.async_block_till_done()
|
||||
discover = signal.mock_calls[0][1][0]
|
||||
# attr's __eq__ somehow breaks here, use tuples instead
|
||||
assert attr.astuple(discover) == attr.astuple(info)
|
||||
assert discover == info
|
||||
signal.reset_mock()
|
||||
|
||||
# discovering it a second time shouldn't
|
||||
@@ -183,8 +185,7 @@ async def test_internal_discovery_callback_fill_out(hass):
|
||||
|
||||
# when called with incomplete info, it should use HTTP to get missing
|
||||
discover = signal.mock_calls[0][1][0]
|
||||
# attr's __eq__ somehow breaks here, use tuples instead
|
||||
assert attr.astuple(discover) == attr.astuple(full_info)
|
||||
assert discover == full_info
|
||||
|
||||
|
||||
async def test_create_cast_device_without_uuid(hass):
|
||||
@@ -214,30 +215,30 @@ async def test_normal_chromecast_not_starting_discovery(hass):
|
||||
with patch('homeassistant.components.media_player.cast.'
|
||||
'_setup_internal_discovery') as setup_discovery:
|
||||
# normal (non-group) chromecast shouldn't start discovery.
|
||||
add_devices = await async_setup_cast(hass, {'host': 'host1'})
|
||||
add_entities = await async_setup_cast(hass, {'host': 'host1'})
|
||||
await hass.async_block_till_done()
|
||||
assert add_devices.call_count == 1
|
||||
assert add_entities.call_count == 1
|
||||
assert setup_discovery.call_count == 0
|
||||
|
||||
# Same entity twice
|
||||
add_devices = await async_setup_cast(hass, {'host': 'host1'})
|
||||
add_entities = await async_setup_cast(hass, {'host': 'host1'})
|
||||
await hass.async_block_till_done()
|
||||
assert add_devices.call_count == 0
|
||||
assert add_entities.call_count == 0
|
||||
assert setup_discovery.call_count == 0
|
||||
|
||||
hass.data[cast.ADDED_CAST_DEVICES_KEY] = set()
|
||||
add_devices = await async_setup_cast(
|
||||
add_entities = await async_setup_cast(
|
||||
hass, discovery_info={'host': 'host1', 'port': 8009})
|
||||
await hass.async_block_till_done()
|
||||
assert add_devices.call_count == 1
|
||||
assert add_entities.call_count == 1
|
||||
assert setup_discovery.call_count == 0
|
||||
|
||||
# group should start discovery.
|
||||
hass.data[cast.ADDED_CAST_DEVICES_KEY] = set()
|
||||
add_devices = await async_setup_cast(
|
||||
add_entities = await async_setup_cast(
|
||||
hass, discovery_info={'host': 'host1', 'port': 42})
|
||||
await hass.async_block_till_done()
|
||||
assert add_devices.call_count == 0
|
||||
assert add_entities.call_count == 0
|
||||
assert setup_discovery.call_count == 1
|
||||
|
||||
|
||||
|
||||
@@ -72,9 +72,9 @@ class TestSamsungTv(unittest.TestCase):
|
||||
"""Testing setup of platform."""
|
||||
with mock.patch(
|
||||
'homeassistant.components.media_player.samsungtv.socket'):
|
||||
add_devices = mock.Mock()
|
||||
add_entities = mock.Mock()
|
||||
setup_platform(
|
||||
self.hass, WORKING_CONFIG, add_devices)
|
||||
self.hass, WORKING_CONFIG, add_entities)
|
||||
|
||||
@MockDependency('samsungctl')
|
||||
@MockDependency('wakeonlan')
|
||||
@@ -82,8 +82,8 @@ class TestSamsungTv(unittest.TestCase):
|
||||
"""Testing setup of platform with discovery."""
|
||||
with mock.patch(
|
||||
'homeassistant.components.media_player.samsungtv.socket'):
|
||||
add_devices = mock.Mock()
|
||||
setup_platform(self.hass, {}, add_devices,
|
||||
add_entities = mock.Mock()
|
||||
setup_platform(self.hass, {}, add_entities,
|
||||
discovery_info=DISCOVERY_INFO)
|
||||
|
||||
@MockDependency('samsungctl')
|
||||
@@ -94,11 +94,11 @@ class TestSamsungTv(unittest.TestCase):
|
||||
"""Testing setup of platform with no data."""
|
||||
with mock.patch(
|
||||
'homeassistant.components.media_player.samsungtv.socket'):
|
||||
add_devices = mock.Mock()
|
||||
setup_platform(self.hass, {}, add_devices,
|
||||
add_entities = mock.Mock()
|
||||
setup_platform(self.hass, {}, add_entities,
|
||||
discovery_info=None)
|
||||
mocked_warn.assert_called_once_with("Cannot determine device")
|
||||
add_devices.assert_not_called()
|
||||
add_entities.assert_not_called()
|
||||
|
||||
@mock.patch(
|
||||
'homeassistant.components.media_player.samsungtv.subprocess.Popen'
|
||||
|
||||
@@ -121,13 +121,13 @@ class SoCoMock():
|
||||
return
|
||||
|
||||
|
||||
def add_devices_factory(hass):
|
||||
def add_entities_factory(hass):
|
||||
"""Add devices factory."""
|
||||
def add_devices(devices, update_befor_add=False):
|
||||
def add_entities(devices, update_befor_add=False):
|
||||
"""Fake add device."""
|
||||
hass.data[sonos.DATA_SONOS].devices = devices
|
||||
|
||||
return add_devices
|
||||
return add_entities
|
||||
|
||||
|
||||
class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@@ -157,7 +157,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch('socket.create_connection', side_effect=socket.error())
|
||||
def test_ensure_setup_discovery(self, *args):
|
||||
"""Test a single device using the autodiscovery provided by HASS."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
|
||||
@@ -240,7 +240,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch('socket.create_connection', side_effect=socket.error())
|
||||
def test_ensure_setup_sonos_discovery(self, *args):
|
||||
"""Test a single device using the autodiscovery provided by Sonos."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass))
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass))
|
||||
devices = list(self.hass.data[sonos.DATA_SONOS].devices)
|
||||
self.assertEqual(len(devices), 1)
|
||||
self.assertEqual(devices[0].name, 'Kitchen')
|
||||
@@ -250,7 +250,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch.object(SoCoMock, 'set_sleep_timer')
|
||||
def test_sonos_set_sleep_timer(self, set_sleep_timerMock, *args):
|
||||
"""Ensuring soco methods called for sonos_set_sleep_timer service."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
device = list(self.hass.data[sonos.DATA_SONOS].devices)[-1]
|
||||
@@ -264,7 +264,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch.object(SoCoMock, 'set_sleep_timer')
|
||||
def test_sonos_clear_sleep_timer(self, set_sleep_timerMock, *args):
|
||||
"""Ensuring soco methods called for sonos_clear_sleep_timer service."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
device = list(self.hass.data[sonos.DATA_SONOS].devices)[-1]
|
||||
@@ -278,7 +278,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch('socket.create_connection', side_effect=socket.error())
|
||||
def test_set_alarm(self, soco_mock, alarm_mock, *args):
|
||||
"""Ensuring soco methods called for sonos_set_sleep_timer service."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
device = list(self.hass.data[sonos.DATA_SONOS].devices)[-1]
|
||||
@@ -308,7 +308,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
@mock.patch.object(soco.snapshot.Snapshot, 'snapshot')
|
||||
def test_sonos_snapshot(self, snapshotMock, *args):
|
||||
"""Ensuring soco methods called for sonos_snapshot service."""
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
device = list(self.hass.data[sonos.DATA_SONOS].devices)[-1]
|
||||
@@ -326,7 +326,7 @@ class TestSonosMediaPlayer(unittest.TestCase):
|
||||
"""Ensuring soco methods called for sonos_restor service."""
|
||||
from soco.snapshot import Snapshot
|
||||
|
||||
sonos.setup_platform(self.hass, {}, add_devices_factory(self.hass), {
|
||||
sonos.setup_platform(self.hass, {}, add_entities_factory(self.hass), {
|
||||
'host': '192.0.2.1'
|
||||
})
|
||||
device = list(self.hass.data[sonos.DATA_SONOS].devices)[-1]
|
||||
|
||||
@@ -279,7 +279,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
bad_config = {'platform': 'universal'}
|
||||
entities = []
|
||||
|
||||
def add_devices(new_entities):
|
||||
def add_entities(new_entities):
|
||||
"""Add devices to list."""
|
||||
for dev in new_entities:
|
||||
entities.append(dev)
|
||||
@@ -288,7 +288,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
try:
|
||||
run_coroutine_threadsafe(
|
||||
universal.async_setup_platform(
|
||||
self.hass, validate_config(bad_config), add_devices),
|
||||
self.hass, validate_config(bad_config), add_entities),
|
||||
self.hass.loop).result()
|
||||
except MultipleInvalid:
|
||||
setup_ok = False
|
||||
@@ -297,7 +297,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
universal.async_setup_platform(
|
||||
self.hass, validate_config(config), add_devices),
|
||||
self.hass, validate_config(config), add_entities),
|
||||
self.hass.loop).result()
|
||||
self.assertEqual(1, len(entities))
|
||||
self.assertEqual('test', entities[0].name)
|
||||
|
||||
@@ -24,7 +24,7 @@ class TestKiraSensor(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -42,7 +42,7 @@ class TestKiraSensor(unittest.TestCase):
|
||||
|
||||
def test_service_call(self):
|
||||
"""Test Kira's ability to send commands."""
|
||||
kira.setup_platform(self.hass, TEST_CONFIG, self.add_devices,
|
||||
kira.setup_platform(self.hass, TEST_CONFIG, self.add_entities,
|
||||
DISCOVERY_INFO)
|
||||
assert len(self.DEVICES) == 1
|
||||
remote = self.DEVICES[0]
|
||||
|
||||
@@ -64,14 +64,14 @@ def captured_sensor():
|
||||
|
||||
|
||||
class PlatformSetupFixture():
|
||||
"""Fixture for testing platform setup call to add_devices()."""
|
||||
"""Fixture for testing platform setup call to add_entities()."""
|
||||
|
||||
def __init__(self):
|
||||
"""Instantiate the platform setup fixture."""
|
||||
self.sensors = None
|
||||
self.update = False
|
||||
|
||||
def add_devices(self, sensors, update):
|
||||
def add_entities(self, sensors, update):
|
||||
"""Mock method for adding devices."""
|
||||
self.sensors = sensors
|
||||
self.update = update
|
||||
@@ -101,7 +101,7 @@ def mock_dispatch():
|
||||
|
||||
def test_setup_with_no_data(platform_setup, hass):
|
||||
"""Test setup_platform with no data."""
|
||||
arlo.setup_platform(hass, None, platform_setup.add_devices)
|
||||
arlo.setup_platform(hass, None, platform_setup.add_entities)
|
||||
assert platform_setup.sensors is None
|
||||
assert not platform_setup.update
|
||||
|
||||
@@ -132,7 +132,7 @@ def test_setup_with_valid_data(platform_setup, hass):
|
||||
})]
|
||||
})
|
||||
|
||||
arlo.setup_platform(hass, config, platform_setup.add_devices)
|
||||
arlo.setup_platform(hass, config, platform_setup.add_entities)
|
||||
assert len(platform_setup.sensors) == 8
|
||||
assert platform_setup.update
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ class TestCanarySensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -51,7 +51,7 @@ class TestCanarySensorSetup(unittest.TestCase):
|
||||
mock_location("Work", True, devices=[online_device_at_work]),
|
||||
]
|
||||
|
||||
canary.setup_platform(self.hass, self.config, self.add_devices, None)
|
||||
canary.setup_platform(self.hass, self.config, self.add_entities, None)
|
||||
|
||||
self.assertEqual(6, len(self.DEVICES))
|
||||
|
||||
|
||||
@@ -62,9 +62,9 @@ class DysonTest(unittest.TestCase):
|
||||
def test_setup_component_with_no_devices(self):
|
||||
"""Test setup component with no devices."""
|
||||
self.hass.data[dyson.DYSON_DEVICES] = []
|
||||
add_devices = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, None, add_devices)
|
||||
add_devices.assert_called_with([])
|
||||
add_entities = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, None, add_entities)
|
||||
add_entities.assert_called_with([])
|
||||
|
||||
def test_setup_component(self):
|
||||
"""Test setup component with devices."""
|
||||
|
||||
@@ -61,7 +61,7 @@ class TestEfergySensor(unittest.TestCase):
|
||||
DEVICES = []
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def add_devices(self, devices, mock):
|
||||
def add_entities(self, devices, mock):
|
||||
"""Mock add devices."""
|
||||
mock_responses(mock)
|
||||
for device in devices:
|
||||
|
||||
@@ -59,8 +59,8 @@ class PyFidoFakeModule():
|
||||
FidoClient = FidoClientMockError
|
||||
|
||||
|
||||
def fake_async_add_devices(component, update_before_add=False):
|
||||
"""Fake async_add_devices function."""
|
||||
def fake_async_add_entities(component, update_before_add=False):
|
||||
"""Fake async_add_entities function."""
|
||||
pass
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ def test_error(hass, caplog):
|
||||
sys.modules['pyfido.client'] = PyFidoClientFakeModule()
|
||||
|
||||
config = {}
|
||||
fake_async_add_devices = MagicMock()
|
||||
fake_async_add_entities = MagicMock()
|
||||
yield from fido.async_setup_platform(hass, config,
|
||||
fake_async_add_devices)
|
||||
assert fake_async_add_devices.called is False
|
||||
fake_async_add_entities)
|
||||
assert fake_async_add_entities.called is False
|
||||
@@ -44,18 +44,18 @@ async def test_default_setup(hass, aioclient_mock):
|
||||
|
||||
async def test_setup_timeout_error(hass, aioclient_mock):
|
||||
"""Expected failures caused by a timeout in API response."""
|
||||
fake_async_add_devices = MagicMock()
|
||||
fake_async_add_entities = MagicMock()
|
||||
|
||||
aioclient_mock.get(re.compile('api.foobot.io/v2/owner/.*'),
|
||||
exc=asyncio.TimeoutError())
|
||||
with pytest.raises(PlatformNotReady):
|
||||
await foobot.async_setup_platform(hass, {'sensor': VALID_CONFIG},
|
||||
fake_async_add_devices)
|
||||
fake_async_add_entities)
|
||||
|
||||
|
||||
async def test_setup_permanent_error(hass, aioclient_mock):
|
||||
"""Expected failures caused by permanent errors in API response."""
|
||||
fake_async_add_devices = MagicMock()
|
||||
fake_async_add_entities = MagicMock()
|
||||
|
||||
errors = [400, 401, 403]
|
||||
for error in errors:
|
||||
@@ -63,13 +63,13 @@ async def test_setup_permanent_error(hass, aioclient_mock):
|
||||
status=error)
|
||||
result = await foobot.async_setup_platform(hass,
|
||||
{'sensor': VALID_CONFIG},
|
||||
fake_async_add_devices)
|
||||
fake_async_add_entities)
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_setup_temporary_error(hass, aioclient_mock):
|
||||
"""Expected failures caused by temporary errors in API response."""
|
||||
fake_async_add_devices = MagicMock()
|
||||
fake_async_add_entities = MagicMock()
|
||||
|
||||
errors = [429, 500]
|
||||
for error in errors:
|
||||
@@ -78,4 +78,4 @@ async def test_setup_temporary_error(hass, aioclient_mock):
|
||||
with pytest.raises(PlatformNotReady):
|
||||
await foobot.async_setup_platform(hass,
|
||||
{'sensor': VALID_CONFIG},
|
||||
fake_async_add_devices)
|
||||
fake_async_add_entities)
|
||||
@@ -99,7 +99,7 @@ def test_error(hass, caplog):
|
||||
sys.modules['pyhydroquebec.client'] = PyHydroQuebecClientFakeModule()
|
||||
|
||||
config = {}
|
||||
fake_async_add_devices = MagicMock()
|
||||
fake_async_add_entities = MagicMock()
|
||||
yield from hydroquebec.async_setup_platform(hass, config,
|
||||
fake_async_add_devices)
|
||||
assert fake_async_add_devices.called is False
|
||||
fake_async_add_entities)
|
||||
assert fake_async_add_entities.called is False
|
||||
@@ -22,7 +22,7 @@ class TestKiraSensor(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -41,7 +41,7 @@ class TestKiraSensor(unittest.TestCase):
|
||||
# pylint: disable=protected-access
|
||||
def test_kira_sensor_callback(self):
|
||||
"""Ensure Kira sensor properly updates its attributes from callback."""
|
||||
kira.setup_platform(self.hass, TEST_CONFIG, self.add_devices,
|
||||
kira.setup_platform(self.hass, TEST_CONFIG, self.add_entities,
|
||||
DISCOVERY_INFO)
|
||||
assert len(self.DEVICES) == 1
|
||||
sensor = self.DEVICES[0]
|
||||
|
||||
@@ -42,10 +42,10 @@ class TestMelissa(unittest.TestCase):
|
||||
self.hass.data[DATA_MELISSA] = self.api
|
||||
|
||||
config = {}
|
||||
add_devices = Mock()
|
||||
add_entities = Mock()
|
||||
discovery_info = {}
|
||||
|
||||
melissa.setup_platform(self.hass, config, add_devices, discovery_info)
|
||||
melissa.setup_platform(self.hass, config, add_entities, discovery_info)
|
||||
|
||||
def test_name(self):
|
||||
"""Test name property."""
|
||||
|
||||
@@ -193,7 +193,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, update):
|
||||
def add_entities(self, devices, update):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -221,7 +221,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'diskspace'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('263.10', device.state)
|
||||
@@ -248,7 +248,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'diskspace'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('263.10', device.state)
|
||||
@@ -275,7 +275,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'commands'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -302,7 +302,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'movies'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -329,7 +329,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -360,7 +360,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -387,7 +387,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'status'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('0.2.0.210', device.state)
|
||||
@@ -413,7 +413,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
],
|
||||
"ssl": "true"
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -441,7 +441,7 @@ class TestRadarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
radarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
radarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(None, device.state)
|
||||
@@ -16,7 +16,7 @@ class TestRingSensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -66,7 +66,7 @@ class TestRingSensorSetup(unittest.TestCase):
|
||||
base_ring.setup(self.hass, VALID_CONFIG)
|
||||
ring.setup_platform(self.hass,
|
||||
self.config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
for device in self.DEVICES:
|
||||
|
||||
@@ -60,7 +60,7 @@ class TestSeason(unittest.TestCase):
|
||||
CONFIG_ASTRONOMICAL = {'type': 'astronomical'}
|
||||
CONFIG_METEOROLOGICAL = {'type': 'meteorological'}
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICE = device
|
||||
|
||||
@@ -16,7 +16,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -49,7 +49,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
|
||||
sleepiq.setup_platform(self.hass,
|
||||
self.config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
MagicMock())
|
||||
self.assertEqual(2, len(self.DEVICES))
|
||||
|
||||
|
||||
@@ -579,7 +579,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, update):
|
||||
def add_entities(self, devices, update):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -607,7 +607,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'diskspace'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('263.10', device.state)
|
||||
@@ -634,7 +634,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'diskspace'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('263.10', device.state)
|
||||
@@ -661,7 +661,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'commands'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -688,7 +688,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'queue'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -715,7 +715,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'series'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -742,7 +742,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'wanted'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -769,7 +769,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -800,7 +800,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -827,7 +827,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'status'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual('2.0.0.1121', device.state)
|
||||
@@ -854,7 +854,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
],
|
||||
"ssl": "true"
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(1, device.state)
|
||||
@@ -882,7 +882,7 @@ class TestSonarrSetup(unittest.TestCase):
|
||||
'upcoming'
|
||||
]
|
||||
}
|
||||
sonarr.setup_platform(self.hass, config, self.add_devices, None)
|
||||
sonarr.setup_platform(self.hass, config, self.add_entities, None)
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
self.assertEqual(None, device.state)
|
||||
@@ -48,14 +48,14 @@ class TestTCPSensor(unittest.TestCase):
|
||||
|
||||
@patch('homeassistant.components.sensor.tcp.TcpSensor.update')
|
||||
def test_setup_platform_valid_config(self, mock_update):
|
||||
"""Check a valid configuration and call add_devices with sensor."""
|
||||
"""Check a valid configuration and call add_entities with sensor."""
|
||||
with assert_setup_component(0, 'sensor'):
|
||||
assert setup_component(self.hass, 'sensor', TEST_CONFIG)
|
||||
|
||||
add_devices = Mock()
|
||||
tcp.setup_platform(None, TEST_CONFIG['sensor'], add_devices)
|
||||
assert add_devices.called
|
||||
assert isinstance(add_devices.call_args[0][0][0], tcp.TcpSensor)
|
||||
add_entities = Mock()
|
||||
tcp.setup_platform(None, TEST_CONFIG['sensor'], add_entities)
|
||||
assert add_entities.called
|
||||
assert isinstance(add_entities.call_args[0][0][0], tcp.TcpSensor)
|
||||
|
||||
def test_setup_platform_invalid_config(self):
|
||||
"""Check an invalid configuration."""
|
||||
|
||||
@@ -14,7 +14,7 @@ class TestTimeDateSensor(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
|
||||
@@ -23,7 +23,7 @@ class TestVultrSensorSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -71,7 +71,7 @@ class TestVultrSensorSetup(unittest.TestCase):
|
||||
|
||||
for config in self.configs:
|
||||
setup = vultr.setup_platform(
|
||||
self.hass, config, self.add_devices, None)
|
||||
self.hass, config, self.add_entities, None)
|
||||
|
||||
self.assertIsNone(setup)
|
||||
|
||||
@@ -160,7 +160,7 @@ class TestVultrSensorSetup(unittest.TestCase):
|
||||
} # No subs at all
|
||||
|
||||
no_sub_setup = vultr.setup_platform(
|
||||
self.hass, bad_conf, self.add_devices, None)
|
||||
self.hass, bad_conf, self.add_entities, None)
|
||||
|
||||
self.assertIsNone(no_sub_setup)
|
||||
self.assertEqual(0, len(self.DEVICES))
|
||||
@@ -26,7 +26,7 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices, action):
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
@@ -71,7 +71,7 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
||||
for config in self.configs:
|
||||
vultr.setup_platform(self.hass,
|
||||
config,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertEqual(len(self.DEVICES), 3)
|
||||
@@ -181,7 +181,7 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
||||
|
||||
no_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertIsNotNone(no_subs_setup)
|
||||
@@ -193,7 +193,7 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
||||
|
||||
wrong_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_devices,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
self.assertIsNotNone(wrong_subs_setup)
|
||||
@@ -290,11 +290,11 @@ async def test_http_api_wrong_data(hass, aiohttp_client):
|
||||
def test_create_matcher():
|
||||
"""Test the create matcher method."""
|
||||
# Basic sentence
|
||||
pattern = conversation._create_matcher('Hello world')
|
||||
pattern = conversation.create_matcher('Hello world')
|
||||
assert pattern.match('Hello world') is not None
|
||||
|
||||
# Match a part
|
||||
pattern = conversation._create_matcher('Hello {name}')
|
||||
pattern = conversation.create_matcher('Hello {name}')
|
||||
match = pattern.match('hello world')
|
||||
assert match is not None
|
||||
assert match.groupdict()['name'] == 'world'
|
||||
@@ -302,7 +302,7 @@ def test_create_matcher():
|
||||
assert no_match is None
|
||||
|
||||
# Optional and matching part
|
||||
pattern = conversation._create_matcher('Turn on [the] {name}')
|
||||
pattern = conversation.create_matcher('Turn on [the] {name}')
|
||||
match = pattern.match('turn on the kitchen lights')
|
||||
assert match is not None
|
||||
assert match.groupdict()['name'] == 'kitchen lights'
|
||||
@@ -313,7 +313,7 @@ def test_create_matcher():
|
||||
assert match is None
|
||||
|
||||
# Two different optional parts, 1 matching part
|
||||
pattern = conversation._create_matcher('Turn on [the] [a] {name}')
|
||||
pattern = conversation.create_matcher('Turn on [the] [a] {name}')
|
||||
match = pattern.match('turn on the kitchen lights')
|
||||
assert match is not None
|
||||
assert match.groupdict()['name'] == 'kitchen lights'
|
||||
@@ -325,13 +325,13 @@ def test_create_matcher():
|
||||
assert match.groupdict()['name'] == 'kitchen light'
|
||||
|
||||
# Strip plural
|
||||
pattern = conversation._create_matcher('Turn {name}[s] on')
|
||||
pattern = conversation.create_matcher('Turn {name}[s] on')
|
||||
match = pattern.match('turn kitchen lights on')
|
||||
assert match is not None
|
||||
assert match.groupdict()['name'] == 'kitchen light'
|
||||
|
||||
# Optional 2 words
|
||||
pattern = conversation._create_matcher('Turn [the great] {name} on')
|
||||
pattern = conversation.create_matcher('Turn [the great] {name} on')
|
||||
match = pattern.match('turn the great kitchen lights on')
|
||||
assert match is not None
|
||||
assert match.groupdict()['name'] == 'kitchen lights'
|
||||
|
||||
@@ -77,9 +77,9 @@ class DysonTest(unittest.TestCase):
|
||||
def test_setup_component_with_no_devices(self):
|
||||
"""Test setup component with no devices."""
|
||||
self.hass.data[dyson.DYSON_DEVICES] = []
|
||||
add_devices = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, {}, add_devices)
|
||||
add_devices.assert_called_with([])
|
||||
add_entities = mock.MagicMock()
|
||||
dyson.setup_platform(self.hass, {}, add_entities)
|
||||
add_entities.assert_called_with([])
|
||||
|
||||
def test_setup_component(self):
|
||||
"""Test setup component with devices."""
|
||||
|
||||
@@ -76,7 +76,7 @@ class TestWeather(unittest.TestCase):
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_devices(self, devices):
|
||||
def add_entities(self, devices):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.update()
|
||||
|
||||
@@ -128,24 +128,24 @@ def test_setup_platform(hass, mock_openzwave):
|
||||
mock_device = MagicMock()
|
||||
hass.data[DATA_NETWORK] = MagicMock()
|
||||
hass.data[zwave.DATA_DEVICES] = {456: mock_device}
|
||||
async_add_devices = MagicMock()
|
||||
async_add_entities = MagicMock()
|
||||
|
||||
result = yield from zwave.async_setup_platform(
|
||||
hass, None, async_add_devices, None)
|
||||
hass, None, async_add_entities, None)
|
||||
assert not result
|
||||
assert not async_add_devices.called
|
||||
assert not async_add_entities.called
|
||||
|
||||
result = yield from zwave.async_setup_platform(
|
||||
hass, None, async_add_devices, {const.DISCOVERY_DEVICE: 123})
|
||||
hass, None, async_add_entities, {const.DISCOVERY_DEVICE: 123})
|
||||
assert not result
|
||||
assert not async_add_devices.called
|
||||
assert not async_add_entities.called
|
||||
|
||||
result = yield from zwave.async_setup_platform(
|
||||
hass, None, async_add_devices, {const.DISCOVERY_DEVICE: 456})
|
||||
hass, None, async_add_entities, {const.DISCOVERY_DEVICE: 456})
|
||||
assert result
|
||||
assert async_add_devices.called
|
||||
assert len(async_add_devices.mock_calls) == 1
|
||||
assert async_add_devices.mock_calls[0][1][0] == [mock_device]
|
||||
assert async_add_entities.called
|
||||
assert len(async_add_entities.mock_calls) == 1
|
||||
assert async_add_entities.mock_calls[0][1][0] == [mock_device]
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
"""Tests for the Device Registry."""
|
||||
import pytest
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from homeassistant.helpers import device_registry
|
||||
|
||||
|
||||
def mock_registry(hass, mock_entries=None):
|
||||
"""Mock the Device Registry."""
|
||||
registry = device_registry.DeviceRegistry(hass)
|
||||
registry.devices = mock_entries or []
|
||||
registry.devices = mock_entries or OrderedDict()
|
||||
|
||||
async def _get_reg():
|
||||
return registry
|
||||
@@ -26,19 +28,73 @@ def registry(hass):
|
||||
async def test_get_or_create_returns_same_entry(registry):
|
||||
"""Make sure we do not duplicate entries."""
|
||||
entry = registry.async_get_or_create(
|
||||
[['bridgeid', '0123']], 'manufacturer', 'model',
|
||||
[['ethernet', '12:34:56:78:90:AB:CD:EF']])
|
||||
config_entry='1234',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry2 = registry.async_get_or_create(
|
||||
[['bridgeid', '0123']], 'manufacturer', 'model',
|
||||
[['ethernet', '11:22:33:44:55:66:77:88']])
|
||||
config_entry='1234',
|
||||
connections={('ethernet', '11:22:33:44:55:66:77:88')},
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry3 = registry.async_get_or_create(
|
||||
[['bridgeid', '1234']], 'manufacturer', 'model',
|
||||
[['ethernet', '12:34:56:78:90:AB:CD:EF']])
|
||||
config_entry='1234',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers={('bridgeid', '1234')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
|
||||
assert len(registry.devices) == 1
|
||||
assert entry is entry2
|
||||
assert entry is entry3
|
||||
assert entry.identifiers == [['bridgeid', '0123']]
|
||||
assert entry.identifiers == {('bridgeid', '0123')}
|
||||
|
||||
|
||||
async def test_requirement_for_identifier_or_connection(registry):
|
||||
"""Make sure we do require some descriptor of device."""
|
||||
entry = registry.async_get_or_create(
|
||||
config_entry='1234',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers=set(),
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry2 = registry.async_get_or_create(
|
||||
config_entry='1234',
|
||||
connections=set(),
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry3 = registry.async_get_or_create(
|
||||
config_entry='1234',
|
||||
connections=set(),
|
||||
identifiers=set(),
|
||||
manufacturer='manufacturer', model='model')
|
||||
|
||||
assert len(registry.devices) == 2
|
||||
assert entry
|
||||
assert entry2
|
||||
assert entry3 is None
|
||||
|
||||
|
||||
async def test_multiple_config_entries(registry):
|
||||
"""Make sure we do not get duplicate entries."""
|
||||
entry = registry.async_get_or_create(
|
||||
config_entry='123',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry2 = registry.async_get_or_create(
|
||||
config_entry='456',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
entry3 = registry.async_get_or_create(
|
||||
config_entry='123',
|
||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
||||
identifiers={('bridgeid', '0123')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
|
||||
assert len(registry.devices) == 1
|
||||
assert entry is entry2
|
||||
assert entry is entry3
|
||||
assert entry.config_entries == {'123', '456'}
|
||||
|
||||
|
||||
async def test_loading_from_storage(hass, hass_storage):
|
||||
@@ -48,7 +104,10 @@ async def test_loading_from_storage(hass, hass_storage):
|
||||
'data': {
|
||||
'devices': [
|
||||
{
|
||||
'connection': [
|
||||
'config_entries': [
|
||||
'1234'
|
||||
],
|
||||
'connections': [
|
||||
[
|
||||
'Zigbee',
|
||||
'01.23.45.67.89'
|
||||
@@ -64,7 +123,7 @@ async def test_loading_from_storage(hass, hass_storage):
|
||||
'manufacturer': 'manufacturer',
|
||||
'model': 'model',
|
||||
'name': 'name',
|
||||
'sw_version': 'version'
|
||||
'sw_version': 'version',
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -73,6 +132,9 @@ async def test_loading_from_storage(hass, hass_storage):
|
||||
registry = await device_registry.async_get_registry(hass)
|
||||
|
||||
entry = registry.async_get_or_create(
|
||||
[['serial', '12:34:56:78:90:AB:CD:EF']], 'manufacturer',
|
||||
'model', [['Zigbee', '01.23.45.67.89']])
|
||||
config_entry='1234',
|
||||
connections={('Zigbee', '01.23.45.67.89')},
|
||||
identifiers={('serial', '12:34:56:78:90:AB:CD:EF')},
|
||||
manufacturer='manufacturer', model='model')
|
||||
assert entry.id == 'abcdefghijklm'
|
||||
assert isinstance(entry.config_entries, set)
|
||||
@@ -123,7 +123,7 @@ class TestHelpersDiscovery:
|
||||
component_calls.append(1)
|
||||
return True
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback,
|
||||
def setup_platform(hass, config, add_entities_callback,
|
||||
discovery_info=None):
|
||||
"""Set up mock platform."""
|
||||
platform_calls.append('disc' if discovery_info else 'component')
|
||||
|
||||
@@ -143,9 +143,9 @@ class TestHelpersEntityComponent(unittest.TestCase):
|
||||
'async_track_time_interval')
|
||||
def test_set_scan_interval_via_config(self, mock_track):
|
||||
"""Test the setting of the scan interval via configuration."""
|
||||
def platform_setup(hass, config, add_devices, discovery_info=None):
|
||||
def platform_setup(hass, config, add_entities, discovery_info=None):
|
||||
"""Test the platform setup."""
|
||||
add_devices([MockEntity(should_poll=True)])
|
||||
add_entities([MockEntity(should_poll=True)])
|
||||
|
||||
loader.set_component(self.hass, 'test_domain.platform',
|
||||
MockPlatform(platform_setup))
|
||||
@@ -165,9 +165,9 @@ class TestHelpersEntityComponent(unittest.TestCase):
|
||||
|
||||
def test_set_entity_namespace_via_config(self):
|
||||
"""Test setting an entity namespace."""
|
||||
def platform_setup(hass, config, add_devices, discovery_info=None):
|
||||
def platform_setup(hass, config, add_entities, discovery_info=None):
|
||||
"""Test the platform setup."""
|
||||
add_devices([
|
||||
add_entities([
|
||||
MockEntity(name='beer'),
|
||||
MockEntity(name=None),
|
||||
])
|
||||
|
||||
@@ -142,9 +142,9 @@ class TestHelpersEntityPlatform(unittest.TestCase):
|
||||
'async_track_time_interval')
|
||||
def test_set_scan_interval_via_platform(self, mock_track):
|
||||
"""Test the setting of the scan interval via platform."""
|
||||
def platform_setup(hass, config, add_devices, discovery_info=None):
|
||||
def platform_setup(hass, config, add_entities, discovery_info=None):
|
||||
"""Test the platform setup."""
|
||||
add_devices([MockEntity(should_poll=True)])
|
||||
add_entities([MockEntity(should_poll=True)])
|
||||
|
||||
platform = MockPlatform(platform_setup)
|
||||
platform.SCAN_INTERVAL = timedelta(seconds=30)
|
||||
@@ -520,9 +520,9 @@ async def test_setup_entry(hass):
|
||||
"""Test we can setup an entry."""
|
||||
registry = mock_registry(hass)
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_devices):
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Mock setup entry method."""
|
||||
async_add_devices([
|
||||
async_add_entities([
|
||||
MockEntity(name='test1', unique_id='unique')
|
||||
])
|
||||
return True
|
||||
|
||||
+98
-4
@@ -16,7 +16,7 @@ from homeassistant.const import (
|
||||
CONF_LATITUDE, CONF_LONGITUDE, CONF_UNIT_SYSTEM, CONF_NAME,
|
||||
CONF_TIME_ZONE, CONF_ELEVATION, CONF_CUSTOMIZE, __version__,
|
||||
CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL, CONF_TEMPERATURE_UNIT,
|
||||
CONF_AUTH_PROVIDERS)
|
||||
CONF_AUTH_PROVIDERS, CONF_AUTH_MFA_MODULES)
|
||||
from homeassistant.util import location as location_util, dt as dt_util
|
||||
from homeassistant.util.yaml import SECRET_YAML
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
@@ -805,6 +805,10 @@ async def test_auth_provider_config(hass):
|
||||
CONF_AUTH_PROVIDERS: [
|
||||
{'type': 'homeassistant'},
|
||||
{'type': 'legacy_api_password'},
|
||||
],
|
||||
CONF_AUTH_MFA_MODULES: [
|
||||
{'type': 'totp'},
|
||||
{'type': 'totp', 'id': 'second'},
|
||||
]
|
||||
}
|
||||
if hasattr(hass, 'auth'):
|
||||
@@ -815,6 +819,9 @@ async def test_auth_provider_config(hass):
|
||||
assert hass.auth.auth_providers[0].type == 'homeassistant'
|
||||
assert hass.auth.auth_providers[1].type == 'legacy_api_password'
|
||||
assert hass.auth.active is True
|
||||
assert len(hass.auth.auth_mfa_modules) == 2
|
||||
assert hass.auth.auth_mfa_modules[0].id == 'totp'
|
||||
assert hass.auth.auth_mfa_modules[1].id == 'second'
|
||||
|
||||
|
||||
async def test_auth_provider_config_default(hass):
|
||||
@@ -834,6 +841,8 @@ async def test_auth_provider_config_default(hass):
|
||||
assert len(hass.auth.auth_providers) == 1
|
||||
assert hass.auth.auth_providers[0].type == 'homeassistant'
|
||||
assert hass.auth.active is True
|
||||
assert len(hass.auth.auth_mfa_modules) == 1
|
||||
assert hass.auth.auth_mfa_modules[0].id == 'totp'
|
||||
|
||||
|
||||
async def test_auth_provider_config_default_api_password(hass):
|
||||
@@ -856,6 +865,27 @@ async def test_auth_provider_config_default_api_password(hass):
|
||||
assert hass.auth.active is True
|
||||
|
||||
|
||||
async def test_auth_provider_config_default_trusted_networks(hass):
|
||||
"""Test loading default auth provider config with trusted networks."""
|
||||
core_config = {
|
||||
'latitude': 60,
|
||||
'longitude': 50,
|
||||
'elevation': 25,
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
}
|
||||
if hasattr(hass, 'auth'):
|
||||
del hass.auth
|
||||
await config_util.async_process_ha_core_config(hass, core_config,
|
||||
has_trusted_networks=True)
|
||||
|
||||
assert len(hass.auth.auth_providers) == 2
|
||||
assert hass.auth.auth_providers[0].type == 'homeassistant'
|
||||
assert hass.auth.auth_providers[1].type == 'trusted_networks'
|
||||
assert hass.auth.active is True
|
||||
|
||||
|
||||
async def test_disallowed_auth_provider_config(hass):
|
||||
"""Test loading insecure example auth provider is disallowed."""
|
||||
core_config = {
|
||||
@@ -865,9 +895,73 @@ async def test_disallowed_auth_provider_config(hass):
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
CONF_AUTH_PROVIDERS: [
|
||||
{'type': 'insecure_example'},
|
||||
]
|
||||
CONF_AUTH_PROVIDERS: [{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
'name': 'Test Name'
|
||||
}],
|
||||
}]
|
||||
}
|
||||
with pytest.raises(Invalid):
|
||||
await config_util.async_process_ha_core_config(hass, core_config)
|
||||
|
||||
|
||||
async def test_disallowed_duplicated_auth_provider_config(hass):
|
||||
"""Test loading insecure example auth provider is disallowed."""
|
||||
core_config = {
|
||||
'latitude': 60,
|
||||
'longitude': 50,
|
||||
'elevation': 25,
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
CONF_AUTH_PROVIDERS: [{
|
||||
'type': 'homeassistant',
|
||||
}, {
|
||||
'type': 'homeassistant',
|
||||
}]
|
||||
}
|
||||
with pytest.raises(Invalid):
|
||||
await config_util.async_process_ha_core_config(hass, core_config)
|
||||
|
||||
|
||||
async def test_disallowed_auth_mfa_module_config(hass):
|
||||
"""Test loading insecure example auth mfa module is disallowed."""
|
||||
core_config = {
|
||||
'latitude': 60,
|
||||
'longitude': 50,
|
||||
'elevation': 25,
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
CONF_AUTH_MFA_MODULES: [{
|
||||
'type': 'insecure_example',
|
||||
'data': [{
|
||||
'user_id': 'mock-user',
|
||||
'pin': 'test-pin'
|
||||
}]
|
||||
}]
|
||||
}
|
||||
with pytest.raises(Invalid):
|
||||
await config_util.async_process_ha_core_config(hass, core_config)
|
||||
|
||||
|
||||
async def test_disallowed_duplicated_auth_mfa_module_config(hass):
|
||||
"""Test loading insecure example auth mfa module is disallowed."""
|
||||
core_config = {
|
||||
'latitude': 60,
|
||||
'longitude': 50,
|
||||
'elevation': 25,
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
CONF_AUTH_MFA_MODULES: [{
|
||||
'type': 'totp',
|
||||
}, {
|
||||
'type': 'totp',
|
||||
}]
|
||||
}
|
||||
with pytest.raises(Invalid):
|
||||
await config_util.async_process_ha_core_config(hass, core_config)
|
||||
+77
-15
@@ -1,11 +1,22 @@
|
||||
"""Test requirements module."""
|
||||
import os
|
||||
from unittest import mock
|
||||
from unittest.mock import patch, call
|
||||
|
||||
from homeassistant import loader, setup
|
||||
from homeassistant.requirements import CONSTRAINT_FILE
|
||||
from homeassistant.requirements import (
|
||||
CONSTRAINT_FILE, PackageLoadable, async_process_requirements)
|
||||
|
||||
from tests.common import get_test_home_assistant, MockModule
|
||||
import pkg_resources
|
||||
|
||||
from tests.common import get_test_home_assistant, MockModule, mock_coro
|
||||
|
||||
RESOURCE_DIR = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), '..', 'resources'))
|
||||
|
||||
TEST_NEW_REQ = 'pyhelloworld3==1.0.0'
|
||||
|
||||
TEST_ZIP_REQ = 'file://{}#{}' \
|
||||
.format(os.path.join(RESOURCE_DIR, 'pyhelloworld3.zip'), TEST_NEW_REQ)
|
||||
|
||||
|
||||
class TestRequirements:
|
||||
@@ -23,11 +34,9 @@ class TestRequirements:
|
||||
"""Clean up."""
|
||||
self.hass.stop()
|
||||
|
||||
@mock.patch('os.path.dirname')
|
||||
@mock.patch('homeassistant.util.package.is_virtual_env',
|
||||
return_value=True)
|
||||
@mock.patch('homeassistant.util.package.install_package',
|
||||
return_value=True)
|
||||
@patch('os.path.dirname')
|
||||
@patch('homeassistant.util.package.is_virtual_env', return_value=True)
|
||||
@patch('homeassistant.util.package.install_package', return_value=True)
|
||||
def test_requirement_installed_in_venv(
|
||||
self, mock_install, mock_venv, mock_dirname):
|
||||
"""Test requirement installed in virtual environment."""
|
||||
@@ -39,15 +48,13 @@ class TestRequirements:
|
||||
MockModule('comp', requirements=['package==0.0.1']))
|
||||
assert setup.setup_component(self.hass, 'comp')
|
||||
assert 'comp' in self.hass.config.components
|
||||
assert mock_install.call_args == mock.call(
|
||||
assert mock_install.call_args == call(
|
||||
'package==0.0.1',
|
||||
constraints=os.path.join('ha_package_path', CONSTRAINT_FILE))
|
||||
|
||||
@mock.patch('os.path.dirname')
|
||||
@mock.patch('homeassistant.util.package.is_virtual_env',
|
||||
return_value=False)
|
||||
@mock.patch('homeassistant.util.package.install_package',
|
||||
return_value=True)
|
||||
@patch('os.path.dirname')
|
||||
@patch('homeassistant.util.package.is_virtual_env', return_value=False)
|
||||
@patch('homeassistant.util.package.install_package', return_value=True)
|
||||
def test_requirement_installed_in_deps(
|
||||
self, mock_install, mock_venv, mock_dirname):
|
||||
"""Test requirement installed in deps directory."""
|
||||
@@ -58,6 +65,61 @@ class TestRequirements:
|
||||
MockModule('comp', requirements=['package==0.0.1']))
|
||||
assert setup.setup_component(self.hass, 'comp')
|
||||
assert 'comp' in self.hass.config.components
|
||||
assert mock_install.call_args == mock.call(
|
||||
assert mock_install.call_args == call(
|
||||
'package==0.0.1', target=self.hass.config.path('deps'),
|
||||
constraints=os.path.join('ha_package_path', CONSTRAINT_FILE))
|
||||
|
||||
|
||||
async def test_install_existing_package(hass):
|
||||
"""Test an install attempt on an existing package."""
|
||||
with patch('homeassistant.util.package.install_package',
|
||||
return_value=mock_coro(True)) as mock_inst:
|
||||
assert await async_process_requirements(
|
||||
hass, 'test_component', ['hello==1.0.0'])
|
||||
|
||||
assert len(mock_inst.mock_calls) == 1
|
||||
|
||||
with patch('homeassistant.requirements.PackageLoadable.loadable',
|
||||
return_value=mock_coro(True)), \
|
||||
patch(
|
||||
'homeassistant.util.package.install_package') as mock_inst:
|
||||
assert await async_process_requirements(
|
||||
hass, 'test_component', ['hello==1.0.0'])
|
||||
|
||||
assert len(mock_inst.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_check_package_global(hass):
|
||||
"""Test for an installed package."""
|
||||
installed_package = list(pkg_resources.working_set)[0].project_name
|
||||
assert await PackageLoadable(hass).loadable(installed_package)
|
||||
|
||||
|
||||
async def test_check_package_zip(hass):
|
||||
"""Test for an installed zip package."""
|
||||
assert not await PackageLoadable(hass).loadable(TEST_ZIP_REQ)
|
||||
|
||||
|
||||
async def test_package_loadable_installed_twice(hass):
|
||||
"""Test that a package is loadable when installed twice.
|
||||
|
||||
If a package is installed twice, only the first version will be imported.
|
||||
Test that package_loadable will only compare with the first package.
|
||||
"""
|
||||
v1 = pkg_resources.Distribution(project_name='hello', version='1.0.0')
|
||||
v2 = pkg_resources.Distribution(project_name='hello', version='2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v1]]):
|
||||
assert not await PackageLoadable(hass).loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v1], [v2]]):
|
||||
assert not await PackageLoadable(hass).loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v2], [v1]]):
|
||||
assert await PackageLoadable(hass).loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v2]]):
|
||||
assert await PackageLoadable(hass).loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v2]]):
|
||||
assert await PackageLoadable(hass).loadable('Hello==2.0.0')
|
||||
@@ -3,10 +3,10 @@
|
||||
from homeassistant.components.image_processing import ImageProcessingEntity
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_devices_callback,
|
||||
async def async_setup_platform(hass, config, async_add_entities_callback,
|
||||
discovery_info=None):
|
||||
"""Set up the test image_processing platform."""
|
||||
async_add_devices_callback([
|
||||
async_add_entities_callback([
|
||||
TestImageProcessing('camera.demo_camera', "Test")])
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ def init(empty=False):
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_devices_callback,
|
||||
async def async_setup_platform(hass, config, async_add_entities_callback,
|
||||
discovery_info=None):
|
||||
"""Return mock devices."""
|
||||
async_add_devices_callback(DEVICES)
|
||||
async_add_entities_callback(DEVICES)
|
||||
@@ -21,7 +21,7 @@ def init(empty=False):
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_devices_callback,
|
||||
async def async_setup_platform(hass, config, async_add_entities_callback,
|
||||
discovery_info=None):
|
||||
"""Find and return test switches."""
|
||||
async_add_devices_callback(DEVICES)
|
||||
async_add_entities_callback(DEVICES)
|
||||
@@ -6,18 +6,12 @@ import sys
|
||||
from subprocess import PIPE
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
import pkg_resources
|
||||
import pytest
|
||||
|
||||
import homeassistant.util.package as package
|
||||
|
||||
RESOURCE_DIR = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), '..', 'resources'))
|
||||
|
||||
TEST_EXIST_REQ = 'pip>=7.0.0'
|
||||
TEST_NEW_REQ = 'pyhelloworld3==1.0.0'
|
||||
TEST_ZIP_REQ = 'file://{}#{}' \
|
||||
.format(os.path.join(RESOURCE_DIR, 'pyhelloworld3.zip'), TEST_NEW_REQ)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -28,14 +22,6 @@ def mock_sys():
|
||||
yield sys_mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_exists():
|
||||
"""Mock package_loadable."""
|
||||
with patch('homeassistant.util.package.package_loadable') as mock:
|
||||
mock.return_value = False
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deps_dir():
|
||||
"""Return path to deps directory."""
|
||||
@@ -89,20 +75,10 @@ def mock_async_subprocess():
|
||||
return async_popen
|
||||
|
||||
|
||||
def test_install_existing_package(mock_exists, mock_popen):
|
||||
"""Test an install attempt on an existing package."""
|
||||
mock_exists.return_value = True
|
||||
assert package.install_package(TEST_EXIST_REQ)
|
||||
assert mock_exists.call_count == 1
|
||||
assert mock_exists.call_args == call(TEST_EXIST_REQ)
|
||||
assert mock_popen.return_value.communicate.call_count == 0
|
||||
|
||||
|
||||
def test_install(mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
def test_install(mock_sys, mock_popen, mock_env_copy, mock_venv):
|
||||
"""Test an install attempt on a package that doesn't exist."""
|
||||
env = mock_env_copy()
|
||||
assert package.install_package(TEST_NEW_REQ, False)
|
||||
assert mock_exists.call_count == 1
|
||||
assert mock_popen.call_count == 1
|
||||
assert (
|
||||
mock_popen.call_args ==
|
||||
@@ -115,11 +91,10 @@ def test_install(mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
|
||||
|
||||
def test_install_upgrade(
|
||||
mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
mock_sys, mock_popen, mock_env_copy, mock_venv):
|
||||
"""Test an upgrade attempt on a package."""
|
||||
env = mock_env_copy()
|
||||
assert package.install_package(TEST_NEW_REQ)
|
||||
assert mock_exists.call_count == 1
|
||||
assert mock_popen.call_count == 1
|
||||
assert (
|
||||
mock_popen.call_args ==
|
||||
@@ -131,8 +106,7 @@ def test_install_upgrade(
|
||||
assert mock_popen.return_value.communicate.call_count == 1
|
||||
|
||||
|
||||
def test_install_target(
|
||||
mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
def test_install_target(mock_sys, mock_popen, mock_env_copy, mock_venv):
|
||||
"""Test an install with a target."""
|
||||
target = 'target_folder'
|
||||
env = mock_env_copy()
|
||||
@@ -144,7 +118,6 @@ def test_install_target(
|
||||
TEST_NEW_REQ, '--user', '--prefix=']
|
||||
|
||||
assert package.install_package(TEST_NEW_REQ, False, target=target)
|
||||
assert mock_exists.call_count == 1
|
||||
assert mock_popen.call_count == 1
|
||||
assert (
|
||||
mock_popen.call_args ==
|
||||
@@ -153,15 +126,14 @@ def test_install_target(
|
||||
assert mock_popen.return_value.communicate.call_count == 1
|
||||
|
||||
|
||||
def test_install_target_venv(
|
||||
mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
def test_install_target_venv(mock_sys, mock_popen, mock_env_copy, mock_venv):
|
||||
"""Test an install with a target in a virtual environment."""
|
||||
target = 'target_folder'
|
||||
with pytest.raises(AssertionError):
|
||||
package.install_package(TEST_NEW_REQ, False, target=target)
|
||||
|
||||
|
||||
def test_install_error(caplog, mock_sys, mock_exists, mock_popen, mock_venv):
|
||||
def test_install_error(caplog, mock_sys, mock_popen, mock_venv):
|
||||
"""Test an install with a target."""
|
||||
caplog.set_level(logging.WARNING)
|
||||
mock_popen.return_value.returncode = 1
|
||||
@@ -171,14 +143,12 @@ def test_install_error(caplog, mock_sys, mock_exists, mock_popen, mock_venv):
|
||||
assert record.levelname == 'ERROR'
|
||||
|
||||
|
||||
def test_install_constraint(
|
||||
mock_sys, mock_exists, mock_popen, mock_env_copy, mock_venv):
|
||||
def test_install_constraint(mock_sys, mock_popen, mock_env_copy, mock_venv):
|
||||
"""Test install with constraint file on not installed package."""
|
||||
env = mock_env_copy()
|
||||
constraints = 'constraints_file.txt'
|
||||
assert package.install_package(
|
||||
TEST_NEW_REQ, False, constraints=constraints)
|
||||
assert mock_exists.call_count == 1
|
||||
assert mock_popen.call_count == 1
|
||||
assert (
|
||||
mock_popen.call_args ==
|
||||
@@ -190,17 +160,6 @@ def test_install_constraint(
|
||||
assert mock_popen.return_value.communicate.call_count == 1
|
||||
|
||||
|
||||
def test_check_package_global():
|
||||
"""Test for an installed package."""
|
||||
installed_package = list(pkg_resources.working_set)[0].project_name
|
||||
assert package.package_loadable(installed_package)
|
||||
|
||||
|
||||
def test_check_package_zip():
|
||||
"""Test for an installed zip package."""
|
||||
assert not package.package_loadable(TEST_ZIP_REQ)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_get_user_site(mock_env_copy):
|
||||
"""Test async get user site directory."""
|
||||
@@ -217,25 +176,3 @@ def test_async_get_user_site(mock_env_copy):
|
||||
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL,
|
||||
env=env)
|
||||
assert ret == os.path.join(deps_dir, 'lib_dir')
|
||||
|
||||
|
||||
def test_package_loadable_installed_twice():
|
||||
"""Test that a package is loadable when installed twice.
|
||||
|
||||
If a package is installed twice, only the first version will be imported.
|
||||
Test that package_loadable will only compare with the first package.
|
||||
"""
|
||||
v1 = pkg_resources.Distribution(project_name='hello', version='1.0.0')
|
||||
v2 = pkg_resources.Distribution(project_name='hello', version='2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v1]]):
|
||||
assert not package.package_loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v1], [v2]]):
|
||||
assert not package.package_loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v2], [v1]]):
|
||||
assert package.package_loadable('hello==2.0.0')
|
||||
|
||||
with patch('pkg_resources.find_distributions', side_effect=[[v2]]):
|
||||
assert package.package_loadable('hello==2.0.0')
|
||||
Reference in new issue
Block a user