From 5ff698c78da44cfd91d342142f1f309a1fb91b38 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 1 Jul 2025 10:15:45 +0200 Subject: [PATCH] Catch access denied errors in webdav and display proper message (#147093) --- .../components/webdav/config_flow.py | 8 +++- homeassistant/components/webdav/strings.json | 4 +- tests/components/webdav/test_config_flow.py | 7 ++- tests/components/webdav/test_init.py | 46 ++++++++++++++++++- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/webdav/config_flow.py b/homeassistant/components/webdav/config_flow.py index e3e46d2575a..95b20761d09 100644 --- a/homeassistant/components/webdav/config_flow.py +++ b/homeassistant/components/webdav/config_flow.py @@ -5,7 +5,11 @@ from __future__ import annotations import logging from typing import Any -from aiowebdav2.exceptions import MethodNotSupportedError, UnauthorizedError +from aiowebdav2.exceptions import ( + AccessDeniedError, + MethodNotSupportedError, + UnauthorizedError, +) import voluptuous as vol import yarl @@ -65,6 +69,8 @@ class WebDavConfigFlow(ConfigFlow, domain=DOMAIN): result = await client.check() except UnauthorizedError: errors["base"] = "invalid_auth" + except AccessDeniedError: + errors["base"] = "access_denied" except MethodNotSupportedError: errors["base"] = "invalid_method" except Exception: diff --git a/homeassistant/components/webdav/strings.json b/homeassistant/components/webdav/strings.json index ac6418f1239..689b27bbf66 100644 --- a/homeassistant/components/webdav/strings.json +++ b/homeassistant/components/webdav/strings.json @@ -21,6 +21,7 @@ "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "access_denied": "The access to the backup path has been denied. Please check the permissions of the backup path.", "invalid_method": "The server does not support the required methods. Please check whether you have the correct URL. Check with your provider for the correct URL.", "unknown": "[%key:common::config_flow::error::unknown%]" }, @@ -35,9 +36,6 @@ "cannot_connect": { "message": "Cannot connect to WebDAV server" }, - "cannot_access_or_create_backup_path": { - "message": "Cannot access or create backup path. Please check the path and permissions." - }, "failed_to_migrate_folder": { "message": "Failed to migrate wrong encoded folder \"{wrong_path}\" to \"{correct_path}\"." } diff --git a/tests/components/webdav/test_config_flow.py b/tests/components/webdav/test_config_flow.py index 9204e6eadab..3ee5c8ae9ad 100644 --- a/tests/components/webdav/test_config_flow.py +++ b/tests/components/webdav/test_config_flow.py @@ -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"), ], diff --git a/tests/components/webdav/test_init.py b/tests/components/webdav/test_init.py index 124a644fa93..89f0e703b22 100644 --- a/tests/components/webdav/test_init.py +++ b/tests/components/webdav/test_init.py @@ -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