You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2025/11/15 01:26:39 UTC
(superset) 05/05: fix(alerts): address new PR comments
This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a commit to branch alerts-reports-unable-to-edit-slack-recipients
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 3f85dcff9ef41d898724c06c5e9aee610efde27c
Author: Gabriel Torres Ruiz <ga...@gmail.com>
AuthorDate: Sun Nov 2 23:19:55 2025 -0400
fix(alerts): address new PR comments
---
.../alerts/components/NotificationMethod.test.tsx | 179 ++++++++++++++++
.../alerts/components/NotificationMethod.tsx | 227 ++++++++++++---------
superset/tasks/slack.py | 51 ++++-
superset/utils/slack.py | 109 +++-------
tests/unit_tests/utils/slack_test.py | 208 +++++++++++++++----
5 files changed, 558 insertions(+), 216 deletions(-)
diff --git a/superset-frontend/src/features/alerts/components/NotificationMethod.test.tsx b/superset-frontend/src/features/alerts/components/NotificationMethod.test.tsx
index dfaff1cda2..5a788dc2b0 100644
--- a/superset-frontend/src/features/alerts/components/NotificationMethod.test.tsx
+++ b/superset-frontend/src/features/alerts/components/NotificationMethod.test.tsx
@@ -1052,3 +1052,182 @@ test('NotificationMethod - should preserve selected channels during search and p
getSpy.mockRestore();
});
+
+test('NotificationMethod - should support comma-separated channel search', async () => {
+ window.featureFlags = { [FeatureFlag.AlertReportSlackV2]: true };
+
+ const mockChannelsPage1 = [
+ { id: 'C001', name: 'engineering', is_private: false, is_member: true },
+ {
+ id: 'C002',
+ name: 'engineering-team',
+ is_private: false,
+ is_member: true,
+ },
+ ];
+ const mockChannelsPage2 = [
+ { id: 'C003', name: 'marketing', is_private: false, is_member: true },
+ { id: 'C004', name: 'sales', is_private: false, is_member: true },
+ ];
+
+ let callCount = 0;
+ const getSpy = jest.spyOn(SupersetClient, 'get').mockImplementation(() => {
+ callCount += 1;
+ if (callCount === 1) {
+ return Promise.resolve({
+ json: {
+ result: mockChannelsPage1,
+ next_cursor: 'cursor_2',
+ has_more: true,
+ },
+ }) as unknown as Promise<Response | JsonResponse | TextResponse>;
+ }
+ return Promise.resolve({
+ json: {
+ result: mockChannelsPage2,
+ next_cursor: null,
+ has_more: false,
+ },
+ }) as unknown as Promise<Response | JsonResponse | TextResponse>;
+ });
+
+ const { getByTestId } = render(
+ <NotificationMethod
+ setting={mockSettingSlackV2}
+ index={0}
+ onUpdate={mockOnUpdate}
+ onRemove={mockOnRemove}
+ onInputChange={mockOnInputChange}
+ email_subject={mockEmailSubject}
+ defaultSubject={mockDefaultSubject}
+ setErrorSubject={mockSetErrorSubject}
+ addDangerToast={mockAddDangerToast}
+ />,
+ );
+
+ await waitFor(
+ () => {
+ expect(getByTestId('recipients')).toBeInTheDocument();
+ },
+ { timeout: 3000 },
+ );
+
+ await waitFor(
+ () => {
+ expect(getSpy).toHaveBeenCalled();
+ },
+ { timeout: 3000 },
+ );
+
+ const apiCalls = getSpy.mock.calls;
+ expect(apiCalls.length).toBeGreaterThan(0);
+
+ getSpy.mockRestore();
+});
+
+test('NotificationMethod - AsyncSelect should not filter results client-side', async () => {
+ window.featureFlags = { [FeatureFlag.AlertReportSlackV2]: true };
+
+ const mockChannels = [
+ { id: 'C001', name: 'analytics-team', is_private: false, is_member: true },
+ { id: 'C002', name: 'analytics-ops', is_private: false, is_member: true },
+ { id: 'C003', name: 'engineering', is_private: false, is_member: true },
+ ];
+
+ const getSpy = jest.spyOn(SupersetClient, 'get').mockImplementation(
+ () =>
+ Promise.resolve({
+ json: {
+ result: mockChannels,
+ next_cursor: null,
+ has_more: false,
+ },
+ }) as unknown as Promise<Response | JsonResponse | TextResponse>,
+ );
+
+ const { getByTestId } = render(
+ <NotificationMethod
+ setting={mockSettingSlackV2}
+ index={0}
+ onUpdate={mockOnUpdate}
+ onRemove={mockOnRemove}
+ onInputChange={mockOnInputChange}
+ email_subject={mockEmailSubject}
+ defaultSubject={mockDefaultSubject}
+ setErrorSubject={mockSetErrorSubject}
+ addDangerToast={mockAddDangerToast}
+ />,
+ );
+
+ await waitFor(
+ () => {
+ expect(getByTestId('recipients')).toBeInTheDocument();
+ },
+ { timeout: 3000 },
+ );
+
+ await waitFor(
+ () => {
+ expect(getSpy).toHaveBeenCalled();
+ },
+ { timeout: 3000 },
+ );
+
+ const recipientsSelect = getByTestId('recipients');
+ expect(recipientsSelect).toBeInTheDocument();
+
+ getSpy.mockRestore();
+});
+
+test('NotificationMethod - AsyncSelect should not use comma as token separator', async () => {
+ window.featureFlags = { [FeatureFlag.AlertReportSlackV2]: true };
+
+ const mockChannels = [
+ { id: 'C001', name: 'engineering', is_private: false, is_member: true },
+ { id: 'C002', name: 'marketing', is_private: false, is_member: true },
+ ];
+
+ const getSpy = jest.spyOn(SupersetClient, 'get').mockImplementation(
+ () =>
+ Promise.resolve({
+ json: {
+ result: mockChannels,
+ next_cursor: null,
+ has_more: false,
+ },
+ }) as unknown as Promise<Response | JsonResponse | TextResponse>,
+ );
+
+ const { getByTestId } = render(
+ <NotificationMethod
+ setting={mockSettingSlackV2}
+ index={0}
+ onUpdate={mockOnUpdate}
+ onRemove={mockOnRemove}
+ onInputChange={mockOnInputChange}
+ email_subject={mockEmailSubject}
+ defaultSubject={mockDefaultSubject}
+ setErrorSubject={mockSetErrorSubject}
+ addDangerToast={mockAddDangerToast}
+ />,
+ );
+
+ await waitFor(
+ () => {
+ expect(getByTestId('recipients')).toBeInTheDocument();
+ },
+ { timeout: 3000 },
+ );
+
+ await waitFor(
+ () => {
+ expect(getSpy).toHaveBeenCalled();
+ },
+ { timeout: 3000 },
+ );
+
+ const recipientsSelect = getByTestId('recipients');
+ expect(recipientsSelect).toBeInTheDocument();
+
+ getSpy.mockRestore();
+});
diff --git a/superset-frontend/src/features/alerts/components/NotificationMethod.tsx b/superset-frontend/src/features/alerts/components/NotificationMethod.tsx
index 1af3521470..8687ec93d8 100644
--- a/superset-frontend/src/features/alerts/components/NotificationMethod.tsx
+++ b/superset-frontend/src/features/alerts/components/NotificationMethod.tsx
@@ -1,3 +1,4 @@
+/* eslint-disable camelcase */
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -23,7 +24,9 @@ import {
useMemo,
useRef,
useEffect,
+ useCallback,
} from 'react';
+import { debounce } from 'lodash';
import rison from 'rison';
import {
@@ -194,10 +197,11 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
{ data: { label: string; value: string }[]; totalCount: number }
>
>({});
- const [refreshKey, setRefreshKey] = useState(0);
const forceRefreshRef = useRef(false);
const [isRefreshing, setIsRefreshing] = useState(false);
- const hasShownErrorToast = useRef(false); // Track if we've shown the error toast
+ const hasShownErrorToast = useRef(false);
+ const [searchGeneration, setSearchGeneration] = useState(0);
+ const lastSearchValueRef = useRef('');
const onMethodChange = (selected: {
label: string;
@@ -220,105 +224,120 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
}
};
- const fetchSlackChannels = async (
- search: string,
- page: number,
- pageSize: number,
- ): Promise<{
- data: { label: string; value: string }[];
- totalCount: number;
- }> => {
- try {
- const cacheKey = `${search}:${page}`;
-
- if (dataCache.current[cacheKey] && !forceRefreshRef.current) {
- return dataCache.current[cacheKey];
- }
-
- const cursor = page > 0 ? cursorRef.current[cacheKey] : null;
-
- const params: Record<string, any> = {
- types: ['public_channel', 'private_channel'],
- limit: pageSize,
- };
-
- if (page === 0 && forceRefreshRef.current) {
- params.force = true;
- forceRefreshRef.current = false;
- }
-
- if (search) {
- params.search_string = search;
- }
+ const fetchSlackChannels = useCallback(
+ async (
+ search: string,
+ page: number,
+ pageSize: number,
+ ): Promise<{
+ data: { label: string; value: string }[];
+ totalCount: number;
+ }> => {
+ try {
+ const cacheKey = `${search}:${page}`;
+
+ if (dataCache.current[cacheKey] && !forceRefreshRef.current) {
+ return dataCache.current[cacheKey];
+ }
- if (cursor) {
- params.cursor = cursor;
- }
+ // Clear cache for previous searches to prevent stale data
+ if (page === 0) {
+ Object.keys(dataCache.current).forEach(key => {
+ if (!key.startsWith(`${search}:`)) {
+ delete dataCache.current[key];
+ delete cursorRef.current[key];
+ }
+ });
+ }
- const queryString = rison.encode(params);
- const endpoint = `/api/v1/report/slack_channels/?q=${queryString}`;
- const response = await SupersetClient.get({ endpoint });
+ const cursor = page > 0 ? cursorRef.current[cacheKey] : null;
- const { result, next_cursor, has_more } = response.json;
+ const params: Record<string, any> = {
+ types: ['public_channel', 'private_channel'],
+ limit: pageSize,
+ };
- if (next_cursor) {
- cursorRef.current[`${search}:${page + 1}`] = next_cursor;
- }
+ if (page === 0 && forceRefreshRef.current) {
+ params.force = true;
+ forceRefreshRef.current = false;
+ }
- const options = result.map((channel: SlackChannel) => ({
- label: channel.name,
- value: channel.id,
- }));
+ if (search) {
+ params.search_string = search;
+ }
- const totalCount = has_more
- ? (page + 1) * pageSize + 1
- : page * pageSize + options.length;
+ if (cursor) {
+ params.cursor = cursor;
+ }
- const responseData = {
- data: options,
- totalCount,
- };
+ const queryString = rison.encode(params);
+ const endpoint = `/api/v1/report/slack_channels/?q=${queryString}`;
+ const response = await SupersetClient.get({ endpoint });
- dataCache.current[cacheKey] = responseData;
+ const { result, next_cursor, has_more } = response.json;
- return responseData;
- } catch (error) {
- logging.error('Failed to fetch Slack channels:', error);
+ if (next_cursor) {
+ cursorRef.current[`${search}:${page + 1}`] = next_cursor;
+ }
- // Show user-friendly error message
- if (addDangerToast && !hasShownErrorToast.current) {
- addDangerToast(
- t(
- 'Unable to load Slack channels. Please check your Slack API token configuration. ' +
- 'Switching to manual channel input.',
- ),
- );
- hasShownErrorToast.current = true;
- }
+ const options = result.map((channel: SlackChannel) => ({
+ label: channel.name,
+ value: channel.id,
+ }));
+
+ const totalCount = has_more
+ ? (page + 1) * pageSize + 1
+ : page * pageSize + options.length;
+
+ const responseData = {
+ data: options,
+ totalCount,
+ };
+
+ dataCache.current[cacheKey] = responseData;
+
+ return responseData;
+ } catch (error) {
+ logging.error('Failed to fetch Slack channels:', error);
+
+ // Show user-friendly error message
+ if (addDangerToast && !hasShownErrorToast.current) {
+ addDangerToast(
+ t(
+ 'Unable to load Slack channels. Please check your Slack API token configuration. ' +
+ 'Switching to manual channel input.',
+ ),
+ );
+ hasShownErrorToast.current = true;
+ }
- // Fallback to Slack v1 without clearing recipients to prevent data loss
- setUseSlackV1(true);
+ // Fallback to Slack v1 without clearing recipients to prevent data loss
+ setUseSlackV1(true);
+
+ // Auto-switch to Slack V1 in the notification method dropdown
+ if (
+ onUpdate &&
+ setting &&
+ setting.method === NotificationMethodOption.SlackV2
+ ) {
+ onUpdate(index, {
+ ...setting,
+ method: NotificationMethodOption.Slack, // Switch from SlackV2 to Slack V1
+ });
+ }
- // Auto-switch to Slack V1 in the notification method dropdown
- if (
- onUpdate &&
- setting &&
- setting.method === NotificationMethodOption.SlackV2
- ) {
- onUpdate(index, {
- ...setting,
- method: NotificationMethodOption.Slack, // Switch from SlackV2 to Slack V1
- // Keep recipients to preserve user data
- });
+ return {
+ data: [],
+ totalCount: 0,
+ };
}
-
- // Return empty result instead of throwing to allow UI to continue
- return {
- data: [],
- totalCount: 0,
- };
- }
- };
+ },
+ // Note: searchGeneration is intentionally included even though not used in function body
+ // Purpose: Trigger function reference change when search changes (after debounce)
+ // Effect: Forces AsyncSelect to clear internal state and show fresh results
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [addDangerToast, onUpdate, setting, index, searchGeneration],
+ );
const handleRefreshSlackChannels = async () => {
setIsRefreshing(true);
@@ -331,8 +350,6 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
// Fetch fresh channel list without clearing user selections
await fetchSlackChannels('', 0, 100);
-
- setRefreshKey(prev => prev + 1);
} catch (error) {
logging.error('Error refreshing channels:', error);
@@ -371,14 +388,12 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
const channel = channelData.data.find(
ch => ch.value.toLowerCase() === id.toLowerCase(),
);
- // If channel found, use its label; otherwise fallback to ID
return channel || { label: id, value: id };
})
.filter(r => r.value); // Filter out empty values
setSlackRecipients(mappedRecipients);
} catch (error) {
- // If fetching fails, use IDs as both label and value
const recipientIds = recipients.split(',').map(id => id.trim());
const fallbackRecipients = recipientIds.map(id => ({
label: id,
@@ -390,9 +405,29 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
};
initializeSlackRecipients();
+ // Note: fetchSlackChannels and slackRecipients are intentionally omitted
+ // - fetchSlackChannels: Would cause re-initialization on every search change
+ // - slackRecipients: Would cause infinite loop (we modify it inside)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [method, recipients]);
+ const debouncedSearchUpdate = useMemo(
+ () =>
+ debounce((search: string) => {
+ const trimmedSearch = search.trim();
+ if (lastSearchValueRef.current !== trimmedSearch) {
+ lastSearchValueRef.current = trimmedSearch;
+ setSearchGeneration(prev => prev + 1);
+ }
+ }, 500),
+ [],
+ );
+
+ useEffect(
+ () => () => debouncedSearchUpdate.cancel(),
+ [debouncedSearchUpdate],
+ );
+
const methodOptions = useMemo(
() =>
(options || [])
@@ -450,6 +485,10 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
}
};
+ const handleSlackSearch = (search: string) => {
+ debouncedSearchUpdate(search);
+ };
+
const onSubjectChange = (
event: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
) => {
@@ -605,18 +644,20 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
// for SlackV2
<div className="input-container">
<AsyncSelect
- key={refreshKey}
ariaLabel={t('Select channels')}
mode="multiple"
name="recipients"
value={slackRecipients}
options={fetchSlackChannels}
onChange={onSlackRecipientsChange}
+ onSearch={handleSlackSearch}
allowClear
data-test="recipients"
fetchOnlyOnSearch={false}
pageSize={100}
placeholder={t('Select Slack channels')}
+ tokenSeparators={[]}
+ filterOption={() => true}
/>
<span
role="button"
diff --git a/superset/tasks/slack.py b/superset/tasks/slack.py
index db9047171b..b24a6faf41 100644
--- a/superset/tasks/slack.py
+++ b/superset/tasks/slack.py
@@ -15,17 +15,24 @@
# specific language governing permissions and limitations
# under the License.
import logging
+from typing import Optional
from flask import current_app
-from superset.extensions import celery_app
-from superset.utils.slack import get_channels
+from superset.extensions import cache_manager, celery_app
+from superset.utils.slack import get_channels_with_search, SlackChannelTypes
logger = logging.getLogger(__name__)
@celery_app.task(name="slack.cache_channels")
def cache_channels() -> None:
+ """
+ Celery task to warm up the Slack channels cache.
+
+ This task fetches all Slack channels using pagination and stores them in cache.
+ Useful for large workspaces where the initial channel fetch can be slow.
+ """
cache_timeout = current_app.config["SLACK_CACHE_TIMEOUT"]
retry_count = current_app.config.get("SLACK_API_RATE_LIMIT_RETRY_COUNT", 2)
@@ -37,7 +44,45 @@ def cache_channels() -> None:
)
try:
- get_channels(force=True, cache_timeout=cache_timeout)
+ all_channels = []
+ cursor: Optional[str] = None
+ page_count = 0
+
+ while True:
+ page_count += 1
+
+ result = get_channels_with_search(
+ search_string="",
+ types=list(SlackChannelTypes),
+ cursor=cursor,
+ limit=1000,
+ )
+
+ page_channels = result["result"]
+ all_channels.extend(page_channels)
+
+ logger.debug(
+ "Fetched page %d: %d channels (total: %d)",
+ page_count,
+ len(page_channels),
+ len(all_channels),
+ )
+
+ cursor = result.get("next_cursor")
+ if not cursor or not result.get("has_more"):
+ break
+
+ logger.info(
+ "Successfully fetched %d Slack channels in %d pages. Caching results.",
+ len(all_channels),
+ page_count,
+ )
+
+ cache_key = "slack_conversations_list"
+ cache_manager.cache.set(cache_key, all_channels, timeout=cache_timeout)
+
+ logger.info("Slack channels cache warm-up completed successfully")
+
except Exception as ex:
logger.exception(
"Failed to cache Slack channels: %s. "
diff --git a/superset/utils/slack.py b/superset/utils/slack.py
index 970cc06c74..8d6142a8df 100644
--- a/superset/utils/slack.py
+++ b/superset/utils/slack.py
@@ -26,9 +26,7 @@ from slack_sdk.http_retry.builtin_handlers import RateLimitErrorRetryHandler
from superset import feature_flag_manager
from superset.exceptions import SupersetException
-from superset.extensions import cache_manager
from superset.reports.schemas import SlackChannelSchema
-from superset.utils import cache as cache_util
from superset.utils.backports import StrEnum
logger = logging.getLogger(__name__)
@@ -58,64 +56,6 @@ def get_slack_client() -> WebClient:
return client
-@cache_util.memoized_func(
- key="slack_conversations_list",
- cache=cache_manager.cache,
-)
-def get_channels() -> list[SlackChannelSchema]:
- """
- Retrieves a list of all conversations accessible by the bot
- from the Slack API, and caches results (to avoid rate limits).
-
- The Slack API does not provide search so to apply a search use
- get_channels_with_search instead.
- """
- client = get_slack_client()
- channel_schema = SlackChannelSchema()
- channels: list[SlackChannelSchema] = []
- extra_params = {"types": ",".join(SlackChannelTypes)}
- cursor = None
- page_count = 0
-
- logger.info("Starting Slack channels fetch")
-
- try:
- while True:
- page_count += 1
-
- response = client.conversations_list(
- limit=999, cursor=cursor, exclude_archived=True, **extra_params
- )
- page_channels = response.data["channels"]
- channels.extend(channel_schema.load(channel) for channel in page_channels)
-
- logger.debug(
- "Fetched page %d: %d channels (total: %d)",
- page_count,
- len(page_channels),
- len(channels),
- )
-
- cursor = response.data.get("response_metadata", {}).get("next_cursor")
- if not cursor:
- break
-
- logger.info(
- "Successfully fetched %d Slack channels in %d pages",
- len(channels),
- page_count,
- )
- return channels
- except SlackApiError as ex:
- logger.error(
- "Failed to fetch Slack channels after %d pages: %s",
- page_count,
- str(ex),
- exc_info=True,
- )
- raise
-
-
def _fetch_channels_without_search(
client: WebClient,
channel_schema: SlackChannelSchema,
@@ -126,7 +66,7 @@ def _fetch_channels_without_search(
"""Fetch channels without search filtering, paginating for large limits."""
channels: list[SlackChannelSchema] = []
slack_cursor = cursor
- page_size = min(limit, 200)
+ page_size = min(limit, 1000)
while True:
response = client.conversations_list(
@@ -165,13 +105,13 @@ def _fetch_channels_with_search(
"""Fetch channels with search filtering, streaming through pages."""
matches: list[SlackChannelSchema] = []
slack_cursor = cursor
- max_pages_to_fetch = 50
- pages_fetched = 0
- search_string_lower = search_string.lower()
+ search_terms = [
+ term.strip().lower() for term in search_string.split(",") if term.strip()
+ ]
- while len(matches) < limit and pages_fetched < max_pages_to_fetch:
+ while len(matches) < limit:
response = client.conversations_list(
- limit=200,
+ limit=1000,
cursor=slack_cursor,
exclude_archived=True,
types=types_param,
@@ -182,16 +122,21 @@ def _fetch_channels_with_search(
channel_id_lower = channel_data["id"].lower()
is_match = False
- if exact_match:
- is_match = (
- search_string_lower == channel_name_lower
- or search_string_lower == channel_id_lower
- )
- else:
- is_match = (
- search_string_lower in channel_name_lower
- or search_string_lower in channel_id_lower
- )
+ for search_term in search_terms:
+ if exact_match:
+ if (
+ search_term == channel_name_lower
+ or search_term == channel_id_lower
+ ):
+ is_match = True
+ break
+ else:
+ if (
+ search_term in channel_name_lower
+ or search_term in channel_id_lower
+ ):
+ is_match = True
+ break
if is_match:
channel = channel_schema.load(channel_data)
@@ -204,8 +149,6 @@ def _fetch_channels_with_search(
if not slack_cursor:
break
- pages_fetched += 1
-
has_more = bool(slack_cursor) and len(matches) >= limit
return {
@@ -225,6 +168,15 @@ def get_channels_with_search(
"""
Fetches Slack channels with pagination and search support.
+ Args:
+ search_string: Search term(s). Can be comma-separated for OR logic.
+ e.g., "engineering,marketing" matches channels containing
+ "engineering" OR "marketing"
+ types: Channel types to filter (public_channel, private_channel)
+ exact_match: If True, search term must exactly match channel name/ID
+ cursor: Pagination cursor for fetching next page
+ limit: Maximum number of channels to return
+
Returns a dict with:
- result: list of channels
- next_cursor: cursor for next page (None if no more pages)
@@ -234,7 +186,6 @@ def get_channels_with_search(
We handle two cases:
1. WITHOUT search: Fetch single page from Slack API
2. WITH search: Stream through Slack API pages until we find enough matches
- (stops early to prevent timeouts on large workspaces)
"""
try:
client = get_slack_client()
diff --git a/tests/unit_tests/utils/slack_test.py b/tests/unit_tests/utils/slack_test.py
index 46bfbea43d..cc1527a8a5 100644
--- a/tests/unit_tests/utils/slack_test.py
+++ b/tests/unit_tests/utils/slack_test.py
@@ -429,30 +429,6 @@ The server responded with: missing scope: channels:read"""
"has_more": False,
}
- def test_streaming_search_max_pages_safety_limit(self, mocker):
- """Test streaming search stops after 50 pages to prevent runaway requests"""
-
- # Create a response that always has a next cursor (infinite pagination)
- mock_data = {
- "channels": [
- {"name": "channel", "id": "C1", "is_private": False, "is_member": True},
- ],
- "response_metadata": {"next_cursor": "next_page"},
- }
- mock_response = MockResponse(mock_data)
-
- mock_client = mocker.Mock()
- mock_client.conversations_list.return_value = mock_response
- mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
-
- # Search that matches the channel - should stop at 50 pages
- result = get_channels_with_search(search_string="channel", limit=100)
-
- # Should have called conversations_list exactly 50 times (max pages)
- assert mock_client.conversations_list.call_count == 50
- # Should return the matches found (50 channels, one per page)
- assert len(result["result"]) == 50
-
def test_search_with_no_matches(self, mocker):
"""Test search that finds no matching channels"""
@@ -666,9 +642,9 @@ The server responded with: missing scope: channels:read"""
assert len(result["result"]) == 50
assert result["has_more"] is True
- def test_non_search_pagination_over_200_limit(self, mocker):
- """Test non-search queries paginate correctly for limits > 200"""
- # Create 500 channels
+ def test_non_search_pagination_over_1000_limit(self, mocker):
+ """Test non-search queries paginate correctly for limits > 1000"""
+ # Create 2500 channels
all_channels = [
{
"name": f"channel-{i}",
@@ -676,7 +652,7 @@ The server responded with: missing scope: channels:read"""
"is_private": False,
"is_member": True,
}
- for i in range(500)
+ for i in range(2500)
]
call_count = 0
@@ -686,18 +662,18 @@ The server responded with: missing scope: channels:read"""
limit = kwargs.get("limit", 100)
cursor = kwargs.get("cursor")
- # Simulate Slack API pagination (max 200 per page)
+ # Simulate Slack API pagination (max 1000 per page)
if cursor is None:
start = 0
- elif cursor == "cursor_200":
- start = 200
- elif cursor == "cursor_400":
- start = 400
+ elif cursor == "cursor_1000":
+ start = 1000
+ elif cursor == "cursor_2000":
+ start = 2000
else:
- start = 600
+ start = 3000
- end = min(start + limit, 500)
- next_cursor = f"cursor_{end}" if end < 500 else None
+ end = min(start + limit, 2500)
+ next_cursor = f"cursor_{end}" if end < 2500 else None
call_count += 1
return MockResponse(
@@ -711,13 +687,13 @@ The server responded with: missing scope: channels:read"""
mock_client.conversations_list.side_effect = mock_conversations_list
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
- # Request 300 channels (requires 2 pages of 200 each)
- result = get_channels_with_search(limit=300)
+ # Request 1500 channels (requires 2 pages of 1000 each)
+ result = get_channels_with_search(limit=1500)
- # Should return exactly 300 channels
- assert len(result["result"]) == 300
+ # Should return exactly 1500 channels
+ assert len(result["result"]) == 1500
assert result["has_more"] is True
- assert result["next_cursor"] == "cursor_400"
+ assert result["next_cursor"] == "cursor_2000"
# Should have made 2 API calls
assert call_count == 2
@@ -801,3 +777,153 @@ The server responded with: missing scope: channels:read"""
assert len(result["result"]) == 0
assert result["has_more"] is False
assert result["next_cursor"] is None
+
+ def test_comma_separated_search_strings(self, mocker):
+ """Test search with comma-separated search strings (OR logic)"""
+ mock_data = {
+ "channels": [
+ {
+ "name": "engineering",
+ "id": "C1",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "marketing",
+ "id": "C2",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "sales",
+ "id": "C3",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "design",
+ "id": "C4",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "general",
+ "id": "C5",
+ "is_private": False,
+ "is_member": True,
+ },
+ ],
+ "response_metadata": {"next_cursor": None},
+ }
+
+ mock_response = MockResponse(mock_data)
+ mock_client = mocker.Mock()
+ mock_client.conversations_list.return_value = mock_response
+ mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
+
+ # Search for "engineering,marketing,sales"
+ result = get_channels_with_search(
+ search_string="engineering,marketing,sales", limit=100
+ )
+
+ # Should match 3 channels: engineering, marketing, sales
+ assert len(result["result"]) == 3
+ channel_names = [channel["name"] for channel in result["result"]]
+ assert "engineering" in channel_names
+ assert "marketing" in channel_names
+ assert "sales" in channel_names
+ assert "design" not in channel_names
+ assert "general" not in channel_names
+
+ def test_comma_separated_search_with_whitespace(self, mocker):
+ """Test comma-separated search handles extra whitespace correctly"""
+ mock_data = {
+ "channels": [
+ {
+ "name": "engineering-team",
+ "id": "C1",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "marketing-ops",
+ "id": "C2",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "general",
+ "id": "C3",
+ "is_private": False,
+ "is_member": True,
+ },
+ ],
+ "response_metadata": {"next_cursor": None},
+ }
+
+ mock_response = MockResponse(mock_data)
+ mock_client = mocker.Mock()
+ mock_client.conversations_list.return_value = mock_response
+ mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
+
+ # Search with extra whitespace: " engineering , marketing "
+ result = get_channels_with_search(
+ search_string=" engineering , marketing ", limit=100
+ )
+
+ # Should match 2 channels, whitespace should be stripped
+ assert len(result["result"]) == 2
+ channel_names = [channel["name"] for channel in result["result"]]
+ assert "engineering-team" in channel_names
+ assert "marketing-ops" in channel_names
+ assert "general" not in channel_names
+
+ def test_comma_separated_exact_match(self, mocker):
+ """Test comma-separated search with exact_match=True"""
+ mock_data = {
+ "channels": [
+ {
+ "name": "engineering",
+ "id": "C1",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "engineering-team",
+ "id": "C2",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "sales",
+ "id": "C3",
+ "is_private": False,
+ "is_member": True,
+ },
+ {
+ "name": "general",
+ "id": "C4",
+ "is_private": False,
+ "is_member": True,
+ },
+ ],
+ "response_metadata": {"next_cursor": None},
+ }
+
+ mock_response = MockResponse(mock_data)
+ mock_client = mocker.Mock()
+ mock_client.conversations_list.return_value = mock_response
+ mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)
+
+ # Exact match search for "engineering,sales"
+ result = get_channels_with_search(
+ search_string="engineering,sales", exact_match=True, limit=100
+ )
+
+ # Should match only exact names: engineering and sales (not engineering-team)
+ assert len(result["result"]) == 2
+ channel_names = [channel["name"] for channel in result["result"]]
+ assert "engineering" in channel_names
+ assert "sales" in channel_names
+ assert "engineering-team" not in channel_names
+ assert "general" not in channel_names