mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Catch access denied errors in webdav and display proper message (#147093)
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from aiowebdav2.exceptions import MethodNotSupportedError, UnauthorizedError
|
||||
from aiowebdav2.exceptions import (
|
||||
AccessDeniedError,
|
||||
MethodNotSupportedError,
|
||||
UnauthorizedError,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
@@ -86,6 +90,7 @@ async def test_form_fail(hass: HomeAssistant, webdav_client: AsyncMock) -> None:
|
||||
("exception", "expected_error"),
|
||||
[
|
||||
(UnauthorizedError("https://webdav.demo"), "invalid_auth"),
|
||||
(AccessDeniedError("https://webdav.demo"), "access_denied"),
|
||||
(MethodNotSupportedError("check", "https://webdav.demo"), "invalid_method"),
|
||||
(Exception("Unexpected error"), "unknown"),
|
||||
],
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from aiowebdav2.exceptions import WebDavError
|
||||
from aiowebdav2.exceptions import AccessDeniedError, UnauthorizedError, WebDavError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.webdav.const import CONF_BACKUP_PATH, DOMAIN
|
||||
@@ -110,3 +110,47 @@ async def test_migrate_error(
|
||||
'Failed to migrate wrong encoded folder "/wrong%20path" to "/wrong path"'
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "expected_message", "expected_state"),
|
||||
[
|
||||
(
|
||||
UnauthorizedError("Unauthorized"),
|
||||
"Invalid username or password",
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
),
|
||||
(
|
||||
AccessDeniedError("/access_denied"),
|
||||
"Access denied to /access_denied",
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
),
|
||||
],
|
||||
ids=["UnauthorizedError", "AccessDeniedError"],
|
||||
)
|
||||
async def test_error_during_setup(
|
||||
hass: HomeAssistant,
|
||||
webdav_client: AsyncMock,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
error: Exception,
|
||||
expected_message: str,
|
||||
expected_state: ConfigEntryState,
|
||||
) -> None:
|
||||
"""Test handling of various errors during setup."""
|
||||
webdav_client.check.side_effect = error
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
title="user@webdav.demo",
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_URL: "https://webdav.demo",
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "supersecretpassword",
|
||||
CONF_BACKUP_PATH: "/backups",
|
||||
},
|
||||
entry_id="01JKXV07ASC62D620DGYNG2R8H",
|
||||
)
|
||||
await setup_integration(hass, config_entry)
|
||||
|
||||
assert expected_message in caplog.text
|
||||
assert config_entry.state is expected_state
|
||||
|
||||
Reference in New Issue
Block a user