1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

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
2019-05-07 22:51:24 -07:00
committed by GitHub
parent 07ee3b2eb9
commit f4016b4aad
9 changed files with 205 additions and 25 deletions

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