Migrate auth tests to use freezegun (#105243)

This commit is contained in:
Jan-Philipp Benecke 2023-12-07 21:18:10 +01:00 committed by GitHub
parent 2daa94b600
commit 6666b796f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 18 deletions

View file

@ -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"}
) )

View file

@ -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: