Reorg auth (#15443)

This commit is contained in:
Paulus Schoutsen
2018-07-13 11:43:08 +02:00
committed by GitHub
parent 23f1b49e55
commit b6ca03ce47
23 changed files with 698 additions and 652 deletions
+1
View File
@@ -0,0 +1 @@
"""Tests for the Home Assistant auth module."""
@@ -2,7 +2,7 @@
import pytest
from homeassistant import data_entry_flow
from homeassistant.auth_providers import homeassistant as hass_auth
from homeassistant.auth.providers import homeassistant as hass_auth
@pytest.fixture
@@ -4,8 +4,8 @@ import uuid
import pytest
from homeassistant import auth
from homeassistant.auth_providers import insecure_example
from homeassistant.auth import auth_store, models as auth_models
from homeassistant.auth.providers import insecure_example
from tests.common import mock_coro
@@ -13,7 +13,7 @@ from tests.common import mock_coro
@pytest.fixture
def store(hass):
"""Mock store."""
return auth.AuthStore(hass)
return auth_store.AuthStore(hass)
@pytest.fixture
@@ -45,7 +45,7 @@ async def test_create_new_credential(provider):
async def test_match_existing_credentials(store, provider):
"""See if we match existing users."""
existing = auth.Credentials(
existing = auth_models.Credentials(
id=uuid.uuid4(),
auth_provider_type='insecure_example',
auth_provider_id=None,
@@ -4,13 +4,14 @@ from unittest.mock import Mock
import pytest
from homeassistant import auth
from homeassistant.auth_providers import legacy_api_password
from homeassistant.auth import auth_store
from homeassistant.auth.providers import legacy_api_password
@pytest.fixture
def store(hass):
"""Mock store."""
return auth.AuthStore(hass)
return auth_store.AuthStore(hass)
@pytest.fixture
+16 -11
View File
@@ -5,6 +5,8 @@ from unittest.mock import Mock, patch
import pytest
from homeassistant import auth, data_entry_flow
from homeassistant.auth import (
models as auth_models, auth_store, const as auth_const)
from homeassistant.util import dt as dt_util
from tests.common import (
MockUser, ensure_auth_manager_loaded, flush_store, CLIENT_ID)
@@ -101,7 +103,7 @@ async def test_login_as_existing_user(mock_hass):
is_active=False,
name='Not user',
).add_to_auth_manager(manager)
user.credentials.append(auth.Credentials(
user.credentials.append(auth_models.Credentials(
id='mock-id2',
auth_provider_type='insecure_example',
auth_provider_id=None,
@@ -116,7 +118,7 @@ async def test_login_as_existing_user(mock_hass):
is_active=False,
name='Paulus',
).add_to_auth_manager(manager)
user.credentials.append(auth.Credentials(
user.credentials.append(auth_models.Credentials(
id='mock-id',
auth_provider_type='insecure_example',
auth_provider_id=None,
@@ -203,7 +205,7 @@ async def test_saving_loading(hass, hass_storage):
await flush_store(manager._store._store)
store2 = auth.AuthStore(hass)
store2 = auth_store.AuthStore(hass)
users = await store2.async_get_users()
assert len(users) == 1
assert users[0] == user
@@ -211,23 +213,25 @@ async def test_saving_loading(hass, hass_storage):
def test_access_token_expired():
"""Test that the expired property on access tokens work."""
refresh_token = auth.RefreshToken(
refresh_token = auth_models.RefreshToken(
user=None,
client_id='bla'
)
access_token = auth.AccessToken(
access_token = auth_models.AccessToken(
refresh_token=refresh_token
)
assert access_token.expired is False
with patch('homeassistant.auth.dt_util.utcnow',
return_value=dt_util.utcnow() + auth.ACCESS_TOKEN_EXPIRATION):
with patch('homeassistant.util.dt.utcnow',
return_value=dt_util.utcnow() +
auth_const.ACCESS_TOKEN_EXPIRATION):
assert access_token.expired is True
almost_exp = dt_util.utcnow() + auth.ACCESS_TOKEN_EXPIRATION - timedelta(1)
with patch('homeassistant.auth.dt_util.utcnow', return_value=almost_exp):
almost_exp = \
dt_util.utcnow() + auth_const.ACCESS_TOKEN_EXPIRATION - timedelta(1)
with patch('homeassistant.util.dt.utcnow', return_value=almost_exp):
assert access_token.expired is False
@@ -242,8 +246,9 @@ async def test_cannot_retrieve_expired_access_token(hass):
access_token = manager.async_create_access_token(refresh_token)
assert manager.async_get_access_token(access_token.token) is access_token
with patch('homeassistant.auth.dt_util.utcnow',
return_value=dt_util.utcnow() + auth.ACCESS_TOKEN_EXPIRATION):
with patch('homeassistant.util.dt.utcnow',
return_value=dt_util.utcnow() +
auth_const.ACCESS_TOKEN_EXPIRATION):
assert manager.async_get_access_token(access_token.token) is None
# Even with unpatched time, it should have been removed from manager
+3 -2
View File
@@ -12,6 +12,7 @@ import threading
from contextlib import contextmanager
from homeassistant import auth, core as ha, data_entry_flow, config_entries
from homeassistant.auth import models as auth_models, auth_store
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.config import async_process_component_config
from homeassistant.helpers import (
@@ -114,7 +115,7 @@ def async_test_home_assistant(loop):
"""Return a Home Assistant object pointing at test config dir."""
hass = ha.HomeAssistant(loop)
hass.config.async_load = Mock()
store = auth.AuthStore(hass)
store = auth_store.AuthStore(hass)
hass.auth = auth.AuthManager(hass, store, {})
ensure_auth_manager_loaded(hass.auth)
INSTANCES.append(hass)
@@ -308,7 +309,7 @@ def mock_registry(hass, mock_entries=None):
return registry
class MockUser(auth.User):
class MockUser(auth_models.User):
"""Mock a user in Home Assistant."""
def __init__(self, id='mock-id', is_owner=True, is_active=True,
+1 -1
View File
@@ -7,7 +7,7 @@ import pytest
from aiohttp import BasicAuth, web
from aiohttp.web_exceptions import HTTPUnauthorized
from homeassistant.auth import AccessToken, RefreshToken
from homeassistant.auth.models import AccessToken, RefreshToken
from homeassistant.components.http.auth import setup_auth
from homeassistant.components.http.const import KEY_AUTHENTICATED
from homeassistant.components.http.real_ip import setup_real_ip
+1 -1
View File
@@ -4,7 +4,7 @@ from unittest.mock import Mock, patch
import pytest
from homeassistant.scripts import auth as script_auth
from homeassistant.auth_providers import homeassistant as hass_auth
from homeassistant.auth.providers import homeassistant as hass_auth
@pytest.fixture