mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00

* 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
74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
"""Tests for the Slack integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from homeassistant.components.slack.const import CONF_DEFAULT_CHANNEL, DOMAIN
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from tests.common import MockConfigEntry, load_fixture
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
|
|
|
AUTH_URL = "https://slack.com/api/auth.test"
|
|
|
|
TOKEN = "abc123"
|
|
TEAM_NAME = "Test Team"
|
|
TEAM_ID = "abc123def"
|
|
|
|
CONF_INPUT = {CONF_API_KEY: TOKEN, CONF_DEFAULT_CHANNEL: "test_channel"}
|
|
|
|
CONF_DATA = CONF_INPUT | {CONF_NAME: TEAM_NAME}
|
|
|
|
|
|
def create_entry(hass: HomeAssistant) -> ConfigEntry:
|
|
"""Add config entry in Home Assistant."""
|
|
entry = MockConfigEntry(
|
|
domain=DOMAIN,
|
|
data=CONF_DATA,
|
|
unique_id=TEAM_ID,
|
|
)
|
|
entry.add_to_hass(hass)
|
|
return entry
|
|
|
|
|
|
def mock_connection(
|
|
aioclient_mock: AiohttpClientMocker, error: str | None = None
|
|
) -> None:
|
|
"""Mock connection."""
|
|
if error is not None:
|
|
if error == "invalid_auth":
|
|
aioclient_mock.post(
|
|
AUTH_URL,
|
|
text=json.dumps({"ok": False, "error": "invalid_auth"}),
|
|
)
|
|
else:
|
|
aioclient_mock.post(
|
|
AUTH_URL,
|
|
text=json.dumps({"ok": False, "error": "cannot_connect"}),
|
|
)
|
|
else:
|
|
aioclient_mock.post(
|
|
AUTH_URL,
|
|
text=load_fixture("slack/auth_test.json"),
|
|
)
|
|
|
|
|
|
async def async_init_integration(
|
|
hass: HomeAssistant,
|
|
aioclient_mock: AiohttpClientMocker,
|
|
skip_setup: bool = False,
|
|
error: str | None = None,
|
|
) -> ConfigEntry:
|
|
"""Set up the Slack integration in Home Assistant."""
|
|
entry = create_entry(hass)
|
|
mock_connection(aioclient_mock, error)
|
|
|
|
if not skip_setup:
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
return entry
|