mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-07-02 11:25:37 +01:00
ed91b18c4b
* tests: enable flake8-pytest-style (PT) ruff rules Enable the `PT` ruff rule set and fix the resulting violations across the test suite: - PT006: pass parametrize argument names as tuples instead of a single comma-separated string. - PT022: switch fixtures that have no teardown from `yield` to `return` so the lack of cleanup is obvious at a glance. - PT011: add `match=` to broad `pytest.raises(ValueError)` blocks so the expected error is anchored to a specific message. - PT012: hoist setup (patches, branching) out of `pytest.raises()` blocks so only the call that is expected to raise remains inside. - PT013: replace `from pytest import X` with `import pytest` and access attributes via the module. - PT015: replace `try/except` + `assert False` patterns with `pytest.raises(...)`. - PT017: replace `assert` on exceptions inside `except` blocks with `pytest.raises(...) as exc_info` and assert on `exc_info.value`. No behavioral changes to the tests; the full suite still passes. * tests: address review feedback on PT ruff rule enablement - Fix fixture return-type annotations after switching `yield` to `return` in tests/conftest.py: drop the `Generator[...]`/`AsyncGenerator[...]` wrapper for `dns_manager_service`, `supervisor_internet`, `websession`, and `mock_update_data` so the annotation matches what the fixture actually returns. - Correct the return-type annotation of `fixture_ip6config_service` from `IP4ConfigService` to `IP6ConfigService`. - Fix recurring "excepiton" typo in tests/utils/test_exception_helper.py. * tests: verify backup cleanup on permission error After `test_new_backup_permission_error` raises `BackupPermissionError`, assert that no tarfile was left behind and `tmp_path` is empty. The previous version only checked that the exception was raised, which missed any regression where a partial tarfile would survive the failed create. * tests: rename DNS_GOOD_V6 to DNS_V6_UNSUPPORTED The constant was named "good" but its tests assert that the URLs are rejected by the DNS validator. The IPv6 URLs are well-formed but currently rejected because IPv6 doesn't work with the Docker network (see `dns_url` in supervisor/validate.py). Rename the constant and the related test to make the intent obvious.
84 lines
2.3 KiB
Python
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
|
|
|
|
return 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
|
|
|
|
return 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")
|