This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+44 -33
View File
@@ -14,27 +14,35 @@ from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
from homeassistant.auth.providers import legacy_api_password, homeassistant
from tests.common import (
async_test_home_assistant, INSTANCES, mock_coro,
mock_storage as mock_storage, MockUser, CLIENT_ID)
async_test_home_assistant,
INSTANCES,
mock_coro,
mock_storage as mock_storage,
MockUser,
CLIENT_ID,
)
from tests.test_util.aiohttp import mock_aiohttp_client
if os.environ.get('UVLOOP') == '1':
if os.environ.get("UVLOOP") == "1":
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
def check_real(func):
"""Force a function to require a keyword _test_real to be passed in."""
@functools.wraps(func)
def guard_func(*args, **kwargs):
real = kwargs.pop('_test_real', None)
real = kwargs.pop("_test_real", None)
if not real:
raise Exception('Forgot to mock or pass "_test_real=True" to %s',
func.__name__)
raise Exception(
'Forgot to mock or pass "_test_real=True" to %s', func.__name__
)
return func(*args, **kwargs)
@@ -42,9 +50,8 @@ def check_real(func):
# Guard a few functions that would make network connections
location.async_detect_location_info = \
check_real(location.async_detect_location_info)
util.get_local_ip = lambda: '127.0.0.1'
location.async_detect_location_info = check_real(location.async_detect_location_info)
util.get_local_ip = lambda: "127.0.0.1"
@pytest.fixture(autouse=True)
@@ -56,8 +63,9 @@ def verify_cleanup():
count = len(INSTANCES)
for inst in INSTANCES:
inst.stop()
pytest.exit("Detected non stopped instances "
"({}), aborting test run".format(count))
pytest.exit(
"Detected non stopped instances " "({}), aborting test run".format(count)
)
@pytest.fixture
@@ -100,12 +108,12 @@ def mock_device_tracker_conf():
devices.append(entity)
with patch(
'homeassistant.components.device_tracker.legacy'
'.DeviceTracker.async_update_config',
side_effect=mock_update_config
"homeassistant.components.device_tracker.legacy"
".DeviceTracker.async_update_config",
side_effect=mock_update_config,
), patch(
'homeassistant.components.device_tracker.legacy.async_load_config',
side_effect=lambda *args: mock_coro(devices)
"homeassistant.components.device_tracker.legacy.async_load_config",
side_effect=lambda *args: mock_coro(devices),
):
yield devices
@@ -114,7 +122,8 @@ def mock_device_tracker_conf():
def hass_access_token(hass, hass_admin_user):
"""Return an access token to access Home Assistant."""
refresh_token = hass.loop.run_until_complete(
hass.auth.async_create_refresh_token(hass_admin_user, CLIENT_ID))
hass.auth.async_create_refresh_token(hass_admin_user, CLIENT_ID)
)
return hass.auth.async_create_access_token(refresh_token)
@@ -127,16 +136,18 @@ def hass_owner_user(hass, local_auth):
@pytest.fixture
def hass_admin_user(hass, local_auth):
"""Return a Home Assistant admin user."""
admin_group = hass.loop.run_until_complete(hass.auth.async_get_group(
GROUP_ID_ADMIN))
admin_group = hass.loop.run_until_complete(
hass.auth.async_get_group(GROUP_ID_ADMIN)
)
return MockUser(groups=[admin_group]).add_to_hass(hass)
@pytest.fixture
def hass_read_only_user(hass, local_auth):
"""Return a Home Assistant read only user."""
read_only_group = hass.loop.run_until_complete(hass.auth.async_get_group(
GROUP_ID_READ_ONLY))
read_only_group = hass.loop.run_until_complete(
hass.auth.async_get_group(GROUP_ID_READ_ONLY)
)
return MockUser(groups=[read_only_group]).add_to_hass(hass)
@@ -144,7 +155,8 @@ def hass_read_only_user(hass, local_auth):
def hass_read_only_access_token(hass, hass_read_only_user):
"""Return a Home Assistant read only user."""
refresh_token = hass.loop.run_until_complete(
hass.auth.async_create_refresh_token(hass_read_only_user, CLIENT_ID))
hass.auth.async_create_refresh_token(hass_read_only_user, CLIENT_ID)
)
return hass.auth.async_create_access_token(refresh_token)
@@ -152,10 +164,9 @@ def hass_read_only_access_token(hass, hass_read_only_user):
def legacy_auth(hass):
"""Load legacy API password provider."""
prv = legacy_api_password.LegacyApiPasswordAuthProvider(
hass, hass.auth._store, {
'type': 'legacy_api_password',
'api_password': 'test-password',
}
hass,
hass.auth._store,
{"type": "legacy_api_password", "api_password": "test-password"},
)
hass.auth._providers[(prv.type, prv.id)] = prv
return prv
@@ -165,9 +176,7 @@ def legacy_auth(hass):
def local_auth(hass):
"""Load local auth provider."""
prv = homeassistant.HassAuthProvider(
hass, hass.auth._store, {
'type': 'homeassistant'
}
hass, hass.auth._store, {"type": "homeassistant"}
)
hass.auth._providers[(prv.type, prv.id)] = prv
return prv
@@ -176,10 +185,12 @@ def local_auth(hass):
@pytest.fixture
def hass_client(hass, aiohttp_client, hass_access_token):
"""Return an authenticated HTTP client."""
async def auth_client():
"""Return an authenticated client."""
return await aiohttp_client(hass.http.app, headers={
'Authorization': "Bearer {}".format(hass_access_token)
})
return await aiohttp_client(
hass.http.app,
headers={"Authorization": "Bearer {}".format(hass_access_token)},
)
return auth_client