Add user via cmd line creates owner (#15470)
* Add user via cmd line creates owner * Ensure access tokens are not verified for inactive users * Stale print * Lint
This commit is contained in:
1 parent
6db069881b
commit
ed0cfc4f31
8 files changed
+97
-101
No files matched your search
@@ -10,7 +10,7 @@ from . import async_setup_auth
|
||||
from tests.common import CLIENT_ID, CLIENT_REDIRECT_URI
|
||||
|
||||
|
||||
async def test_login_new_user_and_refresh_token(hass, aiohttp_client):
|
||||
async def test_login_new_user_and_trying_refresh_token(hass, aiohttp_client):
|
||||
"""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={
|
||||
@@ -34,36 +34,13 @@ async def test_login_new_user_and_refresh_token(hass, aiohttp_client):
|
||||
|
||||
# Exchange code for tokens
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
|
||||
# Use refresh token to get more tokens.
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'refresh_token',
|
||||
'refresh_token': tokens['refresh_token']
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
assert 'refresh_token' not in tokens
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
|
||||
# Test using access token to hit API.
|
||||
resp = await client.get('/api/')
|
||||
assert resp.status == 401
|
||||
|
||||
resp = await client.get('/api/', headers={
|
||||
'authorization': 'Bearer {}'.format(tokens['access_token'])
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code
|
||||
})
|
||||
assert resp.status == 200
|
||||
|
||||
# User is not active
|
||||
assert resp.status == 400
|
||||
|
||||
|
||||
def test_credential_store_expiration():
|
||||
|
||||
@@ -25,40 +25,9 @@ async def async_get_code(hass, aiohttp_client):
|
||||
}]
|
||||
}]
|
||||
client = await async_setup_auth(hass, aiohttp_client, config)
|
||||
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'handler': ['insecure_example', None],
|
||||
'redirect_uri': CLIENT_REDIRECT_URI,
|
||||
})
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
code = step['result']
|
||||
|
||||
# Exchange code for tokens
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
|
||||
access_token = hass.auth.async_get_access_token(tokens['access_token'])
|
||||
assert access_token is not None
|
||||
user = access_token.refresh_token.user
|
||||
assert len(user.credentials) == 1
|
||||
user = await hass.auth.async_create_user(name='Hello')
|
||||
refresh_token = await hass.auth.async_create_refresh_token(user, CLIENT_ID)
|
||||
access_token = hass.auth.async_create_access_token(refresh_token)
|
||||
|
||||
# Now authenticate with the 2nd flow
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
@@ -83,7 +52,7 @@ async def async_get_code(hass, aiohttp_client):
|
||||
'user': user,
|
||||
'code': step['result'],
|
||||
'client': client,
|
||||
'tokens': tokens,
|
||||
'access_token': access_token.token,
|
||||
}
|
||||
|
||||
|
||||
@@ -92,18 +61,17 @@ async def test_link_user(hass, aiohttp_client):
|
||||
info = await async_get_code(hass, aiohttp_client)
|
||||
client = info['client']
|
||||
code = info['code']
|
||||
tokens = info['tokens']
|
||||
|
||||
# Link user
|
||||
resp = await client.post('/auth/link_user', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'code': code
|
||||
}, headers={
|
||||
'authorization': 'Bearer {}'.format(tokens['access_token'])
|
||||
'authorization': 'Bearer {}'.format(info['access_token'])
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
assert len(info['user'].credentials) == 2
|
||||
assert len(info['user'].credentials) == 1
|
||||
|
||||
|
||||
async def test_link_user_invalid_client_id(hass, aiohttp_client):
|
||||
@@ -111,36 +79,34 @@ async def test_link_user_invalid_client_id(hass, aiohttp_client):
|
||||
info = await async_get_code(hass, aiohttp_client)
|
||||
client = info['client']
|
||||
code = info['code']
|
||||
tokens = info['tokens']
|
||||
|
||||
# Link user
|
||||
resp = await client.post('/auth/link_user', json={
|
||||
'client_id': 'invalid',
|
||||
'code': code
|
||||
}, headers={
|
||||
'authorization': 'Bearer {}'.format(tokens['access_token'])
|
||||
'authorization': 'Bearer {}'.format(info['access_token'])
|
||||
})
|
||||
|
||||
assert resp.status == 400
|
||||
assert len(info['user'].credentials) == 1
|
||||
assert len(info['user'].credentials) == 0
|
||||
|
||||
|
||||
async def test_link_user_invalid_code(hass, aiohttp_client):
|
||||
"""Test linking a user to new credentials."""
|
||||
info = await async_get_code(hass, aiohttp_client)
|
||||
client = info['client']
|
||||
tokens = info['tokens']
|
||||
|
||||
# Link user
|
||||
resp = await client.post('/auth/link_user', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'code': 'invalid'
|
||||
}, headers={
|
||||
'authorization': 'Bearer {}'.format(tokens['access_token'])
|
||||
'authorization': 'Bearer {}'.format(info['access_token'])
|
||||
})
|
||||
|
||||
assert resp.status == 400
|
||||
assert len(info['user'].credentials) == 1
|
||||
assert len(info['user'].credentials) == 0
|
||||
|
||||
|
||||
async def test_link_user_invalid_auth(hass, aiohttp_client):
|
||||
@@ -156,4 +122,4 @@ async def test_link_user_invalid_auth(hass, aiohttp_client):
|
||||
}, headers={'authorization': 'Bearer invalid'})
|
||||
|
||||
assert resp.status == 401
|
||||
assert len(info['user'].credentials) == 1
|
||||
assert len(info['user'].credentials) == 0
|
||||
Reference in new issue
Block a user