Migrate auth tests to use freezegun (#105243)
This commit is contained in:
parent
2daa94b600
commit
6666b796f2
2 changed files with 13 additions and 18 deletions
|
@ -894,10 +894,7 @@ async def test_auth_module_expired_session(mock_hass) -> None:
|
||||||
assert step["type"] == data_entry_flow.FlowResultType.FORM
|
assert step["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert step["step_id"] == "mfa"
|
assert step["step_id"] == "mfa"
|
||||||
|
|
||||||
with patch(
|
with freeze_time(dt_util.utcnow() + MFA_SESSION_EXPIRATION):
|
||||||
"homeassistant.util.dt.utcnow",
|
|
||||||
return_value=dt_util.utcnow() + MFA_SESSION_EXPIRATION,
|
|
||||||
):
|
|
||||||
step = await manager.login_flow.async_configure(
|
step = await manager.login_flow.async_configure(
|
||||||
step["flow_id"], {"pin": "test-pin"}
|
step["flow_id"], {"pin": "test-pin"}
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,6 +4,7 @@ from http import HTTPStatus
|
||||||
import logging
|
import logging
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.auth import InvalidAuthError
|
from homeassistant.auth import InvalidAuthError
|
||||||
|
@ -167,28 +168,25 @@ async def test_auth_code_checks_local_only_user(
|
||||||
assert error["error"] == "access_denied"
|
assert error["error"] == "access_denied"
|
||||||
|
|
||||||
|
|
||||||
def test_auth_code_store_expiration(mock_credential) -> None:
|
def test_auth_code_store_expiration(
|
||||||
|
mock_credential, freezer: FrozenDateTimeFactory
|
||||||
|
) -> None:
|
||||||
"""Test that the auth code store will not return expired tokens."""
|
"""Test that the auth code store will not return expired tokens."""
|
||||||
store, retrieve = auth._create_auth_code_store()
|
store, retrieve = auth._create_auth_code_store()
|
||||||
client_id = "bla"
|
client_id = "bla"
|
||||||
now = utcnow()
|
now = utcnow()
|
||||||
|
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
freezer.move_to(now)
|
||||||
code = store(client_id, mock_credential)
|
code = store(client_id, mock_credential)
|
||||||
|
|
||||||
with patch(
|
freezer.move_to(now + timedelta(minutes=10))
|
||||||
"homeassistant.util.dt.utcnow", return_value=now + timedelta(minutes=10)
|
assert retrieve(client_id, code) is None
|
||||||
):
|
|
||||||
assert retrieve(client_id, code) is None
|
|
||||||
|
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
freezer.move_to(now)
|
||||||
code = store(client_id, mock_credential)
|
code = store(client_id, mock_credential)
|
||||||
|
|
||||||
with patch(
|
freezer.move_to(now + timedelta(minutes=9, seconds=59))
|
||||||
"homeassistant.util.dt.utcnow",
|
assert retrieve(client_id, code) == mock_credential
|
||||||
return_value=now + timedelta(minutes=9, seconds=59),
|
|
||||||
):
|
|
||||||
assert retrieve(client_id, code) == mock_credential
|
|
||||||
|
|
||||||
|
|
||||||
def test_auth_code_store_requires_credentials(mock_credential) -> None:
|
def test_auth_code_store_requires_credentials(mock_credential) -> None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue