Catch access denied errors in webdav and display proper message (#147093)

This commit is contained in:
Jan-Philipp Benecke 2025-07-01 10:15:45 +02:00 committed by GitHub
parent 9469c6ad1c
commit 5ff698c78d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 6 deletions

View File

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

View File

@ -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}\"."
}

View File

@ -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"),
],

View File

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