mirror of
https://github.com/home-assistant/core.git
synced 2025-11-13 21:10:25 +00:00
Convert getting and removing access tokens to normal functions (#108670)
This commit is contained in:
@@ -88,9 +88,7 @@ async def test_login_new_user_and_trying_refresh_token(
|
||||
assert resp.status == HTTPStatus.OK
|
||||
tokens = await resp.json()
|
||||
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
)
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
assert tokens["ha_auth_provider"] == "insecure_example"
|
||||
|
||||
# Use refresh token to get more tokens.
|
||||
@@ -106,9 +104,7 @@ async def test_login_new_user_and_trying_refresh_token(
|
||||
assert resp.status == HTTPStatus.OK
|
||||
tokens = await resp.json()
|
||||
assert "refresh_token" not in tokens
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
)
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
|
||||
# Test using access token to hit API.
|
||||
resp = await client.get("/api/")
|
||||
@@ -205,7 +201,7 @@ async def test_ws_current_user(
|
||||
"""Test the current user command with Home Assistant creds."""
|
||||
assert await async_setup_component(hass, "auth", {})
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
|
||||
refresh_token = hass.auth.async_validate_access_token(hass_access_token)
|
||||
user = refresh_token.user
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
|
||||
@@ -275,9 +271,7 @@ async def test_refresh_token_system_generated(
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
tokens = await resp.json()
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
)
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
|
||||
|
||||
async def test_refresh_token_different_client_id(
|
||||
@@ -323,9 +317,7 @@ async def test_refresh_token_different_client_id(
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
tokens = await resp.json()
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
)
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
|
||||
|
||||
async def test_refresh_token_checks_local_only_user(
|
||||
@@ -406,16 +398,14 @@ async def test_revoking_refresh_token(
|
||||
|
||||
assert resp.status == HTTPStatus.OK
|
||||
tokens = await resp.json()
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
)
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is not None
|
||||
|
||||
# Revoke refresh token
|
||||
resp = await client.post(url, data={**base_data, "token": refresh_token.token})
|
||||
assert resp.status == HTTPStatus.OK
|
||||
|
||||
# Old access token should be no longer valid
|
||||
assert await hass.auth.async_validate_access_token(tokens["access_token"]) is None
|
||||
assert hass.auth.async_validate_access_token(tokens["access_token"]) is None
|
||||
|
||||
# Test that we no longer can create an access token
|
||||
resp = await client.post(
|
||||
@@ -454,7 +444,7 @@ async def test_ws_long_lived_access_token(
|
||||
long_lived_access_token = result["result"]
|
||||
assert long_lived_access_token is not None
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(long_lived_access_token)
|
||||
refresh_token = hass.auth.async_validate_access_token(long_lived_access_token)
|
||||
assert refresh_token.client_id is None
|
||||
assert refresh_token.client_name == "GPS Logger"
|
||||
assert refresh_token.client_icon is None
|
||||
@@ -474,7 +464,7 @@ async def test_ws_refresh_tokens(
|
||||
assert result["success"], result
|
||||
assert len(result["result"]) == 1
|
||||
token = result["result"][0]
|
||||
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
|
||||
refresh_token = hass.auth.async_validate_access_token(hass_access_token)
|
||||
assert token["id"] == refresh_token.id
|
||||
assert token["type"] == refresh_token.token_type
|
||||
assert token["client_id"] == refresh_token.client_id
|
||||
@@ -514,7 +504,7 @@ async def test_ws_delete_refresh_token(
|
||||
|
||||
result = await ws_client.receive_json()
|
||||
assert result["success"], result
|
||||
refresh_token = await hass.auth.async_get_refresh_token(refresh_token.id)
|
||||
refresh_token = hass.auth.async_get_refresh_token(refresh_token.id)
|
||||
assert refresh_token is None
|
||||
|
||||
|
||||
@@ -573,7 +563,7 @@ async def test_ws_delete_all_refresh_tokens_error(
|
||||
) in caplog.record_tuples
|
||||
|
||||
for token in tokens:
|
||||
refresh_token = await hass.auth.async_get_refresh_token(token["id"])
|
||||
refresh_token = hass.auth.async_get_refresh_token(token["id"])
|
||||
assert refresh_token is None
|
||||
|
||||
|
||||
@@ -614,7 +604,7 @@ async def test_ws_delete_all_refresh_tokens(
|
||||
result = await ws_client.receive_json()
|
||||
assert result, result["success"]
|
||||
for token in tokens:
|
||||
refresh_token = await hass.auth.async_get_refresh_token(token["id"])
|
||||
refresh_token = hass.auth.async_get_refresh_token(token["id"])
|
||||
assert refresh_token is None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user