core/tests/components/slack/test_config_flow.py
jsuar a2d76cac5a
Fix Slack file upload (#135818)
* pgrade Slack integration to use AsyncWebClient and support files_upload_v2

- Replaced deprecated WebClient with AsyncWebClient throughout the integration.
- Removed the unsupported `run_async` parameter.
- Added a helper function to resolve channel names to channel IDs.
- Updated `_async_send_local_file_message` and `_async_send_remote_file_message` to handle Slack's new API requirements, including per-channel uploads.
- Updated dependency from slackclient==2.5.0 to slack-sdk>=3.0.0.
- Improved error handling and logging for channel resolution and file uploads.

* Fix test to use AsyncWebClient for Slack authentication flow

* Fix Slack authentication URL by removing the www subdomain

* Refactor Slack file upload functionality and add utility for file uploads
2025-01-19 21:09:04 +01:00

95 lines
3.1 KiB
Python

"""Test Slack config flow."""
from unittest.mock import patch
from homeassistant import config_entries
from homeassistant.components.slack.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import CONF_DATA, CONF_INPUT, TEAM_NAME, create_entry, mock_connection
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_flow_user(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test user initialized flow."""
mock_connection(aioclient_mock)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONF_INPUT,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == TEAM_NAME
assert result["data"] == CONF_DATA
async def test_flow_user_already_configured(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test user initialized flow with duplicate server."""
create_entry(hass)
mock_connection(aioclient_mock)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONF_INPUT,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_flow_user_invalid_auth(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test user initialized flow with invalid token."""
mock_connection(aioclient_mock, "invalid_auth")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=CONF_DATA,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
async def test_flow_user_cannot_connect(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test user initialized flow with unreachable server."""
mock_connection(aioclient_mock, "cannot_connect")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=CONF_DATA,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
async def test_flow_user_unknown_error(hass: HomeAssistant) -> None:
"""Test user initialized flow with unreachable server."""
with patch(
"homeassistant.components.slack.config_flow.AsyncWebClient.auth_test"
) as mock:
mock.side_effect = Exception
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=CONF_DATA,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}