Skip to content

Commit

Permalink
Added user credentials to current_user ws endpoint. (home-assistant#1…
Browse files Browse the repository at this point in the history
…5558)

* Added user credentials to current_user ws endpoint.

* Comments. Added another test.

* Return list of credentials.
  • Loading branch information
jeradM authored and michaeldavie committed Jul 31, 2018
1 parent bedcd48 commit 32f8fb1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ def websocket_current_user(hass, connection, msg):
'id': user.id,
'name': user.name,
'is_owner': user.is_owner,
'credentials': [{'auth_provider_type': c.auth_provider_type,
'auth_provider_id': c.auth_provider_id}
for c in user.credentials]
}))
18 changes: 16 additions & 2 deletions tests/components/auth/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import timedelta
from unittest.mock import patch

from homeassistant.auth.models import Credentials
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from homeassistant.components import auth
Expand Down Expand Up @@ -90,12 +91,20 @@ def test_credential_store_expiration():


async def test_ws_current_user(hass, hass_ws_client, hass_access_token):
"""Test the current user command."""
"""Test the current user command with homeassistant creds."""
assert await async_setup_component(hass, 'auth', {
'http': {
'api_password': 'bla'
}
})

user = hass_access_token.refresh_token.user
credential = Credentials(auth_provider_type='homeassistant',
auth_provider_id=None,
data={}, id='test-id')
user.credentials.append(credential)
assert len(user.credentials) == 1

with patch('homeassistant.auth.AuthManager.active', return_value=True):
client = await hass_ws_client(hass, hass_access_token)

Expand All @@ -107,12 +116,17 @@ async def test_ws_current_user(hass, hass_ws_client, hass_access_token):
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
assert len(user_dict['credentials']) == 1

hass_cred = user_dict['credentials'][0]
assert hass_cred['auth_provider_type'] == 'homeassistant'
assert hass_cred['auth_provider_id'] is None
assert 'data' not in hass_cred


async def test_cors_on_token(hass, aiohttp_client):
Expand Down

0 comments on commit 32f8fb1

Please sign in to comment.