1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-05-19 14:18:53 +01:00
Files
supervisor/tests/test_auth.py
T
Stefan Agner f8dbafe0bb Drop redundant @pytest.mark.asyncio decorators (#6795)
The pytest config sets ``asyncio_mode = "auto"``, which already
auto-marks every ``async def test_*`` as a coroutine test. The 38
``@pytest.mark.asyncio`` decorators sprinkled across the suite were
no-ops kept around from before that flag was set. Remove them along
with the now-unused ``import pytest`` lines they were the only
consumer of.

Pure mechanical cleanup; no test behavior changes.
2026-05-04 14:48:18 +02:00

84 lines
2.3 KiB
Python

"""Test auth object."""
import asyncio
from unittest.mock import AsyncMock, MagicMock
import pytest
# pylint: disable=protected-access
@pytest.fixture(name="mock_auth_backend", autouse=True)
def mock_auth_backend_fixture(coresys):
"""Fix auth backend request."""
mock_auth_backend = AsyncMock()
coresys.auth._backend_login = mock_auth_backend
yield mock_auth_backend
@pytest.fixture(name="mock_api_state", autouse=True)
def mock_api_state_fixture(coresys):
"""Fix auth backend request."""
mock_api_state = AsyncMock()
coresys.homeassistant.api.check_api_state = mock_api_state
yield mock_api_state
async def test_auth_request_with_backend(coresys, mock_auth_backend, mock_api_state):
"""Make simple auth request."""
app = MagicMock()
mock_auth_backend.return_value = True
mock_api_state.return_value = True
assert await coresys.auth.check_login(app, "username", "password")
assert mock_auth_backend.called
async def test_auth_request_without_backend(coresys, mock_auth_backend, mock_api_state):
"""Make simple auth without request."""
app = MagicMock()
mock_auth_backend.return_value = True
mock_api_state.return_value = False
assert not await coresys.auth.check_login(app, "username", "password")
assert not mock_auth_backend.called
async def test_auth_request_without_backend_cache(
coresys, mock_auth_backend, mock_api_state
):
"""Make simple auth without request."""
app = MagicMock()
mock_auth_backend.return_value = True
mock_api_state.return_value = False
await coresys.auth._update_cache("username", "password")
assert await coresys.auth.check_login(app, "username", "password")
assert not mock_auth_backend.called
async def test_auth_request_with_backend_cache_update(
coresys, mock_auth_backend, mock_api_state
):
"""Make simple auth without request and cache update."""
app = MagicMock()
mock_auth_backend.return_value = False
mock_api_state.return_value = True
await coresys.auth._update_cache("username", "password")
assert await coresys.auth.check_login(app, "username", "password")
await asyncio.sleep(0)
assert mock_auth_backend.called
await coresys.auth._dismatch_cache("username", "password")
assert not await coresys.auth.check_login(app, "username", "password")