|
| 1 | +"""Test WebDAV component setup.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +from aiowebdav2.exceptions import WebDavError |
| 6 | +import pytest |
| 7 | + |
| 8 | +from homeassistant.components.webdav.const import CONF_BACKUP_PATH, DOMAIN |
| 9 | +from homeassistant.config_entries import ConfigEntryState |
| 10 | +from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME |
| 11 | +from homeassistant.core import HomeAssistant |
| 12 | + |
| 13 | +from . import setup_integration |
| 14 | + |
| 15 | +from tests.common import MockConfigEntry |
| 16 | + |
| 17 | + |
| 18 | +async def test_migrate_wrong_path( |
| 19 | + hass: HomeAssistant, webdav_client: AsyncMock |
| 20 | +) -> None: |
| 21 | + """Test migration of wrong encoded folder path.""" |
| 22 | + webdav_client.list_with_properties.return_value = [ |
| 23 | + {"/wrong%20path": []}, |
| 24 | + ] |
| 25 | + |
| 26 | + config_entry = MockConfigEntry( |
| 27 | + title="user@webdav.demo", |
| 28 | + domain=DOMAIN, |
| 29 | + data={ |
| 30 | + CONF_URL: "https://webdav.demo", |
| 31 | + CONF_USERNAME: "user", |
| 32 | + CONF_PASSWORD: "supersecretpassword", |
| 33 | + CONF_BACKUP_PATH: "/wrong path", |
| 34 | + }, |
| 35 | + entry_id="01JKXV07ASC62D620DGYNG2R8H", |
| 36 | + ) |
| 37 | + await setup_integration(hass, config_entry) |
| 38 | + |
| 39 | + webdav_client.move.assert_called_once_with("/wrong%20path", "/wrong path") |
| 40 | + |
| 41 | + |
| 42 | +async def test_migrate_non_wrong_path( |
| 43 | + hass: HomeAssistant, webdav_client: AsyncMock |
| 44 | +) -> None: |
| 45 | + """Test no migration of correct folder path.""" |
| 46 | + webdav_client.list_with_properties.return_value = [ |
| 47 | + {"/correct path": []}, |
| 48 | + ] |
| 49 | + webdav_client.check.side_effect = lambda path: path == "/correct path" |
| 50 | + |
| 51 | + config_entry = MockConfigEntry( |
| 52 | + title="user@webdav.demo", |
| 53 | + domain=DOMAIN, |
| 54 | + data={ |
| 55 | + CONF_URL: "https://webdav.demo", |
| 56 | + CONF_USERNAME: "user", |
| 57 | + CONF_PASSWORD: "supersecretpassword", |
| 58 | + CONF_BACKUP_PATH: "/correct path", |
| 59 | + }, |
| 60 | + entry_id="01JKXV07ASC62D620DGYNG2R8H", |
| 61 | + ) |
| 62 | + |
| 63 | + await setup_integration(hass, config_entry) |
| 64 | + |
| 65 | + webdav_client.move.assert_not_called() |
| 66 | + |
| 67 | + |
| 68 | +async def test_migrate_error( |
| 69 | + hass: HomeAssistant, |
| 70 | + webdav_client: AsyncMock, |
| 71 | + caplog: pytest.LogCaptureFixture, |
| 72 | +) -> None: |
| 73 | + """Test migration of wrong encoded folder path with error.""" |
| 74 | + webdav_client.list_with_properties.return_value = [ |
| 75 | + {"/wrong%20path": []}, |
| 76 | + ] |
| 77 | + webdav_client.move.side_effect = WebDavError("Failed to move") |
| 78 | + |
| 79 | + config_entry = MockConfigEntry( |
| 80 | + title="user@webdav.demo", |
| 81 | + domain=DOMAIN, |
| 82 | + data={ |
| 83 | + CONF_URL: "https://webdav.demo", |
| 84 | + CONF_USERNAME: "user", |
| 85 | + CONF_PASSWORD: "supersecretpassword", |
| 86 | + CONF_BACKUP_PATH: "/wrong path", |
| 87 | + }, |
| 88 | + entry_id="01JKXV07ASC62D620DGYNG2R8H", |
| 89 | + ) |
| 90 | + await setup_integration(hass, config_entry) |
| 91 | + |
| 92 | + assert config_entry.state is ConfigEntryState.SETUP_RETRY |
| 93 | + assert ( |
| 94 | + 'Failed to migrate wrong encoded folder "/wrong%20path" to "/wrong path"' |
| 95 | + in caplog.text |
| 96 | + ) |
0 commit comments