2018-05-01 16:20:41 +00:00
|
|
|
"""Integration tests for the auth component."""
|
2018-07-10 09:20:22 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2018-07-17 07:24:51 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-07-10 09:20:22 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
from homeassistant.components import auth
|
|
|
|
|
2018-07-09 16:24:46 +00:00
|
|
|
from . import async_setup_auth
|
|
|
|
|
|
|
|
from tests.common import CLIENT_ID, CLIENT_REDIRECT_URI
|
2018-05-01 16:20:41 +00:00
|
|
|
|
|
|
|
|
2018-07-15 18:46:15 +00:00
|
|
|
async def test_login_new_user_and_trying_refresh_token(hass, aiohttp_client):
|
2018-05-01 16:20:41 +00:00
|
|
|
"""Test logging in with new user and refreshing tokens."""
|
|
|
|
client = await async_setup_auth(hass, aiohttp_client, setup_api=True)
|
|
|
|
resp = await client.post('/auth/login_flow', json={
|
2018-07-09 16:24:46 +00:00
|
|
|
'client_id': CLIENT_ID,
|
2018-05-10 08:38:11 +00:00
|
|
|
'handler': ['insecure_example', None],
|
|
|
|
'redirect_uri': CLIENT_REDIRECT_URI,
|
2018-07-09 16:24:46 +00:00
|
|
|
})
|
2018-05-01 16:20:41 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
step = await resp.json()
|
|
|
|
|
|
|
|
resp = await client.post(
|
|
|
|
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
2018-07-09 16:24:46 +00:00
|
|
|
'client_id': CLIENT_ID,
|
2018-05-01 16:20:41 +00:00
|
|
|
'username': 'test-user',
|
|
|
|
'password': 'test-pass',
|
2018-07-09 16:24:46 +00:00
|
|
|
})
|
2018-05-01 16:20:41 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
step = await resp.json()
|
|
|
|
code = step['result']
|
|
|
|
|
|
|
|
# Exchange code for tokens
|
|
|
|
resp = await client.post('/auth/token', data={
|
2018-07-15 18:46:15 +00:00
|
|
|
'client_id': CLIENT_ID,
|
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
'code': code
|
2018-05-01 16:20:41 +00:00
|
|
|
})
|
2018-07-15 18:46:15 +00:00
|
|
|
|
|
|
|
# User is not active
|
2018-07-15 21:09:05 +00:00
|
|
|
assert resp.status == 403
|
|
|
|
data = await resp.json()
|
|
|
|
assert data['error'] == 'access_denied'
|
|
|
|
assert data['error_description'] == 'User is not active'
|
2018-07-10 09:20:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_credential_store_expiration():
|
|
|
|
"""Test that the credential store will not return expired tokens."""
|
|
|
|
store, retrieve = auth._create_cred_store()
|
|
|
|
client_id = 'bla'
|
|
|
|
credentials = 'creds'
|
|
|
|
now = utcnow()
|
|
|
|
|
|
|
|
with patch('homeassistant.util.dt.utcnow', return_value=now):
|
|
|
|
code = store(client_id, credentials)
|
|
|
|
|
|
|
|
with patch('homeassistant.util.dt.utcnow',
|
|
|
|
return_value=now + timedelta(minutes=10)):
|
|
|
|
assert retrieve(client_id, code) is None
|
|
|
|
|
|
|
|
with patch('homeassistant.util.dt.utcnow', return_value=now):
|
|
|
|
code = store(client_id, credentials)
|
|
|
|
|
|
|
|
with patch('homeassistant.util.dt.utcnow',
|
|
|
|
return_value=now + timedelta(minutes=9, seconds=59)):
|
|
|
|
assert retrieve(client_id, code) == credentials
|
2018-07-17 07:24:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_ws_current_user(hass, hass_ws_client, hass_access_token):
|
|
|
|
"""Test the current user command."""
|
|
|
|
assert await async_setup_component(hass, 'auth', {
|
|
|
|
'http': {
|
|
|
|
'api_password': 'bla'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
with patch('homeassistant.auth.AuthManager.active', return_value=True):
|
|
|
|
client = await hass_ws_client(hass, hass_access_token)
|
|
|
|
|
|
|
|
await client.send_json({
|
|
|
|
'id': 5,
|
|
|
|
'type': auth.WS_TYPE_CURRENT_USER,
|
|
|
|
})
|
|
|
|
|
|
|
|
result = await client.receive_json()
|
|
|
|
assert result['success'], result
|
|
|
|
|
|
|
|
user = hass_access_token.refresh_token.user
|
|
|
|
user_dict = result['result']
|
|
|
|
|
|
|
|
assert user_dict['name'] == user.name
|
|
|
|
assert user_dict['id'] == user.id
|
|
|
|
assert user_dict['is_owner'] == user.is_owner
|
2018-07-19 06:37:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_cors_on_token(hass, aiohttp_client):
|
|
|
|
"""Test logging in with new user and refreshing tokens."""
|
|
|
|
client = await async_setup_auth(hass, aiohttp_client)
|
|
|
|
|
|
|
|
resp = await client.options('/auth/token', headers={
|
|
|
|
'origin': 'http://example.com',
|
|
|
|
'Access-Control-Request-Method': 'POST',
|
|
|
|
})
|
|
|
|
assert resp.headers['Access-Control-Allow-Origin'] == 'http://example.com'
|
|
|
|
assert resp.headers['Access-Control-Allow-Methods'] == 'POST'
|
|
|
|
|
|
|
|
resp = await client.post('/auth/token', headers={
|
|
|
|
'origin': 'http://example.com'
|
|
|
|
})
|
|
|
|
assert resp.headers['Access-Control-Allow-Origin'] == 'http://example.com'
|