Add onboarding support (#15492)
* Add onboarding support * Lint * Address comments * Mark user step as done if owner user already created
This commit is contained in:
1 parent
a14d8057ed
commit
a4318682f7
8 files changed
+409
-2
No files matched your search
@@ -0,0 +1,11 @@
|
||||
"""Tests for the onboarding component."""
|
||||
|
||||
from homeassistant.components import onboarding
|
||||
|
||||
|
||||
def mock_storage(hass_storage, data):
|
||||
"""Mock the onboarding storage."""
|
||||
hass_storage[onboarding.STORAGE_KEY] = {
|
||||
'version': onboarding.STORAGE_VERSION,
|
||||
'data': data
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Tests for the init."""
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import onboarding
|
||||
|
||||
from tests.common import mock_coro, MockUser
|
||||
|
||||
from . import mock_storage
|
||||
|
||||
# Temporarily: if auth not active, always set onboarded=True
|
||||
|
||||
|
||||
async def test_not_setup_views_if_onboarded(hass, hass_storage):
|
||||
"""Test if onboarding is done, we don't setup views."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': onboarding.STEPS
|
||||
})
|
||||
|
||||
with patch(
|
||||
'homeassistant.components.onboarding.views.async_setup'
|
||||
) as mock_setup:
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
assert len(mock_setup.mock_calls) == 0
|
||||
assert onboarding.DOMAIN not in hass.data
|
||||
assert onboarding.async_is_onboarded(hass)
|
||||
|
||||
|
||||
async def test_setup_views_if_not_onboarded(hass):
|
||||
"""Test if onboarding is not done, we setup views."""
|
||||
with patch(
|
||||
'homeassistant.components.onboarding.views.async_setup',
|
||||
return_value=mock_coro()
|
||||
) as mock_setup:
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert onboarding.DOMAIN in hass.data
|
||||
|
||||
with patch('homeassistant.auth.AuthManager.active', return_value=True):
|
||||
assert not onboarding.async_is_onboarded(hass)
|
||||
|
||||
|
||||
async def test_is_onboarded():
|
||||
"""Test the is onboarded function."""
|
||||
hass = Mock()
|
||||
hass.data = {}
|
||||
|
||||
with patch('homeassistant.auth.AuthManager.active', return_value=False):
|
||||
assert onboarding.async_is_onboarded(hass)
|
||||
|
||||
with patch('homeassistant.auth.AuthManager.active', return_value=True):
|
||||
assert onboarding.async_is_onboarded(hass)
|
||||
|
||||
hass.data[onboarding.DOMAIN] = True
|
||||
assert onboarding.async_is_onboarded(hass)
|
||||
|
||||
hass.data[onboarding.DOMAIN] = False
|
||||
assert not onboarding.async_is_onboarded(hass)
|
||||
|
||||
|
||||
async def test_having_owner_finishes_user_step(hass, hass_storage):
|
||||
"""If owner user already exists, mark user step as complete."""
|
||||
MockUser(is_owner=True).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
'homeassistant.components.onboarding.views.async_setup'
|
||||
) as mock_setup, patch.object(onboarding, 'STEPS', [onboarding.STEP_USER]):
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
assert len(mock_setup.mock_calls) == 0
|
||||
assert onboarding.DOMAIN not in hass.data
|
||||
assert onboarding.async_is_onboarded(hass)
|
||||
|
||||
done = hass_storage[onboarding.STORAGE_KEY]['data']['done']
|
||||
assert onboarding.STEP_USER in done
|
||||
@@ -0,0 +1,137 @@
|
||||
"""Test the onboarding views."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import onboarding
|
||||
from homeassistant.components.onboarding import views
|
||||
|
||||
from tests.common import register_auth_provider
|
||||
|
||||
from . import mock_storage
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def auth_active(hass):
|
||||
"""Ensure auth is always active."""
|
||||
hass.loop.run_until_complete(register_auth_provider(hass, {
|
||||
'type': 'homeassistant'
|
||||
}))
|
||||
|
||||
|
||||
async def test_onboarding_progress(hass, hass_storage, aiohttp_client):
|
||||
"""Test fetching progress."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': ['hello']
|
||||
})
|
||||
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
with patch.object(views, 'STEPS', ['hello', 'world']):
|
||||
resp = await client.get('/api/onboarding')
|
||||
|
||||
assert resp.status == 200
|
||||
data = await resp.json()
|
||||
assert len(data) == 2
|
||||
assert data[0] == {
|
||||
'step': 'hello',
|
||||
'done': True
|
||||
}
|
||||
assert data[1] == {
|
||||
'step': 'world',
|
||||
'done': False
|
||||
}
|
||||
|
||||
|
||||
async def test_onboarding_user_already_done(hass, hass_storage,
|
||||
aiohttp_client):
|
||||
"""Test creating a new user when user step already done."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': [views.STEP_USER]
|
||||
})
|
||||
|
||||
with patch.object(onboarding, 'STEPS', ['hello', 'world']):
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
resp = await client.post('/api/onboarding/users', json={
|
||||
'name': 'Test Name',
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
|
||||
assert resp.status == 403
|
||||
|
||||
|
||||
async def test_onboarding_user(hass, hass_storage, aiohttp_client):
|
||||
"""Test creating a new user."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': ['hello']
|
||||
})
|
||||
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
resp = await client.post('/api/onboarding/users', json={
|
||||
'name': 'Test Name',
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
users = await hass.auth.async_get_users()
|
||||
assert len(users) == 1
|
||||
user = users[0]
|
||||
assert user.name == 'Test Name'
|
||||
assert len(user.credentials) == 1
|
||||
assert user.credentials[0].data['username'] == 'test-user'
|
||||
|
||||
|
||||
async def test_onboarding_user_invalid_name(hass, hass_storage,
|
||||
aiohttp_client):
|
||||
"""Test not providing name."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': ['hello']
|
||||
})
|
||||
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
resp = await client.post('/api/onboarding/users', json={
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
|
||||
assert resp.status == 400
|
||||
|
||||
|
||||
async def test_onboarding_user_race(hass, hass_storage, aiohttp_client):
|
||||
"""Test race condition on creating new user."""
|
||||
mock_storage(hass_storage, {
|
||||
'done': ['hello']
|
||||
})
|
||||
|
||||
assert await async_setup_component(hass, 'onboarding', {})
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
resp1 = client.post('/api/onboarding/users', json={
|
||||
'name': 'Test 1',
|
||||
'username': '1-user',
|
||||
'password': '1-pass',
|
||||
})
|
||||
resp2 = client.post('/api/onboarding/users', json={
|
||||
'name': 'Test 2',
|
||||
'username': '2-user',
|
||||
'password': '2-pass',
|
||||
})
|
||||
|
||||
res1, res2 = await asyncio.gather(resp1, resp2)
|
||||
|
||||
assert sorted([res1.status, res2.status]) == [200, 403]
|
||||
Reference in new issue
Block a user