Add integration step to onboarding (#23732)

* Return an extra oauth2 auth code during onboarding

* Areas in const

* Add integration step

* Lint

* Fix tests

* Fix test

* Verify integration added to done

* Verify step is marked as done
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-05-07 22:51:24 -07:00
1 parent 07ee3b2eb9
commit f4016b4aad
9 files changed
+205 -25

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ async def test_fetch_auth_providers(hass, aiohttp_client):
async def test_fetch_auth_providers_onboarding(hass, aiohttp_client):
"""Test fetching auth providers."""
client = await async_setup_auth(hass, aiohttp_client)
with patch('homeassistant.components.onboarding.async_is_onboarded',
with patch('homeassistant.components.onboarding.async_is_user_onboarded',
return_value=False):
resp = await client.get('/auth/providers')
assert resp.status == 400
+31 -1
View File
@@ -51,10 +51,28 @@ async def test_is_onboarded():
hass.data[onboarding.DOMAIN] = True
assert onboarding.async_is_onboarded(hass)
hass.data[onboarding.DOMAIN] = False
hass.data[onboarding.DOMAIN] = {
'done': []
}
assert not onboarding.async_is_onboarded(hass)
async def test_is_user_onboarded():
"""Test the is onboarded function."""
hass = Mock()
hass.data = {}
assert onboarding.async_is_user_onboarded(hass)
hass.data[onboarding.DOMAIN] = True
assert onboarding.async_is_user_onboarded(hass)
hass.data[onboarding.DOMAIN] = {
'done': []
}
assert not onboarding.async_is_user_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)
@@ -70,3 +88,15 @@ async def test_having_owner_finishes_user_step(hass, hass_storage):
done = hass_storage[onboarding.STORAGE_KEY]['data']['done']
assert onboarding.STEP_USER in done
async def test_migration(hass, hass_storage):
"""Test migrating onboarding to new version."""
hass_storage[onboarding.STORAGE_KEY] = {
'version': 1,
'data': {
'done': ["user"]
}
}
assert await async_setup_component(hass, 'onboarding', {})
assert onboarding.async_is_onboarded(hass)
+71 -7
View File
@@ -6,7 +6,7 @@ import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import onboarding
from homeassistant.components.onboarding import views
from homeassistant.components.onboarding import const, views
from tests.common import CLIENT_ID, register_auth_provider
@@ -63,6 +63,7 @@ async def test_onboarding_user_already_done(hass, hass_storage,
'name': 'Test Name',
'username': 'test-user',
'password': 'test-pass',
'language': 'en',
})
assert resp.status == 403
@@ -71,10 +72,6 @@ async def test_onboarding_user_already_done(hass, hass_storage,
async def test_onboarding_user(hass, hass_storage, aiohttp_client):
"""Test creating a new user."""
assert await async_setup_component(hass, 'person', {})
mock_storage(hass_storage, {
'done': ['hello']
})
assert await async_setup_component(hass, 'onboarding', {})
client = await aiohttp_client(hass.http.app)
@@ -84,9 +81,12 @@ async def test_onboarding_user(hass, hass_storage, aiohttp_client):
'name': 'Test Name',
'username': 'test-user',
'password': 'test-pass',
'language': 'en',
})
assert resp.status == 200
assert const.STEP_USER in hass_storage[const.DOMAIN]['data']['done']
data = await resp.json()
assert 'auth_code' in data
@@ -98,7 +98,7 @@ async def test_onboarding_user(hass, hass_storage, aiohttp_client):
assert user.credentials[0].data['username'] == 'test-user'
assert len(hass.data['person'].storage_data) == 1
# Request refresh tokens
# Validate refresh token 1
resp = await client.post('/auth/token', data={
'client_id': CLIENT_ID,
'grant_type': 'authorization_code',
@@ -113,12 +113,20 @@ async def test_onboarding_user(hass, hass_storage, aiohttp_client):
is not None
)
# Validate created areas
area_registry = await hass.helpers.area_registry.async_get_registry()
assert len(area_registry.areas) == 3
assert sorted([area.name for area
in area_registry.async_list_areas()]) == [
'Bedroom', 'Kitchen', 'Living Room'
]
async def test_onboarding_user_invalid_name(hass, hass_storage,
aiohttp_client):
"""Test not providing name."""
mock_storage(hass_storage, {
'done': ['hello']
'done': []
})
assert await async_setup_component(hass, 'onboarding', {})
@@ -129,6 +137,7 @@ async def test_onboarding_user_invalid_name(hass, hass_storage,
'client_id': CLIENT_ID,
'username': 'test-user',
'password': 'test-pass',
'language': 'en',
})
assert resp.status == 400
@@ -149,14 +158,69 @@ async def test_onboarding_user_race(hass, hass_storage, aiohttp_client):
'name': 'Test 1',
'username': '1-user',
'password': '1-pass',
'language': 'en',
})
resp2 = client.post('/api/onboarding/users', json={
'client_id': CLIENT_ID,
'name': 'Test 2',
'username': '2-user',
'password': '2-pass',
'language': 'es',
})
res1, res2 = await asyncio.gather(resp1, resp2)
assert sorted([res1.status, res2.status]) == [200, 403]
async def test_onboarding_integration(hass, hass_storage, hass_client):
"""Test finishing integration step."""
mock_storage(hass_storage, {
'done': [const.STEP_USER]
})
assert await async_setup_component(hass, 'onboarding', {})
client = await hass_client()
resp = await client.post('/api/onboarding/integration', json={
'client_id': CLIENT_ID,
})
assert resp.status == 200
data = await resp.json()
assert 'auth_code' in data
# Validate refresh token
resp = await client.post('/auth/token', data={
'client_id': CLIENT_ID,
'grant_type': 'authorization_code',
'code': data['auth_code']
})
assert resp.status == 200
assert const.STEP_INTEGRATION in hass_storage[const.DOMAIN]['data']['done']
tokens = await resp.json()
assert (
await hass.auth.async_validate_access_token(tokens['access_token'])
is not None
)
async def test_onboarding_integration_requires_auth(hass, hass_storage,
aiohttp_client):
"""Test finishing integration step."""
mock_storage(hass_storage, {
'done': [const.STEP_USER]
})
assert await async_setup_component(hass, 'onboarding', {})
client = await aiohttp_client(hass.http.app)
resp = await client.post('/api/onboarding/integration', json={
'client_id': CLIENT_ID,
})
assert resp.status == 401